Changeset 14293 for lang/python

Show
Ignore:
Timestamp:
06/20/08 18:17:12 (5 months ago)
Author:
yoshiori
Message:

とりあえず動くかもレベルで完成

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/python/pit/pit.py

    r14158 r14293  
    11#!/usr/bin/env python 
    22# -*- coding: utf-8 -*- 
    3 import os, yaml 
     3import os, yaml, tempfile 
     4from subprocess import Popen 
    45 
    56class 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 
    936 
    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' : ''} 
    1346 
    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() 
    2051        ret = config['profile'] 
    2152        config['profile'] = name 
    22         print config 
    2353        yaml.dump(config, 
    24                   open(self.__config, 'w'), 
     54                  open(Pit.__config, 'w'), 
    2555                  default_flow_style=False) 
    26         print 'func switch :' + name 
    2756        return ret 
    2857 
    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) 
    3363 
    34         if not os.path.exists(self.__config): 
     64        if not os.path.exists(Pit.__config): 
    3565            yaml.dump({'profile' : 'default'}, 
    36                       open(self.__config, 'w'), 
     66                      open(Pit.__config, 'w'), 
    3767                      default_flow_style=False) 
    38             os.chmod(self.__config, 0600) 
     68            os.chmod(Pit.__config, 0600) 
    3969 
    40         self.switch(self.config()['profile']) 
     70        Pit.switch(Pit.config()['profile']) 
    4171 
    42         if not os.path.exists(self.__profile): 
     72        if not os.path.exists(Pit.__profile): 
    4373            yaml.dump({},  
    44                       open(self.__profile, 'w'),  
     74                      open(Pit.__profile, 'w'),  
    4575                      default_flow_style=False) 
    46             os.chmod(self.__profile, 0600) 
     76            os.chmod(Pit.__profile, 0600) 
     77        return yaml.load(open(Pit.__profile)) or {} 
    4778 
    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)) 
    5282 
    5383if __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']