Coverage for quibble/mediawiki/registry.py: 91%

38 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-08-07 07:21 +0000

1# https://www.mediawiki.org/wiki/Manual:Extension_registration 

2 

3import json 

4import os.path 

5 

6 

7def from_path(path): 

8 if not os.path.isdir(path): 

9 raise NotADirectoryError(path) 

10 # raise Exception('Not a directory: %s' % path) 

11 

12 ext_json = os.path.join(path, 'extension.json') 

13 skin_json = os.path.join(path, 'skin.json') 

14 

15 has_ext = os.path.exists(ext_json) 

16 has_skin = os.path.exists(skin_json) 

17 

18 if has_ext and has_skin: 

19 raise Exception('Found both extension.json and skin.json in %s' % path) 

20 elif not (has_ext or has_skin): 

21 return ExtensionRegistration() 

22 elif has_ext: 22 ↛ 24line 22 didn't jump to line 24 because the condition on line 22 was always true

23 return ExtensionRegistration(ext_json) 

24 elif has_skin: 

25 return ExtensionRegistration(skin_json) 

26 

27 

28def _read(json_file): 

29 with open(json_file) as f: 

30 return json.load(f) 

31 

32 

33def _parse(reg_data): 

34 """ 

35 Returns a `set` of requirements 

36 """ 

37 deps = set() 

38 if 'requires' not in reg_data: 

39 return deps 

40 

41 for kind in ['extensions', 'skins']: 

42 for name in reg_data['requires'].get(kind, {}).keys(): 

43 deps.add('mediawiki/%(kind)s/%(name)s' % locals()) 

44 return deps 

45 

46 

47class ExtensionRegistration: 

48 def __init__(self, json_file=''): 

49 self._raw_json = None 

50 self._requires = set() 

51 if not json_file: 

52 return 

53 self._raw_json = _read(json_file) 

54 self._requires = _parse(self._raw_json) 

55 

56 def getRequiredRepos(self): 

57 return self._requires