def test_file_gen(self): # 正常系 xml xml_data = ('<?xml version="1.0" encoding="UTF-8" ?>' '<foods>' '<food>' '<name>banana</name>' '<color>yellow</color>' '</food>' '<food>' '<name>apple</name>' '<color>red</color>' '</food>' '</foods>') with tempfile.NamedTemporaryFile(mode='w', suffix='.xml') as fp: fp.write(xml_data) fp.seek(0) xml_path = (Path(fp.name),) for i in Action.file_gen(xml_path): self.assertEqual(i, xml_data) # 正常系 json json_data = ('{"Sport":[{"D1":"Japan"},' '{"WRC":"Safari","F1":"Monaco"}]}') expected = json.loads(json_data, encoding='utf8') with tempfile.NamedTemporaryFile(mode='w', suffix='.json') as fp: fp.write(json_data) fp.seek(0) json_path = (Path(fp.name),) for i in Action.file_gen(json_path): self.assertEqual(i, expected) # JSONデコードエラー時 json_data = ('{"Sport":[{"D1":"Japan"},' '{"WRC":"Safari","F1":"Monaco}]}') with tempfile.NamedTemporaryFile(mode='w', suffix='.json') as fp: fp.write(json_data) fp.seek(0) json_path = (Path(fp.name),) with self.assertRaises(SystemExit): for i in Action.file_gen(json_path): print(i)
if args.result_dir is not None: p = Path(args.result_dir) if not p.exists() and not p.is_dir(): sys.exit('パスが不正です') # iniファイル読み込み settings = configparser.ConfigParser() settings.read(Path.cwd() / 'ini' / 'db.ini') con = dict([i for i in settings['DB'].items()]) db = DB(con) jm = JsonManager() convert = Convert() json_files = Action.files_read(args.path, 'json') for file in Action.file_gen(json_files): # Ednman用にjsonをコンバート converted_edman = convert.dict_to_edman(file, mode=args.structure) # コンバート結果を保存する場合 # jm.save({'converted_edman': converted_edman}, args.result_dir, # name='edman_json_list', date=True) # DBへインサート inserted_report = db.insert(converted_edman) if args.result_dir is not None: jm.save({'inserted_report': inserted_report}, args.result_dir, name='inserted', date=True)