1
2
3 from twisted.internet.defer import Deferred
4 from twisted.spread import pb
5 from twisted.python import threadable
6 import time, re, string
7
9 """Returns a sorted copy of l, so that numbers in strings are sorted in the
10 proper order.
11
12 e.g. ['foo10', 'foo1', 'foo2'] will be sorted as ['foo1', 'foo2', 'foo10']
13 instead of the default ['foo1', 'foo10', 'foo2']"""
14 l = l[:]
15 def try_int(s):
16 try:
17 return int(s)
18 except:
19 return s
20 def key_func(item):
21 return [try_int(s) for s in re.split('(\d+)', item)]
22
23 keyed_l = [ (key_func(i), i) for i in l ]
24 keyed_l.sort()
25 l = [ i[1] for i in keyed_l ]
26 return l
27
28 -def now(_reactor=None):
33
44
46 """Specify a list of attributes that are 'important'. These will be used
47 for all comparison operations."""
48
49 compare_attrs = []
50
52
54 alist = [self.__class__] + \
55 [getattr(self, name, self._None) for name in self.compare_attrs]
56 return hash(tuple(map(str,alist)))
57
71
72
73
74 badchars_map = string.maketrans("\t !#$%&'()*+,./:;<=>?@[\\]^{|}~",
75 "______________________________")
77 if isinstance(str, unicode):
78 str = str.encode('utf8')
79 return str.translate(badchars_map)
80
82 if '@' not in url:
83 return url
84 if '://' not in url:
85 return url
86
87
88 protocol_url = url.split('://')
89 protocol = protocol_url[0]
90 repo_url = protocol_url[1].split('@')[-1]
91
92 return protocol + '://' + repo_url
93
95 """
96 A simple least-recently-used cache, with a fixed maximum size. Note that
97 an item's memory will not necessarily be free if other code maintains a reference
98 to it, but this class will "lose track" of it all the same. Without caution, this
99 can lead to duplicate items in memory simultaneously.
100 """
101
102 synchronized = ["get", "add"]
103
105 self._max_size = max_size
106 self._cache = {}
107 self._cached_ids = []
108
110 thing = self._cache.get(id, None)
111 if thing is not None:
112 self._cached_ids.remove(id)
113 self._cached_ids.append(id)
114 return thing
115 __getitem__ = get
116
117 - def add(self, id, thing):
118 if id in self._cache:
119 self._cached_ids.remove(id)
120 self._cached_ids.append(id)
121 return
122 while len(self._cached_ids) >= self._max_size:
123 del self._cache[self._cached_ids.pop(0)]
124 self._cache[id] = thing
125 self._cached_ids.append(id)
126 __setitem__ = add
127
128 threadable.synchronize(LRUCache)
129
130
132 """Cast X to a str if it is not None"""
133 if x is not None and not isinstance(x, str):
134 return str(x)
135 return x
136
137
138
139
140 try:
141 import json
142 except ImportError:
143 import simplejson as json
144 try:
145 _tmp = json.loads
146 except AttributeError:
147 import warnings
148 import sys
149 warnings.warn("Use simplejson, not the old json module.")
150 sys.modules.pop('json')
151 import simplejson as json
152
153
154
155
159 NotABranch = NotABranch()
160