dyngle 0.7.0__py3-none-any.whl → 1.0.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.

Potentially problematic release.


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

dyngle/__init__.py CHANGED
@@ -7,8 +7,8 @@ from wizlib.ui_handler import UIHandler
7
7
 
8
8
  from dyngle.command import DyngleCommand
9
9
  from dyngle.error import DyngleError
10
- from dyngle.expression import expression
11
- from dyngle.operation import Operation
10
+ from dyngle.model.expression import expression
11
+ from dyngle.model.operation import Operation
12
12
 
13
13
 
14
14
  class DyngleApp(WizApp):
@@ -4,8 +4,8 @@ from wizlib.parser import WizParser
4
4
  from yaml import safe_load
5
5
 
6
6
  from dyngle.command import DyngleCommand
7
- from dyngle.expression import expression
8
- from dyngle.template import Template
7
+ from dyngle.model.expression import expression
8
+ from dyngle.model.template import Template
9
9
  from dyngle.error import DyngleError
10
10
 
11
11
 
File without changes
@@ -1,7 +1,8 @@
1
1
  from typing import Callable
2
2
 
3
3
  from dyngle.error import DyngleError
4
- from dyngle.safe_path import SafePath
4
+ from dyngle.model.live_data import LiveData
5
+ from dyngle.model.safe_path import SafePath
5
6
 
6
7
  from datetime import datetime as datetime, date, timedelta
7
8
  import math
@@ -9,7 +10,7 @@ import json
9
10
  import re
10
11
  import yaml
11
12
 
12
- from dyngle.template import Template
13
+ from dyngle.model.template import Template
13
14
 
14
15
 
15
16
  def formatted_datetime(dt: datetime, format_string=None) -> str:
@@ -113,10 +114,33 @@ def _evaluate(expression: str, locals: dict) -> str:
113
114
  return str(result)
114
115
 
115
116
 
117
+ # The 'expression' function returns the expression object itself, which is
118
+ # really just a function.
119
+
116
120
  def expression(text: str) -> Callable[[dict], str]:
117
- def evaluate(data: dict = None) -> str:
118
- items = data.items() if data else ()
119
- locals = {k.replace('-', '_'): v for k, v in items}
120
- def resolve(s): return Template.resolve(s, data)
121
- return _evaluate(text, locals | {'resolve': resolve})
122
- return evaluate
121
+ """Generate an expression, which is a function based on a string
122
+ expression"""
123
+
124
+ def definition(live_data: LiveData | dict | None = None) -> str:
125
+ """The expression function itself"""
126
+
127
+ # Allow for blankness and testability
128
+ live_data = LiveData(live_data)
129
+
130
+ # Translate names to underscore-separated instead of hyphen-separated
131
+ # so they work within the Python namespace.
132
+
133
+ items = live_data.items() if live_data else ()
134
+ locals = LiveData({k.replace('-', '_'): v for k, v in items})
135
+
136
+ # Create a resolve function which allows references using the hyphen
137
+ # syntax too
138
+
139
+ def resolve(key):
140
+ return live_data.resolve(key)
141
+ locals = locals | {'resolve': resolve}
142
+
143
+ # Perform the Python eval, expanded above
144
+ return _evaluate(text, locals)
145
+
146
+ return definition
@@ -0,0 +1,22 @@
1
+ from collections import UserDict
2
+
3
+ from dyngle.error import DyngleError
4
+
5
+
6
+ class LiveData(UserDict):
7
+
8
+ def resolve(self, key: str):
9
+ """Given a key (which might be dot-separated), return
10
+ the value (which might include evaluating expressions)."""
11
+
12
+ parts = key.split('.')
13
+ current = self.data
14
+ for part in parts:
15
+ if part not in current:
16
+ raise DyngleError(
17
+ f"Invalid expression or data reference '{key}'")
18
+ current = current[part]
19
+ if callable(current):
20
+ return current(self)
21
+ else:
22
+ return current
@@ -5,7 +5,8 @@ import shlex
5
5
  import subprocess
6
6
 
