f7f2fba90e64942d933442eeabe1e1f3501993b0
[mw] / src / mw / metadir.py
1 ###
2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2010  Ian Weller <ian@ianweller.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program.  If not, see <http://www.gnu.org/licenses/>.
17 ###
18
19 import ConfigParser
20 import json
21 import os
22 import sys
23
24
25 class Metadir(object):
26
27     def __init__(self):
28         self.me = os.path.basename(sys.argv[0])
29         root = os.getcwd()
30         while True:
31             if '.mw' in os.listdir(root):
32                 self.root = root
33                 break
34             head = os.path.split(root)[0]
35             if head == root:
36                 self.root = os.getcwd()
37                 break
38             root = head
39         self.location = os.path.join(self.root, '.mw')
40         self.config_loc = os.path.join(self.location, 'config')
41         if os.path.isdir(self.location) and \
42            os.path.isfile(self.config_loc):
43             self.config = ConfigParser.RawConfigParser()
44             self.config.read(self.config_loc)
45         else:
46             self.config = None
47
48     def create(self, api_url):
49         # create the directory
50         if os.path.isdir(self.location):
51             print '%s: you are already in a mw repo' % self.me
52             sys.exit(1)
53         else:
54             os.mkdir(self.location, 0755)
55         # metadir versioning
56         fd = file(os.path.join(self.location, 'version'), 'w')
57         fd.write('1') # XXX THIS API VERSION NOT LOCKED IN YET
58         fd.close()
59         # create config
60         self.config = ConfigParser.RawConfigParser()
61         self.config.add_section('remote')
62         self.config.set('remote', 'api_url', api_url)
63         with open(self.config_loc, 'wb') as config_file:
64             self.config.write(config_file)
65         # create cache/
66         os.mkdir(os.path.join(self.location, 'cache'))
67         # create cache/pagedict
68         fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'w')
69         fd.write(json.dumps({}))
70         fd.close()
71         # create cache/pages/
72         os.mkdir(os.path.join(self.location, 'cache', 'pages'), 0755)
73
74     def pagedict_add(self, pagename, pageid, currentrv):
75         fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r+')
76         pagedict = json.loads(fd.read())
77         pagedict[pagename] = {'id': int(pageid), 'currentrv': int(currentrv)}
78         fd.seek(0)
79         fd.write(json.dumps(pagedict))
80         fd.truncate()
81         fd.close()
82
83     def get_pageid_from_pagename(self, pagename):
84         fd = file(os.path.join(self.location, 'cache', 'pagedict'), 'r')
85         pagedict = json.loads(fd.read())
86         if pagename in pagedict.keys():
87             return pagedict[pagename]
88         else:
89             return None
90
91     def pages_add_rv(self, pageid, rv):
92         pagefile = os.path.join(self.location, 'cache', 'pages', str(pageid))
93         fd = file(pagefile, 'w+')
94         pagedata_raw = fd.read()
95         if pagedata_raw == '':
96             pagedata = {}
97         else:
98             pagedata = json.loads(pagedata_raw)
99         rvid = int(rv['revid'])
100         pagedata[rvid] = {
101                 'user': rv['user'], 'timestamp': rv['timestamp']
102         }
103         if '*' in rv.keys():
104             pagedata[rvid]['content'] = rv['*']
105         fd.seek(0)
106         fd.write(json.dumps(pagedata))
107         fd.truncate()
108         fd.close()
109
110     def pages_get_rv_list(self, pageid):
111         pagefile = os.path.join(self.location, 'cache', 'pages',
112                                 str(pageid['id']))
113         fd = file(pagefile, 'r')
114         pagedata = json.loads(fd.read())
115         rvs = [int(x) for x in pagedata.keys()]
116         rvs.sort()
117         return rvs
118
119     def pages_get_rv(self, pageid, rvid):
120         pagefile = os.path.join(self.location, 'cache', 'pages',
121                                 str(pageid['id']))
122         fd = file(pagefile, 'r')
123         pagedata = json.loads(fd.read())
124         return pagedata[str(rvid)]

Benjamin Mako Hill || Want to submit a patch?