|
| 1 | +from typing import Any, Callable, Dict, Optional, Union |
| 2 | + |
| 3 | + |
| 4 | +# Type aliases |
| 5 | +DemandProps = Dict[str, Any] |
| 6 | +SupplyMethod = Callable[[Optional[Any], "Scope"], Any] |
| 7 | +ScopedDemand = Callable[[DemandProps], Any] |
| 8 | +SuppliersMerge = Dict[str, Union[bool, Dict[str, SupplyMethod], list]] |
| 9 | +ExtendSuppliersMethod = Callable[ |
| 10 | + [Dict[str, SupplyMethod], SuppliersMerge], Dict[str, SupplyMethod] |
| 11 | +] |
| 12 | +DemandReturn = Any |
| 13 | + |
| 14 | + |
| 15 | +# Supplier function type (Callable with specific signature) |
| 16 | +def supplier_type(data: Optional[Any], scope: "Scope") -> DemandReturn: |
| 17 | + pass |
| 18 | + |
| 19 | + |
| 20 | +# Scope class for holding demand context |
| 21 | +class Scope: |
| 22 | + def __init__(self, key: str, _type: str, path: str, demand: ScopedDemand): |
| 23 | + self.key = key |
| 24 | + self.type = _type |
| 25 | + self.path = path |
| 26 | + self.demand = demand |
| 27 | + |
| 28 | + |
| 29 | +# Function to merge suppliers with operations: clear, add, remove |
| 30 | +def merge_suppliers( |
| 31 | + original: Dict[str, SupplyMethod], merge_op: SuppliersMerge |
| 32 | +) -> Dict[str, SupplyMethod]: |
| 33 | + merged = {} |
| 34 | + if not merge_op.get("clear", False): |
| 35 | + merged.update(original) |
| 36 | + merged.update(merge_op.get("add", {})) |
| 37 | + for key in merge_op.get("remove", []): |
| 38 | + merged.pop(key, None) |
| 39 | + return merged |
| 40 | + |
| 41 | + |
| 42 | +# Main function to handle global demand |
| 43 | +def global_demand(props: DemandProps) -> DemandReturn: |
| 44 | + key = props["key"] |
| 45 | + _type = props["type"] |
| 46 | + path = props["path"] |
| 47 | + suppliers = props.get("suppliers", {}) |
| 48 | + |
| 49 | + if not key or not _type or not path: |
| 50 | + raise ValueError("Key, Type, and Path are required in global_demand.") |
| 51 | + |
| 52 | + print(f"Global demand called with: Key: {key}, Type: {_type}, Path: {path}") |
| 53 | + |
| 54 | + supplier_func = suppliers.get(_type) |
| 55 | + if supplier_func: |
| 56 | + print(f"Calling supplier for type: {_type}") |
| 57 | + return supplier_func( |
| 58 | + props.get("data"), |
| 59 | + { |
| 60 | + "key": key, |
| 61 | + "type": _type, |
| 62 | + "path": path, |
| 63 | + "demand": create_scoped_demand(props), |
| 64 | + }, |
| 65 | + ) |
| 66 | + print(f"Supplier not found for type: {_type}") |
| 67 | + |
| 68 | + |
| 69 | +# Function to create a demand function that is scoped |
| 70 | +def create_scoped_demand(super_props: DemandProps) -> ScopedDemand: |
| 71 | + def demand_func(props: DemandProps) -> DemandReturn: |
| 72 | + demand_key = props.get("key", super_props["key"]) |
| 73 | + |
| 74 | + if "type" not in props: |
| 75 | + raise ValueError("Type is required in scoped demand.") |
| 76 | + |
| 77 | + path = f"{super_props['path']}/{demand_key}({props['type']})" |
| 78 | + new_suppliers = merge_suppliers( |
| 79 | + super_props["suppliers"], props.get("suppliers_merge", {}) |
| 80 | + ) |
| 81 | + |
| 82 | + return global_demand( |
| 83 | + { |
| 84 | + "key": demand_key, |
| 85 | + "type": props["type"], |
| 86 | + "path": path, |
| 87 | + "data": props.get("data"), |
| 88 | + "suppliers": new_suppliers, |
| 89 | + } |
| 90 | + ) |
| 91 | + |
| 92 | + return demand_func |
| 93 | + |
| 94 | + |
| 95 | +# Initiate supply and demand process |
| 96 | +def supply_demand( |
| 97 | + root_supplier: SupplyMethod, suppliers: Dict[str, SupplyMethod] |
| 98 | +) -> DemandReturn: |
| 99 | + suppliers_copy = suppliers.copy() |
| 100 | + suppliers_copy["$$root"] = root_supplier |
| 101 | + return global_demand( |
| 102 | + { |
| 103 | + "key": "root", |
| 104 | + "type": "$$root", |
| 105 | + "path": "root", |
| 106 | + "suppliers": suppliers_copy, |
| 107 | + } |
| 108 | + ) |
| 109 | + |
| 110 | + |
| 111 | +# Example supplier functions |
| 112 | +def first_supplier(data: Optional[Any], scope: Scope) -> str: |
| 113 | + print("First supplier function called.") |
| 114 | + return "1st" |
| 115 | + |
| 116 | + |
| 117 | +def second_supplier(data: Optional[Any], scope: Scope) -> str: |
| 118 | + print("Second supplier function called.") |
| 119 | + return "2nd" |
| 120 | + |
| 121 | + |
| 122 | +def third_supplier(data: Optional[Any], scope: Scope) -> DemandReturn: |
| 123 | + print("Third supplier function called.") |
| 124 | + return scope["demand"]( |
| 125 | + { |
| 126 | + "type": "first", |
| 127 | + } |
| 128 | + ) |
| 129 | + |
| 130 | + |
| 131 | +# Main execution |
| 132 | +if __name__ == "__main__": |
| 133 | + suppliers = {"first": first_supplier, "second": second_supplier} |
| 134 | + |
| 135 | + def root_supplier(data: Optional[Any], scope: Scope) -> None: |
| 136 | + print("Root supplier function called.") |
| 137 | + res = scope["demand"]( |
| 138 | + { |
| 139 | + "type": "third", |
| 140 | + "suppliers_merge": {"add": {"third": third_supplier}}, |
| 141 | + } |
| 142 | + ) |
| 143 | + print("Root supplier function call result is:", res) |
| 144 | + |
| 145 | + supply_demand(root_supplier, suppliers) |
0 commit comments