Changeset 14293 for lang/python
- Timestamp:
- 06/20/08 18:17:12 (5 months ago)
- Files:
-
- 1 modified
-
lang/python/pit/pit.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
lang/python/pit/pit.py
r14158 r14293 1 1 #!/usr/bin/env python 2 2 # -*- coding: utf-8 -*- 3 import os, yaml 3 import os, yaml, tempfile 4 from subprocess import Popen 4 5 5 6 class Pit: 6 Directory = os.path.expanduser('~/.pitPy') 7 __config = os.path.join(Directory, 'pit.yaml') 8 __profile = os.path.join(Directory, 'default.yaml') 7 VERSION = "0.0.1" 8 DIRECTORY = os.path.expanduser('~/.pit') 9 __config = os.path.join(DIRECTORY, 'pit.yaml') 10 __profile = os.path.join(DIRECTORY, 'default.yaml') 11 12 @staticmethod 13 def set(name, opts={}): 14 profile = Pit.__load() 15 ret = {} 16 if opts.has_key('data'): 17 ret = opts['data'] 18 else: 19 if not os.environ.has_key('EDITOR'): 20 return {} 21 t = tempfile.NamedTemporaryFile() 22 c = yaml.dump(opts['config'] if opts.has_key('config') else Pit.get(name) ,default_flow_style=False) 23 t.write(c) 24 t.flush() 25 path = os.path.abspath(t.name) 26 Popen([os.environ['EDITOR'],path]).communicate() 27 result = open(path).read() 28 if result == c: 29 print 'No Changes' 30 result = yaml.load(result) 31 profile[name] = result 32 yaml.dump(profile, 33 open(Pit.__profile, 'w'), 34 default_flow_style=False) 35 return result 9 36 10 def set(self, name, opts={}): 11 profile = self.__load() 12 # t = os.tmpfile() 37 @staticmethod 38 def get(name, opts={}): 39 load_data = Pit.__load() 40 ret = load_data[name] if load_data.has_key(name) else {} 41 if opts.has_key('require'): 42 for k, v in opts['require'].iteritems(): 43 ret[k] = v 44 ret = Pit.set(name,{'config' : ret}) 45 return ret or {'username' : '', 'password' : ''} 13 46 14 def get(self, name, opts={}): 15 pass 16 17 def switch(self, name, opts={}): 18 self.__profile = os.path.join(self.Directory, '%s.yaml' % name) 19 config = self.config() 47 @staticmethod 48 def switch(name, opts={}): 49 Pit.__profile = os.path.join(Pit.DIRECTORY, '%s.yaml' % name) 50 config = Pit.config() 20 51 ret = config['profile'] 21 52 config['profile'] = name 22 print config23 53 yaml.dump(config, 24 open( self.__config, 'w'),54 open(Pit.__config, 'w'), 25 55 default_flow_style=False) 26 print 'func switch :' + name27 56 return ret 28 57 29 def __load(self): 30 if not os.path.exists(self.Directory): 31 os.mkdir(self.Directory) 32 os.chmod(self.Directory, 0700) 58 @staticmethod 59 def __load(): 60 if not os.path.exists(Pit.DIRECTORY): 61 os.mkdir(Pit.DIRECTORY) 62 os.chmod(Pit.DIRECTORY, 0700) 33 63 34 if not os.path.exists( self.__config):64 if not os.path.exists(Pit.__config): 35 65 yaml.dump({'profile' : 'default'}, 36 open( self.__config, 'w'),66 open(Pit.__config, 'w'), 37 67 default_flow_style=False) 38 os.chmod( self.__config, 0600)68 os.chmod(Pit.__config, 0600) 39 69 40 self.switch(self.config()['profile'])70 Pit.switch(Pit.config()['profile']) 41 71 42 if not os.path.exists( self.__profile):72 if not os.path.exists(Pit.__profile): 43 73 yaml.dump({}, 44 open( self.__profile, 'w'),74 open(Pit.__profile, 'w'), 45 75 default_flow_style=False) 46 os.chmod(self.__profile, 0600) 76 os.chmod(Pit.__profile, 0600) 77 return yaml.load(open(Pit.__profile)) or {} 47 78 48 return yaml.load(open(self.__profile)) or {} 49 50 def config(self): 51 return yaml.load(open(self.__config)) 79 @staticmethod 80 def config(): 81 return yaml.load(open(Pit.__config)) 52 82 53 83 if __name__ == '__main__': 54 Pit().set('vox.com') 55 56 ''' 57 switch の opts は何につかうの? 58 59 60 ''' 84 config = Pit.get('twitter.com',{'require': {'email':'your email','password':'your password'}}) 85 print config['email'] 86 print config['password']
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)