/usr/lib/python2.7/dist-packages/manuel/capture.py is in python-manuel 1.8.0-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | import manuel
import re
import string
import textwrap
CAPTURE_DIRECTIVE = re.compile(
r'^(?P<indent>(\t| )*)\.\.\s*->\s*(?P<name>\S+).*$',
re.MULTILINE)
class Capture(object):
def __init__(self, name, block):
self.name = name
self.block = block
def normalize_whitespace(s):
return s.replace('\t', ' '*8) # turn tabs into spaces
@manuel.timing(manuel.EARLY)
def find_captures(document):
while True:
regions = document.find_regions(CAPTURE_DIRECTIVE)
if not regions:
break
region = regions[-1]
# note that start and end have different bases, "start" is the offset
# from the begining of the region, "end" is a document line number
end = region.lineno - 2
indent = region.start_match.group('indent')
indent = normalize_whitespace(indent)
def indent_matches(line):
"""Is the indentation of a line match what we're looking for?"""
line = normalize_whitespace(line)
if not line.strip():
# the line consists entirely of whitespace (or nothing at all),
# so is not considered to be of the appropriate indentation
return False
if line.startswith(indent):
if line[len(indent)] not in string.whitespace:
return True
# if none of the above found the indentation to be a match, it is
# not a match
return False
# now that we've extracted the information we need, lets slice up the
# document's regions to match
for candidate in document:
if candidate.lineno >= region.lineno:
break
found_region = candidate
lines = found_region.source.splitlines()
if found_region.lineno + len(lines) < end:
raise RuntimeError('both start and end lines must be in the '
'same region')
start = None
for offset, line in reversed(list(enumerate(lines))):
if offset > end - found_region.lineno:
continue
if indent_matches(line):
break
start = offset + 1
if start is None:
raise RuntimeError("couldn't find the start of the block; "
"improper indentation of capture directive?")
_, temp_region = document.split_region(found_region,
found_region.lineno+start)
# there are some extra lines in the new region, trim them off
final_region, _ = document.split_region(temp_region, end+1)
document.remove_region(final_region)
name = region.start_match.group('name')
block = textwrap.dedent(final_region.source)
document.claim_region(region)
region.parsed = Capture(name, block)
def store_capture(region, document, globs):
if not isinstance(region.parsed, Capture):
return
globs[region.parsed.name] = region.parsed.block
class Manuel(manuel.Manuel):
def __init__(self):
manuel.Manuel.__init__(self, [find_captures], [store_capture])
|