Skip to content

Commit

Permalink
0.13.0 Convert variables to numbers if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
nayaverdier committed Jun 7, 2023
1 parent 139a26a commit 9669f24
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 11 deletions.
8 changes: 5 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: set up python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}

Expand All @@ -31,10 +31,12 @@ jobs:
- name: coverage
run: make coverage

- uses: codecov/codecov-action@v1
- uses: codecov/codecov-action@v3
with:
file: coverage.xml
env_vars: PYTHON
env:
CODECOV_IGNORE: true

deploy:
name: Build and Deploy
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 0.13.0 2023-06-06

- Automatically convert int/float from templated variables, instead of being strings
- `become` the `makepkg` user for running `yay` (hardcoded for now)

## 0.12.0 2023-04-24

- Add `pacman_bootstrapped_packages` option to ignore certain packages from the
Expand Down
29 changes: 24 additions & 5 deletions instater/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,39 @@ def print(self, *args, **kwargs):
else:
self.console.print(*args, **kwargs)

def jinja_object(self, template: object, extra_vars: Optional[dict] = None) -> object:
def jinja_object(
self,
template: object,
extra_vars: Optional[dict] = None,
convert_numbers: bool = False,
) -> object:
if isinstance(template, str):
return self.jinja_string(template, extra_vars)
return self.jinja_string(template, extra_vars, convert_numbers=convert_numbers)
elif isinstance(template, list):
return [self.jinja_object(item, extra_vars) for item in template]
return [self.jinja_object(item, extra_vars, convert_numbers=convert_numbers) for item in template]
else:
return template

def jinja_string(self, template: str, extra_vars: Optional[dict] = None) -> str:
def jinja_string(self, template: str, extra_vars: Optional[dict] = None, convert_numbers: bool = False) -> str:
if isinstance(template, str):
vars = self.variables
if extra_vars:
vars = {**vars, **extra_vars}
return self.jinja_env.from_string(template).render(vars)

value = self.jinja_env.from_string(template).render(vars)

if convert_numbers:
try:
return int(value) # type: ignore
except ValueError:
pass

try:
return float(value) # type: ignore
except ValueError:
pass

return value
else:
return template

Expand Down
2 changes: 1 addition & 1 deletion instater/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def _file_variables(files, context: Context):
raw_vars = yaml.safe_load(f)

for var, value in raw_vars.items():
context.variables[var] = context.jinja_string(value)
context.variables[var] = context.jinja_object(value, convert_numbers=True)


def _extract_with(context: Context, task_args: dict) -> List[dict]:
Expand Down
3 changes: 2 additions & 1 deletion instater/tasks/pacman.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def _makepkg_install(self, packages: List[str]):
self._makepkg_install_package(package)

def _yay_install(self, packages: List[str]):
util.shell(["yay", "-Sy", "--noconfirm", "--needed", "--cleanafter", *packages])
# TODO: make the `makepkg` user configurable
util.shell(["yay", "-Sy", "--noconfirm", "--needed", "--cleanafter", *packages], become="makepkg")

def _pacman_install(self, packages: List[str]):
util.shell(["pacman", "-Sy", "--noconfirm", "--noprogressbar", "--needed", *packages])
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

setup(
name="instater",
version="0.12.0",
version="0.13.0",
description=description,
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down

0 comments on commit 9669f24

Please sign in to comment.