Skip to content

Commit

Permalink
fix listdir_dirs,listdir_files (#27)
Browse files Browse the repository at this point in the history
srz-zumix authored May 1, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent cc73bb2 commit a7064ab
Showing 5 changed files with 77 additions and 32 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/io-example.yml
Original file line number Diff line number Diff line change
@@ -3,6 +3,43 @@ on:
pull_request:

jobs:
io-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Create TestData
run: |
cat << 'EOS' > io-test.j2
{%- set dir = "./.github" -%}
{% for f in dir | listdir_dirs %}{{ dir }}/{{ f }}
{% endfor -%}
---
{% for f in dir | listdir_files %}{{ dir }}/{{ f }}
{% endfor -%}
EOS
- name: kamidana
id: kamidana
uses: ./
with:
template: io-test.j2
output_file: test.txt
tee: true
- name: Test
run: |
{
find ./.github -type d -mindepth 1 -maxdepth 1
echo "---"
find ./.github -type f -mindepth 1 -maxdepth 1
} >> output.txt
diff output.txt test.txt
- run: |
cat output.txt
echo "====="
cat test.txt
echo "====="
cat "io-test.j2"
if: failure()
io-example:
runs-on: ubuntu-latest
steps:
24 changes: 15 additions & 9 deletions additionals/filter.py
Original file line number Diff line number Diff line change
@@ -10,32 +10,38 @@
@as_filter
@pass_context
def regex_replace(ctx, v, pattern, repl, count=0, flags=0):
return re.sub(pattern, repl, v, count, flags)
return re.sub(pattern, repl, v, count, flags)


@as_filter
@pass_context
def ternary(ctx, v, true_val, false_val, null_val=None):
if v:
return true_val
if null_val is not None and v is None:
return null_val
return false_val
if v:
return true_val
if null_val is not None and v is None:
return null_val
return false_val


@as_filter
@pass_context
def b64encode(ctx, v, *, encoding='utf-8'):
return base64.b64encode(v.encode(encoding))
return base64.b64encode(v.encode(encoding))


@as_filter
@pass_context
def b64decode(ctx, v, *, encoding='utf-8'):
return base64.b64decode(v).decode(encoding)
return base64.b64decode(v).decode(encoding)


@as_filter
@pass_context
def json_query(ctx, v, query):
return jmespath.search(query, v)
return jmespath.search(query, v)


@as_filter
@pass_context
def typeof(ctx, v):
return type(v)
18 changes: 9 additions & 9 deletions additionals/io.py
Original file line number Diff line number Diff line change
@@ -8,47 +8,47 @@
@as_filter
@pass_context
def relativepath(ctx, v):
dirname = os.path.dirname(os.path.abspath(ctx.name))
return os.path.normpath(os.path.join(dirname, v))
dirname = os.path.dirname(os.path.abspath(ctx.name))
return os.path.normpath(os.path.join(dirname, v))


@as_filter
@pass_context
def abspath(ctx, v):
return os.path.abspath(str(v))
return os.path.abspath(str(v))


@as_filter
@pass_context
def basename(ctx, v):
return os.path.basename(str(v))
return os.path.basename(str(v))


@as_filter
@pass_context
def dirname(ctx, v):
return os.path.dirname(str(v))
return os.path.dirname(str(v))


@as_filter
@pass_context
def path_exists(ctx, v):
return os.path.exists(str(v))
return os.path.exists(str(v))


@as_filter
@pass_context
def listdir(ctx, v):
return os.listdir(str(v))
return os.listdir(str(v))


@as_filter
@pass_context
def listdir_files(ctx, v):
return [f for f in os.listdir(str(v)) if os.path.isfile(f)]
return [f for f in os.listdir(str(v)) if os.path.isfile(os.path.join(str(v), f))]


@as_filter
@pass_context
def listdir_dirs(ctx, v):
return [f for f in os.listdir(str(v)) if os.path.isdir(f)]
return [f for f in os.listdir(str(v)) if os.path.isdir(os.path.join(str(v), f))]
28 changes: 14 additions & 14 deletions additionals/to_yaml.py
Original file line number Diff line number Diff line change
@@ -7,34 +7,34 @@


def represent_odict(dumper, instance):
return dumper.represent_mapping('tag:yaml.org,2002:map', instance.items())
return dumper.represent_mapping('tag:yaml.org,2002:map', instance.items())

ruamel.yaml.add_representer(OrderedDict, represent_odict)


def construct_odict(loader, node):
return OrderedDict(loader.construct_pairs(node))
return OrderedDict(loader.construct_pairs(node))

ruamel.yaml.add_constructor('tag:yaml.org,2002:map', construct_odict)


@as_filter
@pass_context
def to_yaml(ctx, a, *args, **kw):
yaml = ruamel.yaml.YAML(typ=['rt', 'string'])
yaml.default_flow_style = kw.pop('default_flow_style', None)
yaml.allow_unicode=True
yaml.indent=kw.pop('indent', 2)
transformed = yaml.dump_to_string(a)
return transformed
yaml = ruamel.yaml.YAML(typ=['rt', 'string'])
yaml.default_flow_style = kw.pop('default_flow_style', None)
yaml.allow_unicode=True
yaml.indent=kw.pop('indent', 2)
transformed = yaml.dump_to_string(a)
return transformed


@as_filter
@pass_context
def to_nice_yaml(ctx, a, indent=2, *args, **kw):
yaml = ruamel.yaml.YAML(typ=['rt', 'string'])
yaml.default_flow_style=False
yaml.allow_unicode=True
yaml.indent=indent
transformed = yaml.dump_to_string(a)
return transformed
yaml = ruamel.yaml.YAML(typ=['rt', 'string'])
yaml.default_flow_style=False
yaml.allow_unicode=True
yaml.indent=indent
transformed = yaml.dump_to_string(a)
return transformed
2 changes: 2 additions & 0 deletions testdata/io-example.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{{ "." | abspath }}
{{ "." | abspath | basename }}
{{ "." | listdir }}
{{ "." | listdir_files }}
{{ "." | listdir_dirs }}
{{ "LICENSE" | relativepath }}
{% if ("LICENSE" | path_exists) %}
{{ "LICENSE" | read_from_file(relative_self=False) }}

0 comments on commit a7064ab

Please sign in to comment.