-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathtasks.py
134 lines (109 loc) · 3.81 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
from pathlib import Path
from invoke import task
@task
def export(c, dest="data"):
"""Export data to a few formats files."""
tables = {
"elements",
"groups",
"ionicradii",
"ionizationenergies",
"isotopedecaymodes",
"isotopes",
"oxidationstates",
"phasetransitions",
"propertymetadata",
"scattering_factors",
"screeningconstants",
"series",
}
formats = [
"csv",
"html",
"json",
"markdown",
]
root = Path(f"{dest}")
root.mkdir(exist_ok=True)
for fmt in formats:
path = root.joinpath(fmt)
path.mkdir(exist_ok=True)
for table in tables:
print(f"Exporting {table} to {fmt} ... ", end="")
c.run(
f"sqlite3 -{fmt} mendeleev/elements.db 'SELECT * FROM {table};' > {path}/{table}.{fmt}",
echo=True,
)
print("done")
# SQL dump
print("Creating SQL files with the data ... ", end="")
path = root.joinpath("sql")
path.mkdir(exist_ok=True)
c.run(
f"sqlite3 mendeleev/elements.db .dump > {path}/mendeleev.sql",
echo=True,
pty=True,
)
print("done")
def render_doc_table(class_name: str) -> str:
"""
Fetch attributes for a specific class and render a table for documentation.
"""
from mendeleev.utils import apply_rst_format, render_rst_table
from mendeleev.fetch import fetch_table
df = fetch_table("propertymetadata")
df = df.loc[df["class_name"] == class_name]
df = apply_rst_format(df)
cols = ["Attribute name", "Description", "Unit", "Value origin", "Citation keys"]
# display version of the column names
return render_rst_table(df[cols].sort_values("Attribute name"))
def add_leading_spaces(multiline_string: str) -> str:
"Add three spaces in front of each line of a multiline string"
lines = multiline_string.splitlines()
modified_lines = [f" {line}" if line.strip() else line for line in lines]
return "\n".join(modified_lines)
def render_footnotes():
"Render footnotes data in rst markup"
from mendeleev.fetch import fetch_table
df = fetch_table("propertymetadata")
footnotes = ""
for _, row in (
df[df["annotations"].notnull()].sort_values(by="attribute_name").iterrows()
):
footnote_mark = "[#f_" + row["attribute_name"] + "]"
footnotes += f".. {footnote_mark} **{row['attribute_name']}**\n\n"
notes = add_leading_spaces(row["annotations"])
footnotes += f"{notes}\n\n"
return footnotes
@task
def render_data_docs(c):
"""Render data documentation."""
from jinja2 import Environment, FileSystemLoader
env = Environment(
loader=FileSystemLoader("docs/templates"),
)
template = env.get_template("data.rst")
tables = {
"Element": render_doc_table("Element"),
"IonicRadius": render_doc_table("IonicRadius"),
"IonizationEnergy": render_doc_table("IonizationEnergy"),
"Isotope": render_doc_table("Isotope"),
"IsotopeDecayMode": render_doc_table("IsotopeDecayMode"),
"OxidationState": render_doc_table("OxidationState"),
"PhaseTransition": render_doc_table("PhaseTransition"),
"ScatteringFactor": render_doc_table("ScatteringFactor"),
"ScreeningConstant": render_doc_table("ScreeningConstant"),
"footnotes": render_footnotes(),
}
rendered = template.render(**tables)
print(rendered)
with open("docs/source/data.rst", "w") as f:
f.write(rendered)
@task
def sqldiff(c, branch="master"):
"""Run sqldiff."""
c.run(
"git show master:mendeleev/elements.db > mendeleev/elements.db.master",
echo=True,
)
c.run("sqldiff mendeleev/elements.db.master mendeleev/elements.db", echo=True)