123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- """
- demeter core
- name:demeter.py
- author:rabin
- """
- import time
- import os
- import sys
- import getopt
- import ConfigParser
- import subprocess
- class Demeter(object):
- path = ''
- config = {}
- serviceObj = {}
- modelObj = {}
- web = ''
- def __new__(self, *args, **kwargs):
- print 'error'
- sys.exit()
- def __init__(self):
- pass
- @classmethod
- def initConfig(self):
- self.path = File.path()
- if self.config == {}:
- name = 'dev'
- if 'DEMETER_CONF' in os.environ:
- name = os.environ['DEMETER_CONF']
- filename = self.path + 'conf/'+name+'.conf'
- if File.exists(filename):
- config = ConfigParser.ConfigParser()
- config.read(filename)
- for item in config.sections():
- self.config[item] = self.readConfig(config, item)
- return True
- else:
- print filename + ' is not exists'
- sys.exit()
- @staticmethod
- def readConfig(config, type):
- value = config.options(type)
- result = {}
- for item in value:
- result[item] = config.get(type, item)
- return result
- @classmethod
- def echo(self, args):
- module = self.getObject('pprint')
- module.pprint(args)
- @classmethod
- def record(self, key, value):
-
-
- service = self.service('record')
- service.push(key, value)
- @classmethod
- def service(self, name):
- if name not in self.serviceObj:
- path = 'service.'
- if name == 'common':
- path = 'demeter.'
- name = 'service'
- service = self.getClass(name, path)
- self.serviceObj[name] = service()
- return self.serviceObj[name]
- @classmethod
- def model(self, table, name='rdb'):
- if table not in self.modelObj:
- name = self.config['db'][name]
- config = self.config[name]
- obj = self.getObject('db', 'demeter.')
- db = getattr(obj, name.capitalize())
- connect = db(config).get()
- model = self.getClass(table, 'model.')
- self.modelObj[table] = model(name, connect, config)
- return self.modelObj[table]
- @classmethod
- def getClass(self, name, path=''):
- obj = self.getObject(name, path)
- return getattr(obj, name.capitalize())
- @staticmethod
- def getObject(name, path = ''):
- module = __import__(path + name)
- return getattr(module, name)
- @staticmethod
- def bool(value):
- return value == str(True)
- @classmethod
- def runtime(self, path, file, content=''):
- path = self.path + 'runtime/' + path + '/'
- File.mkdir(path)
- file = path + file
- if File.exists(file):
- return False
- else:
- File.write(file, content)
- return True
- @classmethod
- def webstart(self, name):
- self.web = name
- self.webPath = self.path + self.web + '/'
- self.getObject('main', name + '.')
- @classmethod
- def md5(self, value, salt=False):
- module = __import__('md5')
- md5 = getattr(module, 'new')
- md5_obj = md5()
- if salt:
- if salt == True:
- salt = self.rand()
- md5_obj.update(value + salt)
- return md5_obj.hexdigest() + '_' + salt
- else:
- md5_obj.update(value)
- return md5_obj.hexdigest()
- @staticmethod
- def rand(length = 4):
- module = __import__('random')
- rand = getattr(module, 'randint')
- salt = ''
- chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789'
- len_chars = len(chars) - 1
- for i in xrange(length):
- salt += chars[rand(0, len_chars)]
- return salt
- @staticmethod
- def mktime(value):
- module = __import__('time')
- strptime = getattr(module, 'strptime')
- mktime = getattr(module, 'mktime')
- return int(mktime(strptime(value,'%Y-%m-%d %H:%M:%S')))
- @staticmethod
- def date(value):
- module = __import__('datetime')
- datetime = getattr(module, 'datetime')
- fromtimestamp = getattr(datetime, 'fromtimestamp')
- return str(fromtimestamp(value).strftime('%Y-%m-%d %H:%M:%S'))
- @staticmethod
- def error(string):
- print string
- os._exit(0)
- class File(object):
- @staticmethod
- def write(file, content):
- handle = open(file, 'w')
- handle.write(content)
- handle.close()
- Shell.popen('chmod +x ' + file)
- @staticmethod
- def read(path, name):
- handle = open(path + name, 'r')
- content = handle.read()
- handle.close()
- return content
- @staticmethod
- def cur_path():
- return os.path.split(os.path.realpath(__file__))[0] + '/'
- @staticmethod
- def path():
- return os.sys.path[0] + '/'
- @staticmethod
- def exists(name):
- return os.path.exists(name)
- @staticmethod
- def rename(old, new):
- return os.rename(old, new)
- @staticmethod
- def remove(file):
- return os.remove(file)
- @staticmethod
- def mkdir(path):
- if File.exists(path) == False:
- os.mkdir(path)
- return path
- @staticmethod
- def mkdirs(path):
- if File.exists(path) == False:
- os.makedirs(path)
- return path
- class Shell(object):
- @staticmethod
- def popen(command, sub=False, bg=False):
- string = command
- if bg == True:
- command = command + ' 1>/dev/null 2>&1 &'
- if sub == False:
- process = os.popen(command)
- output = process.read()
- process.close()
- return output
- else:
- popen = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
- output = ''
- print string
- while True:
- output = popen.stdout.readline()
- print output
- if popen.poll() is not None:
- break
- return output
- Demeter.initConfig()
|