7
7
  from dyngle.error import DyngleError
8
- from dyngle.template import Template
8
+ from dyngle.model.live_data import LiveData
9
+ from dyngle.model.template import Template
9
10
 
10
11
 
11
12
  @dataclass
@@ -16,12 +17,12 @@ class Operation:
16
17
  steps: list
17
18
 
18
19
  def run(self, data: dict, global_expressions: dict):
19
- # The data dict is mutable
20
- steps = self.steps
21
20
  expressions = global_expressions | self.local_expressions
22
- for markup in steps:
21
+ # Data takes precedence if names match
22
+ live_data = LiveData(expressions) | data
23
+ for markup in self.steps:
23
24
  step = Step(markup)
24
- step.run(data, expressions)
25
+ step.run(live_data)
25
26
 
26
27
 
27
28
  STEP_PATTERN = re.compile(
@@ -46,10 +47,12 @@ class Step:
46
47
  self.input, self.command_template, self.output = \
47
48
  parse_step(self.markup)
48
49
 
49
- def run(self, data, expressions):
50
- command = [Template(word).render(data, expressions)
50
+ def run(self, live_data: LiveData):
51
+ command = [Template(word).render(live_data)
51
52
  for word in self.command_template]
52
53
  pipes = {}
54
+ if self.input:
55
+ pipes["input"] = live_data.resolve(self.input)
53
56
  if self.output:
54
57
  pipes['stdout'] = subprocess.PIPE
55
58
  result = subprocess.run(command, text=True, **pipes)
@@ -57,4 +60,4 @@ class Step:
57
60
  raise DyngleError(
58
61
  f'Step failed with code {result.returncode}: {self.markup}')
59
62
  if self.output:
60
- data[self.output] = result.stdout
63
+ live_data[self.output] = result.stdout
@@ -0,0 +1,30 @@
1
+ from dataclasses import dataclass
2
+ from functools import partial
3
+ import re
4
+
5
+ from dyngle.error import DyngleError
6
+ from dyngle.model.live_data import LiveData
7
+
8
+
9
+ PATTERN = re.compile(r'\{\{\s*([^}]+)\s*\}\}')
10
+
11
+
12
+ @dataclass
13
+ class Template:
14
+
15
+ template: str
16
+
17
+ def render(self, live_data: LiveData | dict | None = None) -> str:
18
+ """Render the template with the provided LiveData (raw data and
19
+ expressions)."""
20
+
21
+ live_data = LiveData(live_data)
22
+ resolver = partial(self._resolve, live_data=live_data)
23
+ return PATTERN.sub(resolver, self.template)
24
+
25
+ def _resolve(self, match, *, live_data: LiveData):
26
+ """Resolve a single name/path from the template. The argument is a
27
+ merge of the raw data and the expressions, either of which are valid
28
+ substitutions."""
29
+ key = match.group(1).strip()
30
+ return live_data.resolve(key)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: dyngle
3
- Version: 0.7.0
3
+ Version: 1.0.0
4
4
  Summary: Run lightweight local workflows
5
5
  License: MIT
6
6
  Author: Steampunk Wizard
@@ -15,13 +15,9 @@ Description-Content-Type: text/markdown
15
15
 
16
16
  # Dyngle
17
17
 
18
- Use cases
19
-
20
- - A task runner
21
- - A lightweight workflow engine
22
- - A replacement for Make in Python projects
23
- - A replacement for short functions in RC files
24
- - Freedom from quirky Bash syntax
18
+ An experimantal, lightweight, easily configurable workflow engine for
19
+ automating development, operations, data processing, and content management
20
+ tasks.
25
21
 
26
22
  Technical foundations
27
23
 
@@ -56,16 +52,18 @@ dyngle run hello
56
52
 
57
53
  ## Configuration
58
54
 
59
- Dyngle reads configuration from YAML files. You can specify the config file location using:
55
+ Dyngle reads configuration from YAML files. Specify the config file location using any of the following (in order of precedence):
60
56
 
61
- - `--config` command line option
62
- - `DYNGLE_CONFIG` environment variable
63
- - `.dyngle.yml` in current directory
64
- - `~/.dyngle.yml` in home directory
57
+ 1. A `--config` command line option, OR
58
+ 2. A `DYNGLE_CONFIG` environment variable, OR
59
+ 3. `.dyngle.yml` in current directory, OR
60
+ 4. `~/.dyngle.yml` in home directory
65
61
 
66
62
  ## Operations
67
63
 
68
- Operations are defined under `dyngle:` in the configuration. In its simplest form, an Operation takes the form of a YAML array defining the steps, as a system command with space-separated arguments. In that sense, a Dyngle operation looks something akin to a "PHONY" Make target, a short Bash script, or a CI/CD job. As a serious example, consider the `init` operation from the Dyngle configuration delivered with the project's source code.
64
+ Operations are defined under `dyngle:` in the configuration. In its simplest form, an Operation is a YAML array defining the Steps, as system commands with space-separated arguments. In that sense, a Dyngle operation looks something akin to a phony Make target, a short Bash script, or a CI/CD job.
65
+
66
+ As a serious example, consider the `init` operation from the Dyngle configuration delivered with the project's source code.
69
67
 
70
68
  ```yaml
71
69
  dyngle:
@@ -76,11 +74,11 @@ dyngle:
76
74
  - .venv/bin/pip install --upgrade pip poetry
77
75
  ```
78
76
 
79
- The elements of the YAML array _look_ like lines of Bash, but Dyngle replaces the shell with YAML-based flow control and Python-based expressions (described below). So shell-specific operators such as `|`, `>`, and `$VARIABLES` won't work.
77
+ The elements of the YAML array _look_ like lines of Bash, but Dyngle processes them directly as system commands, allowing for template substitution and Python expression evaluation (described below). So shell-specific syntax such as `|`, `>`, and `$VARIABLE` won't work.
80
78
 
81
79
  ## Data and Templates
82
80
 
83
- Dyngle maintains a block of Data throughout an operation, which is a set of named values (Python `dict`, YAML "mapping"). The values are usually strings but can also be other data types that are valid in both YAML and Python.
81
+ Dyngle maintains a block of "Live Data" throughout an operation, which is a set of named values (Python `dict`, YAML "mapping"). The values are usually strings but can also be other data types that are valid in both YAML and Python.
84
82
 
85
83
  The `dyngle run` command feeds the contents of stdin to the Operation as Data, by converting a YAML mapping to named Python values. The values may be substituted into commands or arguments in Steps using double-curly-bracket syntax (`{{` and `}}`) similar to Jinja2.
86
84
 
@@ -107,7 +105,7 @@ Hello Francis!
107
105
 
108
106
  ## Expressions
109
107
 
110
- Operations may contain Expressions, written in Python, that can be referenced in operation steps using the same syntax as for Data. In the case of a naming conflict, an Expression takes precedence over Data with the same name. Expressions can reference names in the Data directly.
108
+ Operations may contain Expressions, written in Python, that can be referenced in Operation Step Templates using the same syntax as for Data. In the case of a naming conflict, an Expression takes precedence over Data with the same name. Expressions can reference names in the Data directly.
111
109
 
112
110
  Expressions may be defined in either of two ways in the configuration:
113
111
 
@@ -157,8 +155,6 @@ YAML keys can contain hyphens, which are fully supported in Dyngle. To reference
157
155
  - Reference the name using underscores instead of hyphens (they are automatically replaced), OR
158
156
  - Use the built-in special-purpose `resolve()` function (which can also be used to reference other expressions)
159
157
 
160
-
161
-
162
158
  ```yaml
163
159
  dyngle:
164
160
  expressions:
@@ -222,26 +218,31 @@ dyngle:
222
218
  - echo "Hello {{name}}"
223
219
  ```
224
220
 
225
- ## Data assignment operator
221
+ ## Passing values between Steps in an Operation
222
+
223
+ The Steps parser supports two special operators designed to move data between Steps in an explicit way.
226
224
 
227
- The Steps parser supports one special operator which assigns the output (stdout) of its command to a Data field for use in subsequent steps.
225
+ - The data assignment operator (`=>`) assigns the contents of stdout from the command to an element in the data
226
+ - The data input operator (`->`) assigns the value of an element in the data (or an evaluated expression) to stdin for the command
228
227
 
229
- The operator is `=>` and must go after the command and its arguments. Follow the operator with the name of the Data key to assign.
228
+ The operators must appear in order in the step and must be isolated with whitespace, i.e.
230
229
 
231
- Example:
230
+ ```
231
+ <input-variable-name> -> <command and arguments> => <output-variable-name>
232
+ ```
233
+
234
+ Here we get into more useful functionality, where commands can be strung together in meaningful ways without the need for Bash.
232
235
 
233
236
  ```yaml
234
237
  dyngle:
235
238
  operations:
236
- today:
237
- expressions:
238
- just-the-date: resolve('full-date')[0:10]
239
- steps:
240
- - date => full-date
241
- - echo "Today is {{just-the-date}}"
239
+ weather:
240
+ - curl -s "https://api.open-meteo.com/v1/forecast?latitude=52.52&longitude=13.41&current_weather=true" => weather-data
241
+ - weather-data -> jq -j '.current_weather.temperature' => temperature
242
+ - echo "It's {{temperature}} degrees out there!"
242
243
  ```
243
244
 
244
- (Note Dyngle does provide Python date operations in the Expression namespace, which might provide a better way to perform the same operation as this example, but it suffices to demonstrate assignment)
245
+ If names overlap, data items populated using the data assignment operator take precedence over expressions and data in the original input from the beginning of the Operation.
245
246
 
246
247
  ## Lifecycle
247
248
 
@@ -250,7 +251,7 @@ The lifecycle of an operation is:
250
251
  1. Load Data if it exists from YAML on stdin (if no tty)
251
252
  2. Find the named Operation in the configuration
252
253
  2. Perform template rendering on the first Step, using Data and Expressions
253
- 3. Execute the Step in a subprocess
254
+ 3. Execute the Step in a subprocess, passing in an input value and populating an output value in the Data
254
255
  4. Continue with the next Step
255
256
 
256
257
  Note that operations in the config are _not_ full shell lines. They are passed directly to the system.
@@ -0,0 +1,15 @@
1
+ dyngle/__init__.py,sha256=Mku-FaOXp1HMBoQc6K9ZJfhr9n-CXxvtotRFzSZ042I,2728
2
+ dyngle/__main__.py,sha256=pYRIwzix_AL8CdJaDDis_8yMBBWO2N72NNwkroo1dQo,95
3
+ dyngle/command/__init__.py,sha256=1S86gbef8MYvG-TWD5JRIWzFg7qV5xKhp9QXx9zEx5c,94
4
+ dyngle/command/run_command.py,sha256=yq5VJv3XWtzTf9F_rW-_nANFPGGkIMQO3kiU5atEATo,1525
5
+ dyngle/error.py,sha256=CGcTa8L4O1qsHEYnzp_JBbkvntJTv2Qz46wj_TI8NLk,39
6
+ dyngle/model/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
+ dyngle/model/expression.py,sha256=FqYOfotUzTHDyON9cUCwyO5Cw4jTphY_TuWjQyhvSFY,4220
8
+ dyngle/model/live_data.py,sha256=6YdCfCDjhzMoCC5Znuwn_T-UpqeU_3I38M1mOBOLh2U,629
9
+ dyngle/model/operation.py,sha256=ZRP4XwsU8WaBgwKj6s8lx-VY4rGQaPmmB0HvrzTZjGk,1793
10
+ dyngle/model/safe_path.py,sha256=Hk2AhP6e3yKGh3kKrLLwhvAlMNx-j2jObBYJL-_doAU,3339
11
+ dyngle/model/template.py,sha256=MeXu--ZNtj_ujABU1GjjcQ1Ea_o_M-50LocuXFeOLRE,887
12
+ dyngle-1.0.0.dist-info/METADATA,sha256=ElDftWuR4ZtbLM5_RRGLBCiEdka5HNMng_4r2im6tDk,9660
13
+ dyngle-1.0.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
14
+ dyngle-1.0.0.dist-info/entry_points.txt,sha256=rekiGhtweiHKm9g1jdGb3FhzqDrk1kigJDeSNollZSA,48
15
+ dyngle-1.0.0.dist-info/RECORD,,
dyngle/template.py DELETED
@@ -1,56 +0,0 @@
1
- from dataclasses import dataclass
2
- from functools import partial
3
- import re
4
-
5
- from dyngle.error import DyngleError
6
-
7
-
8
- PATTERN = re.compile(r'\{\{\s*([^}]+)\s*\}\}')
9
-
10
-
11
- @dataclass
12
- class Template:
13
-
14
- template: str
15
-
16
- def render(self, data: dict = None, expressions: dict = None) -> str:
17
- """Render the template with the provided data and expressions.
18
-
19
- Parameters
20
- ----------
21
- data : dict
22
- String data to insert
23
- expressions : dict
24
- Functions to call with data
25
-
26
- Returns
27
- -------
28
- str
29
- Template rendered with expression resolution and values inserted.
30
- """
31
-
32
- data = data if data else {}
33
- expressions = expressions if expressions else {}
34
- resolver = partial(self._resolve, live_data=data | expressions)
35
- return PATTERN.sub(resolver, self.template)
36
-
37
- def _resolve(self, match, *, live_data: dict):
38
- """Resolve a single name/path from the template. The argument is a
39
- merge of the raw data and the expressions, either of which are valid
40
- substitutions."""
41
- key = match.group(1).strip()
42
- return self.resolve(key, live_data)
43
-
44
- @staticmethod
45
- def resolve(key: str, live_data: dict):
46
- parts = key.split('.')
47
- current = live_data
48
- for part in parts:
49
- if part not in current:
50
- raise DyngleError(
51
- f"Invalid expression or data reference '{key}'")
52
- current = current[part]
53
- if callable(current):
54
- return current(live_data)
55
- else:
56
- return current
@@ -1,13 +0,0 @@
1
- dyngle/__init__.py,sha256=kCFjx6sIuf0hiuH2C4QyHIaC9DY6YKudvkiiziFybgk,2716
2
- dyngle/__main__.py,sha256=pYRIwzix_AL8CdJaDDis_8yMBBWO2N72NNwkroo1dQo,95
3
- dyngle/command/__init__.py,sha256=1S86gbef8MYvG-TWD5JRIWzFg7qV5xKhp9QXx9zEx5c,94
4
- dyngle/command/run_command.py,sha256=0cRjCQIfbO0KZupp4f1h_TSZh7A0o_7slG-BXEJqjZs,1513
5
- dyngle/error.py,sha256=CGcTa8L4O1qsHEYnzp_JBbkvntJTv2Qz46wj_TI8NLk,39
6
- dyngle/expression.py,sha256=-uLVbrO8ovNZGGLNqMZWIy_StCK-0laZqcQ1gOPhU6w,3476
7
- dyngle/operation.py,sha256=SCiaFNAxigXep2W95mXaT94-n1EZgQeouAHM0_Wh6GY,1631
8
- dyngle/safe_path.py,sha256=Hk2AhP6e3yKGh3kKrLLwhvAlMNx-j2jObBYJL-_doAU,3339
9
- dyngle/template.py,sha256=IJtHu1R3hfniSeCnI4PhmJ_8ojh2YBhH9l-7_SqsfoE,1613
10
- dyngle-0.7.0.dist-info/METADATA,sha256=8leyci3OsUBplVMVH4mQmZ7nkhzj4Lk6vYNX-MP9wwk,9072
11
- dyngle-0.7.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
12
- dyngle-0.7.0.dist-info/entry_points.txt,sha256=rekiGhtweiHKm9g1jdGb3FhzqDrk1kigJDeSNollZSA,48
13
- dyngle-0.7.0.dist-info/RECORD,,
File without changes
File without changes