forked from jcfr/mwdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mwdoc.py
124 lines (102 loc) · 4.42 KB
/
mwdoc.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import mwclient
class Documentation(object):
N_MAIN = '0'
NS_TALK = '1'
NS_USER = '2'
NS_USERTALK = '3'
NS_TEST = '4'
NS_TESTTALK = '5'
NS_FILE = '6'
NS_FILETALK = '7'
NS_MEDIAWIKI = '8'
NS_MEDIAWIKITALK = '9'
NS_TEMPLATE = '10'
NS_TEMPLATETALK = '11'
NS_HELP = '12'
NS_HELPTALK = '13'
NS_CATEGORY = '14'
NS_CATEGORYTALK = '15'
VERBOSE = True
def __init__(self, host, path = ''):
self.site = mwclient.Site(host, path=path)
def login(self, username, password):
self.site.login(username, password)
def createPage(self, version, pageName, text, prefix = '', summary = ''):
"""Create a new documentation page
A page is identified by its fullpath [<prefix>/]<version>/<pageName>
If no summary is provided, it will default to 'Create page <fullpath>'
"""
if len(pageName) == 0:
pageFullPath = '/'.join((prefix, version))
else:
pageFullPath = '/'.join((prefix, version, pageName))
page = self.site.Pages[pageFullPath]
if page.exists:
if self.VERBOSE:
print "[WARNING] Skip page creation: Page already exists: '%s'" % pageFullPath
return False
if not summary:
summary = 'Create page %s' % pageFullPath
page.save(text, summary = summary)
if self.VERBOSE:
print "[INFO] Page successfully created: '%s'" % pageFullPath
return True
def versionPage(self, sourceVersion, targetVersion, pageBaseName, prefix = ''):
"""Create a new version of a page
The page identified by [<prefix>/]<sourceVersion>/<pageBaseName> will
be copied to [<prefix>/]<targetVersion>/<pageBaseName>
The summary associated with each page will be '<versionSource> ->
<versionTarget>'
"""
if len(pageBaseName) == 0:
pageFullPath = '/'.join((prefix, sourceVersion))
else:
pageFullPath = '/'.join((prefix, sourceVersion, pageBaseName))
sourcePage = self.site.Pages[pageFullPath]
if not sourcePage.exists:
if self.VERBOSE:
print "[WARNING] Skip versioning: Source page doesn't exist: '%s'" % pageFullPath
return False
text = sourcePage.edit()
return self.createPage(targetVersion, pageBaseName, text, prefix = prefix, summary = sourceVersion + ' -> ' + targetVersion)
def listPages(self, prefix, namespace = None):
"""List page associated with a given namespace.
By default, the function will try to guess the namespace using the
provided prefix.
It is also possible to specify a namespace using the defined
constants 'NS_*'
"""
if namespace == None:
namespace = self.site.Pages.guess_namespace(prefix)
prefix_without_ns = mwclient.page.Page.strip_namespace(prefix)
pages = self.site.allpages(prefix=prefix_without_ns, namespace=namespace)
return pages
def versionPages(self, sourceVersion, targetVersion, prefixes = None):
"""Create a new version of given documention set
A documentation set correspond to a collection of pages identified by a
a <sourceVersion> and a list of prefixes.
By providing:
sourceVersion = '0.1'
targetVersion = '0.2'
prefixes = ['Documentation', 'Template:Documentation']
All pages matching both 'Documentation/0.1*' and
'Template:Documentation/0.1*' will be copied into pages having prefixes
'Documentation/0.2' and
'Template:Documentation/0.2'.
"""
prefixes = prefixes or []
results = []
for prefix in prefixes:
sourcePageNamePrefix = '/'.join((prefix, sourceVersion))
sourcePages = self.listPages(sourcePageNamePrefix)
for sourcePage in sourcePages:
parts = sourcePage.name.split(sourcePageNamePrefix + '/')
targetPageBaseName = ''
if len(parts) == 2:
targetPageBaseName = parts[1]
results.append(self.versionPage(sourceVersion, targetVersion, targetPageBaseName, prefix))
return results
if __name__ == '__main__':
import unittest
suite = unittest.TestLoader().loadTestsFromName('mwdoc_test')
result = unittest.TextTestRunner(verbosity=2).run(suite)