comparison MoinMoin/action/SyncPages.py @ 1092:f80e88b33c9d

Added processing of local- and remoteMatch options to the SyncPages action.
author Alexander Schremmer <alex AT alexanderweb DOT de>
date Sat, 29 Jul 2006 23:35:33 +0200
parents 6632f9919a89
children e2cc6b5bed96
comparison
equal deleted inserted replaced
1091:8b309674bf06 1092:f80e88b33c9d
23 23
24 from MoinMoin import wikiutil, config, user 24 from MoinMoin import wikiutil, config, user
25 from MoinMoin.packages import unpackLine 25 from MoinMoin.packages import unpackLine
26 from MoinMoin.PageEditor import PageEditor 26 from MoinMoin.PageEditor import PageEditor
27 from MoinMoin.Page import Page 27 from MoinMoin.Page import Page
28 from MoinMoin.wikidicts import Dict 28 from MoinMoin.wikidicts import Dict, Group
29 29
30 30
31 class ActionStatus(Exception): pass 31 class ActionStatus(Exception): pass
32 32
33 33 # Move these classes to MoinMoin.wikisync
34 class RemotePage(object): 34 class RemotePage(object):
35 """ This class represents a page in (another) wiki. """ 35 """ This class represents a page in (another) wiki. """
36 def __init__(self, name, revno): 36 def __init__(self, name, revno):
37 self.name = name 37 self.name = name
38 self.revno = revno 38 self.revno = revno
39 39
40 def __repr__(self): 40 def __repr__(self):
41 return repr(u"%s<%i>" % (self.name, self.revno)) 41 return repr(unicode(self))
42
43 def __unicode__(self):
44 return u"%s<%i>" % (self.name, self.revno)
42 45
43 def __lt__(self, other): 46 def __lt__(self, other):
44 return self.name < other.name 47 return self.name < other.name
48
49 def filter(cls, rp_list, regex):
50 return [x for x in rp_list if regex.match(x.name)]
51 filter = classmethod(filter)
45 52
46 53
47 class RemoteWiki(object): 54 class RemoteWiki(object):
48 """ This class should be the base for all implementations of remote wiki 55 """ This class should be the base for all implementations of remote wiki
49 classes. """ 56 classes. """
72 self.connection = self.createConnection() 79 self.connection = self.createConnection()
73 # XXX add version and interwiki name checking! 80 # XXX add version and interwiki name checking!
74 81
75 def createConnection(self): 82 def createConnection(self):
76 if self.valid: 83 if self.valid:
77 return xmlrpclib.ServerProxy(self.xmlrpc_url, allow_none=True) 84 return xmlrpclib.ServerProxy(self.xmlrpc_url, allow_none=True, verbose=True)
78 else: 85 else:
79 return None 86 return None
80 87
81 # Methods implementing the RemoteWiki interface 88 # Methods implementing the RemoteWiki interface
82 def getInterwikiName(self): 89 def getInterwikiName(self):
93 class MoinLocalWiki(RemoteWiki): 100 class MoinLocalWiki(RemoteWiki):
94 """ Used for the current MoinMoin wiki. """ 101 """ Used for the current MoinMoin wiki. """
95 def __init__(self, request): 102 def __init__(self, request):
96 self.request = request 103 self.request = request
97 104
105 def getGroupItems(self, group_list):
106 pages = []
107 for group_pagename in group_list:
108 pages.extend(Group(self.request, group_pagename).members())
109 return [self.createRemotePage(x) for x in pages]
110
111 def createRemotePage(self, page_name):
112 return RemotePage(page_name, Page(self.request, page_name).get_real_rev())
113
98 # Methods implementing the RemoteWiki interface 114 # Methods implementing the RemoteWiki interface
99 def getInterwikiName(self): 115 def getInterwikiName(self):
100 return self.request.cfg.interwikiname 116 return self.request.cfg.interwikiname
101 117
102 def getPages(self): 118 def getPages(self):
103 l_pages = [[x, Page(self.request, x).get_real_rev()] for x in self.request.rootpage.getPageList(exists=0)] 119 return [self.createRemotePage(x) for x in self.request.rootpage.getPageList(exists=0)]
104 return [RemotePage(unicode(name), revno) for name, revno in l_pages]
105 120
106 def __repr__(self): 121 def __repr__(self):
107 return "<MoinLocalWiki>" 122 return "<MoinLocalWiki>"
108 123
109 124
137 def fix_params(self, params): 152 def fix_params(self, params):
138 """ Does some fixup on the parameters. """ 153 """ Does some fixup on the parameters. """
139 154
140 # merge the pageList case into the remoteMatch case 155 # merge the pageList case into the remoteMatch case
141 if params["pageList"] is not None: 156 if params["pageList"] is not None:
142 params["remoteMatch"] = u'|'.join([r'^%s$' % re.escape(name) for name in params["pageList"]]) 157 params["localMatch"] = params["remoteMatch"] = u'|'.join([r'^%s$' % re.escape(name)
158 for name in params["pageList"]])
143 159
144 if params["localMatch"] is not None: 160 if params["localMatch"] is not None:
145 params["localMatch"] = re.compile(params["localMatch"], re.U) 161 params["localMatch"] = re.compile(params["localMatch"], re.U)
146 162
147 if params["remoteMatch"] is not None: 163 if params["remoteMatch"] is not None:
181 def sync(self, params, local, remote): 197 def sync(self, params, local, remote):
182 """ This method does the syncronisation work. """ 198 """ This method does the syncronisation work. """
183 199
184 r_pages = remote.getPages() 200 r_pages = remote.getPages()
185 l_pages = local.getPages() 201 l_pages = local.getPages()
202 print "Got %i local, %i remote pages" % (len(l_pages), len(r_pages))
203 if params["localMatch"]:
204 l_pages = RemotePage.filter(l_pages, params["localMatch"])
205 if params["remoteMatch"]:
206 print "Filtering remote pages using regex %r" % params["remoteMatch"].pattern
207 r_pages = RemotePage.filter(r_pages, params["remoteMatch"])
208 print "After filtering: Got %i local, %i remote pages" % (len(l_pages), len(r_pages))
209
210 if params["groupList"]:
211 pages_from_groupList = local.getGroupItems(params["groupList"])
212 if not params["localMatch"]:
213 l_pages = pages_from_groupList
214 else:
215 l_pages += pages_from_groupList
186 216
187 # some initial test code 217 # some initial test code
188 r_new_pages = u",".join(set([repr(x) for x in r_pages]) - set([repr(x) for x in l_pages])) 218 r_new_pages = u", ".join(set([unicode(x) for x in r_pages]) - set([unicode(x) for x in l_pages]))
189 raise ActionStatus("These pages are in the remote wiki, but not local: " + r_new_pages) 219 raise ActionStatus("These pages are in the remote wiki, but not local: " + r_new_pages)
190 220
191 221
192 def execute(pagename, request): 222 def execute(pagename, request):
193 ActionClass(pagename, request).render() 223 ActionClass(pagename, request).render()