annotate SrvTxtUtils.py @ 28:40508e672841

richer metadata for bib. still not sufficient though.
author casties
date Mon, 20 Aug 2012 20:14:45 +0200
parents db5af17f4933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
1 """Utility methods for handling XML, reading HTTP, etc"""
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
2
21
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
3 from App.ImageFile import ImageFile
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
4 from App.Common import rfc1123_date
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
5
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
6 import sys
21
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
7 import os
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
8 import stat
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
9 import urllib
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
10 import urllib2
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
11 import logging
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
12
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
13
21
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
14 srvTxtUtilsVersion = "1.3"
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
15
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
16 def getInt(number, default=0):
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
17 """returns always an int (0 in case of problems)"""
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
18 try:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
19 return int(number)
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
20 except:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
21 return int(default)
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
22
1
e4bae49e657b more renovation
casties
parents: 0
diff changeset
23 def getAt(array, idx, default=None):
e4bae49e657b more renovation
casties
parents: 0
diff changeset
24 """returns element idx from array or default (in case of problems)"""
e4bae49e657b more renovation
casties
parents: 0
diff changeset
25 try:
e4bae49e657b more renovation
casties
parents: 0
diff changeset
26 return array[idx]
e4bae49e657b more renovation
casties
parents: 0
diff changeset
27 except:
e4bae49e657b more renovation
casties
parents: 0
diff changeset
28 return default
e4bae49e657b more renovation
casties
parents: 0
diff changeset
29
12
7f0e2b656e5c more work for non-bib metadata
casties
parents: 1
diff changeset
30 def getText(node, recursive=0):
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
31 """returns all text content of a node and its subnodes"""
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
32 if node is None:
12
7f0e2b656e5c more work for non-bib metadata
casties
parents: 1
diff changeset
33 return ''
7f0e2b656e5c more work for non-bib metadata
casties
parents: 1
diff changeset
34
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
35 # ElementTree:
12
7f0e2b656e5c more work for non-bib metadata
casties
parents: 1
diff changeset
36 text = node.text or ''
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
37 for e in node:
12
7f0e2b656e5c more work for non-bib metadata
casties
parents: 1
diff changeset
38 if recursive:
7f0e2b656e5c more work for non-bib metadata
casties
parents: 1
diff changeset
39 text += getText(e)
7f0e2b656e5c more work for non-bib metadata
casties
parents: 1
diff changeset
40 else:
7f0e2b656e5c more work for non-bib metadata
casties
parents: 1
diff changeset
41 text += e.text or ''
0
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
42 if e.tail:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
43 text += e.tail
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
44
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
45 # 4Suite:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
46 #nodelist=node.childNodes
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
47 #text = ""
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
48 #for n in nodelist:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
49 # if n.nodeType == node.TEXT_NODE:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
50 # text = text + n.data
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
51
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
52 return text
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
53
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
54
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
55
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
56 def getHttpData(url, data=None, num_tries=3, timeout=10):
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
57 """returns result from url+data HTTP request"""
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
58 # we do GET (by appending data to url)
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
59 if isinstance(data, str) or isinstance(data, unicode):
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
60 # if data is string then append
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
61 url = "%s?%s"%(url,data)
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
62 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple):
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
63 # urlencode
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
64 url = "%s?%s"%(url,urllib.urlencode(data))
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
65
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
66 response = None
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
67 errmsg = None
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
68 for cnt in range(num_tries):
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
69 try:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
70 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url))
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
71 if sys.version_info < (2, 6):
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
72 # set timeout on socket -- ugly :-(
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
73 import socket
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
74 socket.setdefaulttimeout(float(timeout))
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
75 response = urllib2.urlopen(url)
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
76 else:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
77 # timeout as parameter
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
78 response = urllib2.urlopen(url,timeout=float(timeout))
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
79 # check result?
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
80 break
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
81 except urllib2.HTTPError, e:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
82 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e))
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
83 errmsg = str(e)
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
84 # stop trying
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
85 break
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
86 except urllib2.URLError, e:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
87 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e))
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
88 errmsg = str(e)
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
89 # stop trying
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
90 #break
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
91
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
92 if response is not None:
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
93 data = response.read()
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
94 response.close()
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
95 return data
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
96
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
97 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg))
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
98 #return None
9f9d9be26e53 first checkin in Mercurial (see history in SVN)
casties
parents:
diff changeset
99
21
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
100
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
101 def refreshingImageFileIndexHtml(self, REQUEST, RESPONSE):
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
102 """index_html method for App.ImageFile that updates the file info for each request."""
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
103 stat_info = os.stat(self.path)
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
104 self.size = stat_info[stat.ST_SIZE]
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
105 self.lmt = float(stat_info[stat.ST_MTIME]) or time.time()
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
106 self.lmh = rfc1123_date(self.lmt)
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
107 # call original method
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
108 return ImageFile.index_html(self, REQUEST, RESPONSE)
db5af17f4933 updated SrvTxtUtils to 1.3
casties
parents: 12
diff changeset
109