Mercurial > hg > bitsyblog
changeset 44:0e7f56709c90
cleanup of blogme, not yet finished
author | k0s <k0scist@gmail.com> |
---|---|
date | Wed, 11 Nov 2009 19:39:27 -0500 |
parents | 8b9aa9f40fa5 |
children | c228832db770 |
files | bitsyblog/blogme.py setup.py |
diffstat | 2 files changed, 29 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/bitsyblog/blogme.py Wed Nov 11 19:25:21 2009 -0500 +++ b/bitsyblog/blogme.py Wed Nov 11 19:39:27 2009 -0500 @@ -1,5 +1,9 @@ #!/usr/bin/env python +""" +command line blogger +""" + import optparse import os import subprocess @@ -7,8 +11,10 @@ import tempfile import urllib2 +from ConfigParser import ConfigParser + # global variable -dotfile='.blogme' +CONFIG = '.blogme' def tmpbuffer(editor=None): """open an editor and retreive the resulting editted buffer""" @@ -24,14 +30,14 @@ def main(args=sys.argv[1:]): - # create the option parser + # parse command line options parser = optparse.OptionParser() - parser.add_option('-s', '--server') + parser.add_option('-c', '--config') + parser.add_option('-H', '--host') parser.add_option('-u', '--user') parser.add_option('-p', '--password') parser.add_option('--private', action='store_true', default=False) parser.add_option('--secret', action='store_true', default=False) - options, args = parser.parse_args() # sanity check @@ -39,18 +45,13 @@ print "post can't be secret and private!" sys.exit(1) - # parse dotfile - home = os.environ.get('HOME') - if home: - dotfile = os.path.join(home, dotfile) - if os.path.exists(dotfile): - prefs = file(dotfile).read().split('\n') - prefs = [ i for i in prefs if i.strip() ] - prefs = [ [ j.strip() for j in i.split(':', 1) ] for i in prefs - if ':' in i] # probably not necessary - prefs = dict(prefs) - else: - prefs = {} + # parse dotfile config + config = ConfigParser() + if not options.config: + home = os.environ['HOME'] + options.config = os.path.join(home, CONFIG) + if os.path.exists(options.config): + config.read(options.config) # determine user name and password fields = [ 'user', 'password' ] @@ -68,12 +69,12 @@ assert password is not None # write the dotfile if it doesn't exist - if not os.path.exists(dotfile): - preffile = file(dotfile, 'w') - print >> preffile, 'user: %s' % user - print >> preffile, 'password: %s' % password - preffile.close() - os.chmod(dotfile, 0600) +# if not os.path.exists(dotfile): +# preffile = file(dotfile, 'w') +# print >> preffile, 'user: %s' % user +# print >> preffile, 'password: %s' % password +# preffile.close() +# os.chmod(dotfile, 0600) # get the blog @@ -83,7 +84,7 @@ msg = tmpbuffer() # open the url - url = '/'.join((options.server, user)) + url = options.host url += '?auth=digest' # specify authentication method if options.private: url += '&privacy=private' @@ -93,12 +94,11 @@ authhandler.add_password('bitsyblog', url, user, password) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener) - try: url = urllib2.urlopen(url, data=msg) print url.url # print the blog post's url - except urllib2.HTTPError: - pass + except urllib2.HTTPError, e: + print e if __name__ == '__main__': main()