def __store(self, item, accumulator): if isinstance(accumulator, dict): accumulator.update(item) elif isinstance(accumulator, list): accumulator.append(item) else: errorout(5) return accumulator
def __brandnewAccForType(self, schema): assert isinstance(schema, dict) _type = self.__getType(schema) if _type == TypeSign.ARRAY: return [] elif _type == TypeSign.OBJ: return {} else: errorout(4, _type)
def renew_acc(cls, schema): Hoare.P(isinstance(schema, dict)) _type = XLSX.__get_type(schema) if _type == TypeSign.ARRAY: return [] elif _type == TypeSign.OBJ: return {} else: errorout(4, _type)
def generate_json(self, sheet_name, acc=None): """ sheet_nameが指すsheetのJSONをaccに追加する """ sheets = self.__name_to_sheets() # pyxl...Workbookで[sheet名]を持っているが、あまり高速処理向けではないため sheet_names = list(sheets.keys()) Util.sprint('in process %s'%sheet_name, self.DEBUG) Hoare.P(sheet_name in sheet_names, '"%s" not found in %s'%(sheet_name ,sheet_names)) root_sheet = sheets[sheet_name] self.check_charcode(root_sheet) columns = [] acc = [] if not acc else acc Util.sprint('I\'ll update {}'.format(acc), self.DEBUG) # COMBAK: 処理速度に問題が出るようであれば分散処理検討 # A1, B1...で場所を特定するか、indexで回すか for i, row in enumerate(root_sheet.iter_rows()): subacc = {} if self.format: self.__output_to_csv(self.format_output, root_sheet, self.char_encode) for j, cell in enumerate(row): v = cell.value # off-by-oneを気にしないといけなくなるので、col_idxではなくenumerate使う if v is None: continue # cell check if i == 0: # 初行 column check # cell.commentは必ずつくが、中身がない場合はNone if hasattr(cell, "comment") and cell.comment: # column 準備 / schemaは遅延せずこの時点で辞書として成立している事を保証 columns.append((v, Util.runtime_dict(cell.comment.text))) else: self.errorout(2, 'sheet = {}, col = {}, row = {}'.format(sheet_name, j, i)) else: # TODO: 関数へ置き換え type = array, objectのケース をカバー # 別sheet評価 if isinstance(v, str) and v.startswith(XLSX.sheet_link_sign): # COMBAK: sheetであることがarray, objectの必要条件になってしまっている # primitive配列をどう表現するかによって改修が必要 __storeに包含させる? link = v.lstrip(XLSX.sheet_link_sign) if link in sheet_names: col_name, col_schema = columns[j] Util.sprint('process %s -> %s'%(col_name, link), self.DEBUG) Util.sprint('current acc = %s'%acc, self.DEBUG) # recursive seed XLSX.__store( self.generate_leaf(root_sheet.title, col_name, link, col_schema), subacc) else: errorout(1, 'sheet = from %s to %s, col = %d, row = %d'%(sheet_name, link, j, i)) else: XLSX.__store(self.type_validator(sheet_name, v, columns[j]), subacc) # pass columns Util.check_emptyOR(lambda x: XLSX.__store(x, acc), subacc) # pass a row return acc
def __store(cls, item, accumulator): """ 評価済みが保証された値を、rootのleafに連結 accumlator: either dict or list """ if isinstance(accumulator, dict): accumulator.update(item) elif isinstance(accumulator, list): accumulator.append(item) else: errorout(5) return accumulator
def __run__(self, **kwargs): args = kwargs['args'] self.args = args fileloc = os.path.abspath(os.path.expanduser(args.input)) workpath = self.__treatFileTypes(fileloc) from xlsx import XLSX self.__print(f'Analyzing... {fileloc}') x = XLSX(workpath, args.encoding, args.output_format) # sys.setrecursionlimit(1024 * 8) j = x.generateJSON(sheet_name=args.root_sheet) with wild_open(args.output, encoding=args.encoding) as f: try: print(json.dumps(j, sort_keys=True, indent=args.human_readable) \ if args.human_readable > 0 else json.dumps(j), file=f) except: errorout(6, args.output) else: self.__print(f'Output json Success ➡️ {args.output}') pass
def __run__(self, **kwargs): args = kwargs['args'] from settings import SettingProcessor, SettingsError # 初期化モード setting_file = r'%s.yaml' % os.path.splitext( os.path.expanduser(args.template_xlsx))[0] if os.path.exists(setting_file): print(r'Setting file found on %s' % setting_file) else: errorout(8, r'Please make sure [%s] location?' % setting_file) settings = SettingProcessor(setting_file, args.template_xlsx, args.encoding) settings.check_settingfile() try: settings.create_sheets() except SettingsError as se: print(se) else: settings.save() print('Construct xlsx file Success ➡️ %s' % args.template_xlsx)
def __run__(self, **kwargs): args = kwargs['args'] from settings import SettingProcessor, SettingsError # 初期化モード setting_file = fr'{os.path.splitext(os.path.expanduser(args.template_xlsx))[0]}.yaml' if os.path.exists(setting_file): print(fr'Setting file found on {setting_file}') else: errorout(8, fr'Please make sure [{setting_file}] location?') settings = SettingProcessor(setting_file, args.template_xlsx, args.encoding) settings.checkSettingFile() try: settings.createSheets() except SettingsError as se: print(se) else: settings.save() print(f'Construct xlsx file Success ➡️ {args.template_xlsx}') pass
def __run__(self, **kwargs): args = kwargs['args'] # default option 対策 seelaso AnalyzeXSeparatedOutPath # https://github.com/setminami/Jsonica/issues/47 refactor_check(args.__class__.__name__ == 'Namespace') if not args.output_format: args.output_format = ('tsv', output_delimiters[1], './output/') self.args = args fileloc = os.path.abspath(os.path.expanduser(args.input)) workpath = Generate.__treatFileTypes(fileloc) from xlsx import XLSX self._print('Analyzing... %s' % fileloc) x = XLSX(workpath, args.encoding, args.output_format) # sys.setrecursionlimit(1024 * 8) j = x.generate_json(sheet_name=args.root_sheet) with wild_open(args.output, encoding=args.encoding) as f: try: print(json.dumps(j, sort_keys=True, indent=args.human_readable) \ if args.human_readable > 0 else json.dumps(j), file=f) except: errorout(6, args.output) else: self._print('Output json Success ➡️ %s' % args.output) Util.sprint('XXX %s XXX' % x.piled_schema, self.DEBUG)
def __treatFileTypes(self, file): if file.endswith('.xlsx'): return file else: errorout(7, f'{file} format is not supported yet.')
def __treatFileTypes(cls, file): if file.endswith('.xlsx'): return file else: errorout(7, '%s format is not supported yet.' % file)