2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2009 Ian Weller <ian@ianweller.org>
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.
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.
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/>.
25 class Metadir(object):
27 self.me = os.path.basename(sys.argv[0])
30 if '.mw' in os.listdir(root):
33 (head, tail) = os.path.split(root)
35 self.root = os.getcwd()
38 self.location = os.path.join(self.root, '.mw')
39 self.config_loc = os.path.join(self.location, 'config')
40 if os.path.isdir(self.location) and \
41 os.path.isfile(self.config_loc):
42 self.config = ConfigParser.RawConfigParser()
43 self.config.read(self.config_loc)
47 def create(self, api_url):
48 # create the directory
49 if os.path.isdir(self.location):
50 print '%s: you are already in a mw repo' % self.me
53 os.mkdir(self.location, 0755)
55 self.config = ConfigParser.RawConfigParser()
56 self.config.add_section('remote')
57 self.config.set('remote', 'api_url', api_url)
58 with open(self.config_loc, 'wb') as config_file:
59 self.config.write(config_file)
61 os.mkdir(os.path.join(self.location, 'cache'))
63 fd = file(os.path.join(self.location, 'cache', 'page'), 'w')
64 fd.write(json.dumps({}))
66 fd = file(os.path.join(self.location, 'cache', 'rv'), 'w')
67 fd.write(json.dumps({}))
69 def add_page_info(self, pageid, pagename, rvids):
70 lulz = file(os.path.join(self.location, 'cache', 'page'), 'r')
71 conf = json.loads(lulz.read())
72 conf[pageid] = {'name': pagename, 'rv': rvids}
73 fd = file(os.path.join(self.location, 'cache', 'page'), 'w')
74 fd.write(json.dumps(conf))
76 def add_rv_info(self, rv):
77 lulz = file(os.path.join(self.location, 'cache', 'rv'), 'r')
78 conf = json.loads(lulz.read())
79 rvid = int(rv['revid'])
81 'user': rv['user'], 'timestamp': rv['timestamp'],
84 conf[rvid]['minor'] = 'minor' in rv
86 conf[rvid]['comment'] = rv['comment']
88 conf[rvid]['comment'] = None
89 fd = file(os.path.join(self.location, 'cache', 'rv'), 'w')
90 fd.write(json.dumps(conf))