[cmsiki-svn] SF.net SVN: cmsiki: [5] trunk/cmsiki/cmsikireplace.py
Status: Pre-Alpha
Brought to you by:
cnu
|
From: <cn...@us...> - 2007-03-22 19:58:57
|
Revision: 5
http://cmsiki.svn.sourceforge.net/cmsiki/?rev=5&view=rev
Author: cnu
Date: 2007-03-22 12:41:51 -0700 (Thu, 22 Mar 2007)
Log Message:
-----------
cmsikireplace module done
Added Paths:
-----------
trunk/cmsiki/cmsikireplace.py
Added: trunk/cmsiki/cmsikireplace.py
===================================================================
--- trunk/cmsiki/cmsikireplace.py (rev 0)
+++ trunk/cmsiki/cmsikireplace.py 2007-03-22 19:41:51 UTC (rev 5)
@@ -0,0 +1,100 @@
+'''CMS - Wiki page replace
+
+Get text from a CMS which contains various predefined tags which has
+to be replaced by data from a wiki. The data is retrieved through
+XMLRPC from the wiki and is replaced back into the original text.'''
+
+__author__ = """
+ Srinivasan R <sri...@gm...>
+ """
+__copyright__ = "Copyright (c) 2007 Srinivasan R"
+
+
+import re
+import xmlrpclib
+
+# List of wiki and its various information.
+# Must change to get the list dynamically from a django database.
+wikilist = [
+ {'name':'localwiki',
+ 'url':'http://localhost:8080/',
+ 'xmlrpc':'http://localhost:8080/?action=xmlrpc2'},
+ {'name':'ChennaiPy',
+ 'url':'http://nrcfosshelpline.in/chennaipy/',
+ 'xmlrpc':'http://nrcfosshelpline.in/chennaipy/?action=xmlrpc2'}
+ ]
+
+def wikirpc(wikiname):
+ '''Return the wiki dictionary for the given wikiname.'''
+ for wiki in wikilist:
+ if wiki['name'] == wikiname:
+ return wiki
+
+
+def getPageHTML(tag):
+ '''Return the page contents from the wiki as HTML.'''
+ page_pattern = re.compile(':[a-zA-Z]+@')
+ wiki_pattern = re.compile('@[a-zA-Z]+\]')
+ page_match = page_pattern.findall(tag)[0]
+ page_name = page_match[1:-1]
+ wiki_match = wiki_pattern.findall(tag)[0]
+ wiki_name = wiki_match[1:-1]
+ wiki = wikirpc(wiki_name)
+ srcwiki = xmlrpclib.ServerProxy(wiki['xmlrpc'])
+ page_html_content = srcwiki.getPageHTML(page_name)
+ return page_html_content.replace('<a href="/','<a href="%s' %(wiki['url']))
+
+def getPage(tag):
+ '''Returns the page contents from the wiki as WikiText.'''
+ page_pattern = re.compile(':[a-zA-Z]+@')
+ wiki_pattern = re.compile('@[a-zA-Z]+\]')
+ page_match = page_pattern.findall(tag)[0]
+ page_name = page_match[1:-1]
+ wiki_match = wiki_pattern.findall(tag)[0]
+ wiki_name = wiki_match[1:-1]
+ wiki = wikirpc(wiki_name)
+ srcwiki = xmlrpclib.ServerProxy(wiki['xmlrpc'])
+ page_content = srcwiki.getPage(page_name)
+ return page_content
+
+def getPageInfo(tag):
+ '''Return the page contents from the wiki as WikiText.'''
+ page_pattern = re.compile(':[a-zA-Z]+@')
+ wiki_pattern = re.compile('@[a-zA-Z]+\]')
+ page_match = page_pattern.findall(tag)[0]
+ page_name = page_match[1:-1]
+ wiki_match = wiki_pattern.findall(tag)[0]
+ wiki_name = wiki_match[1:-1]
+ wiki = wikirpc(wiki_name)
+ srcwiki = xmlrpclib.ServerProxy(wiki['xmlrpc'])
+ page_info = srcwiki.getPageInfo(page_name)
+ # return the wiki page info in a string format
+ page_string_info = 'PageName: %s \nAuthor: %s \nVersion: %s \n' \
+ %(page_info['name'], page_info['author'], page_info['version'])
+ return str(page_string_info)
+
+
+dict_func = {'getPage':getPage,
+ 'getPageHTML':getPageHTML,
+ 'getPageInfo':getPageInfo
+ }
+
+
+def tagreplace(originaltext):
+ '''Replace the originaltext with text from wiki.'''
+ wikireplacelist = []
+ tagpattern = re.compile('\[[a-zA-Z]+:\w+@[a-zA-Z]+\]')
+ tagmatches = tagpattern.findall(originaltext) # find list of all import tags
+ methodpattern = re.compile('\[[a-zA-Z]+')
+ for tag in tagmatches:
+ wikireplace = {}
+ methodmatch = methodpattern.findall(tag)[0][1:] # Find the method in a tag
+ # Must decide which method is better.
+ wikipage = dict_func[methodmatch](tag)
+ # wikipage = eval(methodmatch)(tag)
+ wikireplacelist.append(dict({'tag':tag,'page':wikipage}))
+ for item in wikireplacelist:
+ originaltext = originaltext.replace(item['tag'],item['page'])
+ return originaltext
+
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|