Package muntjac :: Module util
[hide private]
[frames] | no frames]

Source Code for Module muntjac.util

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  import os 
 17  import sys 
 18  import locale 
 19  import collections 
 20   
 21  import paste.webkit 
 22   
 23  from babel.core import Locale, UnknownLocaleError 
 24   
 25   
 26  # Copied from paste.webkit.wsgiapp to avoid paste.deploy dependency. 
27 -def sys_path_install():
28 webware_dir = os.path.join(os.path.dirname(paste.webkit.__file__), 29 'FakeWebware') 30 if webware_dir not in sys.path: 31 sys.path.append(webware_dir)
32 33
34 -def loadClass(className):
35 return (lambda x: getattr(__import__(x.rsplit('.', 1)[0], 36 fromlist=x.rsplit('.', 1)[0]), 37 x.split('.')[-1]))(className)
38 39
40 -def getSuperClass(cls):
41 return cls.__mro__[1] if len(cls.__mro__) > 1 else None
42 43
44 -def clsname(cls):
45 """@return: fully qualified name of given class""" 46 return cls.__module__ + "." + cls.__name__
47 48
49 -def fullname(obj):
50 """@return: fully qualified name of given object's class""" 51 return clsname(obj.__class__)
52 53
54 -def totalseconds(td):
55 return (td.microseconds + (td.seconds + td.days * 24 * 3600) * 1e6) / 1e6
56 57
58 -def defaultLocale():
59 try: 60 lang, _ = locale.getdefaultlocale() 61 except Exception: 62 lang = None 63 64 if lang is not None: 65 try: 66 return Locale.parse(lang) 67 except UnknownLocaleError: 68 pass 69 else: 70 try: 71 return Locale.default() 72 except UnknownLocaleError: 73 return Locale('en', 'US')
74 75
76 -class EventObject(object):
77
78 - def __init__(self, source):
79 self._source = source
80 81
82 - def getSource(self):
83 return self._source
84 85
86 -class IEventListener(object):
87 pass
88 89 90 KEY, PREV, NEXT = range(3) 91 92 # http://code.activestate.com/recipes/576694/
93 -class OrderedSet(collections.MutableSet):
94
95 - def __init__(self, iterable=None):
96 self.end = end = [] 97 end += [None, end, end] # sentinel node for doubly linked list 98 self.map = {} # key --> [key, prev, next] 99 if iterable is not None: 100 self |= iterable
101
102 - def __len__(self):
103 return len(self.map)
104
105 - def __contains__(self, key):
106 return key in self.map
107
108 - def add(self, key):
109 if key not in self.map: 110 end = self.end 111 curr = end[PREV] 112 curr[NEXT] = end[PREV] = self.map[key] = [key, curr, end]
113
114 - def discard(self, key):
115 if key in self.map: 116 key, prev, nxt = self.map.pop(key) 117 prev[NEXT] = nxt 118 nxt[PREV] = prev
119
120 - def __iter__(self):
121 end = self.end 122 curr = end[NEXT] 123 while curr is not end: 124 yield curr[KEY] 125 curr = curr[NEXT]
126
127 - def __reversed__(self):
128 end = self.end 129 curr = end[PREV] 130 while curr is not end: 131 yield curr[KEY] 132 curr = curr[PREV]
133
134 - def pop(self, last=True):
135 if not self: 136 raise KeyError('set is empty') 137 key = next(reversed(self)) if last else next(iter(self)) 138 self.discard(key) 139 return key
140
141 - def __repr__(self):
142 if not self: 143 return '%s()' % (self.__class__.__name__,) 144 return '%s(%r)' % (self.__class__.__name__, list(self))
145
146 - def __eq__(self, other):
147 if isinstance(other, OrderedSet): 148 return len(self) == len(other) and list(self) == list(other) 149 return set(self) == set(other)
150
151 - def __del__(self):
152 self.clear() # remove circular references
153