def test_predefined_json(self): some_class = NestedClass() json = parser.to_json(some_class) self.assertEqual( json, "{ \"py/object\": \"NestedClass\", \"inner_objects\": null }")
def test_default_dumps_comparison(self): some_object = ["Hello", False, 4] default_json = json.dumps(some_object).replace(" ", "") my_json = parser.to_json(some_object).replace(" ", "") self.assertEqual(default_json, my_json)
def test_restore_type(self): some_class = NestedClass() json = parser.to_json(some_class) restored_class = parser.from_json(json, globals()) self.assertEqual(type(some_class), type(restored_class))
def test_two_way_list(self): list = ["Wow", None, "Boom", 5, False, 4.2] json = parser.to_json(list) restored_list = parser.from_json(json, globals()) self.assertEqual(list, restored_list)
def test_nested_class(self): nested_class = NestedClass([NestedClass(), NestedClass()]) json = parser.to_json(nested_class) restored_class = parser.from_json(json, globals()) self.assertEqual(len(nested_class.inner_objects), len(restored_class.inner_objects))
def test_default_loads_comparison(self): some_object = [None, 5, ["hey", "Ouch"]] default_json = json.dumps(some_object) my_json = parser.to_json(some_object) default_restored = json.loads(default_json) my_restored = parser.from_json(my_json, globals()) self.assertEqual(default_restored, my_restored)
def open_modules(root, context, debug): # Include contexts ky = context.copy() for k in ky: v = ky[k] if k in [ 'modules', ]: for el in list(set(v)): #print("opening %s %s, root %s" % (k, el, root)) if el in context and 'root' in context[el]: p = context[el]['root'][0] else: p = el p = os.path.join(root, p) p = os.path.expanduser(p) if not os.path.exists(p) and debug: warn("Directory not found: %s" % p) p1 = os.path.join(p, 'module.conf') if not os.path.exists(p1): continue with open(p1) as f: data = f.read() if not el in context: context[el] = {} error = to_json(context[el], data) if (error == False): return False # Now open modules of contexts ky = context.copy() for k in ky: v = ky[k] if type(v) == dict: if 'root' in v: p = v['root'][0] else: p = k p = os.path.join(root, p) open_modules(p, v, debug) return True
def process(): input_data = parser.generate_data() groupwise_host_info = build_groupwise_host_info(input_data) jsonOutput = parser.to_json(groupwise_host_info) return jsonOutput
if __name__ == '__main__': tests_result = unittest.TextTestRunner(verbosity=0).run(suite()) if not tests_result.wasSuccessful(): exit(1) kevin_the_kid = Person("Kevin", 13, None, None) duck_hunter = Job("CuackDuck Co.", "Hunter") oliver_the_teen = Person("Oliver", 19.5, duck_hunter, None) sony_programmer = Job("Sony", "Programmer") johny_the_daddy = Person("John", 45, sony_programmer, [ kevin_the_kid, oliver_the_teen]) json = parser.to_json(johny_the_daddy) print("Json:\n"+json, end="\n\n") restored_johny = parser.from_json(json, globals()) print("Is Johny old: " + str(restored_johny.isOld), end="\n") print("How many kids does Johny have: " + str(len(restored_johny.kids)), end="\n") print("Where does Johny work: at " + restored_johny.job.company_name, end="\n\n") raw_collection_json = parser.to_json([False, True]) print("Json:\n"+raw_collection_json, end="\n\n") print("Collection: " + str(parser.from_json(raw_collection_json, globals)))
parser.add_argument("-j", help="Parallel build", action="store_true") parser.add_argument("conf", help="A working config file") args = parser.parse_args() config_file = args.conf # Open main configuration file provided by app if not os.path.exists(config_file): print("Error: config file not found: %s" % config_file) sys.exit(2) with open(config_file) as f: data = f.read() config = {} error = to_json(config, data) if (error == False): sys.exit(2) # Merge-in module's configuration files into the main config root = '' open_modules(root, config, args.d) # Process options directive handle_options(root, config) result = proc_directives('', config) # Compile and link the app if not build(result, debug=args.d, parallel=args.j): sys.exit(3)