PageRenderTime 120ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/ svn-cassiopeia --username yuuma.tomita@gmail.com/cas_helper/xldb/utils/re_tools/inc_parse.py

http://svn-cassiopeia.googlecode.com/
Python | 62 lines | 61 code | 0 blank | 1 comment | 0 complexity | 3f40fc8a9db40c9e793388ed9df826c2 MD5 | raw file
  1. #coding:utf8
  2. ur"""
  3. import xldb.utils.re_tools.inc_parse
  4. reload(xldb.utils.re_tools.inc_parse)
  5. inc_parse = xldb.utils.re_tools.inc_parse.inc_parse
  6. inc_parse()
  7. reload(xldb.utils.re_tools.inc_parse)
  8. inc_parse = xldb.utils.re_tools.inc_parse.inc_parse
  9. p=ur'D:\Orant\dev\cas\media\test\?11-1034.txt'
  10. l = inc_parse('jwits', 'odekake', p)
  11. for key ,value in l:
  12. print u' {0:<20}{1:>25} : <{2}>'.format(key, unicode(type(value))[7:-2], value[:20])
  13. .+^????[^?]+?[^\d]+([\d.]+).+
  14. """
  15. import re, dateutil, codecs
  16. from xldb.sheetutils import get_named_tablib
  17. from xldb.utils.parsers.date_parser import date_normalize
  18. from xldb.utils.parsers.mapp import mapp
  19. from xldb.utils.re_tools.replacer_factory import replacer_factory
  20. from xldb.utils.re_tools.post_processor import post_processor
  21. def re_processor(target_strings, re_pattern, repl_pattern):
  22. re_flag = re.I|re.U|re.MULTILINE|re.DOTALL|re.VERBOSE
  23. _re = re.compile(re_pattern, re_flag)
  24. try:
  25. evaluated_string = _re.sub(replacer_factory(repl_pattern), target_strings)
  26. except:
  27. try:
  28. items = [item for item in _re.match(target_strings).groups() if item]
  29. evaluated_string = ' '.join(items)
  30. except:
  31. raise Exception, u"re_pattern:{0}, repl_pattern:{1})".format(re_pattern, repl_pattern)
  32. finally:
  33. error_flag = True
  34. return evaluated_string
  35. def inc_parse(s_user, s_project, filepath):
  36. res_item_list = []
  37. str = '\n'.join(open(filepath, 'r').readlines()).replace('\r\n', '') #'strict'?'ignore'?'replace'
  38. base_target_strings = str.decode('sjis', 'ignore')
  39. d = get_named_tablib(s_user, s_project, u'???')
  40. dd = zip(d['key'], d['pre_exp'], d['pre_repl'], d['main_exp'], d['main_repl'], d['post_exp'], d['post_repl'])
  41. for key, pre_exp, pre_repl, main_exp, main_repl, post_exp, post_repl in dd:
  42. if not pre_exp: ##pre_exp / pre_capture
  43. continue
  44. else:
  45. target_strings = re_processor(base_target_strings, pre_exp, pre_repl)
  46. if not main_exp:
  47. value = target_strings
  48. else:
  49. value = re_processor(target_strings, main_exp, main_repl)
  50. if value and post_exp:
  51. value = post_processor(post_exp, value)
  52. if value and post_repl:
  53. value = post_processor(post_repl, value)
  54. print u' {0:<20}{1:>25} : <{2}>'.format(key, unicode(type(value))[7:-2], value[:100])
  55. res_item_list.append((key,value))
  56. return res_item_list