]> projects.mako.cc - wikipedia-api-cdsw/blob - mwclient/ex.py
db4006c25dd7e4076090e8955fe96a74c2d33ff7
[wikipedia-api-cdsw] / mwclient / ex.py
1 import client
2 import requests
3
4
5 def read_config(config_files, **predata):
6     cfg = {}
7     for config_file in config_files:
8         cfg.update(_read_config_file(
9             config_file, predata))
10     return cfg
11
12
13 def _read_config_file(_config_file, predata):
14     _file = open(_config_file)
15     exec _file in globals(), predata
16     _file.close()
17
18     for _k, _v in predata.iteritems():
19         if not _k.startswith('_'):
20             yield _k, _v
21     for _k, _v in locals().iteritems():
22         if not _k.startswith('_'):
23             yield _k, _v
24
25
26 class SiteList(object):
27
28     def __init__(self):
29         self.sites = {}
30
31     def __getitem__(self, key):
32         if key not in self.sites:
33             self.sites[key] = {}
34         return self.sites[key]
35
36     def __iter__(self):
37         return self.sites.itervalues()
38
39
40 class ConfiguredSite(client.Site):
41
42     def __init__(self, *config_files, **kwargs):
43         self.config = read_config(config_files, sites=SiteList())
44
45         if 'name' in kwargs:
46             self.config.update(self.config['sites'][kwargs['name']])
47
48         do_login = 'username' in self.config and 'password' in self.config
49
50         client.Site.__init__(self, host=self.config['host'],
51                              path=self.config['path'], ext=self.config.get('ext', '.php'),
52                              do_init=not do_login,
53                              retry_timeout=self.config.get('retry_timeout', 30),
54                              max_retries=self.config.get('max_retries', -1))
55
56         if do_login:
57             self.login(self.config['username'],
58                        self.config['password'])
59
60
61 class ConfiguredPool(list):
62
63     def __init__(self, *config_files):
64         self.config = read_config(config_files, sites=SiteList())
65         self.pool = requests.Session()
66
67         config = dict([(k, v) for k, v in self.config.iteritems()
68                        if k != 'sites'])
69
70         for site in self.config['sites']:
71             cfg = config.copy()
72             cfg.update(site)
73             site.update(cfg)
74
75             do_login = 'username' in site and 'password' in site
76
77             self.append(client.Site(host=site['host'],
78                                     path=site['path'], ext=site.get('ext', '.php'),
79                                     pool=self.pool, do_init=not do_login,
80                                     retry_timeout=site.get('retry_timeout', 30),
81                                     max_retries=site.get('max_retries', -1)))
82             if do_login:
83                 self[-1].login(site['username'], site['password'])
84             self[-1].config = site

Benjamin Mako Hill || Want to submit a patch?