/ svn-cassiopeia --username yuuma.tomita@gmail.com/cas_helper/xldb/utils/re_tools/inc_parse.py
Python | 62 lines | 61 code | 0 blank | 1 comment | 0 complexity | 3f40fc8a9db40c9e793388ed9df826c2 MD5 | raw file
- #coding:utf8
- ur"""
- import xldb.utils.re_tools.inc_parse
- reload(xldb.utils.re_tools.inc_parse)
- inc_parse = xldb.utils.re_tools.inc_parse.inc_parse
- inc_parse()
-
- reload(xldb.utils.re_tools.inc_parse)
- inc_parse = xldb.utils.re_tools.inc_parse.inc_parse
-
- p=ur'D:\Orant\dev\cas\media\test\?11-1034.txt'
- l = inc_parse('jwits', 'odekake', p)
- for key ,value in l:
- print u' {0:<20}{1:>25} : <{2}>'.format(key, unicode(type(value))[7:-2], value[:20])
-
- .+^????[^?]+?[^\d]+([\d.]+).+
- """
-
- import re, dateutil, codecs
- from xldb.sheetutils import get_named_tablib
- from xldb.utils.parsers.date_parser import date_normalize
- from xldb.utils.parsers.mapp import mapp
- from xldb.utils.re_tools.replacer_factory import replacer_factory
- from xldb.utils.re_tools.post_processor import post_processor
-
- def re_processor(target_strings, re_pattern, repl_pattern):
- re_flag = re.I|re.U|re.MULTILINE|re.DOTALL|re.VERBOSE
- _re = re.compile(re_pattern, re_flag)
- try:
- evaluated_string = _re.sub(replacer_factory(repl_pattern), target_strings)
- except:
- try:
- items = [item for item in _re.match(target_strings).groups() if item]
- evaluated_string = ' '.join(items)
- except:
- raise Exception, u"re_pattern:{0}, repl_pattern:{1})".format(re_pattern, repl_pattern)
- finally:
- error_flag = True
- return evaluated_string
-
- def inc_parse(s_user, s_project, filepath):
- res_item_list = []
- str = '\n'.join(open(filepath, 'r').readlines()).replace('\r\n', '') #'strict'?'ignore'?'replace'
- base_target_strings = str.decode('sjis', 'ignore')
- d = get_named_tablib(s_user, s_project, u'???')
- dd = zip(d['key'], d['pre_exp'], d['pre_repl'], d['main_exp'], d['main_repl'], d['post_exp'], d['post_repl'])
- for key, pre_exp, pre_repl, main_exp, main_repl, post_exp, post_repl in dd:
- if not pre_exp: ##pre_exp / pre_capture
- continue
- else:
- target_strings = re_processor(base_target_strings, pre_exp, pre_repl)
- if not main_exp:
- value = target_strings
- else:
- value = re_processor(target_strings, main_exp, main_repl)
- if value and post_exp:
- value = post_processor(post_exp, value)
- if value and post_repl:
- value = post_processor(post_repl, value)
- print u' {0:<20}{1:>25} : <{2}>'.format(key, unicode(type(value))[7:-2], value[:100])
- res_item_list.append((key,value))
- return res_item_list