File:  [Repository] / MPIWGWeb / Attic / MPIWGHelper.py
Revision 1.1.2.10: download - view: text, annotated - select for diffs - revision graph
Wed Feb 10 18:25:44 2010 UTC (14 years, 4 months ago) by casties
Branches: r2
fixed encoding issues in project lists

    1: from types import *
    2: import logging
    3: definedFields=['WEB_title','xdata_01','xdata_02','xdata_03','xdata_04','xdata_05','xdata_06','xdata_07','xdata_08','xdata_09','xdata_10','xdata_11','xdata_12','xdata_13','WEB_project_header','WEB_project_description','WEB_related_pub']
    4: 
    5: checkFields = ['xdata_01']
    6: 
    7: #ersetzt logging
    8: def logger(txt,method,txt2):
    9:     """logging""" 
   10:     logging.info(txt+ txt2)
   11:                  
   12: def getTextFromNode(nodename):
   13:     
   14:     nodelist=nodename.childNodes
   15:     rc = ""
   16:     for node in nodelist:
   17:         if node.nodeType == node.TEXT_NODE:
   18:            rc = rc + node.data
   19:     return rc
   20: 
   21: def getTemplate(self, tpName):
   22:     """get a template file either form the instance or from the product"""
   23:     ext=self.ZopeFind(self.aq_parent,obj_ids=[tpName])
   24:     if ext:
   25:         pt = getattr(self,ext[0][1].getId())
   26:     else:
   27:         pt=PageTemplateFile(os.path.join(package_home(globals()), 'zpt/'+tpName)).__of__(self)
   28:     assert(pt)
   29:     return pt
   30: 
   31: def sortStopWordsF(self,xo,yo):
   32:     if not hasattr(self,'_v_stopWords'):
   33:         self._v_stopWords=self.stopwords_en.data.split("\n")
   34:     
   35:     x=unicodify(xo[1])
   36:     y=unicodify(yo[1])
   37:     
   38:     strx=x.split(" ")
   39:     stry=y.split(" ")
   40:     
   41:     for tmp in strx:
   42:         if tmp.lower() in self._v_stopWords:
   43:             del strx[strx.index(tmp)]
   44:     
   45:     for tmp in stry:
   46:         if tmp.lower() in self._v_stopWords:
   47:             del stry[stry.index(tmp)]
   48:             
   49:     return cmp(" ".join(strx)," ".join(stry))
   50:     
   51: def sortStopWords(self):
   52:     return lambda x,y : sortStopWordsF(self,x,y)
   53:     
   54: def sortF(x,y):
   55:     try:
   56:         return cmp(x[1],y[1])
   57:     except:
   58:         try:
   59:             return cmp(str(x[1]),str(y[1]))
   60:         except:           
   61:             
   62:             return 0 
   63:     
   64: def sortI(x,y):
   65:     xsplit=x[1].split(".")
   66:     ysplit=y[1].split(".")
   67:     xret=""
   68:     yret=""
   69:     try:
   70:         for i in range(5):
   71:             try:
   72:                 yret=yret+"%04i"%int(xsplit[i])
   73:             except:
   74:                 yret=yret+"%04i"%0
   75: 
   76:             try:
   77:                 xret=xret+"%04i"%int(ysplit[i])
   78:             except:
   79:                 xret=xret+"%04i"%0
   80:                 
   81:         
   82:         return cmp(int(yret),int(xret))
   83:     except:
   84:         return cmp(x[1],y[1])
   85: 
   86: 
   87: def unicodify(s):
   88:     """decode str (utf-8 or latin-1 representation) into unicode object"""
   89:     #logging.error("unicodify: %s"%str)
   90:     if not s:
   91:         return u""
   92:     if isinstance(s, str):
   93:         try:
   94:             return s.decode('utf-8')
   95:         except:
   96:             return s.decode('latin-1')
   97:     else:
   98:         return s
   99: 
  100: def utf8ify(s):
  101:     """encode unicode object or string into byte string in utf-8 representation"""
  102:     if not s:
  103:         return ""
  104:     if isinstance(s, str):
  105:         return s
  106:     else:
  107:         return s.encode('utf-8')
  108: 
  109: 
  110: def shortenString(s, l, ellipsis='...'):
  111:     """returns a string of length l (or l-1) by omitting characters in the middle of s, replacing with ellipsis."""
  112:     l1 = int((l - len(ellipsis)) / 2)
  113:     return "%s%s%s"%(s[:l1],ellipsis,s[-l1:])
  114: 
  115: 
  116: #
  117: # navigation methods (should better be a mixin class)
  118: #
  119: def getBreadcrumbs(self):
  120:     """return list of breadcrumbs from here to the root"""
  121:     crumbs = [(self.title, self.absolute_url(), self)]
  122:     parent = self.aq_parent
  123:     if hasattr(parent, 'getBreadcrumbs'):
  124:         if self.title:
  125:             return parent.getBreadcrumbs() + crumbs
  126:         else:
  127:             # if there's no title, skip this level
  128:             return parent.getBreadcrumbs()
  129:         
  130:     return crumbs
  131: 
  132: 
  133: def getSection(self, crumbs=None):
  134:     """returns the current section name"""
  135:     # use breadcrumbs if available
  136:     if crumbs is not None and len(crumbs) > 0:
  137:         return crumbs[0][2].getId()
  138: 
  139:     p = self
  140:     sec = None
  141:     # descend parents to the root (and remember the last id)
  142:     while p is not None and p.meta_type != 'MPIWGRoot':
  143:         sec = p.getId()
  144:         p = p.aq_parent
  145:     
  146:     return sec
  147: 
  148: def getSubSection(self, crumbs=None):
  149:     """returns the current subsection name"""
  150:     # use breadcrumbs if available
  151:     if crumbs is not None and len(crumbs) > 1:
  152:         return crumbs[1][2].getId()
  153: 
  154:     p = self
  155:     sec = None
  156:     subsec = None
  157:     # descend parents to the root (and remember the last id)
  158:     while p is not None and p.meta_type != 'MPIWGRoot':
  159:         subsec = sec
  160:         sec = p.getId()
  161:         p = p.aq_parent
  162:     
  163:     return subsec
  164: 

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