From 019f460af2f2b4c3777778ec4aed3f42039fa411 Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Sat, 25 Jul 2020 18:38:35 +1000 Subject: [PATCH] IFCPatch can now be used as a library or as a CLI --- src/ifcpatch/ifcpatch.py | 83 +++++++++++++++++++++------------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/src/ifcpatch/ifcpatch.py b/src/ifcpatch/ifcpatch.py index 8668e13046..00d020bcb5 100644 --- a/src/ifcpatch/ifcpatch.py +++ b/src/ifcpatch/ifcpatch.py @@ -5,53 +5,56 @@ import ifcopenshell import logging import argparse -def execute(args): - logging.basicConfig(filename=args.log, filemode='a', level=logging.DEBUG) +def execute(args, is_library=None): + logging.basicConfig(filename=args['log'], filemode='a', level=logging.DEBUG) logger = logging.getLogger('IFCPatch') print('# Loading IFC file ...') - ifc_file = ifcopenshell.open(args.input) + ifc_file = ifcopenshell.open(args['input']) print('# Loading patch recipe ...') - patcher = getattr(__import__(f'recipes.{args.recipe}'), args.recipe).Patcher( - args.input, ifc_file, logger, args.arguments) + patcher = getattr(__import__('recipes.{}'.format(args['recipe'])), args['recipe']).Patcher( + args['input'], ifc_file, logger, args['arguments']) print('# Patching ...') patcher.patch() + if is_library is not None: + return ifc_file print('# Writing patched file ...') - if not args.output: - args.output = args.input + if not args['output']: + args['output'] = args['input'] ifc_file.write(args.output) -parser = argparse.ArgumentParser( - description='Patches IFC files to fix badly formatted data') -parser.add_argument( - '-i', - '--input', - type=str, - required=True, - help='The IFC file to patch') -parser.add_argument( - '-o', - '--output', - type=str, - help='The output file to save the patched IFC') -parser.add_argument( - '-r', - '--recipe', - type=str, - required=True, - help='Name of the recipe to use when patching') -parser.add_argument( - '-l', - '--log', - type=str, - help='Specify a log file', - default='ifcpatch.log') -parser.add_argument( - '-a', - '--arguments', - nargs='+', - help='Specify custom arguments to the patch recipe') -args = parser.parse_args() +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Patches IFC files to fix badly formatted data') + parser.add_argument( + '-i', + '--input', + type=str, + required=True, + help='The IFC file to patch') + parser.add_argument( + '-o', + '--output', + type=str, + help='The output file to save the patched IFC') + parser.add_argument( + '-r', + '--recipe', + type=str, + required=True, + help='Name of the recipe to use when patching') + parser.add_argument( + '-l', + '--log', + type=str, + help='Specify a log file', + default='ifcpatch.log') + parser.add_argument( + '-a', + '--arguments', + nargs='+', + help='Specify custom arguments to the patch recipe') + args = vars(parser.parse_args()) -execute(args) + execute(args) -print('# All tasks are complete :-)') + print('# All tasks are complete :-)')