Skip directory if name is empty
See original GitHub issueWe are looking to have a configuration like this:
things:
thing1:
name: name1
type: one
thing2:
name: name2
type: one
thing3:
name: name3
type: two
And from it we’d like to stamp out a particular folder for each thing
based on its type
field. I’m wondering if any thought has been given to a feature like this?
One approach would be to support conditional directories. If the name evaluates to empty, then omit it and all its content from being copied. With this, its not ideal, but we could create a fixed number of folders:
- [[things.thing1.name if things.thing1 is not none]]
- [[things.thing2.name if things.thing2 is not none]]
... etc. (I'm not 100% on that jijna syntax, but conceptually make the dir name empty if you don't want to render it).
Another approach would be somehow support for
loops in directory names, but I’m uncertain of exactly how that might look right now.
Has this ever come up before? Is there a feature or enhancement around this that copier
would be willing to accept?
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (9 by maintainers)
Top Results From Across the Web
python 3.x - skip function if folder is empty - Stack Overflow
You can check like this if a directory is empty: if len(os.listdir(path)) == 0: # Directory is empty else: # Directory is not...
Read more >How to exclude the empty directories - UNIX and Linux Forums
Hi, I use find command to list all the files in a directory and its sub-directories, but the problem is to exclude certain...
Read more >Excluding a folder from deleting empty files with find
If you want to exclude that directory and all its descendents, then -prune is what you need:
Read more >Bash scripting: test for empty directory - Super User
I had trouble getting this method to work when the /path/to/dir contained spaces and needed to be quoted. I used [ $(ls -A...
Read more >In tar how can I exclude dir contents but leave empty dir
You can exclude the files in the directories in question without excluding the directories themselves with --exclude=project/cache/* . Be mindful that if ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
A slightly different use case but I needed to have conditional folders today.
My use case: I have a django template structure where I want templates folder to be contents of either
bs5
ortailwind
depending on the answer.For this purpose I have two folders in copier template. One contains bootstrap templates. Second contains tailwind templates.
Didn’t work I tried with this:
[%if project_type == 'bs5' %]templates[%endif%]
[%if project_type == 'tailwind' %]templates[%endif%]
If I specify tailwind, then contents that were inside the bs5 folders get copied to parent as blank path is specified.
Worked I found a trick that worked for me. Sharing that in case someone needs it. I noticed that
excludes
contains__pycache__
. So when I wanted to skip the folder, I named it__pycache__
. Seems to do the trick.This works for me:
[%if project_type == 'bs5' %]templates[%else%]__pycache__[%endif%]
[%if project_type == 'tailwind' %]templates[%else%]__pycache__[%endif%]
Hope this helps someone. -A
Ah, but that’s for a file, not for a dir. However, I see that in the same test we have https://github.com/copier-org/copier/blob/534a888a0ae2222ab2352fe4b9dcfa1fbe82a64a/tests/test_copy.py#L61 which really is our test case.
Thus this should be closed, please reopen if it happens again, thanks everyone. 😊