def build_return(filepath): cached_file = cache.get(filepath) cached_file.dependancies.add(origin) return { 'url': filepath.resolve().relative_to(root.resolve()), **cached_file.headers }
def on_any_event(event): filepath = Path(event.src_path) if event.event_type == 'modified' and filepath in cache: # Skip duplicate events that are within a second of each other if datetime.now() - cache.get(filepath).updated > timedelta( seconds=1): print(f'[*] Recompiling {filepath}') WatchHandler._rebuild(filepath)
def _rebuild(filepath: Path): '''Rebuild the given path and everything that it depends on.''' cache.update(filepath) build_page(filepath, Path(environment.output_root).joinpath(filepath), environment) for dependancy in cache.get(filepath).dependancies: print('[*] Updating dependancy: {}'.format(dependancy)) WatchHandler._rebuild(dependancy)
def build_page(filepath: Path, destination: Path, env: Environment): '''Builds and saves a single page. Params: filepath: Source filepath destination: Destination filepath env: Environment dictionary ''' if filepath.suffix in ['.html']: try: output = env.get_template(filepath).render({ **env.params, **cache.get(filepath).headers, **build_runtime(filepath, env.input_root) }).encode() except TemplateError as err: print('[!] Unable to compile {}: {}'.format(filepath, err)) return else: output = filepath.read_bytes() destination.parent.mkdir(parents=True, exist_ok=True) destination.write_bytes(output)
def get_source(self, environment, template): path = self.root.joinpath(template) file_obj = cache.get(path) return file_obj.content, str( path), lambda: file_obj.updated == cache.get(path).updated