IFCPatch can now be used as a library or as a CLI

This commit is contained in:
Dion Moult
2020-07-25 18:38:35 +10:00
parent 2a7c406412
commit 019f460af2
+19 -16
View File
@@ -5,53 +5,56 @@ import ifcopenshell
import logging import logging
import argparse import argparse
def execute(args): def execute(args, is_library=None):
logging.basicConfig(filename=args.log, filemode='a', level=logging.DEBUG) logging.basicConfig(filename=args['log'], filemode='a', level=logging.DEBUG)
logger = logging.getLogger('IFCPatch') logger = logging.getLogger('IFCPatch')
print('# Loading IFC file ...') print('# Loading IFC file ...')
ifc_file = ifcopenshell.open(args.input) ifc_file = ifcopenshell.open(args['input'])
print('# Loading patch recipe ...') print('# Loading patch recipe ...')
patcher = getattr(__import__(f'recipes.{args.recipe}'), args.recipe).Patcher( patcher = getattr(__import__('recipes.{}'.format(args['recipe'])), args['recipe']).Patcher(
args.input, ifc_file, logger, args.arguments) args['input'], ifc_file, logger, args['arguments'])
print('# Patching ...') print('# Patching ...')
patcher.patch() patcher.patch()
if is_library is not None:
return ifc_file
print('# Writing patched file ...') print('# Writing patched file ...')
if not args.output: if not args['output']:
args.output = args.input args['output'] = args['input']
ifc_file.write(args.output) ifc_file.write(args.output)
parser = argparse.ArgumentParser( if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Patches IFC files to fix badly formatted data') description='Patches IFC files to fix badly formatted data')
parser.add_argument( parser.add_argument(
'-i', '-i',
'--input', '--input',
type=str, type=str,
required=True, required=True,
help='The IFC file to patch') help='The IFC file to patch')
parser.add_argument( parser.add_argument(
'-o', '-o',
'--output', '--output',
type=str, type=str,
help='The output file to save the patched IFC') help='The output file to save the patched IFC')
parser.add_argument( parser.add_argument(
'-r', '-r',
'--recipe', '--recipe',
type=str, type=str,
required=True, required=True,
help='Name of the recipe to use when patching') help='Name of the recipe to use when patching')
parser.add_argument( parser.add_argument(
'-l', '-l',
'--log', '--log',
type=str, type=str,
help='Specify a log file', help='Specify a log file',
default='ifcpatch.log') default='ifcpatch.log')
parser.add_argument( parser.add_argument(
'-a', '-a',
'--arguments', '--arguments',
nargs='+', nargs='+',
help='Specify custom arguments to the patch recipe') help='Specify custom arguments to the patch recipe')
args = parser.parse_args() args = vars(parser.parse_args())
execute(args) execute(args)
print('# All tasks are complete :-)') print('# All tasks are complete :-)')