Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.18 KB | None | 0 0
  1. from common.proxy.ssh import SSHConfig
  2. from ja.user.config.base import UserConfig, Verbosity
  3. from ja.common.job import JobPriority, JobStatus
  4. from datetime import datetime
  5. from typing import List, Tuple, Dict, Iterable, cast
  6.  
  7.  
  8. class QueryCommandConfig(UserConfig):
  9. """
  10. Config for the query command of the user client. If one of the properties of this class is None it is not
  11. considered for filtering query results. Only return jobs that satisfy the individual conditions of each property.
  12. If more than one value is provided for a single property, the condition of the property is satisfied if the job
  13. matches any of the provided values.
  14. """
  15.  
  16. def __init__(self, uid: List[str] = None, label: List[str] = None, owner: List[str] = None,
  17. priority: List[JobPriority] = None, status: List[JobStatus] = None, is_preemtible: bool = None,
  18. special_resources: List[List[str]] = None, cpu_threads: Tuple[int, int] = None,
  19. memory: Tuple[int, int] = None, before: datetime = None, after: datetime = None,
  20. ssh_config: SSHConfig = None, verbosity: Verbosity = Verbosity.NONE):
  21. if cpu_threads[0] > cpu_threads[1] or cpu_threads[0] < 1:
  22. raise ValueError("Invalid range for cpu_threads.")
  23. if memory[0] > memory[1] or memory[0] < 1:
  24. raise ValueError("Invalid range for memory.")
  25. if after < before:
  26. raise ValueError("incorrect datetime")
  27. self._uid = uid
  28. self._label = label
  29. self._owner = owner
  30. self._priority = priority
  31. self._status = status
  32. self._is_preemtible = is_preemtible
  33. self._special_resources = special_resources
  34. self._cpu_threads = cpu_threads
  35. self._memory = memory
  36. self._before = before
  37. self._after = after
  38. super().__init__(ssh_config, verbosity)
  39.  
  40. @property
  41. def uid(self) -> List[str]:
  42. """!
  43. @return: Job uid(s) to filter query results by.
  44. """
  45. return self._uid
  46.  
  47. @property
  48. def label(self) -> List[str]:
  49. """!
  50. @return: Job label(s) to filter query results by.
  51. """
  52. return self._label
  53.  
  54. @property
  55. def owner(self) -> List[str]:
  56. """!
  57. @return: Job owner name(s) to filter query results by.
  58. """
  59. return self._owner
  60.  
  61. @property
  62. def priority(self) -> List[JobPriority]:
  63. """!
  64. @return: Job priority/priorities to filter query results by.
  65. """
  66. return self._priority
  67.  
  68. @property
  69. def status(self) -> List[JobStatus]:
  70. """!
  71. @return: Job status(es) to filter query results by.
  72. """
  73. return self._status
  74.  
  75. @property
  76. def is_preemptible(self) -> bool:
  77. """!
  78. @return: Job.is_preemptible value to filter query results by.
  79. """
  80. return self._is_preemtible
  81.  
  82. @property
  83. def special_resources(self) -> List[List[str]]:
  84. """!
  85. @return: Special resources to filter query results by.
  86. """
  87. return self._special_resources
  88.  
  89. @property
  90. def cpu_threads(self) -> Tuple[int, int]:
  91. """!
  92. @return: Lower and upper bound of job CPU thread count to filter query results by.
  93. """
  94. return self._cpu_threads
  95.  
  96. @property
  97. def memory(self) -> Tuple[int, int]:
  98. """!
  99. @return: Lower and upper bound of job memory allocation in MB to filter query results by.
  100. """
  101. return self._memory
  102.  
  103. @property
  104. def before(self) -> datetime:
  105. """!
  106. Only return jobs scheduled before this point in time.
  107. @return: The latest point in time a job of interest was scheduled.
  108. """
  109. return self._before
  110.  
  111. @property
  112. def after(self) -> datetime:
  113. """!
  114. Only return jobs scheduled after this point in time.
  115. @return: The earliest point in time a job of interest was scheduled.
  116. """
  117. return self._after
  118.  
  119. @property
  120. def verbosity(self) -> Verbosity:
  121. return self._verbosity
  122.  
  123. @property
  124. def ssh_config(self) -> SSHConfig:
  125. return self._ssh_config
  126.  
  127. def __eq__(self, o: object) -> bool:
  128. if isinstance(o, QueryCommandConfig):
  129. return self._uid == o._uid \
  130. and self._label == o._label \
  131. and self._owner == o._owner \
  132. and self._priority == o._priority \
  133. and self._status == o._status \
  134. and self._is_preemtible == o._is_preemtible \
  135. and self._special_resources == o._special_resources \
  136. and self._cpu_threads == o._cpu_threads \
  137. and self._memory == o._memory \
  138. and self._before == o._before \
  139. and self._after == o._after \
  140. and self._ssh_config == o._ssh_config \
  141. and self._verbosity == o._verbosity
  142. else:
  143. return False
  144.  
  145. def __dir__(self) -> Iterable[str]:
  146. return ["_uid", "_label", "_owner", "_priority", "_status", "_is_preemtible",
  147. "_special_resources", "_cpu_treads", "_memory", "_before", "_after",
  148. "_ssh_config", "_verbosity"]
  149.  
  150. def source_from_user_config(self, other_config: "QueryCommandConfig", unset_only: bool = True) -> None:
  151. if unset_only is False:
  152. self.__init__(other_config._uid, other_config._label, other_config._owner,
  153. other_config._priority, other_config._status, other_config._is_preemtible,
  154. other_config._special_resources, other_config._cpu_threads, other_config._memory,
  155. other_config._before, other_config._after)
  156. super().__init__(other_config._ssh_config, other_config._verbosity)
  157. else:
  158. for attr in dir(self):
  159. if getattr(self, attr) is None:
  160. setattr(self, attr, getattr(other_config, attr))
  161.  
  162. def to_dict(self) -> Dict[str, object]:
  163. _dict: Dict[str, object] = dict()
  164. _dict["uid"] = self._uid
  165. _dict["label"] = self._label
  166. _dict["owner"] = self._owner
  167. _dict["priority"] = self._priority
  168. _dict["status"] = self._status
  169. _dict["is_preemtible"] = self._is_preemtible
  170. _dict["special_resources"] = self._special_resources
  171. _dict["cpu_threads"] = self._cpu_threads
  172. _dict["memory"] = self._memory
  173. _dict["before"] = self._before.strftime("%d/%m/%Y %H:%M:%S")
  174. _dict["after"] = self._after.strftime("%d/%m/%Y %H:%M:%S")
  175. _dict["ssh_config"] = self._ssh_config.to_dict()
  176. _dict["verbosity"] = self._verbosity
  177. return _dict
  178.  
  179. @classmethod
  180. def from_dict(cls, property_dict: Dict[str, object]) -> "QueryCommandConfig":
  181. _uid = cls._get_str_list_from_dict(property_dict=property_dict, key="uid", mandatory=False)
  182. _label = cls._get_str_list_from_dict(property_dict=property_dict, key="label", mandatory=False)
  183. _owner = cls._get_str_list_from_dict(property_dict=property_dict, key="owner", mandatory=False)
  184.  
  185. _property: object = cls._get_from_dict(property_dict=property_dict, key="priority")
  186. if not isinstance(_property, list):
  187. cls._raise_error_wrong_type(
  188. key="priority", expected_type="List[JobPriority]",
  189. actual_type=_property.__class__.__name__
  190. )
  191. _object_list = cast(List[object], _property)
  192. _job_priority_list: List[JobPriority] = []
  193. for _object in _object_list:
  194. if not isinstance(_object, int):
  195. cls._raise_error_wrong_type(
  196. key="priority", expected_type="List[JobPriority]",
  197. actual_type="List[object]"
  198. )
  199. _job_priority_list.append(JobPriority(_object))
  200.  
  201. _property: object = cls._get_from_dict(property_dict=property_dict, key="status")
  202. if not isinstance(_property, list):
  203. cls._raise_error_wrong_type(
  204. key="status", expected_type="List[JobStatus]",
  205. actual_type=_property.__class__.__name__
  206. )
  207. _object_list = cast(List[object], _property)
  208. _job_status_list: List[JobStatus] = []
  209. for _object in _object_list:
  210. if not isinstance(_object, int):
  211. cls._raise_error_wrong_type(
  212. key="status", expected_type="List[JobStatus]",
  213. actual_type="List[object]"
  214. )
  215. _job_status_list.append(JobStatus(_object))
  216.  
  217. _is_preemtible = cls._get_bool_from_dict(property_dict=property_dict, key="is_preemtible", mandatory=False)
  218.  
  219. _property: object = cls._get_from_dict(property_dict=property_dict, key="special_resources")
  220. if not isinstance(_property, list):
  221. cls._raise_error_wrong_type(
  222. key="special_resources", expected_type="List[List[str]",
  223. actual_type=_property.__class__.__name__
  224. )
  225. _object_list = cast(List[object], _property)
  226. __special_resources_list: List[List[str]] = []
  227. for _object in _object_list:
  228. if not isinstance(_object, list):
  229. cls._raise_error_wrong_type(
  230. key="special_resources", expected_type="List[List[str]]",
  231. actual_type="List[object]"
  232. )
  233. __special_resources_list.append(_object)
  234.  
  235. _property: object = cls._get_from_dict(property_dict=property_dict, key="cpu_threads")
  236. if not isinstance(_property, tuple):
  237. cls._raise_error_wrong_type(
  238. key="cpu_threads", expected_type="Tuple[int, int]",
  239. actual_type=_property.__class__.__name__
  240. )
  241. _object_tuple = cast(Tuple[int], _property)
  242. _cpu_threads_list: List[int] = []
  243. for _object in _object_tuple:
  244. if not isinstance(_object, int):
  245. cls._raise_error_wrong_type(
  246. key="cpu_threads", expected_type="Tuple[int]",
  247. actual_type="Tuple[object]"
  248. )
  249. _cpu_threads_list.append(cast(int, _object))
  250. _cpu_threads: Tuple[int, int] = tuple(_cpu_threads_list)
  251.  
  252. _property: object = cls._get_from_dict(property_dict=property_dict, key="memory")
  253. if not isinstance(_property, tuple):
  254. cls._raise_error_wrong_type(
  255. key="memory", expected_type="Tuple[int, int]",
  256. actual_type=_property.__class__.__name__
  257. )
  258. _object_tuple = cast(Tuple[int], _property)
  259. _memory_list: List[int] = []
  260. for _object in _object_tuple:
  261. if not isinstance(_object, int):
  262. cls._raise_error_wrong_type(
  263. key="memory", expected_type="Tuple[int]",
  264. actual_type="Tuple[object]"
  265. )
  266. _memory_list.append(cast(int, _object))
  267. _memory: Tuple[int, int] = tuple(_memory_list)
  268.  
  269. _before_string = cls._get_str_from_dict(property_dict=property_dict, key="before", mandatory=False)
  270. _before = datetime.strptime(_before_string, "%d/%m/%Y %H:%M:%S")
  271.  
  272. _after_string = cls._get_str_from_dict(property_dict=property_dict, key="after", mandatory=False)
  273. _after = datetime.strptime(_after_string, "%d/%m/%Y %H:%M:%S")
  274.  
  275. if "ssh_config" in property_dict:
  276. _ssh_config = SSHConfig.from_dict(
  277. cls._get_dict_from_dict(property_dict=property_dict, key="ssh_config", mandatory=False))
  278. else:
  279. _ssh_config = None
  280.  
  281. if "verbosity" in property_dict:
  282. _verbosity = Verbosity(cls._get_from_dict(property_dict=property_dict, key="verbosity", mandatory=False))
  283. else:
  284. _verbosity = None
  285.  
  286. cls._assert_all_properties_used(property_dict)
  287. a = QueryCommandConfig(_uid, _label, _owner, _job_priority_list, _job_status_list, _is_preemtible,
  288. __special_resources_list, _cpu_threads, _memory, _before, _after, _ssh_config)
  289. print(a.to_dict())
  290. return a
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement