New login command (that saves your login, too)
authorIan Weller <ian@ianweller.org>
Sun, 14 Mar 2010 07:45:04 +0000 (01:45 -0600)
committerIan Weller <ian@ianweller.org>
Sun, 14 Mar 2010 07:45:04 +0000 (01:45 -0600)
src/mw/api.py
src/mw/clicommands.py
src/mw/metadir.py

index 59a3947a81f33a45f7c0ac5c494b33473efcca97..334c25edb79c33b94ef73f2aa681e3c958305984 100644 (file)
@@ -19,6 +19,8 @@
 import cookielib
 import gzip
 import json
 import cookielib
 import gzip
 import json
+import mw.metadir
+import os
 from StringIO import StringIO
 import urllib
 import urllib2
 from StringIO import StringIO
 import urllib
 import urllib2
@@ -26,9 +28,17 @@ import urllib2
 
 class API(object):
 
 
 class API(object):
 
-    def __init__(self, api_url):
+    def __init__(self, api_url, metadir):
         self.api_url = api_url
         self.api_url = api_url
-        self.cookiejar = cookielib.CookieJar()
+        self.metadir = metadir
+        self.cookiejar = cookielib.MozillaCookieJar(os.path.join(
+                self.metadir.location, 'cookies'
+        ))
+        try:
+            self.cookiejar.load()
+        except IOError:
+            self.cookiejar.save()
+            self.cookiejar.load()
         self.opener = urllib2.build_opener(
                 urllib2.HTTPCookieProcessor(self.cookiejar))
         self._high_limits = None
         self.opener = urllib2.build_opener(
                 urllib2.HTTPCookieProcessor(self.cookiejar))
         self._high_limits = None
@@ -38,6 +48,7 @@ class API(object):
         request = urllib2.Request(self.api_url, urllib.urlencode(data))
         request.add_header('Accept-encoding', 'gzip')
         response = self.opener.open(request)
         request = urllib2.Request(self.api_url, urllib.urlencode(data))
         request.add_header('Accept-encoding', 'gzip')
         response = self.opener.open(request)
+        self.cookiejar.save()
         if response.headers.get('Content-Encoding') == 'gzip':
             compressed = StringIO(response.read())
             gzipper = gzip.GzipFile(fileobj=compressed)
         if response.headers.get('Content-Encoding') == 'gzip':
             compressed = StringIO(response.read())
             gzipper = gzip.GzipFile(fileobj=compressed)
index 5eab8bdbea9b982834b1e9b700311e08b0783c10..4b62564cfbf03f7a504fe607a132cb2a6e772c78 100644 (file)
@@ -57,8 +57,11 @@ class CommandBase(object):
         result = self.api.call({'action': 'login',
                                 'lgname': user,
                                 'lgpassword': passwd})
         result = self.api.call({'action': 'login',
                                 'lgname': user,
                                 'lgpassword': passwd})
-        if result['login']['result'] != 'Success':
-            raise Exception('Login error: %s' % result['login']['result'])
+        if result['login']['result'] == 'Success':
+            # cookies are saved to a file
+            print 'Login successful! (yay)'
+        else:
+            print 'Login failed: %s' % result['login']['result']
 
     def _die_if_no_init(self):
         if self.metadir.config is None:
 
     def _die_if_no_init(self):
         if self.metadir.config is None:
@@ -67,7 +70,7 @@ class CommandBase(object):
 
     def _api_setup(self):
         self.api_url = self.metadir.config.get('remote', 'api_url')
 
     def _api_setup(self):
         self.api_url = self.metadir.config.get('remote', 'api_url')
-        self.api = mw.api.API(self.api_url)
+        self.api = mw.api.API(self.api_url, self.metadir)
 
 
 class InitCommand(CommandBase):
 
 
 class InitCommand(CommandBase):
@@ -75,15 +78,24 @@ class InitCommand(CommandBase):
     def __init__(self):
         usage = 'API_URL'
         CommandBase.__init__(self, 'init', 'start a mw repo', usage)
     def __init__(self):
         usage = 'API_URL'
         CommandBase.__init__(self, 'init', 'start a mw repo', usage)
-        self.parser.add_option('-u', '--username', dest='username',
-                               help='use wiki with login')
 
     def _do_command(self):
         if len(self.args) < 1:
             self.parser.error('must have URL to remote api.php')
         elif len(self.args) > 1:
             self.parser.error('too many arguments')
 
     def _do_command(self):
         if len(self.args) < 1:
             self.parser.error('must have URL to remote api.php')
         elif len(self.args) > 1:
             self.parser.error('too many arguments')
-        self.metadir.create(self.args[0], self.options.username)
+        self.metadir.create(self.args[0])
+
+
+class LoginCommand(CommandBase):
+
+    def __init__(self):
+        CommandBase.__init__(self, 'login', 'authenticate with wiki')
+
+    def _do_command(self):
+        self._die_if_no_init()
+        self._api_setup()
+        self._login()
 
 
 class PullCommand(CommandBase):
 
 
 class PullCommand(CommandBase):
index 6c6bfeb1e2d45417e132d8dc8e552817353e68f0..b637b67a6a7024da56e1c30f67e5ce6a20b20c34 100644 (file)
@@ -49,7 +49,11 @@ class Metadir(object):
         else:
             self.config = None
 
         else:
             self.config = None
 
-    def create(self, api_url, username=None):
+    def save_config(self):
+        with open(self.config_loc, 'wb') as config_file:
+            self.config.write(config_file)
+
+    def create(self, api_url):
         # create the directory
         if os.path.isdir(self.location):
             print '%s: you are already in a mw repo' % self.me
         # create the directory
         if os.path.isdir(self.location):
             print '%s: you are already in a mw repo' % self.me
@@ -64,10 +68,7 @@ class Metadir(object):
         self.config = ConfigParser.RawConfigParser()
         self.config.add_section('remote')
         self.config.set('remote', 'api_url', api_url)
         self.config = ConfigParser.RawConfigParser()
         self.config.add_section('remote')
         self.config.set('remote', 'api_url', api_url)
-        if username != None:
-            self.config.set('remote', 'username', username)
-        with open(self.config_loc, 'wb') as config_file:
-            self.config.write(config_file)
+        self.save_config()
         # create cache/
         os.mkdir(os.path.join(self.location, 'cache'))
         # create cache/pagedict
         # create cache/
         os.mkdir(os.path.join(self.location, 'cache'))
         # create cache/pagedict

Benjamin Mako Hill || Want to submit a patch?