dsw-tdk 3.13.0__py3-none-any.whl → 4.27.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
dsw/tdk/config.py ADDED
@@ -0,0 +1,151 @@
1
+ import configparser
2
+ import dataclasses
3
+ import os
4
+ import pathlib
5
+
6
+ import dotenv
7
+
8
+ from . import consts
9
+
10
+
11
+ def _rectify_api_url(api_url: str | None) -> str:
12
+ if not api_url or not isinstance(api_url, str):
13
+ return ''
14
+ return api_url.rstrip('/')
15
+
16
+
17
+ def _rectify_api_key(api_key: str | None) -> str:
18
+ if not api_key or not isinstance(api_key, str):
19
+ return ''
20
+ return api_key.strip()
21
+
22
+
23
+ @dataclasses.dataclass
24
+ class TDKWizardEnv:
25
+ api_url: str
26
+ api_key: str
27
+
28
+ def rectify(self):
29
+ self.api_url = _rectify_api_url(self.api_url)
30
+ self.api_key = _rectify_api_key(self.api_key)
31
+
32
+
33
+ class TDKConfig:
34
+ LOCAL_CONFIG = '_local'
35
+ HOME_CONFIG = pathlib.Path.home() / '.dsw-tdk' / 'config.cfg'
36
+
37
+ def __init__(self):
38
+ self.shared_envs = {} # type: dict[str, TDKWizardEnv]
39
+ self.local_env = TDKWizardEnv(
40
+ api_url='',
41
+ api_key='',
42
+ )
43
+ self.current_env_name = self.LOCAL_CONFIG
44
+ self.default_env_name = None # type: str | None
45
+
46
+ def load_dotenv(self, path: pathlib.Path):
47
+ if path.exists():
48
+ dotenv.load_dotenv(path)
49
+ api_url = os.getenv('DSW_API_URL', '')
50
+ api_key = os.getenv('DSW_API_KEY', '')
51
+
52
+ if not api_url or not api_key:
53
+ return
54
+
55
+ self.current_env_name = self.LOCAL_CONFIG
56
+ self.local_env = TDKWizardEnv(
57
+ api_url=api_url,
58
+ api_key=api_key,
59
+ )
60
+ self.local_env.rectify()
61
+
62
+ def load_home_config(self):
63
+ config_path = self.HOME_CONFIG
64
+ if not config_path.exists():
65
+ return
66
+
67
+ config = configparser.ConfigParser()
68
+ config.read(config_path)
69
+ for section in config.sections():
70
+ if section == 'default':
71
+ self.default_env_name = config.get(section, 'env', fallback=None)
72
+ elif section.startswith('env:'):
73
+ env_name = section[4:]
74
+ if env_name not in self.shared_envs:
75
+ self.shared_envs[env_name] = TDKWizardEnv(
76
+ api_url=config.get(section, 'api_url', fallback=''),
77
+ api_key=config.get(section, 'api_key', fallback=''),
78
+ )
79
+ self.shared_envs[env_name].rectify()
80
+ if self.default_env_name is not None:
81
+ self.current_env_name = self.default_env_name
82
+
83
+ @property
84
+ def env(self) -> TDKWizardEnv:
85
+ if self.current_env_name == self.LOCAL_CONFIG:
86
+ return self.local_env
87
+ return self.shared_envs[self.current_env_name]
88
+
89
+ @property
90
+ def has_api_url(self) -> bool:
91
+ return self.env.api_url != ''
92
+
93
+ def set_api_url(self, api_url: str):
94
+ self.env.api_url = _rectify_api_url(api_url)
95
+
96
+ @property
97
+ def has_api_key(self) -> bool:
98
+ return self.env.api_key != ''
99
+
100
+ def set_api_key(self, api_key: str):
101
+ self.env.api_key = _rectify_api_key(api_key)
102
+
103
+ def use_local_env(self):
104
+ self.current_env_name = self.LOCAL_CONFIG
105
+
106
+ def switch_current_env(self, env_name: str | None):
107
+ if env_name not in self.shared_envs:
108
+ raise ValueError(f'Environment "{env_name}" does not exist')
109
+ self.current_env_name = env_name
110
+
111
+ @property
112
+ def is_default_env(self) -> bool:
113
+ return self.current_env_name == self.default_env_name
114
+
115
+ @property
116
+ def env_names(self) -> list[str]:
117
+ return sorted(env_name for env_name in self.shared_envs)
118
+
119
+ def add_shared_env(self, name: str, api_url: str, api_key: str):
120
+ if name == self.LOCAL_CONFIG:
121
+ raise ValueError(f'Environment name "{self.LOCAL_CONFIG}" is reserved')
122
+ if name in self.shared_envs:
123
+ raise ValueError(f'Environment "{name}" already exists')
124
+ self.shared_envs[name] = TDKWizardEnv(
125
+ api_url=api_url,
126
+ api_key=api_key,
127
+ )
128
+
129
+ def persist(self, force: bool):
130
+ output = self.HOME_CONFIG
131
+ if output.exists() and not force:
132
+ raise FileExistsError(f'File {output.as_posix()} already exists (not forced)')
133
+ if not output.parent.exists():
134
+ output.parent.mkdir(parents=True, exist_ok=True)
135
+
136
+ config = configparser.ConfigParser()
137
+ config.add_section('default')
138
+ if self.default_env_name:
139
+ config.set('default', 'env', self.default_env_name)
140
+
141
+ for env_name, env in self.shared_envs.items():
142
+ section_name = f'env:{env_name}'
143
+ config.add_section(section_name)
144
+ config.set(section_name, 'api_url', env.api_url)
145
+ config.set(section_name, 'api_key', env.api_key)
146
+
147
+ with output.open(mode='w', encoding=consts.DEFAULT_ENCODING) as config_file:
148
+ config.write(config_file)
149
+
150
+
151
+ CONFIG = TDKConfig()
dsw/tdk/consts.py ADDED
@@ -0,0 +1,25 @@
1
+ import pathlib
2
+ import re
3
+
4
+ import pathspec
5
+
6
+
7
+ APP = 'dsw-tdk'
8
+ VERSION = '4.27.0'
9
+ METAMODEL_VERSION_MAJOR = 17
10
+ METAMODEL_VERSION_MINOR = 1
11
+ METAMODEL_VERSION = f'{METAMODEL_VERSION_MAJOR}.{METAMODEL_VERSION_MINOR}'
12
+
13
+ REGEX_SEMVER = re.compile(r'^[0-9]+\.[0-9]+\.[0-9]+$')
14
+ REGEX_WIZARD_ID = re.compile(r'^[a-zA-Z0-9-_.]+$')
15
+ REGEX_ORGANIZATION_ID = REGEX_WIZARD_ID
16
+ REGEX_TEMPLATE_ID = REGEX_WIZARD_ID
17
+ REGEX_KM_ID = REGEX_WIZARD_ID
18
+ REGEX_MIME_TYPE = re.compile(r'^(?![-])(?!.*[-]$)[-\w.]+/[-\w.]+$')
19
+
20
+ DEFAULT_LIST_FORMAT = '{template.id:<50} {template.name:<30}'
21
+ DEFAULT_ENCODING = 'utf-8'
22
+ DEFAULT_README = pathlib.Path('README.md')
23
+
24
+ TEMPLATE_FILE = 'template.json'
25
+ PathspecFactory = pathspec.patterns.GitWildMatchPattern