|
1 | 1 | #!/usr/bin/env python |
| 2 | +from __future__ import annotations |
| 3 | +from typing import Dict |
2 | 4 | import csv |
3 | 5 | import sys |
4 | 6 | from datetime import datetime |
5 | 7 | from pathlib import Path |
6 | 8 |
|
7 | 9 | from allocation.domain import model |
| 10 | +from allocation.service_layer import services, unit_of_work |
| 11 | +from allocation.adapters import repository |
8 | 12 |
|
9 | 13 |
|
10 | | -def load_batches(batches_path): |
11 | | - batches = [] |
12 | | - with batches_path.open() as inf: |
13 | | - reader = csv.DictReader(inf) |
14 | | - for row in reader: |
15 | | - if row["eta"]: |
16 | | - eta = datetime.strptime(row["eta"], "%Y-%m-%d").date() |
17 | | - else: |
18 | | - eta = None |
19 | | - batches.append( |
20 | | - model.Batch( |
21 | | - ref=row["ref"], sku=row["sku"], qty=int(row["qty"]), eta=eta |
22 | | - ) |
23 | | - ) |
24 | | - return batches |
| 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() |
25 | 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()) |
26 | 51 |
|
27 | | -def main(folder): |
28 | | - batches_path = Path(folder) / "batches.csv" |
29 | | - orders_path = Path(folder) / "orders.csv" |
30 | | - allocations_path = Path(folder) / "allocations.csv" |
31 | 52 |
|
32 | | - batches = load_batches(batches_path) |
| 53 | +class CsvUnitOfWork(unit_of_work.AbstractUnitOfWork): |
| 54 | + def __init__(self, folder): |
| 55 | + self.batches = CsvRepository(folder) |
33 | 56 |
|
34 | | - with orders_path.open() as inf, allocations_path.open("w") as outf: |
35 | | - reader = csv.DictReader(inf) |
36 | | - writer = csv.writer(outf) |
37 | | - writer.writerow(["orderid", "sku", "batchref"]) |
| 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 |
| 69 | + |
| 70 | + |
| 71 | +def main(folder): |
| 72 | + orders_path = Path(folder) / "orders.csv" |
| 73 | + uow = CsvUnitOfWork(folder) |
| 74 | + with orders_path.open() as f: |
| 75 | + reader = csv.DictReader(f) |
38 | 76 | for row in reader: |
39 | 77 | orderid, sku = row["orderid"], row["sku"] |
40 | 78 | qty = int(row["qty"]) |
41 | | - line = model.OrderLine(orderid, sku, qty) |
42 | | - batchref = model.allocate(line, batches) |
43 | | - writer.writerow([line.orderid, line.sku, batchref]) |
| 79 | + services.allocate(orderid, sku, qty, uow) |
44 | 80 |
|
45 | 81 |
|
46 | 82 | if __name__ == "__main__": |
|
0 commit comments