def convert_robot_data_file(original_filepath): from robot.tidy import Tidy from base64 import b64encode tidy = Tidy(format='robot') if get_robot_version_num() < (3, 2) else Tidy() converted_content = tidy.file(original_filepath) if sys.version_info < (3, 0, 0): return b64encode(converted_content.encode('utf-8')) else: return str(b64encode(bytes(converted_content, 'utf-8')), 'utf-8')
def pretty(self, args, cell): """pretty-print the robot text""" tidier = Tidy() with tempfile.TemporaryDirectory() as td: tdp = Path(td) it = tdp / "ugly.robot" it.write_text(cell, **ENC) tidier.inplace(str(it)) cell = it.read_text(**ENC) lexer = RobotFrameworkLexer() formatter = HtmlFormatter(cssclass=self.PRETTY_CLASS, style=args.style) css = formatter.get_style_defs(f".{self.PRETTY_CLASS}") highlighted = highlight(cell, lexer, formatter) html = HTML(f""" <ul><li> <details> <summary>Formatted Robot Code</summary> <style>{css}</style>{highlighted} </details> </li></ul> """) return html
def convert_robot_data_file(original_filepath): from robot.tidy import Tidy from base64 import b64encode target_filepath = original_filepath.rsplit('.', 1)[0] + '.robot' converted_content = Tidy(format='robot').file(original_filepath) if sys.version_info < (3, 0, 0): return b64encode(converted_content.encode('utf-8')) else: return str(b64encode(bytes(converted_content, 'utf-8')), 'utf-8')
def robot_source_format(source, space_count=4): if not source.strip(): return None from robot.tidy import Tidy t = Tidy(space_count=space_count) from io import StringIO s = StringIO() if isinstance(source, bytes): source = source.decode("utf-8", "replace") s.write(source) s.seek(0) formatted = t.file(s) if not formatted: return None return formatted
def main(argv=None): parser = argparse.ArgumentParser() parser.add_argument('filenames', nargs='*', help='Filenames to run') parser.add_argument('--use-pipes', action='store_true', dest='use_pipes', default=False) parser.add_argument('--space-count', type=int, dest='space_count', default=4) args = parser.parse_args(argv) tidier = Tidy(use_pipes=args.use_pipes, space_count=args.space_count) for filename in args.filenames: try: tidier.inplace(filename) except (DataError, OSError): pass return 0