def find_value(target_file, search_params, encoding=None, trap_undefined_error=False): # auto encoding if encoding is None: encoding = utility.check_encode(target_file) collector = ListCollector(target_file, encoding=encoding) v_params = [ValidatedParam(p) for p in search_params] param_elements = [[] for _ in v_params] for i, element in collector.elements_many(v_params): p = v_params[i] if p.template: element = _apply_template(p.template, element) element = _apply_cast(p.value_type, element, trap_undefined_error) # pyyamlはOrderedDict対応していないため、一般のdictに変更。 # 入力の順番通りにする方法1つはpyyamlをruamel.yamlに更新 param_elements[i].append(dict(element)) # 収集した複数のelementを結ぶ key_value_table = {} for s_param, elements in zip(search_params, param_elements): if not elements: if trap_undefined_error: raise KeyError('no matches found for {}'.format(s_param)) else: # elementがある場合のみ、結果の配列を出力に入れる tmp_table = utility.path2dict(s_param, elements) utility.dict_list_marge(key_value_table, tmp_table) return key_value_table
def find_value(target_file, search_params, encoding=None, trap_undefined_error=False): key_value_table = {} if encoding is None: encoding = utility.check_encode(target_file) with io.open(target_file, 'r', encoding=encoding) as xml_file: codeline = xml_file.readlines() match = re.search(r'^\<\?.*xml.*?encoding.*\?>', codeline[0]) if match: xmlread = ''.join(codeline[1:]) else: xmlread = ''.join(codeline[0:]) xmlencode = xmlread.encode('utf-8') root = ET.fromstring(xmlencode) for param in search_params: if root.find(param['xpath']) is None: if trap_undefined_error: raise LookupError('The specified xpath is missing : %s' % param['xpath']) else: continue for e in root.findall(param['xpath']): if e.get(param['key']) is None: if trap_undefined_error: raise LookupError('The specified KEY is missing : %s' % param['key']) else: continue value = e.get(param['key']) if 'value' in param: template = Template(param['value']) value = template.render({'VALUE': value}) value = utility.cast(value, param.get('value_type'), trap_undefined_error) tmp_table = utility.path2dict(param, value) utility.dict_list_marge(key_value_table, tmp_table) break return key_value_table
def find_value(target_file, search_params, encoding=None, trap_undefined_error=False): # auto encoding if encoding is None: encoding = utility.check_encode(target_file) # return value list. key_value_table = {} # open a file. if raise exception, caller catches exception. with io.open(target_file, "r", encoding=encoding) as f: # read and process line by line. for line in f: # process parameters one by one. for param in search_params: # processed parameter is skipped. if 'processed' in param: continue match = re.match(param['regexp'], line) if match: # found match. value = match.group(1) # processed flag is set. param['processed'] = True if 'value' in param: template = Template(param['value']) value = template.render({'VALUE': value}) value = utility.cast(value, param.get('value_type'), trap_undefined_error) tmp_table = utility.path2dict(param, value) utility.dict_list_marge(key_value_table, tmp_table) # all value have benn found? if trap_undefined_error: for param in search_params: # raise an exception if there is one that is not found even for one case. if 'processed' not in param: raise ValueError('Regex(' + param['regexp'] + ') does not match.') return key_value_table
def find_value(target_file, search_params, encoding=None, trap_undefined_error=False): # auto encoding if encoding is None: encoding = utility.check_encode(target_file) key_value_table = {} # load jsondata with io.open(target_file, 'r', encoding=encoding) as json_file: json_data = json.load(json_file) for param in search_params: json_path = [] # parse json_path string result = __peeler(r'\$', param["path"]) if result is None: msg = 'no root found in path({0})' raise KeyError(msg.format(param["path"])) # parse json_path string while result is not None: if result[0] != '$': json_path.append(result[0]) if result[1] == '': break result = __opener(result[1]) if result is None: msg = 'syntax error found in path({0})' raise KeyError(msg.format(param["path"])) try: # pickup value result = json_data for key in json_path: if key["type"] == "list": suffix = key["value"] suffix = int(suffix) if type(result) != list: msg = 'key([{1}]) in path({0}) points to non-array' raise KeyError(msg.format(param["path"], suffix)) if len(result) <= suffix: msg = 'key([{1}]) in path({0}) out of range' raise KeyError(msg.format(param["path"], suffix)) else: suffix = key["value"] if type(result) != dict: msg = 'key([{1}]) in path({0}) points to non-object' raise KeyError(msg.format(param["path"], suffix)) if suffix not in result: msg = 'invalid key([{1}]) found in path({0})' raise KeyError(msg.format(param["path"], suffix)) result = result[suffix] value = result # push table if 'value' in param: template = Template(param['value']) value = template.render({'VALUE': value}) value = utility.cast(value, param.get('value_type'), trap_undefined_error) tmp_table = utility.path2dict(param, value) utility.dict_list_marge(key_value_table, tmp_table) except KeyError as e: if trap_undefined_error is True: raise e except Exception as e: raise e return key_value_table
def find_value(target_file, search_params, encoding=None, trap_undefined_error=False): if encoding is None: encoding = utility.check_encode(target_file) key_value_table = {} # if INI_FILE in codeList: with io.open(target_file, 'r', encoding=encoding) as testfile: lines = testfile.readlines() for param in search_params: findsection = False findkey = False for line in lines: # Section not found if not findsection: # Find a section ls = line.strip() if ls == '[%s]' % param['section']: # Find it if found findsection = True else: # Search for KEY. When I'm looking for a section, I get an error linereplace = re.sub(r'^\s*%s\s*=' % param['inikey'], '%s' % param['inikey'] + ' = ', line) linesplit = linereplace.split(' = ', 1) if len(linesplit) >= 2: # Exact match with 'inikey' if linesplit[0] == param['inikey']: # Delete blanks and newlines iniValue = linesplit[1].strip() if 'value' in param: template = Template(param['value']) iniValue = template.render({'VALUE': iniValue}) iniValue = utility.cast(iniValue, param.get('value_type'), trap_undefined_error) tmp_table = utility.path2dict(param, iniValue) utility.dict_list_marge(key_value_table, tmp_table) findkey = True break # If no key is found and there is a section if line.strip().startswith('['): if trap_undefined_error: raise LookupError('The specified KEY is missing : %s' % param['inikey']) else: break if not findsection: if trap_undefined_error: raise LookupError('The specified section is missing : %s' % param['section']) if not findkey: if trap_undefined_error: raise LookupError('The specified KEY is missing : %s' % param['inikey']) # Error when key_value_table is empty & 'trap_undefined_error' is true if len(key_value_table) == 0: if trap_undefined_error: raise LookupError('Value can not be acquired.') return key_value_table