# HG changeset patch # User Jeff Hammel # Date 1368572142 25200 # Node ID c29d58e79fc6922320d5c9360c9f3ce16ff98c14 # Parent dd4abe56edf72a09212cf0760e5114cbbaefccaf better command line handling diff -r dd4abe56edf7 -r c29d58e79fc6 bitsyblog/blogme.py --- a/bitsyblog/blogme.py Thu Apr 25 17:13:07 2013 -0700 +++ b/bitsyblog/blogme.py Tue May 14 15:55:42 2013 -0700 @@ -30,6 +30,14 @@ os.remove(tmpfile) return buffer +def save(msg): + """save msg if somethingbadhappen""" + filename = 'blog-%s.txt' % datetime.datetime.now().strftime("%Y%m%d") + with file(filename, 'w') as f: + f.write(msg) + print filename + print msg + def main(args=sys.argv[1:]): # parse command line options @@ -47,7 +55,7 @@ parser.add_option('--secret', action='store_true', default=False, help='denote this blog entry as secret') options, args = parser.parse_args() - + # sanity check if options.private and options.secret: print "post can't be secret and private!" @@ -97,7 +105,7 @@ # determine user name and password fields = [ 'user', 'password' ] for field in fields: - has_option = config.has_option(options.host, field) + has_option = config.has_option(options.host, field) if getattr(options, field): if (not has_option) or (config.get(options.host, field) != getattr(options, field)): @@ -106,7 +114,7 @@ if has_option: setattr(options, field, config.get(options.host, field)) else: - print "%s: " % field, + print "%s: " % field, setattr(options, field, raw_input()) _config[field] = getattr(options, field) @@ -146,17 +154,21 @@ connection = urllib2.urlopen(url, data=msg) print connection.url # print the blog post's url break - except (urllib2.HTTPError, urllib2.URLError, socket.error), e: + except urllib2.HTTPError, e: + if (e.code == 404) and (options.secret or options.private): + # we are still anonymous so we can't see our own post + break + continue + except (urllib2.URLError, socket.error), e: continue except: print >> sys.stderr, "An error has occured:" print >> sys.stderr, sys.exc_info() # XXX pretty crappy + save(msg) break else: - with file('blog-%s.txt' % datetime.datetime.now().strftime("%Y%m%d"), 'w') as f: - f.write(msg) print >> sys.stderr, e - print msg + save(msg) if __name__ == '__main__': main() diff -r dd4abe56edf7 -r c29d58e79fc6 setup.py --- a/setup.py Thu Apr 25 17:13:07 2013 -0700 +++ b/setup.py Tue May 14 15:55:42 2013 -0700 @@ -1,7 +1,7 @@ from setuptools import setup, find_packages import sys, os -version = '2.4.3' +version = '2.4.4' try: description = file('README.txt').read()