Annotation of documentViewer/SrvTxtUtils.py, revision 1.1.2.3

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>