def load_subdirs(cls, dirs, config, nav_path='', parent=None): """ Helper for loading chapters from sub dirs. """ for dir_path in dirs: dir_slug = fs.basename(dir_path) dir_nav_path = nav_path and '/'.join((nav_path, dir_slug)) or dir_slug cls.load_tree(dir_path, config, nav_path=dir_nav_path, parent=parent)
def load_meta(self, file_path): """ Load meta and update base chapter info. """ self.meta = self.extract_meta(file_path) self.file_path = file_path self.file_name = fs.basename(file_path) self.title = self.meta.get('title', self.title) self.sorting = self.meta.get('sorting', self.title) self.icon = self.meta.get('icon', None)
def on_any_event(self, event): """ Filter file/dir events and schedule project rebuild. """ if isinstance(event, self.events_file): name = fs.basename(event.src_path) if any([fs.match(name, mask) for mask in WATCH_PATTERNS_DEFAULT]): log.success(" %s" % "file updated: %s" % event.src_path) self.observer.needs_rebuild() elif isinstance(event, self.events_dirs): log.success(" %s" % "directory updated: %s" % event.src_path) self.observer.needs_rebuild()
def load_tree(cls, path, config, nav_path='', parent=None): """ Load chapters tree recursivelly. """ index_name = config.get('index', cls.INDEX_FILE) files, dirs = [], [] # print ("Load tree: %s, %s" % (path, config)) # scan sub-dirs and files for name in os.listdir(path): full_path = fs.join(path, name) # dirs if fs.isdir(full_path): if not cls.is_name_masked(name): dirs.append(full_path) # files elif fs.isfile(full_path): if cls.is_file_to_render(name): files.append(name) # no index = no data here, but don't give up, try scan deeper! if not index_name in files: cls.load_subdirs(dirs, config, nav_path=nav_path, parent=parent) return # index found = create chapter! initial_title = fs.basename(path).capitalize() chapter = cls(config, title=initial_title, nav_path=nav_path, is_index=True) chapter.load_meta(file_path=fs.join(path, index_name)) files.remove(index_name) # add to parent if parent: parent.add_child(chapter) # load files cls.load_files(path, files, config, nav_path=nav_path, parent=chapter) # load dirs cls.load_subdirs(dirs, config, nav_path=nav_path, parent=chapter) # print ("chapter: %s, files: %s, dirs: %s" % (str(chapter), files, dirs)) return chapter
def script_name(self): return fs.basename(sys.argv[0])