wizlib 3.3.4__py3-none-any.whl → 3.3.6__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.

Potentially problematic release.


This version of wizlib might be problematic. Click here for more details.

wizlib/config_handler.py CHANGED
@@ -1,4 +1,5 @@
1
1
  from argparse import Namespace
2
+ from functools import cached_property
2
3
  from pathlib import Path
3
4
  import os
4
5
  from dataclasses import dataclass
@@ -7,10 +8,9 @@ import shlex
7
8
  import subprocess
8
9
  from unittest.mock import patch
9
10
 
10
- from yaml import load
11
- from yaml import Loader
12
- from wizlib.handler import Handler
11
+ import yaml
13
12
 
13
+ from wizlib.handler import Handler
14
14
  from wizlib.error import ConfigHandlerError
15
15
  from wizlib.parser import WizParser
16
16
 
@@ -27,14 +27,33 @@ class ConfigHandler(Handler):
27
27
 
28
28
  name = 'config'
29
29
 
30
- def __init__(self, file=None):
30
+ @classmethod
31
+ def setup(cls, val):
32
+ """Allow for alternative setup, passing in an injected data value as a
33
+ dict, bypassing file loading, for testing. Possible long-term
34
+ alternative to .fake() below."""
35
+
36
+ if isinstance(val, str) or isinstance(val, Path):
37
+ return cls(file=val)
38
+ elif isinstance(val, dict) or isinstance(val, list):
39
+ return cls(data=val)
40
+
41
+ def __init__(self, file: str = None, data: dict = None):
42
+ """Initiatlize with either a yaml file path or a data block to inject
43
+ (for testing)"""
31
44
  self.file = file
45
+ self.injected_data = data
32
46
  self.cache = {}
33
47
 
34
- @property
35
- def yaml(self):
36
- if hasattr(self, '_yaml'):
37
- return self._yaml
48
+ @cached_property
49
+ def data(self):
50
+ """Returns the full set of configuration data, typically loaded from a
51
+ yaml file. Is cached in code."""
52
+
53
+ # If yaml_dict was provided, use it directly
54
+ if self.injected_data is not None:
55
+ return self.injected_data
56
+
38
57
  path = None
39
58
  if self.file:
40
59
  path = Path(self.file)
@@ -49,35 +68,41 @@ class ConfigHandler(Handler):
49
68
  path = homepath
50
69
  if path:
51
70
  with open(path) as file:
52
- self._yaml = load(file, Loader=Loader)
53
- return self._yaml
71
+ data = yaml.safe_load(file)
72
+ return data
54
73
 
55
74
  @staticmethod
56
75
  def env(name):
57
76
  if (envvar := name.upper().replace('-', '_')) in os.environ:
58
77
  return os.environ[envvar]
59
78
 
60
- def get(self, key: str):
61
- """Return the value for the requested config entry"""
79
+ def get(self, data_path: str):
80
+ """Return the value for the requested config entry. If the value is a
81
+ string, evaluate it for shell-type expressions using $(...) syntax. Can
82
+ also return a dict or array. Note that the value returned is cached
83
+ against the data_path, so future calls may not address nested paths.
84
+
85
+ data_path: Hyphen-separated path through the yaml/dict hierarchy."""
62
86
 
63
87
  # If we already found the value, return it
64
- if key in self.cache:
65
- return self.cache[key]
88
+ if data_path in self.cache:
89
+ return self.cache[data_path]
66
90
 
67
91
  # Environment variables take precedence
68
- if (result := self.env(key)):
69
- self.cache[key] = result
92
+ if (result := self.env(data_path)):
93
+ self.cache[data_path] = result
70
94
  return result
71
95
 
72
- # Otherwise look at the YAML
73
- if (yaml := self.yaml):
74
- split = key.split('-')
75
- while (val := split.pop(0)) and (val in yaml):
76
- yaml = yaml[val] if val in yaml else None
96
+ # Otherwise look at the YAML or injected data
97
+ if (data := self.data):
98
+ split = data_path.split('-')
99
+ while (key := split.pop(0)) and (key in data):
100
+ data = data[key] if key in data else None
77
101
  if not split:
