Compare commits
2 commits
70cec103a7
...
207a2b2a89
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
207a2b2a89 | ||
|
|
cad4cf931a |
82
readme.md
Normal file
82
readme.md
Normal file
|
|
@ -0,0 +1,82 @@
|
||||||
|
# coin
|
||||||
|
|
||||||
|
## Description
|
||||||
|
|
||||||
|
transforms a template string to its refined version by substituting its placeholders with concrete values
|
||||||
|
|
||||||
|
|
||||||
|
## Building
|
||||||
|
|
||||||
|
### Requirements
|
||||||
|
|
||||||
|
- shell interpreter
|
||||||
|
|
||||||
|
|
||||||
|
### Instructions
|
||||||
|
|
||||||
|
- execute `tools/build`
|
||||||
|
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### Requirements
|
||||||
|
|
||||||
|
- shell interpreter
|
||||||
|
|
||||||
|
|
||||||
|
### Instructions
|
||||||
|
|
||||||
|
- (as `root`) execute `tools/install`
|
||||||
|
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Requirements
|
||||||
|
|
||||||
|
- Python 3 interpreter
|
||||||
|
|
||||||
|
|
||||||
|
### Examples
|
||||||
|
|
||||||
|
#### basic
|
||||||
|
|
||||||
|
```
|
||||||
|
echo '{{flowers}} are {{color}}' | coin -a 'flowers:roses' -a 'color:red'
|
||||||
|
|
||||||
|
# roses are red
|
||||||
|
```
|
||||||
|
|
||||||
|
The same result can be produced by using multiple `coin` calls:
|
||||||
|
|
||||||
|
```
|
||||||
|
echo '{{flowers}} are {{color}}' | coin -a 'flowers:roses' | coin -a 'color:red'
|
||||||
|
```
|
||||||
|
|
||||||
|
In some contextes curly brackets might might be reserved or not available for other reasons. This can be mitigated by using different placeholder indicators:
|
||||||
|
|
||||||
|
```
|
||||||
|
echo '<<flowers>> are <<color>>' | coin -o '<<' -c '>>' -a 'flowers:roses' -a 'color:red'
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### file arguments
|
||||||
|
|
||||||
|
```
|
||||||
|
echo -n "cornflowers" > /tmp/flowers.txt
|
||||||
|
echo -n "blue" > /tmp/color.txt
|
||||||
|
echo '{{flowers}} are {{color}}' | coin -a 'flowers:@/tmp/flowers.txt' -a 'color:@/tmp/color.txt'
|
||||||
|
|
||||||
|
# cornflowers are blue
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### data file
|
||||||
|
|
||||||
|
```
|
||||||
|
echo -e "flowers: daffodills\ncolor: yellow" > /tmp/data.yaml
|
||||||
|
echo '{{flowers}} are {{color}}' | coin -d /tmp/data.yaml
|
||||||
|
|
||||||
|
# daffodills are yellow
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import sys as _sys
|
import sys as _sys
|
||||||
|
import re as _re
|
||||||
import json as _json
|
import json as _json
|
||||||
import yaml as _yaml
|
import yaml as _yaml
|
||||||
import argparse as _argparse
|
import argparse as _argparse
|
||||||
|
|
@ -22,23 +23,29 @@ def string_coin(
|
||||||
options = (
|
options = (
|
||||||
{
|
{
|
||||||
"character_open": "{{",
|
"character_open": "{{",
|
||||||
"character_close": "}}"
|
"character_close": "}}",
|
||||||
|
"ignore_whitespaces": True,
|
||||||
}
|
}
|
||||||
|
|
|
|
||||||
(options or {})
|
(options or {})
|
||||||
)
|
)
|
||||||
result = template
|
result = template
|
||||||
for (key, value, ) in arguments.items():
|
for (key, value, ) in arguments.items():
|
||||||
result = result.replace(
|
pattern = (
|
||||||
(
|
_re.escape(options["character_open"])
|
||||||
"%s%s%s"
|
+
|
||||||
% (
|
("\s*" if options["ignore_whitespaces"] else "")
|
||||||
options["character_open"],
|
+
|
||||||
key,
|
_re.escape(key)
|
||||||
options["character_close"],
|
+
|
||||||
|
("\s*" if options["ignore_whitespaces"] else "")
|
||||||
|
+
|
||||||
|
_re.escape(options["character_close"])
|
||||||
)
|
)
|
||||||
),
|
result = _re.sub(
|
||||||
value
|
pattern,
|
||||||
|
value,
|
||||||
|
result
|
||||||
)
|
)
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
@ -154,6 +161,12 @@ def main(
|
||||||
metavar = "<character-close>",
|
metavar = "<character-close>",
|
||||||
help = "placeholder closing character",
|
help = "placeholder closing character",
|
||||||
)
|
)
|
||||||
|
argument_parser.add_argument(
|
||||||
|
"-w",
|
||||||
|
"--heed-whitespaces",
|
||||||
|
action = "store_true",
|
||||||
|
help = "whether whitespace characters in the template string shall be heeded, i.e. not be ignored",
|
||||||
|
)
|
||||||
args = argument_parser.parse_args()
|
args = argument_parser.parse_args()
|
||||||
|
|
||||||
## exec
|
## exec
|
||||||
|
|
@ -169,6 +182,7 @@ def main(
|
||||||
{
|
{
|
||||||
"character_open": args.character_open,
|
"character_open": args.character_open,
|
||||||
"character_close": args.character_close,
|
"character_close": args.character_close,
|
||||||
|
"ignore_whitespaces": (not args.heed_whitespaces),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
_sys.stdout.write(content_out)
|
_sys.stdout.write(content_out)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue