Skip to content

Commit

Permalink
feat: add cli tool for listing available patches
Browse files Browse the repository at this point in the history
  • Loading branch information
MaferMazu committed Feb 21, 2023
1 parent c788bd4 commit b9dddd4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tutor/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,21 @@ def printvalue(context: Context, key: str) -> None:
except KeyError as e:
raise exceptions.TutorError(f"Missing configuration value: {key}") from e

@click.group(
name="patches",
)
def patches_command() -> None:
pass

@click.command(name="list", help="Print the project root")
@click.pass_obj
def patches_list(context: Context) -> None:
config = tutor_config.load(context.root)
renderer = env.PatchRenderer(config)
renderer.print_patches_locations()

config_command.add_command(save)
config_command.add_command(printroot)
config_command.add_command(printvalue)
config_command.add_command(patches_command)
patches_command.add_command(patches_list)
50 changes: 50 additions & 0 deletions tutor/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import typing as t
from copy import deepcopy

import click
import jinja2
import pkg_resources

Expand Down Expand Up @@ -214,6 +215,55 @@ def __render(self, template: jinja2.Template) -> str:
except jinja2.exceptions.UndefinedError as e:
raise exceptions.TutorError(f"Missing configuration value: {e.args[0]}")

class PatchRenderer(Renderer):
"""_summary_
Args:
Renderer (_type_): _description_
"""
def render_template(self, template_name: str) -> t.Union[str, bytes]:
self.current_template = template_name
return super().render_template(template_name)

def patch(self, name: str, separator: str = "\n", suffix: str = "") -> str:
if not self.patches_locations.get(name):
self.patches_locations.update({name: [self.current_template]})
else:
self.patches_locations[name].append(self.current_template)

# Store the template's name, and replace it with the name of this patch.
# This handles the case where patches themselves include patches.
original_template = self.current_template
self.current_template = f"within patch: {name}"

rendered_patch = super().patch(name, separator=separator, suffix=suffix)
self.current_template = original_template # Restore the template's name from before.
return rendered_patch

def render_all(self, *prefix: str) -> None:
"""
`prefix` can be used to limit the templates to render.
"""
for template_name in self.iter_templates_in(*prefix):
rendered = self.render_template(template_name)

def print_patches_locations(self):
click.echo(f"{'PATCH'.ljust(self.space)} LOCATIONS")
self.render_all()
for key, values in self.patches_locations.items():
n_values = 0
for value in values:
if n_values < 1:
click.echo(f"{key.ljust(self.space)} {value}")
n_values += 1
else:
click.echo(f"{''.ljust(self.space)} {value}")

def __init__(self, config: t.Optional[Config] = None, space: int = 40):
self.space = space
self.patches_locations = dict()
super().__init__(config)


def is_rendered(path: str) -> bool:
"""
Expand Down

0 comments on commit b9dddd4

Please sign in to comment.