Skip to content

Commit d3aca23

Browse files
committed
enabled library usage
moved things around a bit to enable library usage
1 parent b115c53 commit d3aca23

File tree

5 files changed

+44
-31
lines changed

5 files changed

+44
-31
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ conda install -c ilastik-forge -c conda-forge conda-3rdparty
1414

1515
## Usages
1616

17-
### Basic
17+
### Command line usage:
1818

1919
```
2020
# generate text file with all licenses, sorted by package name
@@ -24,7 +24,7 @@ conda 3rdparty -n <environment_name> > 3rdparty.txt
2424
conda 3rdparty -n <environment_name> --check
2525
```
2626

27-
### supply missing licenses externally
27+
#### supply missing licenses externally
2828

2929
```
3030
conda 3rdparty -n <environment_name> --fallback-file <path-to-fallback.json>
@@ -33,6 +33,16 @@ conda 3rdparty -n <environment_name> --fallback-file <path-to-fallback.json>
3333
# dict["<package_name>"]["license_file"] = ["list_of_license", "files_relative_to_the_json_file"]
3434
```
3535

36+
### library usage
37+
38+
```python
39+
from conda3rdparty import render_license_info
40+
from pathlib import Path
41+
42+
license_texts = render_license_info(env_name="myenvname")
43+
# also takes `template_file: Path` and `fallback_file: Path` kwargs
44+
```
45+
3646

3747
### jinja2 template support
3848

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="conda-3rdparty",
5-
version="0.0.2dev0",
5+
version="0.0.3dev0",
66
author="ilastik-team",
77
license="MIT",
88
license_files=("LICENSE",),

src/conda3rdparty/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
__version__ = "0.0.2dev0"
1+
from .common import render_license_info
2+
3+
__version__ = "0.0.3dev0"

src/conda3rdparty/cli.py

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import json
2-
import pathlib
31
import sys
42
from argparse import ArgumentParser
5-
from subprocess import check_output
6-
from typing import Iterable, Mapping, Union
3+
from pathlib import Path
74

85
from conda3rdparty import __version__
96

10-
from .common import MISSING, CondaEnv, base_license_renderer
7+
from .common import MISSING, CondaEnv, load_fallback, render_license_info
118

129

1310
class CondaNotFound(Exception):
@@ -31,19 +28,11 @@ def parse_args():
3128
return args
3229

3330

34-
def load_fallback(fallback_path: pathlib.Path) -> dict:
35-
assert fallback_path.exists()
36-
fallback = json.loads(fallback_path.read_text())
37-
for pkg in fallback:
38-
fallback[pkg]["license_file"] = [
39-
fallback_path.parent / license_file for license_file in fallback[pkg]["license_file"]
40-
]
41-
return fallback
42-
43-
44-
def make_check(env: CondaEnv, fallback_file) -> int:
31+
def make_check(env_name: str, fallback_file: Path) -> int:
32+
env = CondaEnv(env_name)
33+
fallback_info = load_fallback(fallback_file) if fallback_file else None
4534
summary = []
46-
for info in env.license_infos(fallback_file):
35+
for info in env.license_infos(fallback_info):
4736
tmp = {"name": info["name"]}
4837
tmp.update(info["3rd_party_license_info"])
4938
tmp["ok"] = not any(x == MISSING for x in tmp["license_texts"])
@@ -60,19 +49,14 @@ def make_check(env: CondaEnv, fallback_file) -> int:
6049

6150
def main():
6251
args = parse_args()
52+
template = Path(args.template) if args.template else None
6353

64-
env = CondaEnv(args.name)
65-
66-
fallback = None
67-
if args.fallback_file:
68-
fallback = load_fallback(pathlib.Path(args.fallback_file))
54+
fallback = Path(args.fallback_file) if args.fallback_file else None
6955

7056
if args.check:
71-
return make_check(env, fallback)
72-
73-
template = pathlib.Path(args.template) if args.template else None
57+
return make_check(args.name, fallback)
7458

75-
print(base_license_renderer(env.license_infos(fallback), template_file=template))
59+
print(render_license_info(args.name, template_file=template, fallback_file=fallback))
7660

7761

7862
if __name__ == "__main__":

src/conda3rdparty/common.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ def ensure_list(x):
6565
return package_info
6666

6767

68+
def load_fallback(fallback_path: Path) -> dict:
69+
assert fallback_path.exists()
70+
fallback = json.loads(fallback_path.read_text())
71+
for pkg in fallback:
72+
fallback[pkg]["license_file"] = [
73+
fallback_path.parent / license_file for license_file in fallback[pkg]["license_file"]
74+
]
75+
return fallback
76+
77+
6878
class CondaEnv:
6979
def __init__(self, env_name: str):
7080
self._name = env_name
@@ -110,9 +120,16 @@ def license_infos(self, fallback_info=None) -> List[Dict[str, Any]]:
110120
"""
111121

112122

113-
def base_license_renderer(license_infos: List[Dict[str, Any]], template_file: Path = None):
123+
def base_license_renderer(license_infos: List[Dict[str, Any]], template_file: Path = None) -> str:
114124
if not template_file:
115125
template = Template(_base_template)
116126
else:
117127
template = Template(template_file.read_text())
118128
return template.render(license_infos=license_infos)
129+
130+
131+
def render_license_info(env_name: str, template_file: Path = None, fallback_file: Path = None) -> str:
132+
env = CondaEnv(env_name)
133+
fallback_info = load_fallback(fallback_file) if fallback_file else None
134+
license_infos = env.license_infos(fallback_info=fallback_info)
135+
return base_license_renderer(license_infos, template_file)

0 commit comments

Comments
 (0)