root/lang/python/pit/pit.py @ 37388

Revision 37388, 3.0 kB (checked in by nishio, 3 years ago)

/lang/python/pit/ FIX: 'int' object has no attribute 'write'. ADD: test and mock-editor

Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3import os, yaml, tempfile
4from subprocess import Popen
5
6class Pit:
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        result = {}
16        if opts.has_key('data'):
17            result = opts['data']
18        else:
19            if not os.environ.has_key('EDITOR'):
20                return {}
21           
22            temp_fd, path = tempfile.mkstemp()
23            t = os.fdopen(temp_fd, "w")
24            c = yaml.dump(opts['config'] if opts.has_key('config') else Pit.get(name) ,default_flow_style=False)
25            t.write(c)
26            t.close()
27            Popen([os.environ['EDITOR'],path]).communicate()
28            t = open(path)
29            result = t.read()
30            t.close()
31            os.remove(path)
32
33            if result == c:
34                print 'No Changes'
35                print profile
36                return profile[name]
37
38            result = yaml.load(result)
39
40        profile[name] = result
41        yaml.dump(profile,
42                  open(Pit._profile, 'w'),
43                  default_flow_style=False)
44        return result
45
46    @staticmethod
47    def get(name, opts={}):
48        load_data = Pit._load()
49        ret = load_data[name] if load_data.has_key(name) else {}
50        if opts.has_key('require'):
51            keys = set(opts['require'].keys()) - set(ret.keys())
52            if keys:
53                for key in keys:
54                    ret[key] = opts['require'][key]
55                ret = Pit.set(name,{'config' : ret})
56       
57        return ret or {'username' : '', 'password' : ''}
58
59    @staticmethod
60    def switch(name, opts={}):
61        Pit._profile = os.path.join(Pit.DIRECTORY, '%s.yaml' % name)
62        config = Pit.config()
63        ret = config['profile']
64        config['profile'] = name
65        yaml.dump(config,
66                  open(Pit._config, 'w'),
67                  default_flow_style=False)
68        return ret
69
70    @staticmethod
71    def _load():
72        if not os.path.exists(Pit.DIRECTORY):
73            os.mkdir(Pit.DIRECTORY)
74            os.chmod(Pit.DIRECTORY, 0700)
75
76        if not os.path.exists(Pit._config):
77            yaml.dump({'profile' : 'default'},
78                      open(Pit._config, 'w'),
79                      default_flow_style=False)
80            os.chmod(Pit._config, 0600)
81
82        Pit.switch(Pit.config()['profile'])
83
84        if not os.path.exists(Pit._profile):
85            yaml.dump({},
86                      open(Pit._profile, 'w'),
87                      default_flow_style=False)
88            os.chmod(Pit._profile, 0600)
89        return yaml.load(open(Pit._profile)) or {}
90
91    @staticmethod
92    def config():
93        return yaml.load(open(Pit._config))
94
95if __name__ == '__main__':
96    config = Pit.get('34twitter.com',{'require': {'email':'your email','password':'your password'}})
97    print config
98    print config['email']
99    print config['password']
Note: See TracBrowser for help on using the browser.