]>
Commit | Line | Data |
---|---|---|
aca310e5 RD |
1 | #---------------------------------------------------------------------------- |
2 | # Name: utillang.py | |
3 | # Purpose: Provide language specific utilities | |
4 | # | |
5 | # Author: Joel Hare | |
6 | # | |
7 | # Created: 8/23/05 | |
8 | # CVS-ID: $Id$ | |
9 | # Copyright: (c) 2004-2005 ActiveGrid, Inc. | |
10 | # License: wxWindows License | |
11 | #---------------------------------------------------------------------------- | |
12 | ||
13 | import os | |
14 | import sys | |
15 | import UserDict | |
16 | import tempfile | |
17 | import xml.sax.saxutils as saxutils | |
18 | ||
19 | import activegrid.util.parser as parser | |
20 | ||
21 | PY2WEB_codepages = { | |
22 | 'cp1251' : 'CP-1251', | |
23 | 'koi8_r' : 'KOI8-R', | |
24 | } | |
25 | ||
26 | def evalXPath(xpath, data, specialEntries=None): | |
27 | codeStr = parser.xpathToCode(xpath) | |
28 | return evalCode(codeStr, data, specialEntries) | |
29 | ||
30 | def evalCode(codeStr, data, specialEntries=None): | |
31 | if isinstance(data, ObjAsDict): | |
32 | namespace = data | |
33 | elif isinstance(data, dict): | |
34 | namespace = dict(data) | |
35 | else: | |
36 | namespace = ObjAsDict(data) | |
37 | if specialEntries: | |
38 | for key, value in specialEntries.items(): | |
39 | namespace.addSpecialEntry(key, value) | |
40 | return eval(codeStr, {}, namespace) | |
41 | ||
42 | def deriveCharset(): | |
43 | charset = None | |
44 | encodingString = sys.getdefaultencoding() | |
45 | if encodingString != 'ascii': | |
46 | charset = PY2WEB_codepages.get(encodingString.lower()) | |
47 | if charset == None: | |
48 | charset = encodingString | |
49 | return charset | |
50 | ||
51 | def toUTF8(value): | |
52 | """ | |
53 | Converts all unicode and non-string values to utf-8. | |
54 | This assumes string instances are already encoded in utf-8. | |
55 | Note that us-ascii is a subset of utf-8. | |
56 | """ | |
57 | if isinstance(value, unicode): | |
58 | return value.encode('utf-8') | |
59 | return str(value) | |
60 | ||
61 | def toUnicode(value): | |
62 | """ | |
63 | Converts all strings non-string values to unicode. | |
64 | This assumes string instances are encoded in utf-8. | |
65 | Note that us-ascii is a subset of utf-8. | |
66 | """ | |
67 | if not isinstance(value, unicode): | |
68 | if not isinstance(value, str): | |
69 | return unicode(value) | |
70 | return unicode(value, 'utf-8') | |
71 | return value | |
72 | ||
73 | ||
74 | def getSystemTempDir(): | |
75 | return tempfile.gettempdir() | |
76 | ||
77 | def getEnvVar(name, defaultVal=None): | |
78 | if os.environ.has_key(name): | |
79 | return os.environ[name] | |
80 | return defaultVal | |
81 | ||
82 | class ObjAsDict(UserDict.DictMixin): | |
83 | """ | |
84 | Passing this to eval as the local variables dictionary allows the | |
85 | evaluated code to access properties in the wrapped object | |
86 | """ | |
87 | def __init__(self, obj): | |
88 | self.obj = obj | |
89 | self.specialEntries = {} | |
90 | ||
91 | def __getitem__(self, key): | |
92 | try: | |
93 | return getattr(self.obj, key) | |
94 | except AttributeError, e: | |
95 | if self.specialEntries.has_key(key): | |
96 | return self.specialEntries[key] | |
97 | raise KeyError(e.args) | |
98 | def __setitem__(self, key, item): setattr(self.obj, key, item) | |
99 | def __delitem__(self, key): delattr(self.obj, key) | |
100 | def keys(self): | |
101 | ret=[] | |
102 | for i in list(dir(self.obj)+self.specialEntries.keys()): | |
103 | if i=="__doc__" or i=="__module__": | |
104 | pass | |
105 | elif i not in ret: | |
106 | ret.append(i) | |
107 | return ret | |
108 | ||
109 | def addSpecialEntry(self, key, value): | |
110 | self.specialEntries[key] = value | |
111 | ||
112 | global saxXMLescapeDoubleQuote | |
113 | saxXMLescapeDoubleQuote = {'"':'"'} | |
114 | ||
115 | global saxXMLescapesAllQuotes | |
116 | # IE doesn't support ' but it doesn't seem like we should need this escaped at all so I took it out. | |
117 | saxXMLescapesAllQuotes = {'"':'"', "'":"'"} | |
118 | ||
119 | global saxXMLunescapes | |
120 | saxXMLunescapes = {'"':'"', "'":"'"} | |
121 | ||
122 | def escape(data, extraEscapes=None): | |
123 | """Escape ', ", &, <, and > in a string of data. | |
124 | ||
125 | Basically, everything that saxutils.escape does (and this calls that, at | |
126 | least for now), but with " and ' added as well. | |
127 | ||
128 | TODO: make this faster; saxutils.escape() is really slow | |
129 | """ | |
130 | ||
131 | global saxXMLescapeDoubleQuote | |
132 | if (extraEscapes == None): | |
133 | extraEscapes = saxXMLescapeDoubleQuote | |
134 | return saxutils.escape(data, extraEscapes) | |
135 | ||
136 | def unescape(data): | |
137 | """Unescape ', ", &, <, and > in a string of data. | |
138 | ||
139 | Basically, everything that saxutils.unescape does (and this calls that, at | |
140 | least for now), but with " and ' added as well. | |
141 | ||
142 | TODO: make this faster; saxutils.unescape() is really slow | |
143 | """ | |
144 | ||
145 | global saxXMLunescapes | |
146 | return saxutils.unescape(data, saxXMLunescapes) |