[Pywikipedia-l] SVN: [6330] trunk/pywikipedia

kim at svn.wikimedia.org kim at svn.wikimedia.org
Thu Feb 5 23:08:58 UTC 2009


Revision: 6330
Author:   kim
Date:     2009-02-05 23:08:58 +0000 (Thu, 05 Feb 2009)

Log Message:
-----------
Add an example on how to use pywikipedia as a library.

Added Paths:
-----------
    trunk/pywikipedia/logindata.py
    trunk/pywikipedia/simple_family.py

Added: trunk/pywikipedia/logindata.py
===================================================================
--- trunk/pywikipedia/logindata.py	                        (rev 0)
+++ trunk/pywikipedia/logindata.py	2009-02-05 23:08:58 UTC (rev 6330)
@@ -0,0 +1,100 @@
+# -*- coding: utf-8  -*- 
+
+#Usable example module: Use of pywikipedia as a
+# library.
+# 
+# Looks up the path to pywikipedia (pywikipedia_path)
+# in a settings.py file. You'll need to provide that,
+# and/or refactor.
+
+# (C) Kim Bruning for Wikiation, sponsored by Kennisnet, 2009
+#
+# Distributed under the terms of the MIT license.
+#
+
+import sys, os
+import settings
+if settings.pywikipedia_path not in sys.path:
+	sys.path.append(settings.pywikipedia_path)
+
+# pywikipedia can only set itself up if everything is
+# done in its own directory. This needs fixing sometime.
+# for now, we live with it.
+cwd=os.getcwd()
+os.chdir(settings.pywikipedia_path)
+import wikipedia, login
+from simple_family import Family
+os.chdir(cwd)
+
+class LoginData:
+	"""An example class that uses pywikipedia as a library.
+	usage example: 
+	
+	from logindata import LoginData, wikipedia
+	target_wiki=LoginData( ... ) # for example, fill in from a settings file, or use code to generate, or ... 
+	site=target_wiki.login()
+	page=wikipedia.Page(site,"Main Page")
+	"""
+
+
+	def __init__(
+		"""
+		paramaters:
+		name: arbitrary name. Pick something easy to remember
+		protocol: http|https
+		server: dns address or ip address
+		scriptpath: path on server itself
+			(ie:  protocol:server/scriptpath   http://6.wikiation.nl/revisions/REL1.13.2)
+		version: mediawiki version of the target mediawiki instance
+		lang: default language, as configured on target mediawiki instance
+		encoding: should (almost) always be utf-8
+		api_supported: Does this mediawiki instance support the mediawiki api?
+		RversionTab: Magic. See superclass for information.
+		user: Username on wiki
+		password: password for this user
+		"""
+
+
+		self,
+		name='MY_NAME_FOR_THIS_SERVER',
+		protocol='http',
+		server='www.my_server.com',
+		scriptpath='/my/script/path/',
+		version='1.13.2',
+		lang='en',
+		encoding='utf-8',
+		user='MY_BOT_USER',
+		password='MY_SECRET_PASSWORD',
+		RversionTab=None,
+		api_supported=False	
+		):
+		self.lang=lang
+		self.user=user
+		self.password=password
+		self.family=base_family.Family(
+			name=name, 
+			protocol=protocol,
+			server=server,
+			scriptpath=scriptpath,
+			version=version,
+			lang=lang,
+			encoding=encoding,
+			RversionTab=RversionTab,
+			api_supported=api_supported)
+		self.site=None
+	
+	def login(self):
+		"""Attempt to log in on the site described
+		by this class. Returns a pywikipedia site object"""
+		self.site=wikipedia.Site(
+			code=self.lang,
+			fam=self.family,
+			user=self.user
+			)
+		loginManager=login.LoginManager(
+			password=self.password,
+			site=self.site,
+			username=self.user
+			)
+		loginManager.login()
+		return self.site

Added: trunk/pywikipedia/simple_family.py
===================================================================
--- trunk/pywikipedia/simple_family.py	                        (rev 0)
+++ trunk/pywikipedia/simple_family.py	2009-02-05 23:08:58 UTC (rev 6330)
@@ -0,0 +1,98 @@
+# -*- coding: utf-8  -*- 
+
+# ============================================
+# NOTE FOR USERS: Unlike the Family files in 
+# the family # directory, you do not need to 
+# edit this file to configure anything.
+# ============================================
+
+# (C) Kim Bruning for Wikiation, sponsored by Kennisnet, 2009
+#
+# Distributed under the terms of the MIT license.
+#
+
+import sys, settings
+if settings.pywikipedia_path not in sys.path:
+	sys.path.append(settings.pywikipedia_path)
+
+import config, family, urllib   
+class Family(family.Family): 
+	"""Friendlier version of the pywikipedia family class.
+	We can use this in conjunction with none-pywikipedia
+	config files.
+	
+	Note that this just handles most common cases.
+	If you run into a special case, you'll have to fall back
+	to your regular pywikipedia.
+	"""
+	
+	def __init__(self,
+		"""name: arbitrary name. Pick something easy to remember
+		protocol: http|https
+		server: dns address or ip address
+		scriptpath: path on server itself
+			(ie:  protocol:server/scriptpath   http://6.wikiation.nl/revisions/REL1.13.2)
+		version: mediawiki version of the target mediawiki instance
+		lang: default language, as configured on target mediawiki instance
+		encoding: should (almost) always be utf-8
+		api_supported: Does this mediawiki instance support the mediawiki api?
+		RversionTab: Magic. See superclass for information.
+		"""
+		name='MY_NAME_FOR_THIS_SERVER',
+		protocol='http',
+		server='www.my_server.com',
+		scriptpath='/my/script/path',
+		version='1.13.2',
+		lang='en',
+		encoding='utf-8',
+		api_supported=False,	
+		RversionTab=None	# very rare beast, you probably won't need it.
+		):              
+
+		family.Family.__init__(self) 
+		self.name = name        # REQUIRED; replace with actual name
+
+		self.langs = {                # REQUIRED
+		    lang: server,  # Include one line for each wiki in family
+		}
+		self._protocol=protocol
+		self._scriptpath=scriptpath
+		self._version=version
+		self._encoding=encoding
+		# may as well add these here, so we can have a 1 stop shop
+		self._lang=lang
+		self._server=server
+		self._api_supported=api_supported
+		self._RversionTab=RversionTab
+
+	def protocol(self, code):
+		"""
+		returns "http" or "https"
+	"""
+		return self._protocol
+
+	def scriptpath(self, code):
+		"""returns the prefix used to locate scripts on this wiki.
+		"""
+		return self._scriptpath
+
+	def apipath(self, code):
+		"""returns whether or not this wiki 
+		if self._api_supported:
+			return '%s/api.php' % self.scriptpath(code)
+		else:
+			raise NotImplementedError, "%s wiki family does not support api.php" % self.name
+
+    # Which version of MediaWiki is used?
+	def version(self, code):
+		# Replace with the actual version being run on your wiki
+		return self._version
+
+	def code2encoding(self, code):
+		"""Return the encoding for a specific language wiki"""
+		# Most wikis nowadays use UTF-8, but change this if yours uses
+		# a different encoding
+		return self._encoding
+
+	def RversionTab(self, code):
+		return self._RversionTab





More information about the Pywikipedia-l mailing list