comparison bitsyblog/blogme.py @ 45:c228832db770

fix blogme
author k0s <k0scist@gmail.com>
date Tue, 24 Nov 2009 19:20:47 -0500
parents 0e7f56709c90
children 66bfcceea3b4
comparison
equal deleted inserted replaced
44:0e7f56709c90 45:c228832db770
10 import sys 10 import sys
11 import tempfile 11 import tempfile
12 import urllib2 12 import urllib2
13 13
14 from ConfigParser import ConfigParser 14 from ConfigParser import ConfigParser
15 from utils import datestamp
15 16
16 # global variable 17 # global variable
17 CONFIG = '.blogme' 18 CONFIG = '.blogme'
18 19
19 def tmpbuffer(editor=None): 20 def tmpbuffer(editor=None):
31 def main(args=sys.argv[1:]): 32 def main(args=sys.argv[1:]):
32 33
33 # parse command line options 34 # parse command line options
34 parser = optparse.OptionParser() 35 parser = optparse.OptionParser()
35 parser.add_option('-c', '--config') 36 parser.add_option('-c', '--config')
37 parser.add_option('-f', '--file')
36 parser.add_option('-H', '--host') 38 parser.add_option('-H', '--host')
37 parser.add_option('-u', '--user') 39 parser.add_option('-u', '--user')
38 parser.add_option('-p', '--password') 40 parser.add_option('-p', '--password')
39 parser.add_option('--private', action='store_true', default=False) 41 parser.add_option('--private', action='store_true', default=False)
40 parser.add_option('--secret', action='store_true', default=False) 42 parser.add_option('--secret', action='store_true', default=False)
43 # sanity check 45 # sanity check
44 if options.private and options.secret: 46 if options.private and options.secret:
45 print "post can't be secret and private!" 47 print "post can't be secret and private!"
46 sys.exit(1) 48 sys.exit(1)
47 49
50 if options.file:
51 # get the blog
52 if args:
53 msg = ' '.join(args)
54 else:
55 msg = tmpbuffer()
56 options.file = os.path.join(options.file, 'entries')
57 for opt in 'private', 'secret':
58 if getattr(options, opt):
59 options.file = os.path.join(options.file, opt)
60 break
61 else:
62 options.file = os.path.join(options.file, 'public')
63 options.file = os.path.join(options.file, datestamp())
64 f = file(options.file, 'w')
65 print >> f, msg
66 f.close()
67 sys.exit(0)
68
48 # parse dotfile config 69 # parse dotfile config
49 config = ConfigParser() 70 config = ConfigParser()
71 _config = {} # config to write out
50 if not options.config: 72 if not options.config:
51 home = os.environ['HOME'] 73 home = os.environ['HOME']
52 options.config = os.path.join(home, CONFIG) 74 options.config = os.path.join(home, CONFIG)
53 if os.path.exists(options.config): 75 if os.path.exists(options.config):
54 config.read(options.config) 76 config.read(options.config)
55 77
78 # get default host, if available
79 if options.host:
80 if config.has_option('DEFAULTS', 'host'):
81 print "Make %s the default host? (Y/n): ",
82 answer = raw_input()
83 if not anwer.lower().startswith('n'):
84 _config['default'] = options.host
85 else:
86 if config.has_option('DEFAULTS', 'host'):
87 options.host = config.get('DEFAULTS', 'host')
88 else:
89 print "Enter URL of host: ",
90 host = raw_input()
91 _config['default'] = host
92
56 # determine user name and password 93 # determine user name and password
57 fields = [ 'user', 'password' ] 94 fields = [ 'user', 'password' ]
58 for field in fields: 95 for field in fields:
59 globals()[field] = prefs.get(field) 96 if hasattr(options, field):
60 97 if (not config.has_option(options.host, field)) or (config.get(options.host, field) != getattr(options, field)):
61 optval = getattr(options, field) 98 _config[field] = getattr(options, field)
62 if optval: 99 else:
63 password = None # needed to ensure prompting for pw from command line 100 print "%s: " % field,
64 globals()[field] = optval 101 setattr(options, field, raw_input())
65 102 _config[field] = getattr(options, field)
66 if globals()[field] is None:
67 globals()[field] = raw_input('%s: ' % field)
68 assert user is not None
69 assert password is not None
70 103
71 # write the dotfile if it doesn't exist 104 # write the dotfile if it doesn't exist
72 # if not os.path.exists(dotfile): 105 if _config:
73 # preffile = file(dotfile, 'w') 106 if 'default' in _config:
74 # print >> preffile, 'user: %s' % user 107 if not config.has_section('DEFAULTS'):
75 # print >> preffile, 'password: %s' % password 108 config.add_section('DEFAULTS')
76 # preffile.close() 109 config.set('DEFAULTS', 'host', options.host)
77 # os.chmod(dotfile, 0600) 110 if not config.has_section(options.host):
78 111 config.add_section(options.host)
112 for field in fields:
113 if not config.has_option(options.host, field):
114 config.set(options.host, field, getattr(options, field))
115 config.write()
79 116
80 # get the blog 117 # get the blog
81 if args: 118 if args:
82 msg = ' '.join(args) 119 msg = ' '.join(args)
83 else: 120 else:
89 if options.private: 126 if options.private:
90 url += '&privacy=private' 127 url += '&privacy=private'
91 if options.secret: 128 if options.secret:
92 url += '&privacy=secret' 129 url += '&privacy=secret'
93 authhandler = urllib2.HTTPDigestAuthHandler() 130 authhandler = urllib2.HTTPDigestAuthHandler()
94 authhandler.add_password('bitsyblog', url, user, password) 131 authhandler.add_password('bitsyblog', url, options.user, options.password)
95 opener = urllib2.build_opener(authhandler) 132 opener = urllib2.build_opener(authhandler)
96 urllib2.install_opener(opener) 133 urllib2.install_opener(opener)
97 try: 134 try:
98 url = urllib2.urlopen(url, data=msg) 135 url = urllib2.urlopen(url, data=msg)
99 print url.url # print the blog post's url 136 print url.url # print the blog post's url