|
1 | | -import os |
| 1 | +# import os |
| 2 | +# |
| 3 | +# import pymongo |
2 | 4 |
|
3 | | -import pymongo |
| 5 | +# from dt4acc import mongodb_ |
| 6 | +# |
| 7 | +# client = pymongo.MongoClient(mongodb_) |
| 8 | +# DB_NAME = os.environ.get("MONGODB_DB", "bessyii") |
| 9 | +# db = client[DB_NAME] |
| 10 | +# collection = db['accelerator.setup'] |
| 11 | +# TODO |
| 12 | +# this is a temporary solution to avoid the MongoDB dependency |
| 13 | +# rework this to use the file repository along with mongodb |
4 | 14 |
|
5 | | -from dt4acc import mongodb_ |
| 15 | +import json |
| 16 | +from pathlib import Path |
| 17 | +from typing import Iterable, List, Dict, Any |
6 | 18 |
|
7 | | -client = pymongo.MongoClient(mongodb_) |
8 | | -DB_NAME = os.environ.get("MONGODB_DB", "bessyii") |
9 | | -db = client[DB_NAME] |
10 | | -collection = db['accelerator.setup'] |
| 19 | +# ----------------------------------------------------------------- |
| 20 | +# locate and load the data file once; keep it cached in _DATA |
| 21 | +# ----------------------------------------------------------------- |
| 22 | +_DATA_FILE = ( |
| 23 | + Path(__file__) |
| 24 | + .resolve() # .../src/dt4acc/custom_epics/queries_json.py |
| 25 | + .parent # .../src/dt4acc/custom_epics/data |
| 26 | + / "standard" |
| 27 | + / "accelerator_setup.json" |
| 28 | +) |
11 | 29 |
|
| 30 | +with _DATA_FILE.open() as fp: |
| 31 | + _DATA: List[Dict[str, Any]] = json.load(fp) |
12 | 32 |
|
| 33 | + |
| 34 | +# ----------------------------------------------------------------- |
| 35 | +# helper: Mongo-style $in filtering for the tiny use-cases we need |
| 36 | +# ----------------------------------------------------------------- |
| 37 | +def _match(doc: Dict[str, Any], field: str, allowed: Iterable[str]) -> bool: |
| 38 | + return doc.get(field) in allowed |
| 39 | + |
| 40 | + |
| 41 | +# ----------------------------------------------------------------- |
| 42 | +# public API – identical signatures to the Mongo version |
| 43 | +# ----------------------------------------------------------------- |
13 | 44 | def get_magnets(): |
14 | | - return collection.find({"type": {"$in": ["Quadrupole", "Sextupole", "Steerer"]}}) |
| 45 | + """Return all Quadrupole/Sextupole/Steerer magnets as an iterator.""" |
| 46 | + wanted = {"Quadrupole", "Sextupole", "Steerer"} |
| 47 | + return (d for d in _DATA if _match(d, "type", wanted)) |
| 48 | + |
15 | 49 |
|
| 50 | +def get_magnets_per_power_converters(pc: str) -> List[Dict[str, Any]]: |
| 51 | + """Return all magnets driven by the given power-converter name.""" |
| 52 | + return [d for d in _DATA if d.get("pc") == pc] |
16 | 53 |
|
17 | | -def get_magnets_per_power_converters(pc): |
18 | | - return list(collection.find({"pc": pc})) |
19 | 54 |
|
| 55 | +def get_unique_power_converters() -> List[str]: |
| 56 | + """Distinct list of power-converter names for Quad/Sext/Steerer magnets.""" |
| 57 | + wanted = {"Quadrupole", "Sextupole", "Steerer"} |
| 58 | + return sorted({d["pc"] for d in _DATA if _match(d, "type", wanted)}) |
20 | 59 |
|
21 | | -def get_unique_power_converters(): |
22 | | - """Fetch unique power converter names from magnets in the DB.""" |
23 | | - return collection.distinct("pc", {"type": {"$in": ["Quadrupole", "Sextupole", "Steerer"]}}) |
24 | 60 |
|
| 61 | +def get_unique_power_converters_type_specified(type_list: Iterable[str]) -> List[str]: |
| 62 | + """Distinct list of power-converter names for the supplied magnet types.""" |
| 63 | + wanted = set(type_list) |
| 64 | + return sorted({d["pc"] for d in _DATA if _match(d, "type", wanted)}) |
25 | 65 |
|
26 | | -def get_unique_power_converters_type_specified(type_list): |
27 | | - """Fetch unique power converter names from magnets in the DB.""" |
28 | | - return collection.distinct("pc", {"type": {"$in": type_list}}) |
| 66 | +# |
| 67 | +# def get_magnets(): |
| 68 | +# return collection.find({"type": {"$in": ["Quadrupole", "Sextupole", "Steerer"]}}) |
| 69 | +# |
| 70 | +# |
| 71 | +# def get_magnets_per_power_converters(pc): |
| 72 | +# return list(collection.find({"pc": pc})) |
| 73 | +# |
| 74 | +# |
| 75 | +# def get_unique_power_converters(): |
| 76 | +# """Fetch unique power converter names from magnets in the DB.""" |
| 77 | +# return collection.distinct("pc", {"type": {"$in": ["Quadrupole", "Sextupole", "Steerer"]}}) |
| 78 | +# |
| 79 | +# |
| 80 | +# def get_unique_power_converters_type_specified(type_list): |
| 81 | +# """Fetch unique power converter names from magnets in the DB.""" |
| 82 | +# return collection.distinct("pc", {"type": {"$in": type_list}}) |
0 commit comments