MetPX / sarracenia

https://MetPX.github.io/sarracenia
GNU General Public License v2.0
46 stars 22 forks source link

Add bash parameter substitution to config's variable #856

Open iMacadan opened 12 months ago

iMacadan commented 12 months ago

Bash param substitution would be a nice to have feature.
Not all of them would be worth it, but, for example, it can certainly help to reduce the need of creating python renamer plugins.

# Bash Parameter Substitution 
# Variable                      Description
${#var}                         # Find the length of the string
${var%pattern}                  # Remove from shortest rear (end) pattern
${var%%pattern}                 # Remove from longest rear (end) pattern
${var:num1:num2}                # Substring
${var#pattern}                  # Remove from shortest front pattern
${var##pattern}                 # Remove from longest front pattern
${var/pattern/string}           # Find and replace (only replace first occurrence)
${var//pattern/string}          # Find and replace all occurrences
${!prefix*}                     # Expands to the names of variables whose names begin with prefix.
${var,}
${var,pattern}                  # Convert first character to lowercase.
${var,,}
${var,,pattern}                 # Convert all characters to lowercase.
${var^}
${var^pattern}                  # Convert first character to uppercase.
${var^^}
${var^^pattern}                 # Convert all character to uppercase.
petersilva commented 12 months ago

Is the pattern a regex? I notice that shell/bash regexes are different from python ones, and that sarra must use the python ones?

Having a trailing pattern is making it pretty complex to figure out what to do.

petersilva commented 12 months ago

If you have something like ${var^^^h} if after the ^^ you have a regex ^h, which means only upper-case if it starts with h. ... and braces are a thing in python regexes, so now have to deal with nested braces... getting hairy.

UPDATE: above pattern is wrong... see patterns link a few posts down.

petersilva commented 12 months ago

need an actual parser at this point... which is probably not a bad thing, just work.

reidsunderland commented 12 months ago

There's details about all the substitutions supported by bash online:

https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html

https://tldp.org/LDP/abs/html/parameter-substitution.html

petersilva commented 12 months ago

the tests/sarracenia/config_test.py file could contain test cases for all these, and then it would just be a matter of running:


pytest -o log_cli=true tests/sarracenia/config_test.py

so there is a good framework to build unit tests for an implementation (that would be in sarracenia/config.py .. ( variableExpansion routine.)

petersilva commented 12 months ago

reading up on the references, the patterns are not regexes, but a different language akin to globbing but different again. sigh... how many pattern matching dialects to implement in a single app.

https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html

petersilva commented 12 months ago

I'm tempted to put in something totally idiosyncractic like... a means of embedding python in the config file. The shell stuff looks complicated to parse, not that poweful, and not that obvious wondering if having a means of embedding python in the config result will be cleaner/easier to understand in future.

so... embedding python to do this stuff is basically exactly what a DESTFNSCRIPT is for, I think. so if you don't have these substitutions, you need to implement one of those, or obviously an after_accept in a normal callback... so one can make the substitutions by other means. The request is about making that doable in a one liner, without having to write any code.

petersilva commented 12 months ago

It would be good to see some sample use cases for this feature.

iMacadan commented 12 months ago

My original ref. was https://www.cyberciti.biz/tips/bash-shell-parameter-substitution-2.html An example use for it would be when a product's file name that contains a lower case station id which needs to be put into an uppercase subdir at destination.
But, my bad, this example falls short...