[MediaWiki-commits] [Gerrit] [FEAT] ParamInfo: Support any submodules - change (pywikibot/core)

2015-07-15 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: [FEAT] ParamInfo: Support any submodules
..


[FEAT] ParamInfo: Support any submodules

This automatically loads the submodules of fetched param info. This allows
ParamInfo to return all modules available and the submodules for a specific
module instead of just specifically query's and main's modules. With that it is
also possible to get the attributes in general for all modules, which is
necessary to get the prefixes. It deprecates some methods because they were
using the name and not the path.

It also enables users to determine to which module any of the parameters belong
which is currently only possible for submodules of the main and query modules.

Change-Id: I7f5438d8d98649405c9510a610743e4ce4fa7c82
---
M pywikibot/data/api.py
M tests/api_tests.py
M tests/dry_api_tests.py
3 files changed, 316 insertions(+), 47 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index c6cbf67..f54fa6b 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -145,6 +145,8 @@
 Partially supports MW 1.11, using data extracted from API 'help'.
 MW 1.10 not supported as module prefixes are not extracted from API 'help'.
 
+It does not support the format modules.
+
 TODO: Rewrite help parser to support earlier releases.
 
 TODO: establish a data structure in the class which prefills
@@ -185,10 +187,11 @@
 
 # Cached data.
 self._prefixes = {}
+self._prefix_map = {}
 self._with_limits = None
 
-self._action_modules = None
-self._query_modules = []  # filled in _init()
+self._action_modules = frozenset()  # top level modules
+self._modules = {}  # filled in _init() (and enlarged in fetch)
 self._limit = None
 
 self.preloaded_modules = self.init_modules
@@ -199,6 +202,24 @@
 self.modules_only_mode = modules_only_mode
 if self.modules_only_mode:
 self.paraminfo_keys = frozenset(['modules'])
+
+def _add_submodules(self, name, modules):
+Add the modules to the internal cache or check if equal.
+# The current implementation here doesn't support submodules inside of
+# submodules, because that would require to fetch all modules when only
+# the names of them were requested
+assert '+' not in name
+modules = frozenset(modules)
+if name == 'main':
+# The main module behaves differently as it has no prefix
+if self._action_modules:
+assert modules == self._action_modules
+else:
+self._action_modules = modules
+elif name in self._modules:
+assert modules == self._modules[name]
+else:
+self._modules[name] = modules
 
 def _init(self):
 _mw_ver = MediaWikiVersion(self.site.version())
@@ -228,7 +249,7 @@
 assert(main_modules_param)
 assert('type' in main_modules_param)
 assert(isinstance(main_modules_param['type'], list))
-self._action_modules = frozenset(main_modules_param['type'])
+assert self._action_modules == set(main_modules_param['type'])
 
 # While deprecated with warning in 1.25, paraminfo param 'querymodules'
 # provides a list of all query modules. This will likely be removed
@@ -242,36 +263,14 @@
 if query_modules_param and 'type' in query_modules_param:
 # 1.19+ 'type' is the list of modules; on 1.18, it is 'string'
 if isinstance(query_modules_param['type'], list):
-self._query_modules = frozenset(query_modules_param['type'])
+self._add_submodules('query', query_modules_param['type'])
 
-if not self._query_modules:
-if 'query' not in self._paraminfo:
-self.fetch(set(['query']), _init=True)
+if 'query' not in self._modules:
+assert 'query' not in self._paraminfo
+self.fetch(set(['query']), _init=True)
+assert 'query' in self._modules
 
-meta_param = self.parameter('query', 'meta')
-prop_param = self.parameter('query', 'prop')
-list_param = self.parameter('query', 'list')
-generator_param = self.parameter('query', 'generator')
-
-assert(meta_param)
-assert(prop_param)
-assert(list_param)
-assert(generator_param)
-assert('type' in meta_param)
-assert('type' in prop_param)
-assert('type' in list_param)
-assert('type' in generator_param)
-assert(isinstance(meta_param['type'], list))
-assert(isinstance(prop_param['type'], list))
-assert(isinstance(list_param['type'], list))
-

