Skip to content

Commit 61350c1

Browse files
authored
Merge pull request #64 from QuLogic/pathify
Use pathlib everywhere.
2 parents 4795fb7 + 767158d commit 61350c1

File tree

3 files changed

+37
-37
lines changed

3 files changed

+37
-37
lines changed

python/build.py

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
#!/usr/bin/env python
2-
import os
2+
3+
from pathlib import Path
34
import pprint
45

56
from jinja2 import Template
67
from yaml import safe_load
78
from markdown import markdown
8-
import glob
99
import requests
1010

1111
# concatenate yml files...
1212

13-
here = os.path.abspath(os.path.dirname(__file__))
14-
15-
try:
16-
os.remove(os.path.join(here, '../packages/all.yml'))
17-
except:
18-
pass
19-
packages = glob.glob(os.path.join(here, '../packages/*'))
13+
here = Path(__file__).parent.resolve()
2014

15+
all_path = here.parent / 'packages/all.yml'
16+
all_path.unlink(missing_ok=True)
17+
packages = (here.parent / 'packages').glob('*')
2118

2219

2320
print("Opening section names file")
24-
with open(os.path.join(here, '../section_names.yml')) as f:
21+
with (here.parent / 'section_names.yml').open() as f:
2522
section_names = safe_load(f)
2623

2724
section_names = section_names['section_names']
@@ -30,7 +27,7 @@
3027
packs = dict()
3128
# divide the yml files into sections based on teh section tag...
3229
for package in packages:
33-
with open(package, 'r') as fin:
30+
with package.open('r') as fin:
3431
pack = safe_load(fin)
3532
if 'section' not in pack:
3633
pack['section'] = 'miscellaneous'
@@ -41,8 +38,7 @@
4138

4239
pprint.pprint(packs)
4340

44-
45-
with open(os.path.join(here, '../packages/all.yml'), 'w') as out:
41+
with all_path.open('w') as out:
4642
for secname in sorted(packs.keys()):
4743
packs_sec = sorted(packs[secname], key= lambda i: i['repo'].split('/')[1])
4844

@@ -58,7 +54,7 @@
5854

5955

6056
print("Opening config file")
61-
with open(os.path.join(here, '../packages/all.yml')) as f:
57+
with all_path.open() as f:
6258
config = safe_load(f)
6359
pprint.pprint(config)
6460
print()
@@ -130,14 +126,16 @@
130126
else:
131127
package['site_protocol'], package['site'] = package['site'].rstrip('/').split('://')
132128

133-
template = Template(open(os.path.join(here, 'template.rst'), 'r').read())
129+
template = Template((here / 'template.rst').read_text())
134130

135131
config = sorted(config, key = lambda i: i['name'])
136132

137133

138-
with open(os.path.join(here, '../docs/source/packages.rst'), 'w') as f:
139-
f.write("Third-party and user-contributed packages\n")
140-
f.write("=========================================\n\n")
141-
f.write(".. include:: intro.rst\n\n")
142-
# f.write(".. include:: html\n\n")
143-
f.write(template.render(config=config))
134+
(here.parent / 'docs/source/packages.rst').write_text(f"""\
135+
Third-party and user-contributed packages
136+
=========================================
137+
138+
.. include:: intro.rst
139+
140+
{template.render(config=config)}
141+
""")

python/build_cache.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/usr/bin/env python
22

33
import os
4+
from pathlib import Path
45
from yaml import safe_load
56
import requests
67

7-
here = os.path.abspath(os.path.dirname(__file__))
8-
cache_path = os.path.join(here, '..', 'doc', '_static', 'cache')
8+
here = Path(__file__).parent.resolve()
9+
cache_path = here.parent / 'doc/_static/cache'
910
print(cache_path)
1011
badge = os.getenv('BADGE')
1112

@@ -20,10 +21,9 @@
2021
raise ValueError((f'{badge} not in {", ".join(cache.keys())}, use env '
2122
'var BADGE to set.'))
2223

23-
if not os.path.exists(cache_path):
24-
os.mkdir(cache_path)
24+
cache_path.mkdir(exist_ok=True)
2525

26-
with open(os.path.join(here, 'tools.yml')) as f:
26+
with (here / 'tools.yml').open() as f:
2727
config = safe_load(f)
2828

2929
for section in config:
@@ -39,5 +39,5 @@
3939
print(f" * package: {package.get('pypi_name', '')}")
4040
rendered_url = url.format(repo=package['repo'], pypi_name=package['pypi_name'])
4141
r = requests.get(rendered_url)
42-
with open(os.path.join(cache_path, f"{package['name']}_{badge}_badge.svg"), 'wb') as f:
43-
f.write(r.content)
42+
badge = cache_path / f"{package['name']}_{badge}_badge.svg"
43+
badge.write_bytes(r.content)

python/conda_downloads.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,19 @@
44
from the previous month.
55
"""
66

7+
import datetime
78
import os
9+
from pathlib import Path
810
from yaml import safe_load
9-
import requests
10-
import datetime
11+
1112
import intake
13+
import requests
1214
import colorcet as cc
1315
import numpy as np
1416

1517

16-
here = os.path.abspath(os.path.dirname(__file__))
17-
cache_path = os.path.join(here, '..', 'doc', '_static', 'cache')
18+
here = Path(__file__).parent.resolve()
19+
cache_path = here.parent / 'doc/_static/cache'
1820
cat = intake.open_catalog('https://raw.githubusercontent.com/ContinuumIO/anaconda-package-data/master/catalog/anaconda_package_data.yaml')
1921

2022
colors = cc.palette_n.rainbow[-20:80:-1]
@@ -34,8 +36,8 @@
3436
columns=['pkg_name', 'counts']).to_dask()
3537
per_package_downloads = monthly.groupby('pkg_name').sum().compute()
3638

37-
if not os.path.exists(cache_path):
38-
os.mkdir(cache_path)
39+
cache_path.mkdir(exist_ok=True)
40+
3941

4042
def get_conda_badge(conda_package):
4143
conda_package = conda_package.lower()
@@ -61,7 +63,7 @@ def get_conda_badge(conda_package):
6163

6264
return f"https://img.shields.io/badge/conda-{downloads}/month-{color}.svg"
6365

64-
with open(os.path.join(here, 'tools.yml')) as f:
66+
with (here / 'tools.yml').open() as f:
6567
config = safe_load(f)
6668

6769
for section in config:
@@ -75,5 +77,5 @@ def get_conda_badge(conda_package):
7577
url = get_conda_badge(package.get('conda_package', package['name']))
7678
rendered_url = url
7779
r = requests.get(rendered_url)
78-
with open(os.path.join(cache_path, f"{package['name']}_conda_downloads_badge.svg"), 'wb') as f:
79-
f.write(r.content)
80+
badge = (cache_path / f"{package['name']}_conda_downloads_badge.svg")
81+
badge.write_bytes(r.content)

0 commit comments

Comments
 (0)