1
0
Fork 0

Add suport for generating other sections in defaulter

master
Pavel 'LEdoian' Turinský 2 years ago
parent 02b54a9418
commit a270780487

@ -104,7 +104,7 @@ def process_file(fn: str, out_mapping: dict[str, list[str]]) -> None:
files = list(filter(lambda x: x != '', files.split(';')))
process_assignment(pat, files, out_mapping)
def dump_mapping(mapping: dict[str, list[str]], filename: str = None) -> None:
def dump_mapping(mapping: dict[str, list[str]], filename: str = None, header: str = '[Default Applications]') -> None:
if filename is None:
filename = '/dev/stdout'
path = Path(filename)
@ -115,19 +115,35 @@ def dump_mapping(mapping: dict[str, list[str]], filename: str = None) -> None:
with open(path, 'w') as f:
def fprint(*a, **kwa): print(*a, **kwa | {'file': f})
fprint('[Default Applications]')
fprint(header)
for mimetype, desktop_files in mapping.items():
fprint(f"{mimetype}={';'.join(desktop_files)};")
def main():
if '--help' in sys.argv or len(sys.argv) <= 1:
print(f'Usage: {sys.argv[0]} file ...\n\nThis script processes regex-based rules for default applications.', file=sys.stderr)
sys.exit()
import argparse
parser = argparse.ArgumentParser()
parser.description = 'This script processes regex-based rules for default applications.'
parser.add_argument('--add', help="Generate [Added Associations] section instead", action='store_true')
parser.add_argument('--remove', help="Generate [Removed Associations] section instead", action='store_true')
parser.add_argument('files', help="Source of the rules", nargs='+')
parser.usage='%(prog)s [--add|--remove] files ...'
parser.epilog = 'By default a [Default Applications] section is created. There is no option to apply this behaviour.'
args = parser.parse_args()
if sum([args.add, args.remove]) > 1:
print('Cannot generate conflicting sections!', file=sys.stderr)
sys.exit(1)
params = {}
if args.add: params['header'] = '[Added Associations]'
if args.remove: params['header'] = '[Removed Associations]'
out_mapping = {}
for fn in sys.argv[1:]:
for fn in args.files:
process_file(fn, out_mapping)
dump_mapping(out_mapping)
dump_mapping(out_mapping, **params)
# TODO: def resolve_more_specific_mimetypes(mimetype: str) -> list[str]: ...

Loading…
Cancel
Save