You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.2 KiB
83 lines
2.2 KiB
from calculate.utils.package import PackageAtomParser
|
|
from pyparsing import Regex, Literal, LineEnd, restOfLine, LineStart
|
|
|
|
|
|
ADD, REMOVE, MOVE = range(0, 3)
|
|
|
|
|
|
def parse_add(parse_result):
|
|
target = parse_result.asDict()['target']
|
|
path = parse_result.asDict()['path']
|
|
return {'action': ADD, 'target': target, 'path': path}
|
|
|
|
|
|
def parse_remove(parse_result):
|
|
target = parse_result.asDict()['target']
|
|
path = parse_result.asDict()['path']
|
|
return {'action': REMOVE, 'target': target, 'path': path}
|
|
|
|
|
|
def parse_move(parse_result):
|
|
source = parse_result.asDict()['source']
|
|
target = parse_result.asDict()['target']
|
|
path = parse_result.asDict()['path']
|
|
return {'action': MOVE, 'source': source, 'target': target, 'path': path}
|
|
|
|
|
|
def parse_skip(parse_result):
|
|
print('skip:', parse_result)
|
|
return [None]
|
|
|
|
|
|
def parse_unexpected(parse_result):
|
|
print('unexpected')
|
|
result = parse_result[0]
|
|
if not result:
|
|
return [None]
|
|
else:
|
|
return {'unexpected': result}
|
|
|
|
|
|
atom = Regex(PackageAtomParser.atom_regex)
|
|
remove_symbol = Literal('!')
|
|
path = Regex(r'\S+')
|
|
|
|
add_parser = atom('target') + path('path') + LineEnd().suppress()
|
|
add_parser.setParseAction(parse_add)
|
|
|
|
move_parser = (atom('source') + Literal(',').suppress() + atom('target')
|
|
+ path('path') + LineEnd().suppress())
|
|
move_parser.setParseAction(parse_move)
|
|
|
|
remove_parser = (remove_symbol.suppress() + atom('target') + path('path')
|
|
+ LineEnd().suppress())
|
|
remove_parser.setParseAction(parse_remove)
|
|
|
|
empty_line = LineStart().suppress() + LineEnd().suppress()
|
|
empty_line.setParseAction(parse_skip)
|
|
|
|
comment = Literal('#') + restOfLine + LineEnd().suppress()
|
|
comment.setParseAction(parse_skip)
|
|
|
|
unexpected = restOfLine + LineEnd().suppress()
|
|
unexpected.setParseAction(parse_unexpected)
|
|
|
|
|
|
template_parser = (move_parser | remove_parser | add_parser | comment
|
|
| empty_line | unexpected)
|
|
|
|
text = '''dev-lang/python-3.6[abi_x86_64,ssl] /usr/dir/file
|
|
|
|
# comment
|
|
!sys-fs/udisks-1.0.5-r1 /usr/bin/file
|
|
|
|
ll
|
|
dev-lang/python-3.6[abi_x86_64,ssl], sys-fs/udisks-1.0.5-r1 /usr/bin/file
|
|
|
|
'''
|
|
|
|
for tokens, start, end in template_parser.scanString(text):
|
|
result = tokens[0]
|
|
if result is not None:
|
|
print(result)
|