changeset 24:045544951756

this actually works, by god
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 03 Aug 2011 14:39:56 -0700
parents e6ed732e8ce6
children 0ce6b501f62d
files document_it.py
diffstat 1 files changed, 37 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/document_it.py	Wed Aug 03 12:15:50 2011 -0700
+++ b/document_it.py	Wed Aug 03 14:39:56 2011 -0700
@@ -89,12 +89,33 @@
 def item_url(item, dest):
     if '://' in dest:
         if '%(page)s' in dest:
-            return dest % {'page': item}
+            return dest % {'page': item.replace('/', r'%25%32%66')}
         else:
             return '%s/%s' % (dest.lstrip('/'), item.rstrip('/'))
     else:
         return 'file://%s' % (os.path.join(dest, item))
-        
+
+
+def render(filename):
+    """render a file in markdown"""
+    return markdown.Markdown().convert(file(filename).read())    
+
+def post(content, url, user, password):
+    """post to the specified URL"""
+    # XXX cheat and use curl for now
+    try:
+        from subprocess import check_call as call
+    except:
+        from subprocess import call
+    content = content.replace('\r', '').replace('\n', '\r\n') # just to make sure
+    fd, filename = tempfile.mkstemp()
+    os.write(fd, content)
+    os.close(fd)
+    command = ["curl", "-u", "%s:%s" % (user, password),
+               "--data-binary", "@%s" % filename,
+               "-i", url]
+    call(command)
+    os.remove(filename)
 
 def main(args=sys.argv[1:]):
 
@@ -124,6 +145,9 @@
                       help='user password')
     parser.add_option('--list', dest='list', action='store_true', default=False,
                       help="list files")
+    parser.add_option('-l', '--list-destinations', dest='list_destinations',
+                      action='store_true', default=False,
+                      help='list preconfigured destinations')
     parser.add_option('--validate', dest='validate', # TODO unused
                       action='store_true', default=False,
                       help="validate the rendering but don't output")
@@ -134,8 +158,16 @@
         parser.print_help()
         parser.exit()
 
-    # get base url
+    # print preconfigured destinations if asked
+    if options.list_destinations:
+        for key in sorted(destinations.keys()):
+            print '%s: %s' % (key, destinations[key])
+        return # you're done
+
+    # get destination
     assert options.dest
+    if options.dest in destinations:
+        options.dest = destinations[options.dest]
     if options.dest.startswith('file://'):
         options.dest = options.dest[len('file://'):]
     if '://' not in options.dest:
@@ -163,11 +195,8 @@
         assert options.user and options.password, "Please supply your --user and --password"
 
         # upload the files
-        # XXX just use curl for now
-        import subprocess
         for src, dest in files:
-            dest += '/contents?edittime=now'
-            print src, dest
+            post(render(src), dest, options.user, options.password)
 
     else:
 
@@ -199,8 +228,7 @@
 
             # render
             f = file(dest, 'w')
-            buffer = markdown.Markdown().convert(file(src).read())
-            f.write(buffer)
+            f.write(render(src))
             f.close()
 
     # print out destination directory if using the temporary default