def test_printer_errors(self): with TemporaryDirectory() as td: filename = td + '/test.txt' with safer.printer(filename): pass with self.assertRaises(IOError) as m: with safer.printer(filename, 'r'): pass assert 'not open' in m.exception.args[0].lower() with self.assertRaises(IOError) as m: with safer.printer(filename, 'rb'): pass assert 'not open' in m.exception.args[0].lower()
def write_256(): with safer.printer(FILE) as print: wikipedia.print_header('an algorithm', print) print('COLORS = (') count = 0 cube = range(0, 256, 51) # (0, 95, 135, 175, 215, 255) colors_seen = set(xterm.COLORS) def add_name(r, g, b): color = COLORS.closest((r, g, b)) name = str(color) index = 0 while name in colors_seen: name = '%s %s' % (color, chr(index + ord('B'))) index += 1 colors_seen.add(name) print(f" ('{name}', ({r}, {g}, {b})),") nonlocal count count += 1 for r in cube: for g in cube: for b in cube: add_name(r, g, b) for i in range(12, 248, 10): add_name(i, i, i) print(')') print() print('PRESERVE_CAPITALIZATION = True') old_print('count =', count)
def write_colors(): for name, (title, *urls) in SOURCES.items(): filename = (PALETTE_DIR / name).with_suffix('.py') print('Creating', filename, '...', end='') with safer.printer(filename, 'w') as pr: colors(urls, title, pr) print('done')
def test_make_doc(self): with TemporaryDirectory() as td: filename = td + '/README.rst' with safer.printer(filename) as print: doc_safer.make_doc(print) actual = read_text(filename) in_repo = read_text(doc_safer.README_FILE) assert actual == in_repo
def test_printer_errors(self): with safer.printer(self.filename): pass with self.assertRaises(IOError) as m: with safer.printer(self.filename, 'r'): pass assert 'not open' in m.exception.args[0].lower() with self.assertRaises(IOError) as m: with safer.printer(self.filename, 'rb'): pass assert 'not open' in m.exception.args[0].lower() with self.assertRaises(ValueError) as m: with safer.printer(self.filename, 'wb'): pass assert 'binary mode' in m.exception.args[0].lower()
def test_line_buffering(self, safer_open): temp_file = safer_open is not safer.open sp = safer.printer(FILENAME, buffering=1, temp_file=temp_file) with sp as print: print('foo') print('b', end='') print('ar') assert FILENAME.read_text() == 'foo\nbar\n'
def test_printer_dry_run_callable(self): results = [] assert not self.filename.exists() with safer.printer(self.filename, dry_run=results.append) as print: assert not self.filename.exists() print('hello') assert not self.filename.exists() assert results == ['hello\n']
def _write_file(name, values): name = ''.join(i if i.isalnum() else '_' for i in name) name = name.replace('__', '_').lower() with safer.printer(DIR / (name + '.py')) as print: wikipedia.print_header('ANSI escape codes', print) print('COLORS = {') for color, value in values.items(): print(f" '{color}': {value},") print('}')
def print_re_key(shields): with safer.printer(FILE) as print: first = True for root, items in sorted(shields.items()): if first: first = False else: print() print('%s:' % root) for alt, url, category in sorted(items): print("- ['%s', '%s', '%s']" % (url, alt, category))
def write_docs(readme=README): files = ROOT / 'backer' / 'task' stems = (f.stem for f in files.iterdir() if f.suffix == '.py') files = sorted(s for s in stems if not s.startswith('_')) lines = [] for line in readme.open(): lines.append(line) if line.lower().startswith(DIVIDER): break with safer.printer(readme) as print: print(*lines, sep='\n') print('-' * len(lines[-1]) + '\n') for name in files: print(_describe_one(name))
def _describe_one(name, print=print): cls = task_class(name) desc = describe.describe(cls) intro, body = _get_doc(cls) filename = (ROOT / 'doc' / name).with_suffix('.rst') with safer.printer(filename) as print: title = '%s: %s' % (name, intro) print(title) print('-' * len(title)) print() for j, (name, field) in enumerate(desc.items()): j and print() print('``{name}: {type} = {default!r}``'.format(**field)) for line in _split(field['doc'], 76): print(' ', line) return f'``{name}``:\n {intro}\n'
def test_printer(self): with TemporaryDirectory() as td: filename = td + '/test.txt' with safer.printer(filename) as print: print('hello') assert read_text(filename) == 'hello\n'
def _printer(file): with safer.printer(file) as _print: yield _print
def test_printer_dry_run(self): assert not self.filename.exists() with safer.printer(self.filename, dry_run=True) as print: assert not self.filename.exists() print('hello') assert not self.filename.exists()
def test_printer(self): with safer.printer(self.filename) as print: print('hello') assert self.filename.read_text() == 'hello\n'
def main(): with safer.printer(README_FILE) as print: make_doc(print)