comparison bitsyblog/blogme.py @ 43:8b9aa9f40fa5

moving most of blogme to its own function
author k0s <k0scist@gmail.com>
date Wed, 11 Nov 2009 19:25:21 -0500
parents ffe95e34066f
children 0e7f56709c90
comparison
equal deleted inserted replaced
42:ffe95e34066f 43:8b9aa9f40fa5
5 import subprocess 5 import subprocess
6 import sys 6 import sys
7 import tempfile 7 import tempfile
8 import urllib2 8 import urllib2
9 9
10 # global variables 10 # global variable
11
12 EDITOR='emacs -nw'
13 SERVER='http://bitsyblog.biz'
14 dotfile='.blogme' 11 dotfile='.blogme'
15 12
16 parser = optparse.OptionParser() 13 def tmpbuffer(editor=None):
17 parser.add_option('-s', '--server', default=SERVER)
18 parser.add_option('-u', '--user')
19 parser.add_option('-p', '--password')
20 parser.add_option('--private', action='store_true', default=False)
21 parser.add_option('--secret', action='store_true', default=False)
22
23 options, args = parser.parse_args()
24
25 if options.private and options.secret:
26 print "post can't be secret and private!"
27 sys.exit(1)
28
29 # parse dotfile
30
31 home = os.environ.get('HOME')
32 if home:
33 dotfile = os.path.join(home, dotfile)
34 if os.path.exists(dotfile):
35 prefs = file(dotfile).read().split('\n')
36 prefs = [ i for i in prefs if i.strip() ]
37 prefs = [ [ j.strip() for j in i.split(':', 1) ] for i in prefs
38 if ':' in i] # probably not necessary
39 prefs = dict(prefs)
40 else:
41 prefs = {}
42
43 # determine user name and password
44 fields = [ 'user', 'password' ]
45 for field in fields:
46 globals()[field] = prefs.get(field)
47
48 optval = getattr(options, field)
49 if optval:
50 password = None # needed to ensure prompting for pw from command line
51 globals()[field] = optval
52
53 if globals()[field] is None:
54 globals()[field] = raw_input('%s: ' % field)
55 assert user is not None
56 assert password is not None
57
58 # write the dotfile if it doesn't exist
59 if not os.path.exists(dotfile):
60 preffile = file(dotfile, 'w')
61 print >> preffile, 'user: %s' % user
62 print >> preffile, 'password: %s' % password
63 preffile.close()
64 os.chmod(dotfile, 0600)
65
66 def tmpbuffer(editor=EDITOR):
67 """open an editor and retreive the resulting editted buffer""" 14 """open an editor and retreive the resulting editted buffer"""
15 if not editor:
16 editor = os.environ['EDITOR']
68 tmpfile = tempfile.mktemp(suffix='.txt') 17 tmpfile = tempfile.mktemp(suffix='.txt')
69 cmdline = editor.split() 18 cmdline = editor.split()
70 cmdline.append(tmpfile) 19 cmdline.append(tmpfile)
71 edit = subprocess.call(cmdline) 20 edit = subprocess.call(cmdline)
72 buffer = file(tmpfile).read().strip() 21 buffer = file(tmpfile).read().strip()
73 os.remove(tmpfile) 22 os.remove(tmpfile)
74 return buffer 23 return buffer
75 24
76 # get the blog 25 def main(args=sys.argv[1:]):
77 26
78 if args: 27 # create the option parser
79 msg = ' '.join(args) 28 parser = optparse.OptionParser()
80 else: 29 parser.add_option('-s', '--server')
81 msg = tmpbuffer() 30 parser.add_option('-u', '--user')
31 parser.add_option('-p', '--password')
32 parser.add_option('--private', action='store_true', default=False)
33 parser.add_option('--secret', action='store_true', default=False)
82 34
83 # open the url 35 options, args = parser.parse_args()
36
37 # sanity check
38 if options.private and options.secret:
39 print "post can't be secret and private!"
40 sys.exit(1)
84 41
85 url = '/'.join((options.server, user)) 42 # parse dotfile
86 url += '?auth=digest' # specify authentication method 43 home = os.environ.get('HOME')
44 if home:
45 dotfile = os.path.join(home, dotfile)
46 if os.path.exists(dotfile):
47 prefs = file(dotfile).read().split('\n')
48 prefs = [ i for i in prefs if i.strip() ]
49 prefs = [ [ j.strip() for j in i.split(':', 1) ] for i in prefs
50 if ':' in i] # probably not necessary
51 prefs = dict(prefs)
52 else:
53 prefs = {}
87 54
88 if options.private: 55 # determine user name and password
89 url += '&privacy=private' 56 fields = [ 'user', 'password' ]
90 if options.secret: 57 for field in fields:
91 url += '&privacy=secret' 58 globals()[field] = prefs.get(field)
92 59
93 authhandler = urllib2.HTTPDigestAuthHandler() 60 optval = getattr(options, field)
94 authhandler.add_password('bitsyblog', url, user, password) 61 if optval:
95 opener = urllib2.build_opener(authhandler) 62 password = None # needed to ensure prompting for pw from command line
96 urllib2.install_opener(opener) 63 globals()[field] = optval
97 64
98 try: 65 if globals()[field] is None:
99 url = urllib2.urlopen(url, data=msg) 66 globals()[field] = raw_input('%s: ' % field)
100 print url.url # print the blog post's url 67 assert user is not None
101 except urllib2.HTTPError: 68 assert password is not None
102 pass 69
70 # write the dotfile if it doesn't exist
71 if not os.path.exists(dotfile):
72 preffile = file(dotfile, 'w')
73 print >> preffile, 'user: %s' % user
74 print >> preffile, 'password: %s' % password
75 preffile.close()
76 os.chmod(dotfile, 0600)
77
78
79 # get the blog
80 if args:
81 msg = ' '.join(args)
82 else:
83 msg = tmpbuffer()
84
85 # open the url
86 url = '/'.join((options.server, user))
87 url += '?auth=digest' # specify authentication method
88 if options.private:
89 url += '&privacy=private'
90 if options.secret:
91 url += '&privacy=secret'
92 authhandler = urllib2.HTTPDigestAuthHandler()
93 authhandler.add_password('bitsyblog', url, user, password)
94 opener = urllib2.build_opener(authhandler)
95 urllib2.install_opener(opener)
96
97 try:
98 url = urllib2.urlopen(url, data=msg)
99 print url.url # print the blog post's url
100 except urllib2.HTTPError:
101 pass
102
103 if __name__ == '__main__':
104 main()