Make subcommands finally work properly
[mw] / src / mw / metadir.py
1 ###
2 # mw - VCS-like nonsense for MediaWiki websites
3 # Copyright (C) 2009  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, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 ###
19
20 import ConfigParser
21 import json
22 import os
23 import sys
24 import time
25
26 class Metadir(object):
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, tail) = os.path.split(root)
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         try:
51             os.mkdir(self.location, 0755)
52         except OSError, e:
53             print '%s: you are already in a mw repo' % self.me
54             sys.exit(1)
55         # create config
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)
61         # create cache
62         os.mkdir(os.path.join(self.location, 'cache'))
63         # create cache/page
64         fd = file(os.path.join(self.location, 'cache', 'page'), 'w')
65         fd.write(json.dumps({}))
66         # create cache/rv
67         fd = file(os.path.join(self.location, 'cache', 'rv'), 'w')
68         fd.write(json.dumps({}))
69
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))
76
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'])
81         conf[rvid] = {
82                 'user': rv['user'], 'timestamp': rv['timestamp'],
83                 'content': rv['*']
84         }
85         conf[rvid]['minor'] = 'minor' in rv
86         if 'comment' in rv:
87             conf[rvid]['comment'] = rv['comment']
88         else:
89             conf[rvid]['comment'] = None
90         fd = file(os.path.join(self.location, 'cache', 'rv'), 'w')
91         fd.write(json.dumps(conf))

Benjamin Mako Hill || Want to submit a patch?