1 #---------------------------------------------------------------------------- 
   3 # Purpose:      Provide language specific utilities  
   9 # Copyright:    (c) 2004-2005 ActiveGrid, Inc. 
  10 # License:      wxWindows License 
  11 #---------------------------------------------------------------------------- 
  17 import xml
.sax
.saxutils 
as saxutils
 
  19 import activegrid
.util
.parser 
as parser
 
  26 def evalXPath(xpath
, data
, specialEntries
=None): 
  27     codeStr 
= parser
.xpathToCode(xpath
) 
  28     return evalCode(codeStr
, data
, specialEntries
) 
  30 def evalCode(codeStr
, data
, specialEntries
=None): 
  31     if isinstance(data
, ObjAsDict
): 
  33     elif isinstance(data
, dict): 
  34         namespace 
= dict(data
) 
  36         namespace 
= ObjAsDict(data
) 
  38         for key
, value 
in specialEntries
.items(): 
  39             namespace
.addSpecialEntry(key
, value
) 
  40     return eval(codeStr
, {}, namespace
) 
  44     encodingString 
= sys
.getdefaultencoding() 
  45     if encodingString 
!= 'ascii': 
  46         charset 
= PY2WEB_codepages
.get(encodingString
.lower()) 
  48             charset 
= encodingString
 
  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. 
  57     if isinstance(value
, unicode): 
  58         return value
.encode('utf-8') 
  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. 
  67     if not isinstance(value
, unicode): 
  68         if not isinstance(value
, str): 
  70         return unicode(value
, 'utf-8') 
  74 def getSystemTempDir(): 
  75     return tempfile
.gettempdir() 
  77 def getEnvVar(name
, defaultVal
=None): 
  78     if os
.environ
.has_key(name
): 
  79         return os
.environ
[name
] 
  82 class ObjAsDict(UserDict
.DictMixin
): 
  84     Passing this to eval as the local variables dictionary allows the 
  85     evaluated code to access properties in the wrapped object 
  87     def __init__(self
, obj
): 
  89         self
.specialEntries 
= {} 
  91     def __getitem__(self
, key
): 
  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
) 
 102         for i 
in list(dir(self
.obj
)+self
.specialEntries
.keys()): 
 103             if i
=="__doc__" or i
=="__module__": 
 109     def addSpecialEntry(self
, key
, value
): 
 110         self
.specialEntries
[key
] = value
 
 112 global saxXMLescapeDoubleQuote
 
 113 saxXMLescapeDoubleQuote 
= {'"':'"'}
 
 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 
= {'"':'"', "'":"'"}
 
 119 global saxXMLunescapes
 
 120 saxXMLunescapes 
= {'"':'"', "'":"'"}
 
 122 def escape(data
, extraEscapes
=None): 
 123     """Escape ', ", &, <, and > in a string of data. 
 125     Basically, everything that saxutils.escape does (and this calls that, at 
 126     least for now), but with " and ' added as well. 
 128     TODO: make this faster; saxutils.escape() is really slow 
 131     global saxXMLescapeDoubleQuote
 
 132     if (extraEscapes 
== None): 
 133         extraEscapes 
= saxXMLescapeDoubleQuote
 
 134     return saxutils
.escape(data
, extraEscapes
) 
 137     """Unescape ', ", &, <, and > in a string of data. 
 139     Basically, everything that saxutils.unescape does (and this calls that, at 
 140     least for now), but with " and ' added as well. 
 142     TODO: make this faster; saxutils.unescape() is really slow 
 145     global saxXMLunescapes
 
 146     return saxutils
.unescape(data
, saxXMLunescapes
)