import os import re import textwrap from sybil import Region, Example, Document from testfixtures import diff FILEBLOCK_START = re.compile(r'^\.\.\s*topic::?\s*(.+)\b', re.MULTILINE) FILEBLOCK_END = re.compile(r'(\n\Z|\n(?=\S))') CLASS = re.compile(r'\s+:class:\s*(read|write)-file') class FileBlock(object): def __init__(self, path, content, action): self.path, self.content, self.action = path, content, action class FileParser(object): """ A `Sybil <http://sybil.readthedocs.io>`__ parser that parses certain ReST sections to read and write files in the configured :class:`~testfixtures.TempDirectory`. :param name: This is the name of the :class:`~testfixtures.TempDirectory` to use in the Sybil test namespace. """ def __init__(self, name: str): self.name = name def __call__(self, document: Document): for start_match, end_match, source in document.find_region_sources( FILEBLOCK_START, FILEBLOCK_END ): lines = source.splitlines() class_ = CLASS.match(lines[1]) if not class_: continue index = 3 if lines[index].strip() == '::': index += 1 source = textwrap.dedent('\n'.join(lines[index:])).lstrip() if source[-1] != '\n': source += '\n' parsed = FileBlock( path=start_match.group(1), content=source, action=class_.group(1) ) yield Region( start_match.start(), end_match.end(), parsed, self.evaluate ) def evaluate(self, example: Example): block: FileBlock = example.parsed dir = example.namespace[self.name] if block.action == 'read': actual = dir.read(block.path, 'ascii').replace(os.linesep, '\n') if actual != block.content: return diff( block.content, actual, 'File %r, line %i:' % (example.path, example.line), 'Reading from "%s":' % dir.as_string(block.path) ) if block.action == 'write': dir.write(block.path, block.content, 'ascii')
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
__pycache__ | Folder | 0755 |
|
|
tests | Folder | 0755 |
|
|
__init__.py | File | 1.19 KB | 0644 |
|
comparison.py | File | 39.14 KB | 0644 |
|
compat.py | File | 224 B | 0644 |
|
components.py | File | 1.31 KB | 0644 |
|
datetime.py | File | 24.42 KB | 0644 |
|
django.py | File | 2.88 KB | 0644 |
|
logcapture.py | File | 10.75 KB | 0644 |
|
mock.py | File | 1.21 KB | 0644 |
|
outputcapture.py | File | 4.69 KB | 0644 |
|
popen.py | File | 9.89 KB | 0644 |
|
replace.py | File | 12.15 KB | 0644 |
|
resolve.py | File | 2.05 KB | 0644 |
|
rmtree.py | File | 2.52 KB | 0644 |
|
shouldraise.py | File | 3.58 KB | 0644 |
|
shouldwarn.py | File | 2.21 KB | 0644 |
|
sybil.py | File | 2.28 KB | 0644 |
|
tempdirectory.py | File | 12.89 KB | 0644 |
|
twisted.py | File | 4.8 KB | 0644 |
|
utils.py | File | 2.74 KB | 0644 |
|
version.txt | File | 6 B | 0644 |
|