core.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 signal
  11. import re
  12. import sys
  13. import getopt
  14. import json
  15. import ConfigParser
  16. import subprocess
  17. class Demeter(object):
  18. path = ''
  19. config = {}
  20. serviceObj = {}
  21. modelObj = {}
  22. web = ''
  23. request = False
  24. def __new__(self, *args, **kwargs):
  25. sys.exit()
  26. def __init__(self):
  27. pass
  28. @staticmethod
  29. def isset(v):
  30. try :
  31. type (eval(v))
  32. except :
  33. return 0
  34. else :
  35. return 1
  36. @classmethod
  37. def initConfig(self):
  38. self.path = File.path()
  39. if self.config == {}:
  40. name = 'dev'
  41. if 'DEMETER_CONF' in os.environ:
  42. name = os.environ['DEMETER_CONF']
  43. filename = self.path + 'conf/'+name+'.conf'
  44. if File.exists(filename):
  45. config = ConfigParser.ConfigParser()
  46. config.read(filename)
  47. for item in config.sections():
  48. self.config[item] = self.readConfig(config, item)
  49. return True
  50. else:
  51. print filename + ' is not exists'
  52. sys.exit()
  53. @staticmethod
  54. def readConfig(config, type):
  55. value = config.options(type)
  56. result = {}
  57. for item in value:
  58. result[item] = config.get(type, item)
  59. return result
  60. @classmethod
  61. def temp(self, key='', name='', value=''):
  62. temp = Demeter.path + 'conf/temp.conf'
  63. if File.exists(temp):
  64. config = ConfigParser.ConfigParser()
  65. config.read(temp)
  66. if key and name:
  67. config.set(key, name, value)
  68. config.write(open(temp, 'w'))
  69. else:
  70. result = {}
  71. for item in config.sections():
  72. result[item] = self.readConfig(config, item)
  73. return result
  74. @classmethod
  75. def echo(self, args):
  76. module = self.getObject('pprint')
  77. module.pprint(args)
  78. @classmethod
  79. def record(self, key, value):
  80. # 记录日志
  81. # self.log(key, value)
  82. service = self.service('record')
  83. service.push(key, value)
  84. @classmethod
  85. def service(self, name):
  86. path = 'service.'
  87. if name == 'common':
  88. path = 'demeter.'
  89. name = 'service'
  90. service = self.getClass(name, path)
  91. return service()
  92. """
  93. if name not in self.serviceObj:
  94. path = 'service.'
  95. if name == 'common':
  96. path = 'demeter.'
  97. name = 'service'
  98. service = self.getClass(name, path)
  99. self.serviceObj[name] = service()
  100. return self.serviceObj[name]
  101. """
  102. @classmethod
  103. def model(self, table, name='rdb'):
  104. name = self.config['db'][name]
  105. config = self.config[name]
  106. obj = self.getObject('db', 'demeter.')
  107. db = getattr(obj, name.capitalize())
  108. connect = db(config).get()
  109. model = self.getClass(table, 'model.')
  110. return model(name, connect, config)
  111. """
  112. if table not in self.modelObj:
  113. name = self.config['db'][name]
  114. config = self.config[name]
  115. obj = self.getObject('db', 'demeter.')
  116. db = getattr(obj, name.capitalize())
  117. connect = db(config).get()
  118. model = self.getClass(table, 'model.')
  119. self.modelObj[table] = model(name, connect, config)
  120. return self.modelObj[table]
  121. """
  122. @classmethod
  123. def getClass(self, name, path=''):
  124. obj = self.getObject(name, path)
  125. return getattr(obj, name.capitalize())
  126. @staticmethod
  127. def getObject(name, path = ''):
  128. module = __import__(path + name)
  129. return getattr(module, name)
  130. @staticmethod
  131. def bool(value):
  132. return value == str(True)
  133. @classmethod
  134. def runtime(self, path, file, content=''):
  135. path = self.path + 'runtime/' + path + '/'
  136. File.mkdir(path)
  137. file = path + file
  138. if File.exists(file):
  139. return False
  140. else:
  141. File.write(file, content)
  142. return True
  143. @classmethod
  144. def webstart(self, name):
  145. self.web = name
  146. self.webPath = self.path + self.web + '/'
  147. self.getObject('main', name + '.')
  148. @classmethod
  149. def md5(self, value, salt=False):
  150. module = __import__('md5')
  151. md5 = getattr(module, 'new')
  152. md5_obj = md5()
  153. if salt:
  154. if salt == True:
  155. salt = self.rand()
  156. md5_obj.update(value + salt)
  157. return md5_obj.hexdigest() + '_' + salt
  158. else:
  159. md5_obj.update(value)
  160. return md5_obj.hexdigest()
  161. @staticmethod
  162. def rand(length = 4):
  163. module = __import__('random')
  164. rand = getattr(module, 'randint')
  165. salt = ''
  166. chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
  167. len_chars = len(chars) - 1
  168. for i in xrange(length):
  169. salt += chars[rand(0, len_chars)]
  170. return salt
  171. @staticmethod
  172. def hour(value):
  173. if value < 10:
  174. return '0' + str(value)
  175. return value
  176. @staticmethod
  177. def time():
  178. return int(time.time())
  179. @staticmethod
  180. def mktime(value, string='%Y-%m-%d %H:%M:%S'):
  181. if ' ' in string and ' ' not in value:
  182. value = value + ' 00:00:00'
  183. return int(time.mktime(time.strptime(value,string)))
  184. @staticmethod
  185. def date(value, string='%Y-%m-%d %H:%M:%S'):
  186. module = __import__('datetime')
  187. datetime = getattr(module, 'datetime')
  188. fromtimestamp = getattr(datetime, 'fromtimestamp')
  189. return str(fromtimestamp(value).strftime(string))
  190. @staticmethod
  191. def isJson(value):
  192. result = False
  193. try:
  194. result = json.loads(value)
  195. except ValueError:
  196. return result
  197. return result
  198. @staticmethod
  199. def compressUuid(value):
  200. row = value.replace('-', '')
  201. code = ''
  202. hash = [x for x in "0123456789-abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ"]
  203. for i in xrange(10):
  204. enbin = "%012d" % int(bin(int(row[i * 3] + row[i * 3 + 1] + row[i * 3 + 2], 16))[2:], 10)
  205. code += (hash[int(enbin[0:6], 2)] + hash[int(enbin[6:12], 2)])
  206. return code
  207. @staticmethod
  208. def checkMobile(request):
  209. if 'Demeter-Mobile' in request.headers:
  210. return True
  211. userAgent = request.headers['User-Agent']
  212. # userAgent = env.get('HTTP_USER_AGENT')
  213. _long_matches = r'googlebot-mobile|android|avantgo|blackberry|blazer|elaine|hiptop|ip(hone|od)|kindle|midp|mmp|mobile|o2|opera mini|palm( os)?|pda|plucker|pocket|psp|smartphone|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce; (iemobile|ppc)|xiino|maemo|fennec'
  214. _long_matches = re.compile(_long_matches, re.IGNORECASE)
  215. _short_matches = r'1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|e\-|e\/|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(di|rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|xda(\-|2|g)|yas\-|your|zeto|zte\-'
  216. _short_matches = re.compile(_short_matches, re.IGNORECASE)
  217. if _long_matches.search(userAgent) != None:
  218. return True
  219. user_agent = userAgent[0:4]
  220. if _short_matches.search(user_agent) != None:
  221. return True
  222. return False
  223. @staticmethod
  224. def exp(exp, value):
  225. if exp:
  226. exp = exp.replace('{n}', value)
  227. value = str(eval(exp))
  228. return value
  229. @classmethod
  230. def curl(self, url):
  231. import urllib2
  232. req = urllib2.Request(url)
  233. response = urllib2.urlopen(req)
  234. return response.read()
  235. @classmethod
  236. def error(self, string):
  237. from tornado.web import Finish
  238. if self.request:
  239. self.request.out(string)
  240. #self.request.finish()
  241. raise Finish()
  242. else:
  243. print string
  244. #os._exit(0)
  245. class File(object):
  246. @staticmethod
  247. def write(file, content):
  248. handle = open(file, 'w')
  249. handle.write(content)
  250. handle.close()
  251. Shell.popen('chmod +x ' + file)
  252. @staticmethod
  253. def read(path, name):
  254. handle = open(path + name, 'r')
  255. content = handle.read()
  256. handle.close()
  257. return content
  258. @staticmethod
  259. def cur_path():
  260. return os.path.split(os.path.realpath(__file__))[0] + '/'
  261. @staticmethod
  262. def path():
  263. return os.sys.path[0] + '/'
  264. @staticmethod
  265. def exists(name):
  266. return os.path.exists(name)
  267. @staticmethod
  268. def rename(old, new):
  269. return os.rename(old, new)
  270. @staticmethod
  271. def remove(file):
  272. return os.remove(file)
  273. @staticmethod
  274. def mkdir(path):
  275. if File.exists(path) == False:
  276. os.mkdir(path)
  277. return path
  278. @staticmethod
  279. def mkdirs(path):
  280. if File.exists(path) == False:
  281. os.makedirs(path)
  282. return path
  283. class Shell(object):
  284. @staticmethod
  285. def popen(command, sub=False, bg=False, timeout=0):
  286. string = command
  287. if bg == True:
  288. command = command + ' 1>/dev/null 2>&1 &'
  289. if timeout > 0:
  290. proc = subprocess.Popen(command,bufsize=0,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True, close_fds=True, preexec_fn = os.setsid)
  291. poll_seconds = .250
  292. deadline = time.time() + timeout
  293. while time.time() < deadline and proc.poll() == None:
  294. time.sleep(poll_seconds)
  295. if proc.poll() == None:
  296. os.killpg(proc.pid, signal.SIGTERM)
  297. return 'timeout'
  298. stdout,stderr = proc.communicate()
  299. return stdout
  300. elif sub == False:
  301. process = os.popen(command)
  302. output = process.read()
  303. process.close()
  304. return output
  305. else:
  306. popen = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
  307. output = ''
  308. print string
  309. while True:
  310. output = popen.stdout.readline()
  311. print output
  312. if popen.poll() is not None:
  313. break
  314. return output
  315. class Check(object):
  316. @staticmethod
  317. def match(rule, value):
  318. if not rule.match(value):
  319. return False
  320. return True
  321. @staticmethod
  322. def mobile(value):
  323. rule = re.compile(r'1\d{10}')
  324. return Check.match(rule, value)
  325. @staticmethod
  326. def number(value):
  327. try:
  328. int(value)
  329. return True
  330. except ValueError:
  331. return False
  332. @staticmethod
  333. def numberFloat(value):
  334. try:
  335. float(value)
  336. return True
  337. except ValueError:
  338. return False
  339. Demeter.initConfig()