-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.py
412 lines (310 loc) · 10.6 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""
This script is used to manage the [**Who's Who in Astrochemistry**][whoswho]
database, a contact list of astrochemists from around the globe. If you are
seeing this help message, you have probably cloned the original repository,
or an original fork of it, on your local machine. You have installed all the
dependencies (probably because you saw a `requirements.txt` file) and you
figured out how to display this help message. Whether you were just ideally
curious, or you were thinking of contributing, you will find all the answers
you desire here.
First of all, what does this script do? Well, if you have this repository on
your system, you probably rummaged through the folders and saw a lot of YAML
files. These files power the entire Who's Who website, thanks to the awesome
[**Lowdefy**][lowdefy] framework. I use this script to build those YAML files
from a bunch of [**Mako**][mako] templates, locally serve the Who's Who app
(to see if everything is working as intended), and update my local copy of the
database that ultimately makes it into the [**Github repository**][repository].
This script also calculates a few statistics about the database, such as the
total number of astrochemists, how many can be contacted (via their email or
Twitter), and how many of them are tweeting.
We use the [**invoke**][invoke] package to power the entire script. **invoke**
is a framework for running use-defined *tasks*. These tasks can be anything:
Python functions, shell commands, etc. In this simplest scenario (which is what
we user here), all the user needs to do is define the tasks in a `tasks.py` file,
and then invoke any task using the `invoke` CLI. The settings for the `invoke`
CLI are customized through the `invoke.yaml` file.
To see how the site looks, just type:
```bash
invoke serve
```
This will start a local server and serve the Who's Who web application. As you
carry out your changes, you will be able to see them show up instantly! To make
a change, it is recommended that you change the templates (in the `templates`
directory) rather than the YAML pages themselves (which you will find in the
`pages` directory), because otherwise all your changes will vanish anytime you
recompile your changes with:
```bash
invoke compile
```
Run the above command everytime you wish to see your changes show up in the YAML
files andf in the web application. You can check out some stats about the database
by typing:
```bash
invoke statistics
```
If you wish to update your copy of the database, just run:
```bash
invoke update
```
[whoswho]: {main}
[issues]: {issues}
[repository]: {repo}
[discussions]: {discussions}
[lowdefy]: https://lowdefy.com
[invoke]: http://www.pyinvoke.org
[mako]: https://www.makotemplates.org
"""
import attr
import yaml
import arrow # type: ignore
import pandas as pd # type: ignore
from invoke import task # type: ignore
from pathlib import Path
from textwrap import dedent
from rich.panel import Panel
from rich.table import Table
from typing import List, Union
from rich.markdown import Markdown
from mako.lookup import TemplateLookup # type: ignore
from rich.console import Console, Group
console = Console()
@attr.s(auto_attribs=True)
class Meta(object):
name: str
email: str
author: str
license: str
version: str
description: str
license_file: Union[str, Path]
long_description: Union[str, Path]
@attr.s(auto_attribs=True)
class Dirs(object):
pages: Path
public: Path
templates: Path
@attr.s(auto_attribs=True)
class Urls(object):
main: str
repo: str
data: str
issues: str
discussions: str
@attr.s(auto_attribs=True)
class Settings(object):
pgctx: str
pages: List
gutter: str
pgalign: str
mnalign: str
ttalign: str
txalign: str
bgcolor: str
fgcolor: str
bdfont: str
hdfont: str
ttfont: str
with open("config.yaml", "r") as f:
config = yaml.load(f.read(), Loader=yaml.SafeLoader)
meta = Meta(**config["meta"])
urls = Urls(
**{
name: value.replace(" ", "").strip()
for (
name,
value,
) in config["urls"].items()
}
)
dirs = Dirs(
**{
name: (Path.cwd() / value).resolve()
for (
name,
value,
) in config["dirs"].items()
}
)
settings = Settings(**config["settings"])
@attr.s(repr=False, auto_attribs=True)
class Stats(object):
""""""
data: pd.DataFrame
onfile: Union[str, Path]
def __rich__(self):
grid = Table.grid(expand=False)
grid.add_column(justify="left")
grid.add_column(justify="right")
grid.add_row(f"[b]Total count:[/]", f"[b]{self.count}[/]")
grid.add_row(f"[b]Last updated:[/]", f"[b]{self.updated}[/]")
grid.add_row(f"[b]Astrochemists tweeting:[/]", f"[b]{self.tweeters}[/]")
grid.add_row(f"[b]Contactable astrochemists:[/]", f"[b]{self.contactable}[/]")
covs = Table.grid(expand=True)
covs.add_column(justify="left")
covs.add_column(justify="right")
for key, val in self.coverages.items():
covs.add_row(f"{key} Coverage:", f"[b]{val}[/] %")
grid.add_row("[b]Data Coverage:[/]", Panel(covs))
grid.add_row("[b]Mean Coverage:[/]", f"[b]{self.mean_coverage}[/] %")
notes = """
* An astrochemist is considered **contactable** if they have an email they can
be reached at or if they are on Twitter.
* The *Twitter* column is not taken into account while calculating the mean
coverage, because it is an *optional* field.
"""
return Panel(
Group(
Panel(
grid,
title="Statistics",
title_align="left",
padding=2,
expand=False,
),
"\n",
Panel(
Markdown(dedent(notes)),
title="Notes",
title_align="left",
padding=2,
expand=False,
),
),
expand=True,
)
@classmethod
def load(cls):
onfile = dirs.public / "whoswho.csv"
data = pd.read_csv(onfile, index_col=0)
return cls(data=data, onfile=onfile)
@property
def updated(self):
fmt = "dddd DD MMMM, YYYY hh:mm:ss a ZZZ"
return arrow.get(self.onfile.stat().st_mtime).format(fmt)
@property
def columns(self):
return list(self.data.columns)
@property
def count(self):
return len(self.data.index)
@property
def contactable(self):
rows = self.data.iterrows()
flag = lambda r: any([pd.notna(r.Email), pd.notna(r.Twitter)])
return len([row for _, row in rows if flag(row)])
@property
def tweeters(self):
return self.data.Twitter.count()
@property
def coverages(self):
coverage = lambda col: round((col.count() / self.count) * 100, 2)
return pd.Series({col: coverage(self.data[col]) for col in self.columns})
@property
def mean_coverage(self):
excludes = ["Twitter"]
covsum = sum(cov for col, cov in self.coverages.items() if col not in excludes)
covmean = covsum / (100 * (self.coverages.count() - len(excludes)))
covmean = round(covmean * 100, 2)
return covmean
stats = Stats.load()
@task
def help(c):
""""""
with console.pager(styles=True):
console.print(
Panel(
Markdown(__doc__.format(**attr.asdict(urls)), justify="full"),
padding=2,
expand=True,
title=f"[b]{meta.name}[/]: [i]{meta.description}[/]",
title_align="left",
)
)
@task
def version(c):
""""""
console.print(f"[u]Version[/]: [b]{meta.version}[/]")
@task
def update(c):
""""""
with console.status(status="Updating database..."):
path = dirs.public / "whoswho.json"
df = pd.read_csv(urls.data)
df.to_csv(path.with_suffix(".csv"))
df.to_json(path, indent=4, orient="records")
@task(update)
def compile(c):
""""""
with console.status(status="Compiling templates..."):
lookup = TemplateLookup(directories=[dirs.templates])
for page in settings.pages:
with open(dirs.pages / f"{page}.yaml", "w+") as f:
f.write(
lookup.get_template(f"pages/{page}.mako").render(
pgid=page,
meta=meta,
dirs=dirs,
urls=urls,
stats=stats,
settings=settings,
)
)
@task(update)
def statistics(c):
""""""
with console.pager(styles=True):
console.print(stats)
@task(compile)
def serve(c):
""""""
c.run("npx lowdefy@latest dev")
@task
def clean(c):
""""""
c.run("rm -rf pages/*")
c.run("rm -rf .lowdefy")
c.run("rm -rf .mypy_cache")
@task
def readme(c):
""""""
readme = dedent(
"""
<div align="center">
# {name}
<br/><br/>
<img
alt="Header Image"
src="https://raw.githubusercontent.com/astrogewgaw/logos/main/rasters/whoswho.png"
/>
<br/><br/>
![License][license]
[![Gitmoji][gitmoji-badge]][gitmoji]
![Last Updated][updated]
![Count][count]
![Contactable][contactable]
![Tweeters][tweeters]
</div>
<br/>
<div align="justify">
{doc}
[gitmoji]: https://gitmoji.dev
[license]: https://img.shields.io/github/license/astrogewgaw/whoswho?style=for-the-badge
[count]: https://img.shields.io/badge/Astrochemists-{count}-blueviolet?style=for-the-badge
[updated]: https://img.shields.io/badge/Last%20Updated-{updated}-purple?style=for-the-badge
[gitmoji-badge]: https://img.shields.io/badge/gitmoji-%20😜%20😍-FFDD67.svg?style=for-the-badge
[tweeters]: https://img.shields.io/badge/Tweeters-{tweeters}-blue?style=for-the-badge&logo=twitter
[contactable]: https://img.shields.io/badge/Contactable-{contactable}-darkgreen?style=for-the-badge&logo=gmail
</div>
"""
)
with open("README.md", "w+") as f:
f.write(
readme.format(
name=meta.name,
doc=__doc__.strip(),
count=stats.count,
tweeters=stats.tweeters,
contactable=stats.contactable,
updated=stats.updated.replace(" ", "%20"),
)
)