def format(filename, env=None): """ Format a file :param filename: Path to file :type filename: str or :py:class:`pathlib.Path` :param dict[str,str] env: List of variables """ ext = str(filename).split('.')[-1] driver = DriverManager('eastern.formatter', ext) driver.propagate_map_exceptions = True env = env or {} if isinstance(filename, Path): file_obj = filename else: file_obj = Path(filename) body = file_obj.read_text() return driver(lambda ext: ext.plugin(body, file_obj.parent, env).format())
def parse_line(self, line): if '#' not in line: return line line = self.plugin.chain('line_pre_hook', line, formatter=self) before, after = line.split('#', 1) # line must only have precending spaces if not re.match(r'^\s*$', before): return line splitted = after.strip().split(' ', 1) command = splitted[0] args = [] if len(splitted) > 1: args = splitted[1] try: func = DriverManager(DRIVER_NS, command) func.propagate_map_exceptions = True except NoMatches: self.logger.debug('Command not found %s', command, exc_info=True) return line output = func(lambda ext: ext.plugin(args, line=line, formatter=self)) if output is None: output = [] elif isinstance(output, str): output = output.split(os.linesep) output = os.linesep.join([before + item for item in output]) output = self.plugin.chain('line_post_hook', output, formatter=self) return output