1"""
2This module contains a class for managing recourses such as scenarios or SCADA data.
3"""
4from typing import Any
5import uuid
6
7
[docs]
8class ResourceManager():
9 """
10 Class implementing a simple resource manager where resources are associated with UUIDs.
11 """
12 def __init__(self):
13 self.__resources = {}
14
15 def __create_uuid(self) -> str:
16 return str(uuid.uuid4())
17
[docs]
18 def validate_uuid(self, item_uuid: str) -> bool:
19 """
20 Validates a given UUID -- i.e. checks if there is a resource associated with the given UUID.
21
22 Parameters
23 ----------
24 item_uuid : `str`
25 UUID of the item.
26
27 Returns
28 -------
29 `bool`
30 True if the given UUID is valid, False otherwise.
31 """
32 return item_uuid in self.__resources
33
[docs]
34 def create_new_item(self, item: Any) -> str:
35 """
36 Adds a new item to the resource manager.
37
38 Parameters
39 ----------
40 item : `Any`
41 Item to be added.
42
43 Returns
44 -------
45 `str`
46 UUID of the new item.
47 """
48 new_item_uuid = self.__create_uuid()
49 self.__resources[new_item_uuid] = item
50
51 return new_item_uuid
52
[docs]
53 def get(self, item_uuid: str) -> Any:
54 """
55 Gets the item associated with a given UUID.
56
57 Parameters
58 ----------
59 item_uuid : `str`
60 UUID of the item.
61
62 Returns
63 -------
64 `Any`
65 Resource item.
66 """
67 if item_uuid not in self.__resources:
68 raise ValueError(f"Invalid UUID '{item_uuid}'")
69
70 return self.__resources[item_uuid]
71
[docs]
72 def close_item(self, item: Any):
73 """
74 Closes a given resource item -- i.e. all clean-up logic for an item should be called here.
75
76 Parameters
77 ----------
78 item : `Any`
79 Resource item.
80 """
81
[docs]
82 def remove(self, item_uuid: str) -> None:
83 """
84 Removes the scenario associated with a given UUID
85
86 Parameters
87 ----------
88 item_uuid : `str`
89 UUID of the item.
90 """
91 if item_uuid not in self.__resources:
92 raise ValueError(f"Invalid UUID '{item_uuid}'")
93
94 self.close_item(self.__resources[item_uuid])
95 del self.__resources[item_uuid]