Skip to content

Commit b7ed1b8

Browse files
akbakb
authored andcommitted
Set up first aliases
1 parent 032f3f3 commit b7ed1b8

File tree

5 files changed

+129
-2
lines changed

5 files changed

+129
-2
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ default_section = THIRDPARTY
3131
forced_separate = test_mario_addons
3232
not_skip = __init__.py
3333
skip = migrations
34-
known_third_party=click,requests,setuptools
34+
known_third_party=click,mario,requests,setuptools,toml
3535
ignore =
3636
.flake8
3737
dev-requirements.in

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ def read(*names, **kwargs):
8181
# "rst": ["docutils>=0.11"],
8282
# ":python_version=="2.6"": ["argparse"],
8383
},
84-
entry_points={"console_scripts": ["mario-addons = mario_addons.cli:cli"]},
84+
entry_points={"mario_plugins": "addons = mario_addons.plugins"},
8585
)

src/mario_addons/plugins/__init__.py

Whitespace-only changes.

src/mario_addons/plugins/addons.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import csv
2+
import importlib.resources
3+
import typing as t
4+
5+
import toml
6+
from mario import plug
7+
8+
9+
registry = plug.make_plugin_aliases_registry("mario_addons.plugins")
10+
print(registry)
11+
# TODO Add test cases in the toml.
12+
13+
14+
def read_csv(
15+
file, header: bool, **kwargs
16+
) -> t.Iterable[t.Dict[t.Union[str, int], str]]:
17+
"Read csv rows into an iterable of dicts."
18+
19+
rows = list(file)
20+
21+
first_row = next(csv.reader(rows))
22+
if header:
23+
fieldnames = first_row
24+
reader = csv.DictReader(rows, fieldnames=fieldnames, **kwargs)
25+
return list(reader)[1:]
26+
27+
fieldnames = range(len(first_row))
28+
return csv.DictReader(rows, fieldnames=fieldnames, **kwargs)
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
### Convert yaml to json
2+
3+
[[alias]]
4+
5+
name = "yml2json"
6+
short_help = "Convert yaml to json"
7+
8+
[[alias.stage]]
9+
10+
command = "stack"
11+
options = {code="yaml.safe_load ! json.dumps"}
12+
13+
### Search for xpath elements with xpath
14+
15+
[[alias]]
16+
name="xpath"
17+
short_help = "Find xml elements matching xpath query."
18+
arguments = [{name="query", type="str"}]
19+
inject_values=["query"]
20+
21+
[[alias.stage]]
22+
23+
command = "stack"
24+
options= {code="x.encode() ! io.BytesIO ! lxml.etree.parse ! x.findall(query) ! map(lambda y: y, x) ! list" }
25+
26+
[[alias.stage]]
27+
command="chain"
28+
29+
### Generate json objects
30+
31+
[[alias]]
32+
33+
34+
name="jo"
35+
short_help="Make json objects"
36+
arguments=[{name="pairs", type="str"}]
37+
inject_values=["pairs"]
38+
39+
[[alias.stage]]
40+
command = "eval"
41+
options = {code="pairs"}
42+
43+
[[alias.stage]]
44+
command = "map"
45+
options = {code="shlex.split(x, posix=False)"}
46+
47+
[[alias.stage]]
48+
command = "chain"
49+
50+
[[alias.stage]]
51+
command = "map"
52+
options = {code="x.partition('=') ! [x[0], ast.literal_eval(re.sub(r'^(?P<value>[A-Za-z]+)$', r'\"\\g<value>\"', x[2]))]"}
53+
54+
[[alias.stage]]
55+
command = "apply"
56+
options = {"code"="dict"}
57+
58+
[[alias.stage]]
59+
command = "map"
60+
options = {code="json.dumps"}
61+
62+
63+
[[alias]]
64+
65+
name = "jsonl"
66+
short_help = "Load jsonlines into python objects"
67+
68+
[[alias.stage]]
69+
70+
command = "map"
71+
72+
options = {code="json.loads"}
73+
74+
75+
[[alias]]
76+
name = "csv"
77+
help = "Load csv rows into python objects"
78+
inject_values=["delimiter", "header"]
79+
80+
[[alias.options]]
81+
name = "--delimiter"
82+
default = ","
83+
help = "field delimiter character"
84+
85+
[[alias.options]]
86+
name = "--header/--no-header"
87+
default=true
88+
help = "Treat the first row as a header?"
89+
90+
[[alias.stage]]
91+
command = "apply"
92+
options = {code="mario_addons.plugins.addons.read_csv(x, header=header)"}
93+
94+
[[alias.stage]]
95+
command = "chain"
96+
97+
[[alias.stage]]
98+
command = "map"
99+
options = {code="dict(x)"}

0 commit comments

Comments
 (0)