|
| 1 | +# -*- coding: utf-8 |
| 2 | +import os, re |
| 3 | +import ConfigParser |
| 4 | + |
| 5 | +CONFIG_FILE = 'config.ini' |
| 6 | + |
| 7 | +def notice(msg, title="notice"): |
| 8 | + ''' notoce message in notification center''' |
| 9 | + os.system('osascript -e \'display notification "%s" with title "%s"\'' % (msg, title)) |
| 10 | + |
| 11 | +def read_config(): |
| 12 | + ''' read congig from config.ini, return a five tuple''' |
| 13 | + if not os.path.exists(CONFIG_FILE): |
| 14 | + return |
| 15 | + cf = ConfigParser.ConfigParser() |
| 16 | + cf.read(CONFIG_FILE) |
| 17 | + |
| 18 | + qiniu_section = 'qiniu' |
| 19 | + try: |
| 20 | + ak = cf.get(qiniu_section, 'ak') |
| 21 | + sk = cf.get(qiniu_section, 'sk') |
| 22 | + url = cf.get(qiniu_section, 'url') |
| 23 | + bucket = cf.get(qiniu_section, 'bucket') |
| 24 | + prefix = cf.get(qiniu_section, 'prefix') |
| 25 | + except ConfigParser.NoOptionError: |
| 26 | + return |
| 27 | + |
| 28 | + keys = ('ak', 'sk', 'url', 'bucket', 'prefix') |
| 29 | + res = (ak, sk, url, bucket, prefix) |
| 30 | + if not all(map(lambda x: re.match(r'\w+', x), res)): |
| 31 | + return |
| 32 | + return dict(zip(keys, res)) |
| 33 | + |
| 34 | +def open_with_editor(filepath): |
| 35 | + ''' open file with apple's text editor''' |
| 36 | + os.system('open -b "com.apple.TextEdit" "./%s"' % CONFIG_FILE) |
| 37 | + |
| 38 | +def generate_config_file(): |
| 39 | + import textwrap |
| 40 | + config_file_init_content = '''\ |
| 41 | + [qiniu] |
| 42 | + ak=七牛图床的Access Key |
| 43 | + sl=七牛图床的Secret Key |
| 44 | + url=七牛图床地址 |
| 45 | + bucket=七牛图床空间名 |
| 46 | + prefix=七牛图床资源前缀名''' |
| 47 | + with open(CONFIG_FILE, 'w') as fp: |
| 48 | + fp.write(textwrap.dedent(config_file_init_content)) |
0 commit comments