Skip to content

Commit 5988c6e

Browse files
committed
move repository and uow out into a file [appendix_csvs_ends]
1 parent 20a0818 commit 5988c6e

File tree

2 files changed

+68
-64
lines changed

2 files changed

+68
-64
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# pylint: disable=too-few-public-methods, protected-access
2+
import csv
3+
from datetime import datetime
4+
from pathlib import Path
5+
from typing import Dict
6+
7+
from allocation.domain import model
8+
from allocation.adapters import repository
9+
from allocation.service_layer import unit_of_work
10+
11+
12+
class CsvRepository(repository.AbstractRepository):
13+
def __init__(self, folder):
14+
self._batches_path = Path(folder) / "batches.csv"
15+
self._allocations_path = Path(folder) / "allocations.csv"
16+
self._batches = {} # type: Dict[str, model.Batch]
17+
self._load()
18+
19+
def get(self, reference):
20+
return self._batches.get(reference)
21+
22+
def add(self, batch):
23+
self._batches[batch.reference] = batch
24+
25+
def _load(self):
26+
with self._batches_path.open() as f:
27+
reader = csv.DictReader(f)
28+
for row in reader:
29+
ref, sku = row["ref"], row["sku"]
30+
qty = int(row["qty"])
31+
if row["eta"]:
32+
eta = datetime.strptime(row["eta"], "%Y-%m-%d").date()
33+
else:
34+
eta = None
35+
self._batches[ref] = model.Batch(ref=ref, sku=sku, qty=qty, eta=eta)
36+
if self._allocations_path.exists() is False:
37+
return
38+
with self._allocations_path.open() as f:
39+
reader = csv.DictReader(f)
40+
for row in reader:
41+
batchref, orderid, sku = row["batchref"], row["orderid"], row["sku"]
42+
qty = int(row["qty"])
43+
line = model.OrderLine(orderid, sku, qty)
44+
batch = self._batches[batchref]
45+
batch._allocations.add(line)
46+
47+
def list(self):
48+
return list(self._batches.values())
49+
50+
51+
class CsvUnitOfWork(unit_of_work.AbstractUnitOfWork):
52+
def __init__(self, folder):
53+
self.batches = CsvRepository(folder)
54+
55+
def commit(self):
56+
with self.batches._allocations_path.open("w") as f:
57+
writer = csv.writer(f)
58+
writer.writerow(["orderid", "sku", "qty", "batchref"])
59+
for batch in self.batches.list():
60+
for line in batch._allocations:
61+
writer.writerow(
62+
[line.orderid, line.sku, line.qty, batch.reference]
63+
)
64+
65+
def rollback(self):
66+
pass

src/bin/allocate-from-csv

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,14 @@
11
#!/usr/bin/env python
2-
from __future__ import annotations
3-
from typing import Dict
42
import csv
53
import sys
6-
from datetime import datetime
74
from pathlib import Path
85

9-
from allocation.domain import model
10-
from allocation.service_layer import services, unit_of_work
11-
from allocation.adapters import repository
12-
13-
14-
class CsvRepository(repository.AbstractRepository):
15-
def __init__(self, folder):
16-
self._batches_path = Path(folder) / "batches.csv"
17-
self._allocations_path = Path(folder) / "allocations.csv"
18-
self._batches = {} # type: Dict[str, model.Batch]
19-
self._load()
20-
21-
def get(self, reference):
22-
return self._batches.get(reference)
23-
24-
def add(self, batch):
25-
self._batches[batch.reference] = batch
26-
27-
def _load(self):
28-
with self._batches_path.open() as f:
29-
reader = csv.DictReader(f)
30-
for row in reader:
31-
ref, sku = row["ref"], row["sku"]
32-
qty = int(row["qty"])
33-
if row["eta"]:
34-
eta = datetime.strptime(row["eta"], "%Y-%m-%d").date()
35-
else:
36-
eta = None
37-
self._batches[ref] = model.Batch(ref=ref, sku=sku, qty=qty, eta=eta)
38-
if self._allocations_path.exists() is False:
39-
return
40-
with self._allocations_path.open() as f:
41-
reader = csv.DictReader(f)
42-
for row in reader:
43-
batchref, orderid, sku = row["batchref"], row["orderid"], row["sku"]
44-
qty = int(row["qty"])
45-
line = model.OrderLine(orderid, sku, qty)
46-
batch = self._batches[batchref]
47-
batch._allocations.add(line)
48-
49-
def list(self):
50-
return list(self._batches.values())
51-
52-
53-
class CsvUnitOfWork(unit_of_work.AbstractUnitOfWork):
54-
def __init__(self, folder):
55-
self.batches = CsvRepository(folder)
56-
57-
def commit(self):
58-
with self.batches._allocations_path.open("w") as f:
59-
writer = csv.writer(f)
60-
writer.writerow(["orderid", "sku", "qty", "batchref"])
61-
for batch in self.batches.list():
62-
for line in batch._allocations:
63-
writer.writerow(
64-
[line.orderid, line.sku, line.qty, batch.reference]
65-
)
66-
67-
def rollback(self):
68-
pass
6+
from allocation.service_layer import csv_uow, services
697

708

719
def main(folder):
7210
orders_path = Path(folder) / "orders.csv"
73-
uow = CsvUnitOfWork(folder)
11+
uow = csv_uow.CsvUnitOfWork(folder)
7412
with orders_path.open() as f:
7513
reader = csv.DictReader(f)
7614
for row in reader:

0 commit comments

Comments
 (0)