Annotation of MPIWGWeb/MPIWGHelper.py, revision 1.1.2.10
1.1.2.2 dwinter 1: from types import *
1.1.2.4 dwinter 2: import logging
1.1.2.1 dwinter 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:
1.1.2.7 dwinter 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
1.1.2.1 dwinter 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:
1.1.2.5 dwinter 35: x=unicodify(xo[1])
36: y=unicodify(yo[1])
1.1.2.1 dwinter 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:
1.1.2.10! casties 87: def unicodify(s):
1.1.2.1 dwinter 88: """decode str (utf-8 or latin-1 representation) into unicode object"""
1.1.2.6 casties 89: #logging.error("unicodify: %s"%str)
1.1.2.10! casties 90: if not s:
! 91: return u""
! 92: if isinstance(s, str):
1.1.2.1 dwinter 93: try:
1.1.2.10! casties 94: return s.decode('utf-8')
1.1.2.1 dwinter 95: except:
1.1.2.10! casties 96: return s.decode('latin-1')
1.1.2.1 dwinter 97: else:
1.1.2.10! casties 98: return s
1.1.2.1 dwinter 99:
1.1.2.10! casties 100: def utf8ify(s):
1.1.2.1 dwinter 101: """encode unicode object or string into byte string in utf-8 representation"""
1.1.2.10! casties 102: if not s:
1.1.2.1 dwinter 103: return ""
1.1.2.10! casties 104: if isinstance(s, str):
! 105: return s
1.1.2.1 dwinter 106: else:
1.1.2.10! casties 107: return s.encode('utf-8')
1.1.2.1 dwinter 108:
1.1.2.6 casties 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:])
1.1.2.8 casties 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:
1.1.2.9 casties 133: def getSection(self, crumbs=None):
1.1.2.8 casties 134: """returns the current section name"""
1.1.2.9 casties 135: # use breadcrumbs if available
136: if crumbs is not None and len(crumbs) > 0:
137: return crumbs[0][2].getId()
138:
1.1.2.8 casties 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:
1.1.2.9 casties 148: def getSubSection(self, crumbs=None):
1.1.2.8 casties 149: """returns the current subsection name"""
1.1.2.9 casties 150: # use breadcrumbs if available
151: if crumbs is not None and len(crumbs) > 1:
152: return crumbs[1][2].getId()
153:
1.1.2.8 casties 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>