demeter.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. demeter core
  5. name:demeter.py
  6. author:rabin
  7. """
  8. import time
  9. import os
  10. import sys
  11. import getopt
  12. import ConfigParser
  13. import subprocess
  14. PATH = ''
  15. class Demeter(object):
  16. path = ''
  17. config = {}
  18. serviceObj = {}
  19. modelObj = {}
  20. web = ''
  21. def __new__(cls, *args, **kwargs):
  22. print 'error'
  23. sys.exit()
  24. def __init__(self):
  25. pass
  26. @classmethod
  27. def initConfig(cls):
  28. global PATH
  29. if PATH == '':
  30. PATH = File.path()
  31. cls.path = PATH
  32. if cls.config == {}:
  33. filename = cls.path + 'demeter.conf'
  34. if File.exists(filename):
  35. config = ConfigParser.ConfigParser()
  36. config.read(filename)
  37. for item in config.sections():
  38. cls.config[item] = cls.readConfig(config, item)
  39. return True
  40. else:
  41. print filename + ' is not exists'
  42. sys.exit()
  43. @staticmethod
  44. def readConfig(config, type):
  45. value = config.options(type)
  46. result = {}
  47. for item in value:
  48. result[item] = config.get(type, item)
  49. return result
  50. @classmethod
  51. def echo(cls, args):
  52. module = cls.getObject('pprint')
  53. module.pprint(args)
  54. @classmethod
  55. def record(cls, key, value):
  56. # 记录日志
  57. # cls.log(key, value)
  58. service = cls.service('record')
  59. service.push(key, value)
  60. @classmethod
  61. def service(cls, name):
  62. if name not in cls.serviceObj:
  63. service = cls.getClass(name, 'service.')
  64. cls.serviceObj[name] = service()
  65. return cls.serviceObj[name]
  66. @classmethod
  67. def model(cls, table, type='rdb'):
  68. if table not in cls.modelObj:
  69. type = cls.config['db'][type]
  70. config = cls.config[type]
  71. db = cls.getClass(type, 'db.')
  72. connect = db(config).get()
  73. model = cls.getClass(table, 'model.')
  74. cls.modelObj[table] = model(type, connect, config)
  75. return cls.modelObj[table]
  76. @classmethod
  77. def getClass(cls, name, path=''):
  78. obj = cls.getObject(name, path)
  79. return getattr(obj, name.capitalize())
  80. @staticmethod
  81. def getObject(name, path = ''):
  82. module = __import__(path + name)
  83. return getattr(module, name)
  84. @staticmethod
  85. def bool(value):
  86. return value == str(True)
  87. @classmethod
  88. def runtime(cls, path, file, content=''):
  89. path = cls.path + 'runtime/' + path + '/'
  90. File.mkdir(path)
  91. file = path + file
  92. if File.exists(file):
  93. return False
  94. else:
  95. File.write(file, content)
  96. return True
  97. @classmethod
  98. def webstart(cls, name):
  99. cls.web = name
  100. cls.getObject('main', name + '.')
  101. @classmethod
  102. def md5(cls, value, salt=False):
  103. module = __import__('md5')
  104. md5 = getattr(module, 'new')
  105. md5_obj = md5()
  106. if salt:
  107. if salt == True:
  108. salt = cls.rand()
  109. md5_obj.update(value + salt)
  110. return md5_obj.hexdigest() + '_' + salt
  111. else:
  112. md5_obj.update(value)
  113. return md5_obj.hexdigest()
  114. @staticmethod
  115. def rand(length = 4):
  116. module = __import__('random')
  117. rand = getattr(module, 'randint')
  118. salt = ''
  119. chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
  120. len_chars = len(chars) - 1
  121. for i in xrange(length):
  122. salt += chars[rand(0, len_chars)]
  123. return salt
  124. @staticmethod
  125. def mktime(value):
  126. module = __import__('time')
  127. strptime = getattr(module, 'strptime')
  128. mktime = getattr(module, 'mktime')
  129. return int(mktime(strptime(value,'%Y-%m-%d %H:%M:%S')))
  130. @staticmethod
  131. def date(value):
  132. module = __import__('datetime')
  133. datetime = getattr(module, 'datetime')
  134. fromtimestamp = getattr(datetime, 'fromtimestamp')
  135. return str(fromtimestamp(value).strftime('%Y-%m-%d %H:%M:%S'))
  136. @staticmethod
  137. def error(string):
  138. print string
  139. os._exit(0)
  140. class File(object):
  141. @staticmethod
  142. def write(file, content):
  143. handle = open(file, 'w')
  144. handle.write(content)
  145. handle.close()
  146. Shell.popen('chmod +x ' + file)
  147. @staticmethod
  148. def read(path, name):
  149. handle = open(path + name, 'r')
  150. content = handle.read()
  151. handle.close()
  152. return content
  153. @staticmethod
  154. def path():
  155. return os.path.split(os.path.realpath(__file__))[0] + '/'
  156. @staticmethod
  157. def exists(name):
  158. return os.path.exists(name)
  159. @staticmethod
  160. def rename(old, new):
  161. return os.rename(old, new)
  162. @staticmethod
  163. def remove(file):
  164. return os.remove(file)
  165. @staticmethod
  166. def mkdir(path):
  167. if File.exists(path) == False:
  168. os.mkdir(path)
  169. return path
  170. @staticmethod
  171. def mkdirs(path):
  172. if File.exists(path) == False:
  173. os.makedirs(path)
  174. return path
  175. class Shell(object):
  176. @staticmethod
  177. def popen(command, sub=False, bg=False):
  178. string = command
  179. if bg == True:
  180. command = command + ' 1>/dev/null 2>&1 &'
  181. if sub == False:
  182. process = os.popen(command)
  183. output = process.read()
  184. process.close()
  185. return output
  186. else:
  187. popen = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
  188. output = ''
  189. print string
  190. while True:
  191. output = popen.stdout.readline()
  192. print output
  193. if popen.poll() is not None:
  194. break
  195. return output
  196. Demeter.initConfig()