78
- result = evaluate_yaml_value(yaml)
79
- self.cache[key] = result
80
- return result
102
+ if isinstance(data, str):
103
+ data = evaluate_string(data)
104
+ self.cache[data_path] = data
105
+ return data
81
106
 
82
107
  @classmethod
83
108
  def fake(cls, **vals):
@@ -88,14 +113,14 @@ class ConfigHandler(Handler):
88
113
 
89
114
 
90
115
  def os_process(match):
91
- """Run a subprocess"""
116
+ """Run a subprocess in shell form"""
92
117
  command_string = match.group(1).strip()
93
118
  command = shlex.split(command_string)
94
119
  result = subprocess.run(command, capture_output=True)
95
120
  return result.stdout.decode().strip()
96
121
 
97
122
 
98
- def evaluate_yaml_value(yaml: str) -> str:
99
- """When getting a value from YAML, evaluate shell commands"""
123
+ def evaluate_string(yaml: str) -> str:
124
+ """Evaluate shell commands in string values"""
100
125
  text = yaml.strip()
101
126
  return re.sub(r'\$\((.*?)\)', os_process, text)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: wizlib
3
- Version: 3.3.4
3
+ Version: 3.3.6
4
4
  Summary: Framework for flexible and powerful command-line applications
5
5
  License: MIT
6
6
  Author: Steampunk Wizard
@@ -2,7 +2,7 @@ wizlib/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  wizlib/app.py,sha256=ke0NbaYC6ArmdtLTySl6VIGIGicxTsIuzW9wag6b7rU,4180
3
3
  wizlib/class_family.py,sha256=tORSVAaPeWTQMcz2DX-MQClj1GQR3vCmkALPXxHa_pk,4506
4
4
  wizlib/command.py,sha256=NO1558EYuXxfkpSmX6ljjzae8n8g4w6yytZKTJtigvo,1708
5
- wizlib/config_handler.py,sha256=dS-23bAJlg26WKPfY_SaRVc7IZ45BaeEVPkre5otJD4,3113
5
+ wizlib/config_handler.py,sha256=jKQV-6iP3V3XLx-swLWUmuAaMHsFp9DN_dd6dMeaBhk,4301
6
6
  wizlib/error.py,sha256=ypwdMOYhtgKWd48ccfOX8idmCXmm-Skwx3gkPwqJB3c,46
7
7
  wizlib/handler.py,sha256=Oz80aPhDyeY9tdppZ1dvtN-19JU5ydEDVW6jtppVoD4,446
8
8
  wizlib/io.py,sha256=tkRRcjvpm6Uq1KRSK_sboi_hr3Qs-8XyE59O2QQGLrk,716
@@ -15,6 +15,6 @@ wizlib/ui/shell/__init__.py,sha256=VFtxcvTh5Mhyx6JHbR1C8yIDHlOsrNjdqSVfH5ZPAtQ,1
15
15
  wizlib/ui/shell/line_editor.py,sha256=OXOgHBhp7EfheZK1mdOykYqB_aXDn2bHA12dvHN9kFg,8087
16
16
  wizlib/ui/shell_ui.py,sha256=jre7E_5vP_SceNH7GYenfXZpFf4h9Sbh9cWZZycTYk8,1911
17
17
  wizlib/ui_handler.py,sha256=JoZadtw9DKAtGvHKP3_BJF2NaYqmcQYNdsY4PeRnOjg,634
18
- wizlib-3.3.4.dist-info/METADATA,sha256=nKgNrD5w_jhxRDfc0wwho_hXYYovCUcNg55UKhOQKeo,1028
19
- wizlib-3.3.4.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
20
- wizlib-3.3.4.dist-info/RECORD,,
18
+ wizlib-3.3.6.dist-info/METADATA,sha256=-ymtlzWercPpAD8WuSiHsp8HgUp-CGn0pfpCMQULRzA,1028
19
+ wizlib-3.3.6.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
20
+ wizlib-3.3.6.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.1.2
2
+ Generator: poetry-core 2.1.3
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any