コード例 #1
0
ファイル: test_safer.py プロジェクト: jayd2446/safer
    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()
コード例 #2
0
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)
コード例 #3
0
ファイル: wikipedia.py プロジェクト: rec/nc
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')
コード例 #4
0
ファイル: test_safer.py プロジェクト: jayd2446/safer
 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
コード例 #5
0
    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()
コード例 #6
0
 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'
コード例 #7
0
    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']
コード例 #8
0
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('}')
コード例 #9
0
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))
コード例 #10
0
ファイル: __main__.py プロジェクト: rec/backer
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))
コード例 #11
0
ファイル: __main__.py プロジェクト: rec/backer
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'
コード例 #12
0
ファイル: test_safer.py プロジェクト: jayd2446/safer
 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'
コード例 #13
0
ファイル: versy.py プロジェクト: rec/versy
def _printer(file):
    with safer.printer(file) as _print:
        yield _print
コード例 #14
0
 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()
コード例 #15
0
 def test_printer(self):
     with safer.printer(self.filename) as print:
         print('hello')
     assert self.filename.read_text() == 'hello\n'
コード例 #16
0
def main():
    with safer.printer(README_FILE) as print:
        make_doc(print)