def test_html_printer(self): """Tests the HTML printer. """ target_filepath = utils.get_resource_path( "tests/test_files/persons_html.html") input_filepath = utils.get_resource_path( "tests/test_files/persons_json.json") file_format = formats_registry.get(extension=input_filepath.suffix) printer = printers_registry.get(name="HTML") # read / write data input_data = file_format.read_file(input_filepath) printer.print_data(input_data) # compare input / output file for parity self.assertTrue( filecmp.cmp(ustr(printer.temp_filepath), ustr(target_filepath)))
def _write_file(self, filepath, data, **options): options = { "sort_keys": True, "indent": 4, "separators": (',', ': '), "ensure_ascii": False } json_str = ustr(json.dumps(data, **options)) filepath.write_text(json_str)
def list(self, **filter): """Lists registered classes. Output can be filtered by supplying filter options via ``filter`` Args: **filter(:obj:`dict`): filters classes based on class members / values, e.g. "name" == "JSON" Returns: :obj:`list` of :obj:`class` """ results = list(self._classes) for item in self._classes: for attr, filter_value in filter.items(): item_value = getattr(item, attr, "") if callable(item_value): item_value = item_value() if ustr(item_value).lower() != ustr(filter_value).lower(): results.remove(item) return [self._get_instance(cls) for cls in results]
def test_stdout_printer(self): """Tests the Stdout table printer. """ target_filepath = utils.get_resource_path( "tests/test_files/persons_stdout.txt") input_filepath = utils.get_resource_path( "tests/test_files/persons_json.json") file_format = formats_registry.get(extension=input_filepath.suffix) printer = printers_registry.get(name="Stdout") input_data = file_format.read_file(input_filepath) with utils.temp_dir("printtest") as temp_dir: test_filepath = temp_dir.joinpath("persons_stdout.txt") with utils.stdout_to_file(test_filepath): printer.print_data(input_data) # compare input / output file for parity self.assertTrue( filecmp.cmp(ustr(test_filepath), ustr(target_filepath)))
def test_read_write(self): """Tests read / write for all file formats. """ samples_dir = utils.get_resource_path("tests/test_files") with utils.temp_dir("rwtest") as test_dir: for file_format in formats_registry.list(): filename = "persons_{name}{ext}".format( name=file_format.name().lower(), ext=file_format.extension()) input_filepath = samples_dir.joinpath(filename) output_filepath = test_dir.joinpath(filename) # write read sample, write to temp file input_data = file_format.read_file(input_filepath) file_format.write_file(output_filepath, input_data) # compare input / output file for parity self.assertTrue( filecmp.cmp(ustr(input_filepath), ustr(output_filepath)), "Read / Write test failed for format: '{}'".format( file_format))
def temp_dir(directory_name): """Context manager providing a temp directory that will be deleted on exit. Args: directory_name(:obj:`str`): directory name """ dir_path = Path.home().joinpath("tmp", "personio", directory_name) try: dir_path.mkdir(parents=True, exist_ok=True) yield dir_path finally: if dir_path.exists(): shutil.rmtree(ustr(dir_path))
def _print_data(self, data, **options): # generate HTML code header_titles = Person.fields() html_str = '<table>\n<tr><th>{}</th></tr>\n'.format('</th><th>'.join(header_titles)) for item in [item.values() for item in data]: html_str += '<tr><td>{}</td></tr>\n'.format('</td><td>'.join(item)) html_str += '</table>' # write HTML code to file print("Write temp HTML file and open it in webbrowser: '{}'".format(self.temp_filepath)) self.temp_filepath.parent.mkdir(parents=True, exist_ok=True) self.temp_filepath.write_text(ustr(html_str)) # open in webbrowser webbrowser.open(self.temp_filepath.as_uri())
def open_link_from_email(self, email_index, link_index=0): """Open link URL from given ``link_index`` in email message body of given ``email_index``. Returns HTML content of opened link URL. Arguments: - ``email_index``: An email index to identity the email message. - ``link_index``: The link index to be open. (Default 0) Examples: | Open Link From Email | | Open Link From Email | 1 | """ urls = self.get_links_from_email(email_index) if len(urls) > link_index: resp = urlopen(urls[link_index]) content_type = resp.headers.getheader('content-type') if content_type: enc = content_type.split('charset=')[-1] return ustr(resp.read(), enc) else: return resp.read() else: raise AssertionError("Link number %i not found!" % link_index)
def _write_file(self, filepath, data, **options): json_str = ustr(json.dumps(data, ensure_ascii=False)) filepath.write_text(json_str)