def __init__(self, watchdir, use_observer=True, observer=None): super(ProjDirFactory, self).__init__() self._lock = threading.RLock() self.observer = None self.watchdir = watchdir self._files = {} # mapping of file pathnames to _FileInfo objects self._classes = {} # mapping of class names to _FileInfo objects try: added_set = set() changed_set = set() deleted_set = set() modeldir = watchdir + PROJ_DIR_EXT if modeldir not in sys.path: sys.path = [modeldir] + sys.path logger.info("added %s to sys.path" % modeldir) for pyfile in find_files(self.watchdir, "*.py"): self.on_modified(pyfile, added_set, changed_set, deleted_set) if use_observer: self._start_observer(observer) self.publish_updates(added_set, changed_set, deleted_set) else: # sometimes for debugging/testing it's easier to turn observer off self.observer = None except Exception as err: self._error(str(err)) logger.error(str(err))
def on_deleted(self, fpath, deleted_set): with self._lock: if os.path.isdir(fpath): for pyfile in find_files(self.watchdir, "*.py"): self.on_deleted(pyfile, deleted_set) else: finfo = self._files.get(fpath) if finfo: deleted_set.update(finfo.classes.keys()) self._remove_fileinfo(fpath)
def on_deleted(self, fpath, deleted_set): with self._lock: if os.path.isdir(fpath): for pyfile in find_files(self.watchdir, "*.py"): self.on_deleted(pyfile, deleted_set) else: try: del self.imported[fpath] except KeyError: pass visitor = self.analyzer.fileinfo[fpath][0] deleted_set.update(visitor.classes.keys()) self.analyzer.remove_file(fpath)
def __init__(self, watchdir, use_observer=True, observer=None): super(ProjDirFactory, self).__init__() self._lock = threading.RLock() self.watchdir = watchdir self.imported = {} # imported files vs (module, ctor dict) try: self.analyzer = PythonSourceTreeAnalyser() added_set = set() changed_set = set() deleted_set = set() for pyfile in find_files(self.watchdir, "*.py"): self.on_modified(pyfile, added_set, changed_set, deleted_set) if use_observer: self._start_observer(observer) self.publish_updates(added_set, changed_set, deleted_set) else: self.observer = None # sometimes for debugging/testing it's easier to turn observer off except Exception as err: logger.error(str(err))