|
|
@ -104,7 +104,7 @@ def process_file(fn: str, out_mapping: dict[str, list[str]]) -> None:
|
|
|
|
files = list(filter(lambda x: x != '', files.split(';')))
|
|
|
|
files = list(filter(lambda x: x != '', files.split(';')))
|
|
|
|
process_assignment(pat, files, out_mapping)
|
|
|
|
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:
|
|
|
|
if filename is None:
|
|
|
|
filename = '/dev/stdout'
|
|
|
|
filename = '/dev/stdout'
|
|
|
|
path = Path(filename)
|
|
|
|
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:
|
|
|
|
with open(path, 'w') as f:
|
|
|
|
def fprint(*a, **kwa): print(*a, **kwa | {'file': f})
|
|
|
|
def fprint(*a, **kwa): print(*a, **kwa | {'file': f})
|
|
|
|
|
|
|
|
|
|
|
|
fprint('[Default Applications]')
|
|
|
|
fprint(header)
|
|
|
|
for mimetype, desktop_files in mapping.items():
|
|
|
|
for mimetype, desktop_files in mapping.items():
|
|
|
|
fprint(f"{mimetype}={';'.join(desktop_files)};")
|
|
|
|
fprint(f"{mimetype}={';'.join(desktop_files)};")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
def main():
|
|
|
|
if '--help' in sys.argv or len(sys.argv) <= 1:
|
|
|
|
import argparse
|
|
|
|
print(f'Usage: {sys.argv[0]} file ...\n\nThis script processes regex-based rules for default applications.', file=sys.stderr)
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
sys.exit()
|
|
|
|
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 = {}
|
|
|
|
out_mapping = {}
|
|
|
|
for fn in sys.argv[1:]:
|
|
|
|
for fn in args.files:
|
|
|
|
process_file(fn, out_mapping)
|
|
|
|
process_file(fn, out_mapping)
|
|
|
|
dump_mapping(out_mapping)
|
|
|
|
dump_mapping(out_mapping, **params)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: def resolve_more_specific_mimetypes(mimetype: str) -> list[str]: ...
|
|
|
|
# TODO: def resolve_more_specific_mimetypes(mimetype: str) -> list[str]: ...
|
|
|
|