Skip to content

Commit 1c4d75f

Browse files
authored
Merge pull request #135 from guzman-raphael/add-raw-component
Expose generic component
2 parents 3de1856 + a104f5d commit 1c4d75f

File tree

5 files changed

+44
-92
lines changed

5 files changed

+44
-92
lines changed

.github/workflows/development.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
- uses: actions/checkout@v2
3939
- name: Compile docs static artifacts
4040
run: |
41-
export PHARUS_VERSION=$(cat pharus/version.py | tail -1 | awk -F\" '{print $2}')
41+
export PHARUS_VERSION=$(cat pharus/version.py | grep -oP '\d+\.\d+\.\d+')
4242
export HOST_UID=$(id -u)
4343
docker-compose -f docker-compose-docs.yaml up --exit-code-from pharus-docs --build
4444
echo "PHARUS_VERSION=${PHARUS_VERSION}" >> $GITHUB_ENV
@@ -67,7 +67,7 @@ jobs:
6767
- uses: actions/checkout@v2
6868
- name: Compile image
6969
run: |
70-
export PHARUS_VERSION=$(cat pharus/version.py | tail -1 | awk -F\" '{print $2}')
70+
export PHARUS_VERSION=$(cat pharus/version.py | grep -oP '\d+\.\d+\.\d+')
7171
export HOST_UID=$(id -u)
7272
docker-compose -f docker-compose-build.yaml up --exit-code-from pharus --build
7373
IMAGE=$(docker images --filter "reference=datajoint/pharus*" \
@@ -110,7 +110,7 @@ jobs:
110110
- uses: actions/checkout@v2
111111
- name: Determine package version
112112
run: |
113-
PHARUS_VERSION=$(cat pharus/version.py | tail -1 | awk -F\" '{print $2}')
113+
PHARUS_VERSION=$(cat pharus/version.py | grep -oP '\d+\.\d+\.\d+')
114114
echo "PHARUS_VERSION=${PHARUS_VERSION}" >> $GITHUB_ENV
115115
- name: Fetch image artifact
116116
uses: actions/download-artifact@v2
@@ -141,7 +141,7 @@ jobs:
141141
- uses: actions/checkout@v2
142142
- name: Determine package version
143143
run: |
144-
PHARUS_VERSION=$(cat pharus/version.py | tail -1 | awk -F\" '{print $2}')
144+
PHARUS_VERSION=$(cat pharus/version.py | grep -oP '\d+\.\d+\.\d+')
145145
echo "PHARUS_VERSION=${PHARUS_VERSION}" >> $GITHUB_ENV
146146
- name: Get changelog entry
147147
id: changelog_reader
@@ -232,7 +232,7 @@ jobs:
232232
- uses: actions/checkout@v2
233233
- name: Determine package version
234234
run: |
235-
PHARUS_VERSION=$(cat pharus/version.py | tail -1 | awk -F\" '{print $2}')
235+
PHARUS_VERSION=$(cat pharus/version.py | grep -oP '\d+\.\d+\.\d+')
236236
echo "PHARUS_VERSION=${PHARUS_VERSION}" >> $GITHUB_ENV
237237
- name: Fetch image artifact
238238
uses: actions/download-artifact@v2
@@ -281,7 +281,7 @@ jobs:
281281
- uses: actions/checkout@v2
282282
- name: Determine package version
283283
run: |
284-
PHARUS_VERSION=$(cat pharus/version.py | tail -1 | awk -F\" '{print $2}')
284+
PHARUS_VERSION=$(cat pharus/version.py | grep -oP '\d+\.\d+\.\d+')
285285
echo "PHARUS_VERSION=${PHARUS_VERSION}" >> $GITHUB_ENV
286286
- name: Fetch docs static artifacts
287287
uses: actions/download-artifact@v2

docker-compose-build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PY_VER=3.8 IMAGE=djbase DISTRO=alpine PHARUS_VERSION=$(cat pharus/version.py | tail -1 | awk -F\" '{print $2}') HOST_UID=$(id -u) docker-compose -f docker-compose-build.yaml up --exit-code-from pharus --build
1+
# PY_VER=3.8 IMAGE=djbase DISTRO=alpine PHARUS_VERSION=$(cat pharus/version.py | grep -oP '\d+\.\d+\.\d+') HOST_UID=$(id -u) docker compose -f docker-compose-build.yaml up --exit-code-from pharus --build
22
#
33
# Intended for updating dependencies and docker image.
44
# Used to build release artifacts.

docker-compose-dev.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PY_VER=3.8 IMAGE=djbase DISTRO=alpine PHARUS_VERSION=$(cat pharus/version.py | tail -1 | awk -F\" '{print $2}') HOST_UID=$(id -u) docker-compose -f docker-compose-dev.yaml up
1+
# PY_VER=3.8 IMAGE=djbase DISTRO=alpine PHARUS_VERSION=$(cat pharus/version.py | grep -oP '\d+\.\d+\.\d+') HOST_UID=$(id -u) docker compose -f docker-compose-dev.yaml up
22
#
33
# Intended for normal development. Supports hot/live reloading.
44
# Note: If requirements or Dockerfile change, will need to add --build flag to docker-compose.

docker-compose-test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# PY_VER=3.8 IMAGE=djtest DISTRO=alpine AS_SCRIPT=FALSE PHARUS_VERSION=$(cat pharus/version.py | tail -1 | awk -F\" '{print $2}') HOST_UID=$(id -u) docker-compose -f docker-compose-test.yaml up --exit-code-from pharus
1+
# PY_VER=3.8 IMAGE=djtest DISTRO=alpine AS_SCRIPT=FALSE PHARUS_VERSION=$(cat pharus/version.py | grep -oP '\d+\.\d+\.\d+') HOST_UID=$(id -u) docker compose -f docker-compose-test.yaml up --exit-code-from pharus
22
#
33
# Intended for running test suite locally.
44
# Note: If requirements or Dockerfile change, will need to add --build flag.

pharus/component_interface.py

Lines changed: 35 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import types
1313
import io
1414
import numpy as np
15-
from functools import reduce
1615

1716

1817
class NumpyEncoder(json.JSONEncoder):
@@ -45,18 +44,18 @@ def dumps(cls, obj):
4544
return json.dumps(obj, cls=cls)
4645

4746

48-
class FetchComponent:
47+
class Component:
4948
def __init__(
5049
self,
5150
name,
5251
component_config,
5352
static_config,
5453
connect_creds: dict,
54+
payload=None,
5555
):
56-
lcls = locals()
5756
self.name = name
58-
if static_config:
59-
self.static_variables = types.MappingProxyType(static_config)
57+
self.type = component_config["type"]
58+
self.route = component_config["route"]
6059
if not all(k in component_config for k in ("x", "y", "height", "width")):
6160
self.mode = "dynamic"
6261
else:
@@ -65,8 +64,22 @@ def __init__(
6564
self.y = component_config["y"]
6665
self.height = component_config["height"]
6766
self.width = component_config["width"]
68-
self.type = component_config["type"]
69-
self.route = component_config["route"]
67+
if static_config:
68+
self.static_variables = types.MappingProxyType(static_config)
69+
self.connection = dj.conn(
70+
host=connect_creds["databaseAddress"],
71+
user=connect_creds["username"],
72+
password=connect_creds["password"],
73+
reset=True,
74+
)
75+
self.payload = payload
76+
77+
78+
class FetchComponent(Component):
79+
def __init__(self, *args, **kwargs):
80+
super().__init__(*args, **kwargs)
81+
component_config = kwargs.get("component_config", args[1] if args else None)
82+
lcls = locals()
7083
exec(component_config["dj_query"], globals(), lcls)
7184
self.dj_query = lcls["dj_query"]
7285
if "restriction" in component_config:
@@ -78,12 +91,7 @@ def __init__(
7891
dj.VirtualModule(
7992
s,
8093
s,
81-
connection=dj.conn(
82-
host=connect_creds["databaseAddress"],
83-
user=connect_creds["username"],
84-
password=connect_creds["password"],
85-
reset=True,
86-
),
94+
connection=self.connection,
8795
)
8896
for s in inspect.getfullargspec(self.dj_query).args
8997
]
@@ -125,37 +133,12 @@ def dj_query_route(self):
125133
)
126134

127135

128-
class InsertComponent:
136+
class InsertComponent(Component):
129137
fields_route_format = "{route}/fields"
130138

131-
def __init__(
132-
self,
133-
name,
134-
component_config,
135-
static_config,
136-
payload,
137-
connect_creds: dict,
138-
):
139-
self.name = name
140-
self.payload = payload
141-
if static_config:
142-
self.static_variables = types.MappingProxyType(static_config)
143-
if not all(k in component_config for k in ("x", "y", "height", "width")):
144-
self.mode = "dynamic"
145-
else:
146-
self.mode = "fixed"
147-
self.x = component_config["x"]
148-
self.y = component_config["y"]
149-
self.height = component_config["height"]
150-
self.width = component_config["width"]
151-
self.type = component_config["type"]
152-
self.route = component_config["route"]
153-
self.connection = dj.conn(
154-
host=connect_creds["databaseAddress"],
155-
user=connect_creds["username"],
156-
password=connect_creds["password"],
157-
reset=True,
158-
)
139+
def __init__(self, *args, **kwargs):
140+
super().__init__(*args, **kwargs)
141+
component_config = kwargs.get("component_config", args[1] if args else None)
159142
self.fields_map = component_config.get("map")
160143
self.tables = [
161144
getattr(
@@ -181,37 +164,22 @@ def __init__(
181164
),
182165
key=lambda p: p.full_table_name,
183166
)
167+
self.destination_lookup = {
168+
sub_m.get("input", sub_m["destination"]): sub_m["destination"]
169+
for m in (self.fields_map or [])
170+
for sub_m in (m.get("map", []) + [m])
171+
}
172+
self.input_lookup = {v: k for k, v in self.destination_lookup.items()}
184173

185174
def dj_query_route(self):
186175
with self.connection.transaction:
187-
destination_lookup = reduce(
188-
lambda m0, m1: dict(
189-
m0,
190-
**(
191-
{
192-
m_t["input"]
193-
if "input" in m_t
194-
else m_t["destination"]: m_t["destination"]
195-
for m_t in m1["map"]
196-
}
197-
if m1["type"] == "table"
198-
else {
199-
m1["input"]
200-
if "input" in m1
201-
else m1["destination"]: m1["destination"]
202-
}
203-
),
204-
),
205-
self.fields_map or [],
206-
{},
207-
)
208176
for t in self.tables:
209177
t.insert(
210178
[
211179
{
212180
a: v
213181
for k, v in r.items()
214-
if (a := destination_lookup.get(k, k))
182+
if (a := self.destination_lookup.get(k, k))
215183
in t.heading.attributes
216184
}
217185
for r in self.payload["submissions"]
@@ -244,27 +212,11 @@ def fields_route(self):
244212
fields=[
245213
dict(
246214
(field := source_fields.pop(m["destination"])),
247-
name=m["input" if "input" in m else "destination"],
215+
name=m.get("input", m["destination"]),
248216
**(
249217
{
250-
"values": field["values"]
251-
if "map" not in m
252-
else [
253-
{
254-
input_lookup[k]: v
255-
for k, v in r.items()
256-
if k
257-
in (
258-
input_lookup := {
259-
table_m["destination"]: table_m[
260-
"input"
261-
if "input" in table_m
262-
else "destination"
263-
]
264-
for table_m in m["map"]
265-
}
266-
)
267-
}
218+
"values": [
219+
{self.input_lookup.get(k, k): v for k, v in r.items()}
268220
for r in field["values"]
269221
]
270222
}

0 commit comments

Comments
 (0)