changeset 45:c228832db770

fix blogme
author k0s <k0scist@gmail.com>
date Tue, 24 Nov 2009 19:20:47 -0500
parents 0e7f56709c90
children 66bfcceea3b4
files bitsyblog/blogme.py bitsyblog/factory.py bitsyblog/utils.py
diffstat 3 files changed, 60 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/bitsyblog/blogme.py	Wed Nov 11 19:39:27 2009 -0500
+++ b/bitsyblog/blogme.py	Tue Nov 24 19:20:47 2009 -0500
@@ -12,6 +12,7 @@
 import urllib2
 
 from ConfigParser import ConfigParser
+from utils import datestamp
 
 # global variable
 CONFIG = '.blogme'
@@ -33,6 +34,7 @@
     # parse command line options
     parser = optparse.OptionParser()
     parser.add_option('-c', '--config')
+    parser.add_option('-f', '--file')
     parser.add_option('-H', '--host')
     parser.add_option('-u', '--user')
     parser.add_option('-p', '--password')
@@ -45,37 +47,72 @@
         print "post can't be secret and private!"
         sys.exit(1)
 
+    if options.file:
+        # get the blog
+        if args:
+            msg = ' '.join(args)
+        else:
+            msg = tmpbuffer()
+        options.file = os.path.join(options.file, 'entries')
+        for opt in 'private', 'secret':
+            if getattr(options, opt):
+                options.file = os.path.join(options.file, opt)
+                break
+        else:
+            options.file = os.path.join(options.file, 'public')
+        options.file = os.path.join(options.file, datestamp())
+        f = file(options.file, 'w')
+        print >> f, msg
+        f.close()
+        sys.exit(0)
+
     # parse dotfile config
     config = ConfigParser()
+    _config = {} # config to write out
     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)
 
+    # get default host, if available
+    if options.host:
+        if config.has_option('DEFAULTS', 'host'):
+            print "Make %s the default host? (Y/n): ",
+            answer = raw_input()
+            if not anwer.lower().startswith('n'):
+                _config['default'] = options.host
+    else:
+        if config.has_option('DEFAULTS', 'host'):
+            options.host = config.get('DEFAULTS', 'host')
+        else:
+            print "Enter URL of host: ",
+            host = raw_input()
+            _config['default'] = host
+
     # determine user name and password
     fields = [ 'user', 'password' ]
     for field in fields:
-        globals()[field] = prefs.get(field)
-
-        optval = getattr(options, field)
-        if optval:
-            password = None # needed to ensure prompting for pw from command line
-            globals()[field] = optval
-
-        if globals()[field] is None:
-            globals()[field] = raw_input('%s: ' % field)
-    assert user is not None
-    assert password is not None
+        if hasattr(options, field):
+            if (not config.has_option(options.host, field)) or (config.get(options.host, field) != getattr(options, field)):
+                _config[field] = getattr(options, field)
+        else:
+            print "%s: " % field, 
+            setattr(options, field, raw_input())
+            _config[field] = getattr(options, field)
 
     # 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 _config:
+        if 'default' in _config:
+            if not config.has_section('DEFAULTS'):
+                config.add_section('DEFAULTS')
+            config.set('DEFAULTS', 'host', options.host)
+        if not config.has_section(options.host):
+            config.add_section(options.host)
+        for field in fields:
+            if not config.has_option(options.host, field):
+                config.set(options.host, field, getattr(options, field))
+        config.write()
 
     # get the blog
     if args:
@@ -91,7 +128,7 @@
     if options.secret:
         url += '&privacy=secret'
     authhandler = urllib2.HTTPDigestAuthHandler()
-    authhandler.add_password('bitsyblog', url, user, password)
+    authhandler.add_password('bitsyblog', url, options.user, options.password)
     opener = urllib2.build_opener(authhandler)
     urllib2.install_opener(opener)
     try:
--- a/bitsyblog/factory.py	Wed Nov 11 19:39:27 2009 -0500
+++ b/bitsyblog/factory.py	Tue Nov 24 19:20:47 2009 -0500
@@ -1,4 +1,4 @@
-from bitsyauth.bitsyauth import BitsyAuth
+from bitsyauth import BitsyAuth
 from bitsyblog import BitsyBlog, BitsierBlog
 from getpass import getpass 
 from paste.httpexceptions import HTTPExceptionHandler
--- a/bitsyblog/utils.py	Wed Nov 11 19:39:27 2009 -0500
+++ b/bitsyblog/utils.py	Tue Nov 24 19:20:47 2009 -0500
@@ -25,5 +25,7 @@
         datestamp = datestamp[len(i):]
     return datetime.datetime(*retval)
 
-def datestamp(date):
+def datestamp(date=None):
+    if date is None:
+        date = datetime.datetime.now()
     return date.strftime(timestamp)