From e5ac78fd19391a33ca958071fe2011edc5ff9074 Mon Sep 17 00:00:00 2001 From: Ian Weller Date: Sun, 14 Mar 2010 01:45:04 -0600 Subject: [PATCH] New login command (that saves your login, too) --- src/mw/api.py | 15 +++++++++++++-- src/mw/clicommands.py | 24 ++++++++++++++++++------ src/mw/metadir.py | 11 ++++++----- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/mw/api.py b/src/mw/api.py index 59a3947..334c25e 100644 --- a/src/mw/api.py +++ b/src/mw/api.py @@ -19,6 +19,8 @@ import cookielib import gzip import json +import mw.metadir +import os from StringIO import StringIO import urllib import urllib2 @@ -26,9 +28,17 @@ import urllib2 class API(object): - def __init__(self, api_url): + def __init__(self, api_url, metadir): 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 @@ -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) + self.cookiejar.save() if response.headers.get('Content-Encoding') == 'gzip': compressed = StringIO(response.read()) gzipper = gzip.GzipFile(fileobj=compressed) diff --git a/src/mw/clicommands.py b/src/mw/clicommands.py index 5eab8bd..4b62564 100644 --- a/src/mw/clicommands.py +++ b/src/mw/clicommands.py @@ -57,8 +57,11 @@ class CommandBase(object): 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: @@ -67,7 +70,7 @@ class CommandBase(object): 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): @@ -75,15 +78,24 @@ class InitCommand(CommandBase): 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') - 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): diff --git a/src/mw/metadir.py b/src/mw/metadir.py index 6c6bfeb..b637b67 100644 --- a/src/mw/metadir.py +++ b/src/mw/metadir.py @@ -49,7 +49,11 @@ class Metadir(object): 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 @@ -64,10 +68,7 @@ class Metadir(object): 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 -- 2.39.5