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, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 class Metadir(object):
28 self.me = os.path.basename(sys.argv[0])
31 if '.mw' in os.listdir(root):
34 (head, tail) = os.path.split(root)
36 self.root = os.getcwd()
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)
48 def create(self, api_url):
49 # create the directory
51 os.mkdir(self.location, 0755)
53 print '%s: you are already in a mw repo' % self.me
56 self.config = ConfigParser.RawConfigParser()
57 self.config.add_section('remote')
58 self.config.set('remote', 'api_url', api_url)
59 with open(self.config_loc, 'wb') as config_file:
60 self.config.write(config_file)
62 os.mkdir(os.path.join(self.location, 'cache'))
64 fd = file(os.path.join(self.location, 'cache', 'page'), 'w')
65 fd.write(json.dumps({}))
67 fd = file(os.path.join(self.location, 'cache', 'rv'), 'w')
68 fd.write(json.dumps({}))
70 def add_page_info(self, pageid, pagename, rvids):
71 lulz = file(os.path.join(self.location, 'cache', 'page'), 'r')
72 conf = json.loads(lulz.read())
73 conf[pageid] = {'name': pagename, 'rv': rvids}
74 fd = file(os.path.join(self.location, 'cache', 'page'), 'w')
75 fd.write(json.dumps(conf))
77 def add_rv_info(self, rv):
78 lulz = file(os.path.join(self.location, 'cache', 'rv'), 'r')
79 conf = json.loads(lulz.read())
80 rvid = int(rv['revid'])
82 'user': rv['user'], 'timestamp': rv['timestamp'],
85 conf[rvid]['minor'] = 'minor' in rv
87 conf[rvid]['comment'] = rv['comment']
89 conf[rvid]['comment'] = None
90 fd = file(os.path.join(self.location, 'cache', 'rv'), 'w')
91 fd.write(json.dumps(conf))