| 1 |
from c_common.fsutil import match_glob as _match_glob |
| 2 |
from .parser import parse as _parse |
| 3 |
from .preprocessor import get_preprocessor as _get_preprocessor |
| 4 |
|
| 5 |
|
| 6 |
def parse_file(filename, *, |
| 7 |
match_kind=None, |
| 8 |
get_file_preprocessor=None, |
| 9 |
file_maxsizes=None, |
| 10 |
): |
| 11 |
if get_file_preprocessor is None: |
| 12 |
get_file_preprocessor = _get_preprocessor() |
| 13 |
yield from _parse_file( |
| 14 |
filename, match_kind, get_file_preprocessor, file_maxsizes) |
| 15 |
|
| 16 |
|
| 17 |
def parse_files(filenames, *, |
| 18 |
match_kind=None, |
| 19 |
get_file_preprocessor=None, |
| 20 |
file_maxsizes=None, |
| 21 |
): |
| 22 |
if get_file_preprocessor is None: |
| 23 |
get_file_preprocessor = _get_preprocessor() |
| 24 |
for filename in filenames: |
| 25 |
try: |
| 26 |
yield from _parse_file( |
| 27 |
filename, match_kind, get_file_preprocessor, file_maxsizes) |
| 28 |
except Exception: |
| 29 |
print(f'# requested file: <{filename}>') |
| 30 |
raise # re-raise |
| 31 |
|
| 32 |
|
| 33 |
def _parse_file(filename, match_kind, get_file_preprocessor, maxsizes): |
| 34 |
srckwargs = {} |
| 35 |
maxsize = _resolve_max_size(filename, maxsizes) |
| 36 |
if maxsize: |
| 37 |
srckwargs['maxtext'], srckwargs['maxlines'] = maxsize |
| 38 |
|
| 39 |
# Preprocess the file. |
| 40 |
preprocess = get_file_preprocessor(filename) |
| 41 |
preprocessed = preprocess() |
| 42 |
if preprocessed is None: |
| 43 |
return |
| 44 |
|
| 45 |
# Parse the lines. |
| 46 |
srclines = ((l.file, l.data) for l in preprocessed if l.kind == 'source') |
| 47 |
for item in _parse(srclines, **srckwargs): |
| 48 |
if match_kind is not None and not match_kind(item.kind): |
| 49 |
continue |
| 50 |
if not item.filename: |
| 51 |
raise NotImplementedError(repr(item)) |
| 52 |
yield item |
| 53 |
|
| 54 |
|
| 55 |
def _resolve_max_size(filename, maxsizes): |
| 56 |
for pattern, maxsize in (maxsizes.items() if maxsizes else ()): |
| 57 |
if _match_glob(filename, pattern): |
| 58 |
break |
| 59 |
else: |
| 60 |
return None |
| 61 |
if not maxsize: |
| 62 |
return None, None |
| 63 |
maxtext, maxlines = maxsize |
| 64 |
if maxtext is not None: |
| 65 |
maxtext = int(maxtext) |
| 66 |
if maxlines is not None: |
| 67 |
maxlines = int(maxlines) |
| 68 |
return maxtext, maxlines |
| 69 |
|
| 70 |
|
| 71 |
def parse_signature(text): |
| 72 |
raise NotImplementedError |
| 73 |
|
| 74 |
|
| 75 |
# aliases |
| 76 |
from .info import resolve_parsed |