-
Notifications
You must be signed in to change notification settings - Fork 0
/
_globals.py
382 lines (339 loc) · 10.6 KB
/
_globals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/python
# -*- coding: utf-8 -*-
################################################################################
# _globals.py
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
################################################################################
# Imports.
from Products.PageTemplates.Expressions import SecureModuleImporter
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
# MD5
import AccessControl
import hashlib
class MD5DigestScheme:
def encrypt(self, pw):
enc = hashlib.md5(pw)
enc = enc.hexdigest()
return enc
def validate(self, reference, attempt):
compare = self.encrypt(attempt)[:-1]
return (compare == reference)
import AccessControl.AuthEncoding
AccessControl.AuthEncoding.registerScheme('MD5', MD5DigestScheme())
# Umlauts
umlaut_map = {
# German
u'ä' : 'ae',
u'ö' : 'oe',
u'ü' : 'ue',
u'Ä' : 'Ae',
u'Ö' : 'Oe',
u'Ü' : 'Ue',
u'ß' : 'ss',
# Cyrillic
u'а' : 'a',
u'б' : 'b',
u'в' : 'v',
u'г' : 'g',
u'д' : 'd',
u'е' : 'e',
u'ё' : 'e',
u'ж' : 'zh',
u'з' : 'z',
u'и' : 'i',
u'й' : 'j',
u'к' : 'k',
u'л' : 'l',
u'м' : 'm',
u'н' : 'n',
u'о' : 'o',
u'п' : 'p',
u'р' : 'r',
u'с' : 's',
u'т' : 't',
u'у' : 'u',
u'ф' : 'f',
u'х' : 'h',
u'ц' : 'c',
u'ч' : 'ch',
u'ш' : 'sh',
u'щ' : 'sch',
u'ь' : "'",
u'ы' : 'y',
u'ь' : "'",
u'э' : 'e',
u'ю' : 'ju',
u'я' : 'ja',
u'А' : 'A',
u'Б' : 'B',
u'В' : 'V',
u'Г' : 'G',
u'Д' : 'D',
u'Е' : 'E',
u'Ё' : 'E',
u'Ж' : 'ZH',
u'З' : 'Z',
u'И' : 'I',
u'Й' : 'J',
u'К' : 'K',
u'Л' : 'L',
u'М' : 'M',
u'Н' : 'N',
u'О' : 'O',
u'П' : 'P',
u'Р' : 'R',
u'С' : 'S',
u'Т' : 'T',
u'У' : 'U',
u'Ф' : 'F',
u'Х' : 'H',
u'Ц' : 'C',
u'Ч' : 'CH',
u'Ш' : 'SH',
u'Щ' : 'SCH',
u'Ъ' : "'",
u'Ы' : 'Y',
u'Ь' : "'",
u'Э' : 'E',
u'Ю' : 'JU',
u'Я' : 'JA',}
def sort_item( i):
if type( i) is str:
i = unicode(i,'utf-8')
mapping = umlaut_map
for key in mapping.keys():
try: i = i.replace(key,mapping[key])
except: pass
return i
# Datatypes.
DT_UNKNOWN = 0
DT_BOOLEAN = 1
DT_DATE = 2
DT_DATETIME = 3
DT_DICT = 4
DT_FILE = 5
DT_FLOAT = 6
DT_IMAGE = 7
DT_INT = 8
DT_LIST = 9
DT_PASSWORD = 10
DT_STRING = 11
DT_TEXT = 12
DT_TIME = 13
DT_URL = 14
DT_ID = 15
DT_XML = 16
DT_AMOUNT = 17
DT_TEXTS = [ DT_STRING, DT_TEXT ]
DT_STRINGS = [ DT_STRING, DT_TEXT, DT_URL, DT_PASSWORD, DT_XML ]
DT_BLOBS = [ DT_IMAGE, DT_FILE ]
DT_INTS = [ DT_INT, DT_BOOLEAN ]
DT_NUMBERS = [ DT_INT, DT_FLOAT, DT_AMOUNT ]
DT_DATETIMES = [ DT_DATE, DT_TIME, DT_DATETIME ]
datatype_map = [
[ 'unknown',''],
[ 'boolean',0],
[ 'date',None],
[ 'datetime',None],
[ 'dictionary',{}],
[ 'file',None],
[ 'float',0.0],
[ 'image',None],
[ 'int',0],
[ 'list',[]],
[ 'password',''],
[ 'string',''],
[ 'text',''],
[ 'time',None],
[ 'url',''],
[ 'identifier',''],
[ 'xml',''],
[ 'amount',0.0],
]
def datatype_key(datatype):
for dt_index in range(len(datatype_map)):
if datatype_map[dt_index][0] == datatype:
return dt_index
else:
return DT_UNKNOWN
def is_str_type(v):
"""
2to3: types.StringTypes removed in Python-3.x
"""
try:
return isinstance(v, basestring)
except NameError:
return isinstance(v, str)
def get_size(v):
"""
Returns size of given object v in bytes.
@return: Size in bytes
@rtype: C{int}
"""
size = 0
if v is not None:
if is_str_type(v):
size = size + len(v)
elif type(v) is list:
size = sum( map( lambda x: get_size(x), v))
elif type(v) is dict:
size = sum( map( lambda x: get_size(x) + get_size(v[x]), v.keys()))
elif type(v) is int or type(v) is float:
size = size + 4
elif hasattr(v,'get_real_size') and callable(getattr(v,'get_real_size')):
try:
size = size + v.get_real_size()
except:
pass
elif hasattr(v,'get_size') and callable(getattr(v,'get_size')):
try:
size = size + v.get_size()
except:
pass
return size
################################################################################
# Define StaticPageTemplateFile.
################################################################################
class StaticPageTemplateFile(PageTemplateFile):
def setEnv(self,context,options):
self.context = context
self.options = options
def pt_getContext(self):
root = self.context.getPhysicalRoot()
context = self.context
options = self.options
c = {'template': self,
'here': context,
'context': context,
'options': options,
'root': root,
'request': getattr(root, 'REQUEST', None),
'modules': SecureModuleImporter,
}
return c
################################################################################
# Define MyClass.
################################################################################
class MyClass:
# ----------------------------------------------------------------------------
# MyClass.keys:
# ----------------------------------------------------------------------------
def keys(self):
return self.__dict__.keys()
################################################################################
# Define MySectionizer.
################################################################################
class MySectionizer:
# --------------------------------------------------------------------------
# MySectionizer.__init__:
#
# Constructor.
# --------------------------------------------------------------------------
def __init__(self, levelnfc='0'):
self.levelnfc = levelnfc
self.sections = []
# --------------------------------------------------------------------------
# MySectionizer.__str__:
#
# Returns a string representation of the object.
# --------------------------------------------------------------------------
def __str__(self):
s = ''
for i in range(len(self.sections)):
if self.levelnfc == '0':
s += str(self.sections[i]) + '.'
elif self.levelnfc == '1':
s += chr(self.sections[i] - 1 + ord('A')) + '.'
elif self.levelnfc == '2':
s += chr(self.sections[i] - 1 + ord('a')) + '.'
return s
# --------------------------------------------------------------------------
# MySectionizer.clone:
#
# Creates and returns a copy of this object.
# --------------------------------------------------------------------------
def clone(self):
ob = MySectionizer(self.levelnfc)
ob.sections = copy.deepcopy(self.sections)
return ob
# --------------------------------------------------------------------------
# MySectionizer.getLevel:
# --------------------------------------------------------------------------
def getLevel(self):
return len(self.sections)
# --------------------------------------------------------------------------
# MySectionizer.processLevel:
# --------------------------------------------------------------------------
def processLevel(self, level):
# Increase section counter on this level.
if level > 0:
if level == len(self.sections):
self.sections[level-1] = self.sections[level-1] + 1
elif level > len(self.sections):
for i in range(len(self.sections),level):
self.sections.append(1)
elif level < len(self.sections):
for i in range(level,len(self.sections)):
del self.sections[len(self.sections)-1]
self.sections[level-1] = self.sections[level-1] + 1
################################################################################
# Define MyStack.
################################################################################
class MyStack:
# --------------------------------------------------------------------------
# MyStack.__init__:
#
# Constructor.
# --------------------------------------------------------------------------
def __init__(self):
self.clear()
# --------------------------------------------------------------------------
# MyStack.__str__:
#
# Returns a string representation of the object.
# --------------------------------------------------------------------------
def __str__(self):
s = ''
for el in self._stack:
s += str(el) + ';'
return s[:-1]
def size(self):
return len(self._stack)
def clear(self):
self._stack = []
def push(self, x):
self._stack.append(x)
def pop(self):
if len(self._stack) > 0:
return self._stack.pop()
else:
return None
def get_all(self):
return self._stack
def get(self, i=0):
if len(self._stack) > 0 and abs(i) < len(self._stack):
if i < 0:
return self._stack[len(self._stack)+i-1]
else:
return self._stack[i]
else:
return None
def top(self):
if len(self._stack) > 0:
return self._stack[len(self._stack)-1]
else:
return None
################################################################################