Mercurial > hg > documentViewer
comparison documentViewer.py @ 458:48b135b089c8 elementtree
more renovation
author | casties |
---|---|
date | Tue, 19 Jul 2011 20:46:35 +0200 |
parents | 9fb231ad0bd2 |
children | aabfa6124cfb |
comparison
equal
deleted
inserted
replaced
457:9fb231ad0bd2 | 458:48b135b089c8 |
---|---|
1 | |
2 from OFS.Folder import Folder | 1 from OFS.Folder import Folder |
3 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate | 2 from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate |
4 from Products.PageTemplates.PageTemplateFile import PageTemplateFile | 3 from Products.PageTemplates.PageTemplateFile import PageTemplateFile |
5 from AccessControl import ClassSecurityInfo | 4 from AccessControl import ClassSecurityInfo |
6 from AccessControl import getSecurityManager | 5 from AccessControl import getSecurityManager |
7 from Globals import package_home | 6 from Globals import package_home |
8 from Products.zogiLib.zogiLib import browserCheck | |
9 | 7 |
10 #from Ft.Xml import EMPTY_NAMESPACE, Parse | 8 #from Ft.Xml import EMPTY_NAMESPACE, Parse |
11 #import Ft.Xml.Domlette | 9 #import Ft.Xml.Domlette |
12 | 10 |
13 import xml.etree.ElementTree as ET | 11 import xml.etree.ElementTree as ET |
14 | 12 |
15 import os.path | 13 import os.path |
16 import sys | 14 import sys |
17 import urllib | 15 import urllib |
18 import urllib2 | |
19 import logging | 16 import logging |
20 import math | 17 import math |
21 import urlparse | 18 import urlparse |
22 import re | 19 import re |
23 import string | 20 import string |
24 | 21 |
22 from SrvTxtUtils import getInt, getText, getHttpData | |
23 | |
25 def logger(txt,method,txt2): | 24 def logger(txt,method,txt2): |
26 """logging""" | 25 """logging""" |
27 logging.info(txt+ txt2) | 26 logging.info(txt+ txt2) |
28 | 27 |
29 | 28 |
30 def getInt(number, default=0): | |
31 """returns always an int (0 in case of problems)""" | |
32 try: | |
33 return int(number) | |
34 except: | |
35 return int(default) | |
36 | |
37 def getText(node): | |
38 """get the cdata content of a node""" | |
39 if node is None: | |
40 return "" | |
41 # ET: | |
42 text = node.text or "" | |
43 for e in node: | |
44 text += gettext(e) | |
45 if e.tail: | |
46 text += e.tail | |
47 | |
48 # 4Suite: | |
49 #nodelist=node.childNodes | |
50 #text = "" | |
51 #for n in nodelist: | |
52 # if n.nodeType == node.TEXT_NODE: | |
53 # text = text + n.data | |
54 | |
55 return text | |
56 | |
57 getTextFromNode = getText | |
58 | |
59 def serializeNode(node, encoding="utf-8"): | 29 def serializeNode(node, encoding="utf-8"): |
60 """returns a string containing node as XML""" | 30 """returns a string containing node as XML""" |
61 s = ET.tostring(node) | 31 s = ET.tostring(node) |
62 | 32 |
63 # 4Suite: | 33 # 4Suite: |
126 bt['isIEMac'] = bt['isIE'] and bt['isMac'] | 96 bt['isIEMac'] = bt['isIE'] and bt['isMac'] |
127 bt['staticHTML'] = False | 97 bt['staticHTML'] = False |
128 | 98 |
129 return bt | 99 return bt |
130 | 100 |
131 | |
132 def getParentDir(path): | 101 def getParentDir(path): |
133 """returns pathname shortened by one""" | 102 """returns pathname shortened by one""" |
134 return '/'.join(path.split('/')[0:-1]) | 103 return '/'.join(path.split('/')[0:-1]) |
135 | 104 |
136 | 105 def getBibdataFromDom(dom): |
137 def getHttpData(url, data=None, num_tries=3, timeout=10): | 106 """returns dict with all elements from bib-tag""" |
138 """returns result from url+data HTTP request""" | 107 bibinfo = {} |
139 # we do GET (by appending data to url) | 108 bib = dom.find(".//meta/bib") |
140 if isinstance(data, str) or isinstance(data, unicode): | 109 if bib is not None: |
141 # if data is string then append | 110 # put type in @type |
142 url = "%s?%s"%(url,data) | 111 type = bib.get('type') |
143 elif isinstance(data, dict) or isinstance(data, list) or isinstance(data, tuple): | 112 bibinfo['@type'] = type |
144 # urlencode | 113 # put all subelements in dict |
145 url = "%s?%s"%(url,urllib.urlencode(data)) | 114 for e in bib: |
146 | 115 bibinfo[e.tag] = getText(e) |
147 response = None | 116 |
148 errmsg = None | 117 return bibinfo |
149 for cnt in range(num_tries): | |
150 try: | |
151 logging.debug("getHttpData(#%s %ss) url=%s"%(cnt+1,timeout,url)) | |
152 if sys.version_info < (2, 6): | |
153 # set timeout on socket -- ugly :-( | |
154 import socket | |
155 socket.setdefaulttimeout(float(timeout)) | |
156 response = urllib2.urlopen(url) | |
157 else: | |
158 response = urllib2.urlopen(url,timeout=float(timeout)) | |
159 # check result? | |
160 break | |
161 except urllib2.HTTPError, e: | |
162 logging.error("getHttpData: HTTP error(%s): %s"%(e.code,e)) | |
163 errmsg = str(e) | |
164 # stop trying | |
165 break | |
166 except urllib2.URLError, e: | |
167 logging.error("getHttpData: URLLIB error(%s): %s"%(e.reason,e)) | |
168 errmsg = str(e) | |
169 # stop trying | |
170 #break | |
171 | |
172 if response is not None: | |
173 data = response.read() | |
174 response.close() | |
175 return data | |
176 | |
177 raise IOError("ERROR fetching HTTP data from %s: %s"%(url,errmsg)) | |
178 #return None | |
179 | 118 |
180 ## | 119 ## |
181 ## documentViewer class | 120 ## documentViewer class |
182 ## | 121 ## |
183 class documentViewer(Folder): | 122 class documentViewer(Folder): |
302 @param url: url which contains display information | 241 @param url: url which contains display information |
303 @param viewMode: if images display images, if text display text, default is images (text,images or auto) | 242 @param viewMode: if images display images, if text display text, default is images (text,images or auto) |
304 | 243 |
305 ''' | 244 ''' |
306 logging.debug("HHHHHHHHHHHHHH:load the rss") | 245 logging.debug("HHHHHHHHHHHHHH:load the rss") |
307 logger("documentViewer (index)", logging.INFO, "mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn)) | 246 logging.debug("documentViewer (index) mode: %s url:%s start:%s pn:%s"%(mode,url,start,pn)) |
308 | 247 |
309 if not hasattr(self, 'template'): | 248 if not hasattr(self, 'template'): |
310 # create template folder if it doesn't exist | 249 # create template folder if it doesn't exist |
311 self.manage_addFolder('template') | 250 self.manage_addFolder('template') |
312 | 251 |
632 | 571 |
633 docinfo['indexMetaPath']=self.getIndexMetaPath(path); | 572 docinfo['indexMetaPath']=self.getIndexMetaPath(path); |
634 | 573 |
635 logging.debug("documentViewer (getbibinfofromindexmeta cutted) path: %s"%(path)) | 574 logging.debug("documentViewer (getbibinfofromindexmeta cutted) path: %s"%(path)) |
636 # put in all raw bib fields as dict "bib" | 575 # put in all raw bib fields as dict "bib" |
637 bib = dom.find(".//bib") | 576 bib = getBibdataFromDom(dom) |
638 #bib = dom.xpath("//bib/*") | 577 docinfo['bib'] = bib |
639 if bib is not None: | |
640 bibinfo = {} | |
641 for e in bib: | |
642 bibinfo[e.tag] = getText(e) | |
643 | |
644 docinfo['bib'] = bibinfo | |
645 | 578 |
646 # extract some fields (author, title, year) according to their mapping | 579 # extract some fields (author, title, year) according to their mapping |
647 metaData=self.metadata.main.meta.bib | 580 metaData=self.metadata.main.meta.bib |
648 bibtype=bib.get("type") | 581 bibtype=bib.get("@type") |
649 #bibtype=dom.xpath("//bib/@type") | 582 #bibtype=dom.xpath("//bib/@type") |
650 if not bibtype: | 583 if not bibtype: |
651 bibtype="generic" | 584 bibtype="generic" |
652 | 585 |
653 bibtype=bibtype.replace("-"," ") # wrong typesiin index meta "-" instead of " " (not wrong! ROC) | 586 bibtype=bibtype.replace("-"," ") # wrong types in index meta "-" instead of " " (not wrong! ROC) |
654 docinfo['bib_type'] = bibtype | 587 docinfo['bib_type'] = bibtype |
655 bibmap=metaData.generateMappingForType(bibtype) | 588 bibmap=metaData.generateMappingForType(bibtype) |
656 logging.debug("documentViewer (getbibinfofromindexmeta) bibmap:"+repr(bibmap)) | 589 logging.debug("documentViewer (getbibinfofromindexmeta) bibmap:"+repr(bibmap)) |
657 logging.debug("documentViewer (getbibinfofromindexmeta) bibtype:"+repr(bibtype)) | 590 logging.debug("documentViewer (getbibinfofromindexmeta) bibtype:"+repr(bibtype)) |
658 # if there is no mapping bibmap is empty (mapping sometimes has empty fields) | 591 # if there is no mapping bibmap is empty (mapping sometimes has empty fields) |
659 logging.debug("bibmap: %s"%repr(bibmap)) | |
660 if len(bibmap) > 0 and bibmap.get('author',None) or bibmap.get('title',None): | 592 if len(bibmap) > 0 and bibmap.get('author',None) or bibmap.get('title',None): |
661 try: | 593 try: |
662 docinfo['author']=getText(bib.find(bibmap['author'][0])) | 594 docinfo['author']=bib.get(bibmap['author'][0]) |
663 except: pass | 595 except: pass |
664 try: | 596 try: |
665 docinfo['title']=getText(bib.find(bibmap['title'][0])) | 597 docinfo['title']=bib.get(bibmap['title'][0]) |
666 except: pass | 598 except: pass |
667 try: | 599 try: |
668 docinfo['year']=getText(bib.find(bibmap['year'][0])) | 600 docinfo['year']=bib.get(bibmap['year'][0]) |
669 except: pass | 601 except: pass |
670 | 602 |
671 # ROC: why is this here? | 603 # ROC: why is this here? |
672 # logging.debug("documentViewer (getbibinfofromindexmeta) using mapping for %s"%bibtype) | 604 # logging.debug("documentViewer (getbibinfofromindexmeta) using mapping for %s"%bibtype) |
673 # try: | 605 # try: |
894 | 826 |
895 # FIXME: fake texturlpath | 827 # FIXME: fake texturlpath |
896 if not docinfo.has_key('textURLPath'): | 828 if not docinfo.has_key('textURLPath'): |
897 docinfo['textURLPath'] = None | 829 docinfo['textURLPath'] = None |
898 | 830 |
899 logging.debug("documentViewer (getdocinfo) docinfo: %s"%docinfo) | 831 logging.debug("documentViewer (getdocinfo) docinfo: keys=%s"%docinfo.keys()) |
900 #logging.debug("documentViewer (getdocinfo) docinfo: %s"%) | 832 #logging.debug("documentViewer (getdocinfo) docinfo: %s"%docinfo) |
901 self.REQUEST.SESSION['docinfo'] = docinfo | 833 self.REQUEST.SESSION['docinfo'] = docinfo |
902 return docinfo | 834 return docinfo |
903 | 835 |
904 def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None, viewMode=None, tocMode=None): | 836 def getPageinfo(self, current, start=None, rows=None, cols=None, docinfo=None, viewMode=None, tocMode=None): |
905 """returns pageinfo with the given parameters""" | 837 """returns pageinfo with the given parameters""" |