[MediaWiki-commits] [Gerrit] [FEAT] ParamInfo: Support any submodules - change (pywikibot/core)

2015-03-20 Thread XZise (Code Review)
XZise has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/198247

Change subject: [FEAT] ParamInfo: Support any submodules
..

[FEAT] ParamInfo: Support any submodules

This automatically loads the submodules of fetched param info and
supports for example 'flow+reply'. It deprecates some methods because
they were using the name and not the path.

Change-Id: I7f5438d8d98649405c9510a610743e4ce4fa7c82
---
M pywikibot/data/api.py
M tests/api_tests.py
M tests/dry_api_tests.py
3 files changed, 144 insertions(+), 54 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/47/198247/1

diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index 9a9c030..bff730a 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -179,8 +179,7 @@
 self._prefixes = {}
 self._with_limits = None
 
-self._action_modules = None
-self._query_modules = []  # filled in _init()
+self._modules = {}  # filled in _init() (and enlarged in fetch)
 self._limit = None
 
 self.preloaded_modules = self.init_modules
@@ -191,6 +190,18 @@
 self.modules_only_mode = modules_only_mode
 if self.modules_only_mode:
 self.paraminfo_keys = frozenset(['modules'])
+
+def _add_submodules(self, name, modules):
+Add the modules to the internal cache or check if equal.
+# The current implemention doesn't support submodules inside of
+# submodules, because that would require to fetch all modules when only
+# the names of them were requested
+assert('+' not in name)
+modules = frozenset(modules)
+if name in self._modules:
+assert(modules == self._modules[name])
+else:
+self._modules[name] = modules
 
 def _init(self):
 _mw_ver = MediaWikiVersion(self.site.version())
@@ -216,7 +227,7 @@
 assert(main_modules_param)
 assert('type' in main_modules_param)
 assert(isinstance(main_modules_param['type'], list))
-self._action_modules = frozenset(main_modules_param['type'])
+self._add_submodules('', main_modules_param['type'])
 
 # While deprecated with warning in 1.25, paraminfo param 'querymodules'
 # provides a list of all query modules. This will likely be removed
@@ -230,36 +241,14 @@
 if query_modules_param and 'type' in query_modules_param:
 # 1.19+ 'type' is the list of modules; on 1.18, it is 'string'
 if isinstance(query_modules_param['type'], list):
-self._query_modules = frozenset(query_modules_param['type'])
+self._add_submodules('query', query_modules_param['type'])
 
-if not self._query_modules:
-if 'query' not in self._paraminfo:
-self.fetch(set(['query']), _init=True)
+if 'query' not in self._modules:
+assert('query' not in self._paraminfo)
+self.fetch(set(['query']), _init=True)
+assert('query' in self._modules)
 
-meta_param = self.parameter('query', 'meta')
-prop_param = self.parameter('query', 'prop')
-list_param = self.parameter('query', 'list')
-generator_param = self.parameter('query', 'generator')
-
-assert(meta_param)
-assert(prop_param)
-assert(list_param)
-assert(generator_param)
-assert('type' in meta_param)
-assert('type' in prop_param)
-assert('type' in list_param)
-assert('type' in generator_param)
-assert(isinstance(meta_param['type'], list))
-assert(isinstance(prop_param['type'], list))
-assert(isinstance(list_param['type'], list))
-assert(isinstance(generator_param['type'], list))
-
-self._query_modules = frozenset(
-meta_param['type'] + prop_param['type'] + list_param['type'] +
-generator_param['type']
-)
-
-_reused_module_names = self._action_modules  self._query_modules
+_reused_module_names = self._modules['']  self._modules['query']
 
 # The only name clash in core between actions and query submodules is
 # action=tokens and actions=querymeta=tokens, and this will warn if
@@ -305,7 +294,7 @@
 if not modules:
 return
 
-assert(self._query_modules or _init)
+assert('query' in self._modules or _init)
 
 # This can be further optimised, by grouping them in more stable
 # subsets, which are unlikely to change. i.e. first request core
@@ -334,11 +323,15 @@
 if self.modules_only_mode:
 params['modules'] = module_batch
 else:
-params['modules'] = [mod for mod in module_batch
- if not mod.startswith('query+') and
-