utils.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # -*- coding: utf-8 -*-
  2. """Helper utilities and decorators."""
  3. import fnmatch
  4. import sys
  5. import time
  6. from datetime import datetime
  7. import os
  8. import re
  9. from flask import flash
  10. from invoke import Responder
  11. def flash_errors(form, category='warning'):
  12. """Flash all errors for a form."""
  13. for field, errors in list(form.errors.items()):
  14. for error in errors:
  15. flash('{0} - {1}'.format(getattr(form, field).label.text, error), category)
  16. def date_str_to_obj(ymd):
  17. return time.strptime(ymd, '%Y-%m-%d')
  18. def datetime_str_to_obj(ymd_his):
  19. return datetime.strptime(ymd_his, "%Y-%m-%d %H:%i:%s")
  20. PY2 = int(sys.version[0]) == 2
  21. if PY2:
  22. text_type = unicode # noqa
  23. binary_type = str
  24. string_types = (str, unicode) # noqa
  25. unicode = unicode # noqa
  26. basestring = basestring # noqa
  27. reload(sys)
  28. sys.setdefaultencoding('utf8')
  29. else:
  30. text_type = str
  31. binary_type = bytes
  32. string_types = (str,)
  33. unicode = str
  34. basestring = (str, bytes)
  35. def detailtrace():
  36. from flask import current_app
  37. retStr = ""
  38. f = sys._getframe()
  39. f = f.f_back
  40. while hasattr(f, "f_code"):
  41. co = f.f_code
  42. retStr = "->%s(%s:%s)\n" % (os.path.basename(co.co_filename),
  43. co.co_name,
  44. f.f_lineno) + retStr
  45. f = f.f_back
  46. current_app.logger.info(retStr)
  47. print(retStr)
  48. def color_clean(text_with_color):
  49. '''
  50. e.g \x1b[?1h\x1b=
  51. e.g \x1b[?1l\x1b>
  52. @param text_with_color:
  53. @return:
  54. '''
  55. pure_text = text_with_color.strip()
  56. pure_text = re.sub('\x1B\[[0-9;]*[mGK]', '', pure_text, flags=re.I)
  57. pure_text = re.sub('\x1B\[\?[0-9;]*[a-z]\x1B[=><]', '', pure_text, flags=re.I)
  58. return pure_text.strip()
  59. def say_yes():
  60. return Responder(
  61. pattern=r'yes/no',
  62. response='yes\n',
  63. )
  64. def excludes_format(path, excludes_string=None):
  65. '''
  66. 排除文件,支持正则匹配,支持多选字符串
  67. @param path:
  68. @param excludes_string:
  69. @return:
  70. '''
  71. path = os.path.basename(path) + '/'
  72. if not excludes_string:
  73. return path
  74. prefix = '--exclude='
  75. excludes = [prefix + i for i in excludes_string.split('\n') if i.strip()]
  76. return ' {excludes} {path} '.format(excludes=' '.join(excludes), path=path)
  77. def includes_format(path, includes_string=None):
  78. '''
  79. 指定发布文件,支持正则匹配,如:*.war。支持多行字符串。
  80. @param path: release目录,非路径
  81. @param includes_string:
  82. @return:
  83. '''
  84. path = os.path.basename(path) + '/'
  85. if not includes_string:
  86. return path
  87. prefix = path
  88. includes = [prefix + i for i in includes_string.split('\n') if i.strip()]
  89. if not includes:
  90. return path
  91. return ' '.join(includes)