X-Git-Url: http://dolda2000.com/gitweb/?a=blobdiff_plain;ds=sidebyside;f=pdm%2Fperf.py;h=7aef95a40b15b9e1e313889a868ecbf3f6d9125e;hb=HEAD;hp=f0e6c89f8c713e137575107721b15ac796d6d807;hpb=0e2552cf694e8bb14c9b6202bfc46b6c50dd09fc;p=pdm.git diff --git a/pdm/perf.py b/pdm/perf.py index f0e6c89..307dccb 100644 --- a/pdm/perf.py +++ b/pdm/perf.py @@ -51,6 +51,13 @@ __all__ = ["attrinfo", "simpleattr", "valueattr", "eventobj", "staticdir", "event", "procevent", "startevent", "finishevent"] +class error(Exception): + pass +class nosuchname(LookupError, error): + pass +class nosuchproto(error): + pass + class attrinfo(object): """The return value of the `attrinfo' method on `attr' objects as described in L{pdm.srv.perf}. @@ -63,7 +70,7 @@ class attrinfo(object): class perfobj(object): def __init__(self, *args, **kwargs): - super(perfobj, self).__init__() + super().__init__() def pdm_protocols(self): return [] @@ -74,7 +81,7 @@ class simpleattr(perfobj): read. """ def __init__(self, func, info = None, *args, **kwargs): - super(simpleattr, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.func = func if info is None: info = attrinfo() @@ -87,7 +94,7 @@ class simpleattr(perfobj): return self.info def pdm_protocols(self): - return super(simpleattr, self).pdm_protocols() + ["attr"] + return super().pdm_protocols() + ["attr"] class valueattr(perfobj): """An implementation of the `attr' interface, which is initialized @@ -95,7 +102,7 @@ class valueattr(perfobj): updates to the value are reflected in subsequent reads. """ def __init__(self, init, info = None, *args, **kwargs): - super(valueattr, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.value = init if info is None: info = attrinfo() @@ -108,7 +115,7 @@ class valueattr(perfobj): return self.info def pdm_protocols(self): - return super(valueattr, self).pdm_protocols() + ["attr"] + return super().pdm_protocols() + ["attr"] class eventobj(perfobj): """An implementation of the `event' interface. It keeps track of @@ -116,7 +123,7 @@ class eventobj(perfobj): subscribers when submitted with the `notify' method. """ def __init__(self, *args, **kwargs): - super(eventobj, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.subscribers = set() def subscribe(self, cb): @@ -129,13 +136,13 @@ class eventobj(perfobj): def notify(self, event): """Notify all subscribers with the given event object.""" - for cb in self.subscribers: + for cb in list(self.subscribers): try: cb(event) except: pass def pdm_protocols(self): - return super(eventobj, self).pdm_protocols() + ["event"] + return super().pdm_protocols() + ["event"] class staticdir(perfobj): """An implementation of the `dir' interface. Put other PERF @@ -143,7 +150,7 @@ class staticdir(perfobj): return them to requesting clients. """ def __init__(self, *args, **kwargs): - super(staticdir, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) self.map = {} def __setitem__(self, name, ob): @@ -159,13 +166,13 @@ class staticdir(perfobj): return self.map.get(name, default) def listdir(self): - return self.map.keys() + return list(self.map.keys()) def lookup(self, name): return self.map[name] def pdm_protocols(self): - return super(staticdir, self).pdm_protocols() + ["dir"] + return super().pdm_protocols() + ["dir"] class simplefunc(perfobj): """An implementation of the `invoke' interface. Put callables in @@ -173,7 +180,7 @@ class simplefunc(perfobj): when invoked with the corresponding method name. Additionally, it updates itself with any keyword-arguments it is initialized with.""" def __init__(self, *args, **kwargs): - super(simplefunc, self).__init__(*args) + super().__init__(*args) self.map = {} self.map.update(kwargs) @@ -186,10 +193,10 @@ class simplefunc(perfobj): def invoke(self, method, *args, **kwargs): if method not in self.map: raise AttributeError(method) - self.map[method](*args, **kwargs) + return self.map[method](*args, **kwargs) def pdm_protocols(self): - return super(simplefunc, self).pdm_protocols() + ["invoke"] + return super().pdm_protocols() + ["invoke"] class event(object): """This class should be subclassed by all event objects sent via @@ -233,7 +240,7 @@ class procevent(event): `finishevent' emitted when the connection is closed. """ def __init__(self, id): - super(procevent, self).__init__() + super().__init__() if isinstance(id, procevent): self.id = id.id else: @@ -242,7 +249,7 @@ class procevent(event): class startevent(procevent): """A subclass of `procevent'. See its documentation for details.""" def __init__(self): - super(startevent, self).__init__(getprocid()) + super().__init__(getprocid()) class finishevent(procevent): """A subclass of `procevent'. Intended to be emitted when a @@ -251,7 +258,7 @@ class finishevent(procevent): distinction is meaningful. The `start' parameter should be the `startevent' instance used when the process was initiated.""" def __init__(self, start, aborted = False): - super(finishevent, self).__init__(start) + super().__init__(start) self.aborted = aborted sysres = staticdir() @@ -278,4 +285,12 @@ sysinfo["uname"] = simpleattr(func = os.uname) sysinfo["hostname"] = simpleattr(func = socket.gethostname) sysinfo["platform"] = valueattr(init = sys.platform) -sysctl = simplefunc(exit=lambda status=0: os._exit(status)) +def reload(modname): + mod = sys.modules.get(modname) + if mod is None: + raise ValueError(modname) + import importlib + importlib.reload(mod) + +sysctl = simplefunc(exit=lambda status=0: os._exit(status), + reload=reload)