def main(): """ Description: generate c language for parse json map string object Interface: None History: 2019-06-17 """ parser = argparse.ArgumentParser( prog='generate.py', usage='%(prog)s [options] path [path ...]', description='Generate C header and source from json-schema') parser.add_argument('path', nargs='+', help='File or directory to parse') parser.add_argument( '--root', required=True, help= 'All schema files must be placed in root directory or sub-directory of root," \ " and naming of C variables is started from this path') parser.add_argument('--gen-common', action='store_true', help='Generate json_common.c and json_common.h') parser.add_argument( '--gen-ref', action='store_true', help='Generate reference file defined in schema with key \"$ref\"') parser.add_argument( '-r', '--recursive', action='store_true', help='Recursively generate all schema files in directory') parser.add_argument( '--out', help= 'Specify a directory to save C header and source(default is current directory)' ) args = parser.parse_args() if not args.root: print('Missing root path, see help') sys.exit(1) root_path = os.path.realpath(args.root) if not os.path.exists(root_path): print('Root %s is not exist') % args.root sys.exit(1) MyRoot.root_path = root_path if args.out: srcpath = helpers.FilePath(args.out) else: srcpath = helpers.FilePath(os.getcwd()) if not os.path.exists(srcpath.name): os.makedirs(srcpath.name) if args.gen_common: gen_common_files(srcpath.name) handle_files(args, srcpath)
def schema_from_file(filepath, srcpath): """ Description: generate c language for parse json map string object Interface: None History: 2019-06-17 """ schemapath = helpers.FilePath(filepath) prefix = get_prefix_from_file(schemapath.name) header = helpers.FilePath(os.path.join(srcpath, prefix + ".h")) source = helpers.FilePath(os.path.join(srcpath, prefix + ".c")) schema_info = helpers.SchemaInfo(schemapath, header, source, prefix, srcpath) return schema_info
def handle_files(args, srcpath): """ Description: generate c language for parse json map string object Interface: None History: 2019-06-17 """ for path in args.path: gen_ref = args.gen_ref schemapath = helpers.FilePath(path) handle_single_file(args, srcpath, gen_ref, schemapath)