import cookielib
import gzip
import json
+import mw.metadir
+import os
from StringIO import StringIO
import urllib
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
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)
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 _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):
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):
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
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