Mercurial > hg > bitsyblog
changeset 96:c29d58e79fc6
better command line handling
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Tue, 14 May 2013 15:55:42 -0700 |
parents | dd4abe56edf7 |
children | a20fd4fef727 |
files | bitsyblog/blogme.py setup.py |
diffstat | 2 files changed, 20 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- 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()