Skip to content

Commit

Permalink
execute custom code to update environment
Browse files Browse the repository at this point in the history
  • Loading branch information
mzuther committed Dec 26, 2022
1 parent 4502b0d commit 78358cb
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
'name': 'Integer_Column',
'type': 'INT',
'default': '10',
'comment': 'this comment has been added automatically'
'comment': 'este es un comentario automatico',
},
{
'name': 'Numeric_Column',
Expand All @@ -61,7 +61,7 @@
SELECT
{% for column in columns if not column.ignore_cte -%}
{%- if column.comment %}
-- {{ column.comment }}
-- {{ column.comment | upper_first() | add_exclamation_mark(column.comment is spanish) }}
{% endif %}
{{ column.name }}
{{- ',' if not loop.last }}
Expand Down
2 changes: 1 addition & 1 deletion 20-output/10-sql_server/10-MultiFile_Target.sql
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CREATE TABLE DEMO.MultiFile_Target
-- -------------------------------------------------- --
String_Column NVARCHAR(200) COLLATE Latin1_General_100_CI_AS,
-- -------------------------------------------------- --
-- this comment has been added automatically
-- este es un comentario automatico
Integer_Column INT DEFAULT 10,
Numeric_Column DECIMAL(10, 5) NOT NULL,

Expand Down
6 changes: 3 additions & 3 deletions 20-output/10-sql_server/20-MultiFile_Procedure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ BEGIN
-- -------------------------------------------------- --
String_Column,
-- -------------------------------------------------- --
-- this comment has been added automatically
-- ¡Este es un comentario automatico!
Integer_Column
FROM
DEMO.MultiFile_Source
Expand Down Expand Up @@ -54,7 +54,7 @@ BEGIN
SELECT
tgt.String_Column,
-- -------------------------------------------------- --
-- this comment has been added automatically
-- este es un comentario automatico
tgt.Integer_Column,
tgt.Numeric_Column

Expand All @@ -63,7 +63,7 @@ BEGIN
SELECT
src.String_Column,
-- -------------------------------------------------- --
-- this comment has been added automatically
-- este es un comentario automatico
src.Integer_Column,
src.Numeric_Column
)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ List containing file extensions (e.g. `.sql`). Only files with a
matching extension are considered to be templates and will be passed
to Jinja2.

### `update_environment`

List containing paths to Python files. After creating the Jinja2
environment, these files will be sorted alphabetically (to guarantee a
stable execution order) and executed. Use this feature to add filters
to the environment, or perform any other task Python is capable of.

_Warning: there are no security checks to prevent you from deleting
all of your files and doing other mischief, so please be careful!_

### `last_run_file`

Path to the file in which the time of the last successful run will be
Expand Down
8 changes: 7 additions & 1 deletion settings_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
"template_dir": "../10-templates",
"output_dir": "../20-output",
"stencil_dir_name": "00-stencils",
"included_file_extensions": [".jinja"],
"included_file_extensions": [
".jinja"
],
"update_environment": [
"./custom/add_filters.py",
"./custom/add_tests.py"
],
"last_run_file": "../.last_run",
"file_separator": "### File: "
}
30 changes: 29 additions & 1 deletion src/StempelWerk.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
from DirWalk.DirWalk import dirwalk


VERSION = '0.4.2'
VERSION = '0.5.0'

# ensure that this script can be called from anywhere
script_dir = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -77,6 +77,7 @@ class Settings:
output_dir: str
stencil_dir_name: str
included_file_extensions: list
update_environment: list
last_run_file: str = '../.last_run'
file_separator: str = '### File: '

Expand Down Expand Up @@ -204,12 +205,39 @@ def render_template(settings, jinja_environment, template_filename):
print()


def update_environment(jinja_environment, execute_filenames):
# sort filenames to guarantee a stable execution order
for code_filename in sorted(execute_filenames):
print(f'CUSTOM: Executing "{ code_filename}" ...')

try:
with open(code_filename) as f:
custom_code = f.read()

except FileNotFoundError:
print(f'ERROR: File "{ code_filename }" not found.')
print()
exit(1)

compiled_code = compile(custom_code, code_filename, mode='exec')
exec(compiled_code)

print(f'CUSTOM: Done.')
print()

return jinja_environment


def process_templates(settings_path, only_modified=False):
settings = load_settings(settings_path)

# create Jinja2 environment and pre-load stencils
jinja_environment = cache_templates(settings, list_templates=False)

# update environment and execute custom code
jinja_environment = update_environment(
jinja_environment, settings.update_environment)

# do not end entries with path separators ("/" or "\")!
inclusions = {
'excluded_directory_names': [
Expand Down
32 changes: 32 additions & 0 deletions src/custom/add_filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
def update_environment(jinja_environment):

def display_environment_change(new, old, display_type):
added = ['"' + s + '"' for s in sorted(new - old)]
count = len(added)
added = ', '.join(added)

print(f'CUSTOM: Added { count } { display_type }: { added }.')


def uppercase_first(string):
return string[0].upper() + string[1:].lower()


def add_exclamation_mark(string, is_spanish=False):
if is_spanish:
string = '¡' + string
return string + '!'


old_filters = set(jinja_environment.filters)

jinja_environment.filters["upper_first"] = uppercase_first
jinja_environment.filters["add_exclamation_mark"] = add_exclamation_mark

new_filters = set(jinja_environment.filters)
display_environment_change(new_filters, old_filters, 'filters')

return jinja_environment


update_environment(jinja_environment) # noqa: F821
25 changes: 25 additions & 0 deletions src/custom/add_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
def update_environment(jinja_environment):

def display_environment_change(new, old, display_type):
added = ['"' + s + '"' for s in sorted(new - old)]
count = len(added)
added = ', '.join(added)

print(f'CUSTOM: Added { count } { display_type }: { added }.')


def is_spanish(string):
return 'este es' in string


old_tests = set(jinja_environment.tests)

jinja_environment.tests["spanish"] = is_spanish

new_tests = set(jinja_environment.tests)
display_environment_change(new_tests, old_tests, 'tests')

return jinja_environment


update_environment(jinja_environment) # noqa: F821

0 comments on commit 78358cb

Please sign in to comment.