Coverage for src/lcdoc/mkdocs/lp/plugs/python/pyplugs/project_dependencies/__init__.py: 98.28%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

76 statements  

1import httpx lp|index.md

2from pip._internal.commands.show import search_packages_info lp|index.md

3 

4from lcdoc.mkdocs import markdown lp|index.md

5from lcdoc.mkdocs.lp.plugs import python lp|index.md

6from lcdoc.tools import app, os, project, write_file lp|index.md

7 

8config, page, Session = (python.config, python.page, python.Session) lp|index.md

9 

10TP = ''' 

11|Package | Description | Version | License 

12|- | - | - | - 

13%s 

14''' 

15TC = ''' 

16 

17## Run Dependencies 

18 

19_RD_ 

20 

21## Development Dependencies 

22 

23_DD_ 

24 

25## Indirect Dependencies 

26 

27_ID_ 

28 

29**Many thanks to all authors, for all these brilliant software packages!** 

30 

31 

32''' 

33 

34 

35def fetch_pypi(pkg, attrs): lp|index.md

36 

37 app.info('from pypi', pkg=pkg) lp|about/credits.md

38 p = httpx.get('https://pypi.python.org/pypi/%s/json' % pkg) lp|about/credits.md

39 p = p.json()['info'] lp|about/credits.md

40 p['home-page'] = p['home_page'] or p['project_url'] or p['package_url'] lp|about/credits.md

41 p = {_: p[_] for _ in attrs} lp|about/credits.md

42 return p lp|about/credits.md

43 

44 

45def get_credits_data() -> dict: lp|index.md

46 """ 

47 Return data used to generate the credits file. 

48 

49 Returns: 

50 Data required to render the credits template. 

51 """ 

52 project_dir = project.root() lp|about/credits.md

53 # metadata = toml.load(project_dir + "/pyproject.toml")["tool"]["poetry"] 

54 

55 lock_data = project.lock_data() lp|about/credits.md

56 project_name = project.name() lp|about/credits.md

57 direct_deps = {dep.lower() for dep in project.dependencies()} lp|about/credits.md

58 dev_deps = {dep.lower() for dep in project.dev_dependencies()} lp|about/credits.md

59 'python' in direct_deps and direct_deps.remove('python') lp|about/credits.md

60 indirect_deps = {pkg['name'].lower() for pkg in lock_data['package']} lp|about/credits.md

61 indirect_deps -= direct_deps lp|about/credits.md

62 indirect_deps -= dev_deps lp|about/credits.md

63 deps = sorted(direct_deps | dev_deps | indirect_deps) lp|about/credits.md

64 app.info( lp|about/credits.md

65 'Generating credits', deps=deps, indirect_deps=indirect_deps, 

66 ) 

67 

68 packages = {} lp|about/credits.md

69 attrs = ('name', 'home-page', 'license', 'version', 'summary') lp|about/credits.md

70 # pip changed from dict to instance with 21.2.0: 

71 g = lambda o, n: o[n] if isinstance(o, dict) else getattr(o, n.replace('-', '')) lp|about/credits.md

72 

73 for pkg in search_packages_info(deps): lp|about/credits.md

74 pkg = {_: g(pkg, _) for _ in attrs} lp|about/credits.md

75 app.level < 20 and app.debug('pkg', json=pkg) lp|about/credits.md

76 packages[pkg['name'].lower()] = pkg lp|about/credits.md

77 

78 for dep in deps: lp|about/credits.md

79 if dep not in packages: lp|about/credits.md

80 pkg = fetch_pypi(dep, attrs) lp|about/credits.md

81 packages[pkg['name'].lower()] = pkg lp|about/credits.md

82 else: 

83 app.debug('found', dep=dep) lp|about/credits.md

84 

85 for d in sorted(indirect_deps): lp|about/credits.md

86 if not d in packages: lp|about/credits.md

87 app.warning('Not found in packages', dep=d) lp|about/credits.md

88 continue lp|about/credits.md

89 packages[d]['for'] = [ 

90 packages[k['name']] for k in lock_data['package'] if d in k.get('deps', ()) 

91 ] 

92 

93 lnk = lambda p: '[`%(name)s`](%(home-page)s)' % p lp|about/credits.md

94 lico = lambda p: (p.get('license') or 'n.a.').replace('UNKNOWN', 'n.a.') lp|about/credits.md

95 

96 def lic(p): lp|about/credits.md

97 l = lico(p) lp|about/credits.md

98 if l.startswith('http'): 98 ↛ 99line 98 didn't jump to line 99, because the condition on line 98 was never truelp|about/credits.md

99 l = '[%s](%s)' % ((l + '///').split('/')[3], l) 

100 for k in 'license', 'License', 'version', 'Version': lp|about/credits.md

101 l = l.replace(k, '') lp|about/credits.md

102 return l lp|about/credits.md

103 

104 def smry(p): lp|about/credits.md

105 s = p['summary'] lp|about/credits.md

106 f = ' '.join([lnk(k) for k in p.get('for', ())]) lp|about/credits.md

107 return s + ' ' + f lp|about/credits.md

108 

109 def tbl(what, all=packages): lp|about/credits.md

110 ps = [packages[n] for n in what if n in packages] lp|about/credits.md

111 ps = [['', lnk(p), smry(p), p['version'], lic(p)] for p in ps] lp|about/credits.md

112 r = ['|'.join(l) for l in ps] lp|about/credits.md

113 return TP % '\n'.join(r) lp|about/credits.md

114 

115 t = TC lp|about/credits.md

116 t = t.replace('_ME_', '`%s`' % project_name) lp|about/credits.md

117 t = t.replace('_RD_', tbl(sorted(direct_deps))) lp|about/credits.md

118 t = t.replace('_DD_', tbl(sorted(dev_deps))) lp|about/credits.md

119 t = t.replace('_ID_', tbl(sorted(indirect_deps))) lp|about/credits.md

120 t = t.replace('_PN_', project_name) lp|about/credits.md

121 return t lp|about/credits.md

122 

123 

124def register(fmts): lp|index.md

125 fmts['project_dependencies'] = render_deps lp|index.md

126 

127 

128def render_deps(s, **kw): lp|index.md

129 project.root(config()) lp|about/credits.md

130 app.info('Regenerating dependencies') lp|about/credits.md

131 r = get_credits_data() lp|about/credits.md

132 return r lp|about/credits.md