getting file size for all dict files to be downloaded. coming to be 400mb or so.
[worddb.git] / libs / openid / oidutil.py
blob5246d1605902f008d1c7bbaea42990a89e03aa7a
1 """This module contains general utility code that is used throughout
2 the library.
4 For users of this library, the C{L{log}} function is probably the most
5 interesting.
6 """
8 __all__ = ['log', 'appendArgs', 'toBase64', 'fromBase64', 'autoSubmitHTML']
10 import binascii
11 import sys
12 import urlparse
14 from urllib import urlencode
16 elementtree_modules = [
17 'lxml.etree',
18 'xml.etree.cElementTree',
19 'xml.etree.ElementTree',
20 'cElementTree',
21 'elementtree.ElementTree',
24 def autoSubmitHTML(form, title='OpenID transaction in progress'):
25 return """
26 <html>
27 <head>
28 <title>%s</title>
29 </head>
30 <body onload="document.forms[0].submit();">
32 <script>
33 var elements = document.forms[0].elements;
34 for (var i = 0; i < elements.length; i++) {
35 elements[i].style.display = "none";
37 </script>
38 </body>
39 </html>
40 """ % (title, form)
42 def importElementTree(module_names=None):
43 """Find a working ElementTree implementation, trying the standard
44 places that such a thing might show up.
46 >>> ElementTree = importElementTree()
48 @param module_names: The names of modules to try to use as
49 ElementTree. Defaults to C{L{elementtree_modules}}
51 @returns: An ElementTree module
52 """
53 if module_names is None:
54 module_names = elementtree_modules
56 for mod_name in module_names:
57 try:
58 ElementTree = __import__(mod_name, None, None, ['unused'])
59 except ImportError:
60 pass
61 else:
62 # Make sure it can actually parse XML
63 try:
64 ElementTree.XML('<unused/>')
65 except (SystemExit, MemoryError, AssertionError):
66 raise
67 except:
68 why = sys.exc_info()[1]
69 log('Not using ElementTree library %r because it failed to '
70 'parse a trivial document: %s' % (mod_name, why))
71 else:
72 return ElementTree
73 else:
74 raise ImportError('No ElementTree library found. '
75 'You may need to install one. '
76 'Tried importing %r' % (module_names,)
79 def log(message, level=0):
80 """Handle a log message from the OpenID library.
82 This implementation writes the string it to C{sys.stderr},
83 followed by a newline.
85 Currently, the library does not use the second parameter to this
86 function, but that may change in the future.
88 To install your own logging hook::
90 from openid import oidutil
92 def myLoggingFunction(message, level):
93 ...
95 oidutil.log = myLoggingFunction
97 @param message: A string containing a debugging message from the
98 OpenID library
99 @type message: str
101 @param level: The severity of the log message. This parameter is
102 currently unused, but in the future, the library may indicate
103 more important information with a higher level value.
104 @type level: int or None
106 @returns: Nothing.
109 sys.stderr.write(message)
110 sys.stderr.write('\n')
112 def appendArgs(url, args):
113 """Append query arguments to a HTTP(s) URL. If the URL already has
114 query arguemtns, these arguments will be added, and the existing
115 arguments will be preserved. Duplicate arguments will not be
116 detected or collapsed (both will appear in the output).
118 @param url: The url to which the arguments will be appended
119 @type url: str
121 @param args: The query arguments to add to the URL. If a
122 dictionary is passed, the items will be sorted before
123 appending them to the URL. If a sequence of pairs is passed,
124 the order of the sequence will be preserved.
125 @type args: A dictionary from string to string, or a sequence of
126 pairs of strings.
128 @returns: The URL with the parameters added
129 @rtype: str
131 if hasattr(args, 'items'):
132 args = args.items()
133 args.sort()
134 else:
135 args = list(args)
137 if len(args) == 0:
138 return url
140 if '?' in url:
141 sep = '&'
142 else:
143 sep = '?'
145 # Map unicode to UTF-8 if present. Do not make any assumptions
146 # about the encodings of plain bytes (str).
147 i = 0
148 for k, v in args:
149 if type(k) is not str:
150 k = k.encode('UTF-8')
152 if type(v) is not str:
153 v = v.encode('UTF-8')
155 args[i] = (k, v)
156 i += 1
158 return '%s%s%s' % (url, sep, urlencode(args))
160 def toBase64(s):
161 """Represent string s as base64, omitting newlines"""
162 return binascii.b2a_base64(s)[:-1]
164 def fromBase64(s):
165 try:
166 return binascii.a2b_base64(s)
167 except binascii.Error, why:
168 # Convert to a common exception type
169 raise ValueError(why[0])
171 class Symbol(object):
172 """This class implements an object that compares equal to others
173 of the same type that have the same name. These are distict from
174 str or unicode objects.
177 def __init__(self, name):
178 self.name = name
180 def __eq__(self, other):
181 return type(self) is type(other) and self.name == other.name
183 def __ne__(self, other):
184 return not (self == other)
186 def __hash__(self):
187 return hash((self.__class__, self.name))
189 def __repr__(self):
190 return '<Symbol %s>' % (self.name,)