def _assert_invariants(self): iter = self.l.__iter__() try: oldx = iter.next() while True: x = iter.next() # self.l is required to be sorted _assert(x >= oldx, x, oldx) # every element of self.l is required to appear in self.d _assert(self.d.has_key(x[1]), x) oldx = x except StopIteration: pass for ( k, v, ) in self.d.iteritems(): i = bisect_left(self.l, ( v, k, )) while (self.l[i][0] is not v) or (self.l[i][1] is not k): i += 1 _assert(i < len(self.l), i, len(self.l), k, v, self.l) _assert(self.l[i][0] is v, i, v, l=self.l, d=self.d) _assert(self.l[i][1] is k, i, k, l=self.l, d=self.d) return True
def g(n): assert inputs.has_key(n) _assert(isinstance(inputs[n][0], str), "Required to be a string.", inputs[n][0]) assert len(inputs[n][0]) == n _assert(isinstance(inputs[n][1], str), "Required to be a string.", inputs[n][1]) assert len(inputs[n][1]) == n for SF in SFUNCS: assert f(inputs[n][0], inputs[n][1]) == SF(inputs[n][0], inputs[n][1]) return f(inputs[n][0], inputs[n][1])
def _test_popitem(self, C): c = C({"a": 1}) res = c.popitem() _assert(res == ( "a", 1, ), C, c, res) self.assertTrue(res == ( "a", 1, ))
def _test_popitem(self, C): c = C({"a": 1}) res = c.popitem() _assert(res == ( "a", 1, ), C, c, res) self.failUnless(res == ( "a", 1, ))
def g(n): assert n in inputs _assert(isinstance(inputs[n][0], str), "Required to be a string.", inputs[n][0]) assert len(inputs[n][0]) == n _assert(isinstance(inputs[n][1], str), "Required to be a string.", inputs[n][1]) assert len(inputs[n][1]) == n for SF in SFUNCS: assert f(inputs[n][0], inputs[n][1]) == SF(inputs[n][0], inputs[n][1]) return f(inputs[n][0], inputs[n][1])
def watch(self, path, mask=IN_WATCH_MASK, autoAdd=False, callbacks=None, recursive=False): precondition(isinstance(autoAdd, bool), autoAdd=autoAdd) precondition(isinstance(recursive, bool), recursive=recursive) assert autoAdd == False path_u = path.path if not isinstance(path_u, unicode): path_u = path_u.decode('utf-8') _assert(isinstance(path_u, unicode), path_u=path_u) if path_u not in self._callbacks.keys(): self._callbacks[path_u] = callbacks or [] self._watches[path_u] = self._observer.schedule( INotifyEventHandler(path_u, mask, self._callbacks[path_u], self._pending_delay), path=path_u, recursive=False, )
def should_ignore_file(path_u): precondition(isinstance(path_u, unicode), path_u=path_u) for suffix in IGNORE_SUFFIXES: if path_u.endswith(suffix): return True while path_u != u"": oldpath_u = path_u path_u, tail_u = os.path.split(path_u) if tail_u.startswith(u"."): return True if path_u == oldpath_u: return True # the path was absolute _assert(len(path_u) < len(oldpath_u), path_u=path_u, oldpath_u=oldpath_u) return False
def _assert_invariants(self): iter = self.l.__iter__() try: oldx = iter.next() while True: x = iter.next() # self.l is required to be sorted _assert(x >= oldx, x, oldx) # every element of self.l is required to appear in self.d _assert(self.d.has_key(x[1]), x) oldx =x except StopIteration: pass for (k, v,) in self.d.iteritems(): i = bisect_left(self.l, (v, k,)) while (self.l[i][0] is not v) or (self.l[i][1] is not k): i += 1 _assert(i < len(self.l), i, len(self.l), k, v, self.l) _assert(self.l[i][0] is v, i, v, l=self.l, d=self.d) _assert(self.l[i][1] is k, i, k, l=self.l, d=self.d) return True
def test_commonsuffix(self): _assert( strutil.commonsuffix([ "foo", "foobarooo", "foosplat", ]) == '', strutil.commonsuffix([ "foo", "foobarooo", "foosplat", ])) _assert( strutil.commonsuffix([ "foo", "foobarooo", "foosplato", ]) == 'o', strutil.commonsuffix([ "foo", "foobarooo", "foosplato", ])) _assert( strutil.commonsuffix([ "foo", "foobarooofoo", "foosplatofoo", ]) == 'foo', strutil.commonsuffix([ "foo", "foobarooofoo", "foosplatofoo", ]))
def test_commonprefix(self): _assert( strutil.commonprefix([ "foo", "foobarooo", "foosplat", ]) == 'foo', strutil.commonprefix([ "foo", "foobarooo", "foosplat", ])) _assert( strutil.commonprefix([ "foo", "afoobarooo", "foosplat", ]) == '', strutil.commonprefix([ "foo", "afoobarooo", "foosplat", ]))
def watch(self, path, mask=IN_WATCH_MASK, autoAdd=False, callbacks=None, recursive=False): precondition(self._state == NOT_STARTED, "watch() can only be called before startReading()", state=self._state) precondition(self._filter is None, "only one watch is supported") precondition(isinstance(autoAdd, bool), autoAdd=autoAdd) precondition(isinstance(recursive, bool), recursive=recursive) #precondition(autoAdd == recursive, "need autoAdd and recursive to be the same", autoAdd=autoAdd, recursive=recursive) self._path = path path_u = path.path if not isinstance(path_u, unicode): path_u = path_u.decode(sys.getfilesystemencoding()) _assert(isinstance(path_u, unicode), path_u=path_u) self._filter = FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE if mask & (IN_ACCESS | IN_CLOSE_NOWRITE | IN_OPEN): self._filter = self._filter | FILE_NOTIFY_CHANGE_LAST_ACCESS if mask & IN_ATTRIB: self._filter = self._filter | FILE_NOTIFY_CHANGE_ATTRIBUTES | FILE_NOTIFY_CHANGE_SECURITY self._recursive = TRUE if recursive else FALSE self._callbacks = callbacks or [] self._hDirectory = _open_directory(path_u)
def test_split(self): _assert( strutil.split_on_newlines("x\r\ny") == [ "x", "y", ], strutil.split_on_newlines("x\r\ny")) _assert( strutil.split_on_newlines("x\r\ny\r\n") == [ "x", "y", '', ], strutil.split_on_newlines("x\r\ny\r\n")) _assert( strutil.split_on_newlines("x\n\ny\n\n") == [ "x", '', "y", '', '', ], strutil.split_on_newlines("x\n\ny\n\n"))
def deep_swap(struc): if is_numeric(struc[0]): _assert(len(struc) == 2, (type(struc), repr(struc))) _assert(is_numeric(struc[1]), (type(struc), repr(struc))) return swap(struc) return [deep_swap(sub) for sub in struc]
def deep_swap(struc): if is_numeric(struc[0]): _assert (len(struc) == 2, (type(struc), repr(struc))) _assert (is_numeric(struc[1]), (type(struc), repr(struc))) return swap(struc) return [deep_swap(sub) for sub in struc]
def _help_test_is_power_of_k(self, k): for i in range(2, 40): _assert(mathutil.is_power_of_k(k**i, k), k, i)
def _help_test_eq_but_notis(self, klass): d = klass({'a': 3, 'b': EqButNotIs(3), 'c': 3}) d.pop('b') d.clear() d['a'] = 3 d['b'] = EqButNotIs(3) d['c'] = 3 d.pop('b') d.clear() d['b'] = EqButNotIs(3) d['a'] = 3 d['c'] = 3 d.pop('b') d.clear() d['a'] = EqButNotIs(3) d['c'] = 3 d['a'] = 3 d.clear() fake3 = EqButNotIs(3) fake7 = EqButNotIs(7) d[fake3] = fake7 d[3] = 7 d[3] = 8 _assert([x for x in iter(d.values()) if x is 8]) _assert([x for x in iter(d.values()) if x is fake7]) _assert(not [x for x in iter(d.values()) if x is 7 ]) # The real 7 should have been ejected by the d[3] = 8. _assert([x for x in iter(d.keys()) if x is fake3]) _assert([x for x in iter(d.keys()) if x is 3]) d[fake3] = 8 d.clear() d[3] = 7 fake3 = EqButNotIs(3) fake7 = EqButNotIs(7) d[fake3] = fake7 d[3] = 8 _assert([x for x in iter(d.values()) if x is 8]) _assert([x for x in iter(d.values()) if x is fake7]) _assert(not [x for x in iter(d.values()) if x is 7 ]) # The real 7 should have been ejected by the d[3] = 8. _assert([x for x in iter(d.keys()) if x is fake3]) _assert([x for x in iter(d.keys()) if x is 3]) d[fake3] = 8
def _test_popitem(self, C): c = C({"a": 1}) res = c.popitem() _assert(res == ("a", 1,), C, c, res) self.failUnless(res == ("a", 1,))
def test_commonsuffix(self): _assert(strutil.commonsuffix(["foo","foobarooo", "foosplat",]) == '', strutil.commonsuffix(["foo","foobarooo", "foosplat",])) _assert(strutil.commonsuffix(["foo","foobarooo", "foosplato",]) == 'o', strutil.commonsuffix(["foo","foobarooo", "foosplato",])) _assert(strutil.commonsuffix(["foo","foobarooofoo", "foosplatofoo",]) == 'foo', strutil.commonsuffix(["foo","foobarooofoo", "foosplatofoo",]))
def test_commonprefix(self): _assert(strutil.commonprefix(["foo","foobarooo", "foosplat",]) == 'foo', strutil.commonprefix(["foo","foobarooo", "foosplat",])) _assert(strutil.commonprefix(["foo","afoobarooo", "foosplat",]) == '', strutil.commonprefix(["foo","afoobarooo", "foosplat",]))
def test_split(self): _assert(strutil.split_on_newlines("x\r\ny") == ["x", "y",], strutil.split_on_newlines("x\r\ny")) _assert(strutil.split_on_newlines("x\r\ny\r\n") == ["x", "y", '',], strutil.split_on_newlines("x\r\ny\r\n")) _assert(strutil.split_on_newlines("x\n\ny\n\n") == ["x", '', "y", '', '',], strutil.split_on_newlines("x\n\ny\n\n"))
def __init__(self, size=1024): self.size = size self.buffer = create_string_buffer(size) address = addressof(self.buffer) _assert(address & 3 == 0, "address 0x%X returned by create_string_buffer is not DWORD-aligned" % (address,)) self.data = None
def _help_test_eq_but_notis(self, klass): d = klass({'a': 3, 'b': EqButNotIs(3), 'c': 3}) d.pop('b') d.clear() d['a'] = 3 d['b'] = EqButNotIs(3) d['c'] = 3 d.pop('b') d.clear() d['b'] = EqButNotIs(3) d['a'] = 3 d['c'] = 3 d.pop('b') d.clear() d['a'] = EqButNotIs(3) d['c'] = 3 d['a'] = 3 d.clear() fake3 = EqButNotIs(3) fake7 = EqButNotIs(7) d[fake3] = fake7 d[3] = 7 d[3] = 8 _assert(filter(lambda x: x is 8, d.itervalues())) _assert(filter(lambda x: x is fake7, d.itervalues())) _assert(not filter(lambda x: x is 7, d.itervalues())) # The real 7 should have been ejected by the d[3] = 8. _assert(filter(lambda x: x is fake3, d.iterkeys())) _assert(filter(lambda x: x is 3, d.iterkeys())) d[fake3] = 8 d.clear() d[3] = 7 fake3 = EqButNotIs(3) fake7 = EqButNotIs(7) d[fake3] = fake7 d[3] = 8 _assert(filter(lambda x: x is 8, d.itervalues())) _assert(filter(lambda x: x is fake7, d.itervalues())) _assert(not filter(lambda x: x is 7, d.itervalues())) # The real 7 should have been ejected by the d[3] = 8. _assert(filter(lambda x: x is fake3, d.iterkeys())) _assert(filter(lambda x: x is 3, d.iterkeys())) d[fake3] = 8
def _help_test_eq_but_notis(self, klass): d = klass({'a': 3, 'b': EqButNotIs(3), 'c': 3}) d.pop('b') d.clear() d['a'] = 3 d['b'] = EqButNotIs(3) d['c'] = 3 d.pop('b') d.clear() d['b'] = EqButNotIs(3) d['a'] = 3 d['c'] = 3 d.pop('b') d.clear() d['a'] = EqButNotIs(3) d['c'] = 3 d['a'] = 3 d.clear() fake3 = EqButNotIs(3) fake7 = EqButNotIs(7) d[fake3] = fake7 d[3] = 7 d[3] = 8 _assert(filter(lambda x: x is 8, d.itervalues())) _assert(filter(lambda x: x is fake7, d.itervalues())) _assert(not filter(lambda x: x is 7, d.itervalues()) ) # The real 7 should have been ejected by the d[3] = 8. _assert(filter(lambda x: x is fake3, d.iterkeys())) _assert(filter(lambda x: x is 3, d.iterkeys())) d[fake3] = 8 d.clear() d[3] = 7 fake3 = EqButNotIs(3) fake7 = EqButNotIs(7) d[fake3] = fake7 d[3] = 8 _assert(filter(lambda x: x is 8, d.itervalues())) _assert(filter(lambda x: x is fake7, d.itervalues())) _assert(not filter(lambda x: x is 7, d.itervalues()) ) # The real 7 should have been ejected by the d[3] = 8. _assert(filter(lambda x: x is fake3, d.iterkeys())) _assert(filter(lambda x: x is 3, d.iterkeys())) d[fake3] = 8
def _thread(self): try: _assert(self._filter is not None, "no watch set") # To call Twisted or Tahoe APIs, use reactor.callFromThread as described in # <http://twistedmatrix.com/documents/current/core/howto/threading.html>. fni = FileNotifyInformation() while True: self._state = STARTED action = start_action( action_type=u"read-changes", directory=self._path.path, recursive=self._recursive, filter=self._filter, ) try: with action: fni.read_changes(self._hDirectory, self._recursive, self._filter) except WindowsError as e: self._state = STOPPING if self._check_stop(): return for info in fni: path = self._path.preauthChild(info.filename) # FilePath with Unicode path if info.action == FILE_ACTION_MODIFIED and path.isdir(): Message.log( message_type=u"filtering-out", info=repr(info), ) continue else: Message.log( message_type=u"processing", info=repr(info), ) #mask = _action_to_inotify_mask.get(info.action, IN_CHANGED) @log_call( action_type=MAYBE_NOTIFY.action_type, include_args=[], include_result=False, ) def _do_pending_calls(): event_mask = IN_CHANGED self._pending_call = None for path1 in self._pending: if self._callbacks: for cb in self._callbacks: try: with CALLBACK(inotify_events=event_mask): cb(None, path1, event_mask) except Exception as e2: log.err(e2) self._pending = set() def _maybe_notify(path2): if path2 not in self._pending: self._pending.add(path2) if self._state not in [STOPPING, STOPPED]: _do_pending_calls() # if self._pending_call is None and self._state not in [STOPPING, STOPPED]: # self._pending_call = reactor.callLater(self._pending_delay, _do_pending_calls) reactor.callFromThread(_maybe_notify, path) if self._check_stop(): return except Exception as e: log.err(e) self._state = STOPPED raise
def _help_test_eq_but_notis(self, klass): d = klass({'a': 3, 'b': EqButNotIs(3), 'c': 3}) d.pop('b') d.clear() d['a'] = 3 d['b'] = EqButNotIs(3) d['c'] = 3 d.pop('b') d.clear() d['b'] = EqButNotIs(3) d['a'] = 3 d['c'] = 3 d.pop('b') d.clear() d['a'] = EqButNotIs(3) d['c'] = 3 d['a'] = 3 d.clear() fake3 = EqButNotIs(3) fake7 = EqButNotIs(7) d[fake3] = fake7 d[3] = 7 d[3] = 8 _assert(any(x for x in d.values() if x is 8)) _assert(any(x for x in d.values() if x is fake7)) _assert(not any( x for x in d.values() if x is 7)) # The real 7 should have been ejected by the d[3] = 8. _assert(any(x for x in d if x is fake3)) _assert(any(x for x in d if x is 3)) d[fake3] = 8 d.clear() d[3] = 7 fake3 = EqButNotIs(3) fake7 = EqButNotIs(7) d[fake3] = fake7 d[3] = 8 _assert(any(x for x in d.values() if x is 8)) _assert(any(x for x in d.values() if x is fake7)) _assert(not any( x for x in d.values() if x is 7)) # The real 7 should have been ejected by the d[3] = 8. _assert(any(x for x in d if x is fake3)) _assert(any(x for x in d if x is 3)) d[fake3] = 8