Revision: 4502 Author: misza13 Date: 2007-11-04 20:24:44 +0000 (Sun, 04 Nov 2007)
Log Message: ----------- Starting the "rewrite" branch. Committing basic HTTP and API access classes as well as a unit test.
Added Paths: ----------- branches/rewrite/ branches/rewrite/pywikibot/ branches/rewrite/pywikibot/data/ branches/rewrite/pywikibot/data/__init__.py branches/rewrite/pywikibot/data/api.py branches/rewrite/pywikibot/data/http.py branches/rewrite/pywikibot/data/test.py
Property changes on: branches/rewrite ___________________________________________________________________ Name: svn:ignore + *.pyc
Property changes on: branches/rewrite/pywikibot ___________________________________________________________________ Name: svn:ignore + *.pyc
Property changes on: branches/rewrite/pywikibot/data ___________________________________________________________________ Name: svn:ignore + *.pyc
Added: branches/rewrite/pywikibot/data/__init__.py =================================================================== --- branches/rewrite/pywikibot/data/__init__.py (rev 0) +++ branches/rewrite/pywikibot/data/__init__.py 2007-11-04 20:24:44 UTC (rev 4502) @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +""" +The "data" module provides several physical layers of data access to the wiki. +""" +# +# (C) Pywikipedia bot team, 2007 +# +# Distributed under the terms of the MIT license. +# +__version__ = '$Id: $' + +#Import the classes exposed by this module: +from http import HTTP +from api import API
Property changes on: branches/rewrite/pywikibot/data/__init__.py ___________________________________________________________________ Name: svn:eol-style + native
Added: branches/rewrite/pywikibot/data/api.py =================================================================== --- branches/rewrite/pywikibot/data/api.py (rev 0) +++ branches/rewrite/pywikibot/data/api.py 2007-11-04 20:24:44 UTC (rev 4502) @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +""" +Interface functions to Mediawiki's api.php +""" +# +# (C) Pywikipedia bot team, 2007 +# +# Distributed under the terms of the MIT license. +# +__version__ = '$Id: $' + + +import urllib +import http + + +class API: + + def __init__(self, site): + self.site = site + + def query(prop, **kwargs): + params = dict(kwargs) + params['action'] = 'query' + if not params.has_key('format'): #Most probably, we want the XML format + params['format'] = 'xml' + address = '/w/api.php?' + urllib.urlencode(params) + + return http.HTTP(None).GET(address) #TODO: Use site's HTTP object instead
Property changes on: branches/rewrite/pywikibot/data/api.py ___________________________________________________________________ Name: svn:eol-style + native
Added: branches/rewrite/pywikibot/data/http.py =================================================================== --- branches/rewrite/pywikibot/data/http.py (rev 0) +++ branches/rewrite/pywikibot/data/http.py 2007-11-04 20:24:44 UTC (rev 4502) @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- +""" +Basic HTTP access interface (GET/POST wrappers). +""" +# +# (C) Pywikipedia bot team, 2007 +# +# Distributed under the terms of the MIT license. +# +__version__ = '$Id: $' + + +import httplib + + +class HTTP: + + def __init__(self, site): + self.site = site + self.useragent = 'PythonWikipediaBot/2.0' + #TODO: Initiate persistent connection here? + + + def GET(self, address): + #TODO: Resuse said connection. + conn = httplib.HTTPConnection('en.wikipedia.org',80) #TODO: Obviously, get these from the site object (unimplemented yet) + conn.putrequest('GET',address) + conn.putheader('User-agent',self.useragent) + conn.endheaders() + conn.send('') + + response = conn.getresponse() + data = response.read() + + return response.status, data
Property changes on: branches/rewrite/pywikibot/data/http.py ___________________________________________________________________ Name: svn:eol-style + native
Added: branches/rewrite/pywikibot/data/test.py =================================================================== --- branches/rewrite/pywikibot/data/test.py (rev 0) +++ branches/rewrite/pywikibot/data/test.py 2007-11-04 20:24:44 UTC (rev 4502) @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +""" +Set of test suites for the data module. +""" +# +# (C) Pywikipedia bot team, 2007 +# +# Distributed under the terms of the MIT license. +# +__version__ = '$Id: $' + + +import unittest +import http, api + + +class HTTPTest(unittest.TestCase): + + def setUp(self): + self.HTTP = http.HTTP(None) #TODO: Replace None with an actual Site object once implemented + + def testGETMainPage(self): + """GETting the Main Page should give a HTTP 200 response.""" + status, data = self.HTTP.GET('/w/index.php?title=Main_Page') + self.assertEqual(status, 200) + + +class APITest(unittest.TestCase): + + def setUp(self): + self.API = api.API(None) #TODO: Replace None with an actual Site object once implemented + + def testGETMainPage(self): + """Querying for nothing should return an empty <api /> tag.""" + status, data = self.API.query() + self.assertEqual(status, 200) + self.assertEqual(data, '<?xml version="1.0" encoding="utf-8"?><api />') + + +if __name__ == '__main__': + unittest.main()
Property changes on: branches/rewrite/pywikibot/data/test.py ___________________________________________________________________ Name: svn:eol-style + native