|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2023 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dataclass containing the metadata of a virtual environment. |
|
8 """ |
|
9 |
|
10 from dataclasses import asdict, dataclass |
|
11 |
|
12 |
|
13 @dataclass |
|
14 class VirtualenvMetaData: |
|
15 """ |
|
16 Class implementing a container for the metadata of a virtual environment. |
|
17 """ |
|
18 |
|
19 name: str # name of the virtual environment |
|
20 path: str = "" # directory of the virtual environment (empty for a global one) |
|
21 interpreter: str = "" # path of the Python interpreter |
|
22 is_global: bool = False # flag indicating a global environment |
|
23 is_conda: bool = False # flag indicating an Anaconda environment |
|
24 is_remote: bool = False # flag indicating a remotely accessed environment |
|
25 exec_path: str = "" # string to be prefixed to the PATH environment setting |
|
26 description: str = "" # description of the environment |
|
27 |
|
28 def as_dict(self): |
|
29 """ |
|
30 Public method to convert the metadata into a dictionary. |
|
31 |
|
32 @return dictionary containing the metadata |
|
33 @rtype dict |
|
34 """ |
|
35 return asdict(self) |
|
36 |
|
37 @classmethod |
|
38 def from_dict(cls, data): |
|
39 """ |
|
40 Class method to create a metadata object from the given dictionary. |
|
41 |
|
42 @param data dictionary containing the metadata |
|
43 @type dict |
|
44 @return created metadata object |
|
45 @rtype VirtualenvMetaData |
|
46 """ |
|
47 return cls( |
|
48 name=data["name"], |
|
49 path=data.get("path", ""), |
|
50 interpreter=data.get("interpreter", ""), |
|
51 is_global=data.get("is_global", False), |
|
52 is_conda=data.get("is_conda", False), |
|
53 is_remote=data.get("is_remote", False), |
|
54 exec_path=data.get("exec_path", ""), |
|
55 description=data.get("description", ""), |
|
56 ) |