def __init__(self, tree, path): self.tree = tree self.path = path self.root = VirtualDir(None, path) self.config_path = os.path.join( self.tree.get_real_path('/'), self.path.strip('/'), 'canivett.json' ) self.create_control_dir() self.create_config_dir() self.create_actions_dir() self.create_status_dir() self.init()
class BaseModule: def __init__(self, tree, path): self.tree = tree self.path = path self.root = VirtualDir(None, path) self.config_path = os.path.join( self.tree.get_real_path('/'), self.path.strip('/'), 'canivett.json' ) self.create_control_dir() self.create_config_dir() self.create_actions_dir() self.create_status_dir() self.init() def init(self): pass def destroy(self): pass def create_control_dir(self): # Virtual dir: ctl_dir = VirtualDir(self.root, '.canivett') self.control_dir = ctl_dir self.root.add_child(ctl_dir) def create_config_dir(self): config_dirname = os.path.dirname(self.config_path) if not os.path.exists(config_dirname): os.makedirs(os.path.basename(config_dirname)) # Real configuration file: if not os.path.exists(self.config_path): self.config = {} with open(self.config_path, 'w') as file_obj: file_obj.write(ultrajson.dumps(self.config)) else: with open(self.config_path) as file_obj: self.config = ultrajson.loads(file_obj.read()) # Virtual dir: cfg_dir = ConfigDir(self.control_dir, 'config', self.config, self) self.control_dir.add_child(cfg_dir) self.config_dir = cfg_dir return cfg_dir def create_actions_dir(self): actions_dir = VirtualDir(self.control_dir, 'actions') self.control_dir.add_child(actions_dir) self.actions_dir = actions_dir return actions_dir def create_status_dir(self): status_dir = VirtualDir(self.control_dir, 'status') self.control_dir.add_child(status_dir) self.status_dir = status_dir return status_dir def persist_config(self): s = ultrajson.dumps(self.config) with open(self.config_path, 'w') as file_obj: file_obj.write(s) # Events from related ConfigDir: def update_config(self, new_config): self.config = new_config self.persist_config() # Actions: def on_action(self, path, argument): relative_path = re.sub('^' + self.actions_dir.path, '', path).strip('/') print('Action "{}": {}'.format(relative_path, argument))