Skip to content

Commit

Permalink
feat: Extend BashLike with '{l[/#/;]},{l[/%/;]},{s[#;]},{s[##*;]}'
Browse files Browse the repository at this point in the history
  • Loading branch information
erikmd committed Jun 29, 2024
1 parent fa5bcbf commit 782428b
Showing 1 changed file with 57 additions and 3 deletions.
60 changes: 57 additions & 3 deletions bash_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# cf. <https://spdx.org/licenses/MIT.html>

from string import Formatter
import functools
import _string
import re

Expand Down Expand Up @@ -60,10 +61,11 @@ def get_field(self, field_name, args, kwargs):
mslice = re.match('^([0-9]+):([0-9]+)$', i)
msuffixgreedy = re.match('^%%(.+)$', i)
msuffix = re.match('^%(.+)$', i) # to test after greedy
mprefixgreedy = re.match('^##(.+)$', i)
mprefix = re.match('^#(.+)$', i)
msed = re.match('^//([^/]+)/(.*)$', i)
# Note: we could also define
# mprefixgreedy = re.match('^##(.+)$', i)
# mprefix = re.match('^#(.+)$', i)
mprefixjoin = re.match('^/#/(.*)$', i) # useful on a list var
msuffixjoin = re.match('^/%/(.*)$', i) # useful on a list var
if mslice:
a, b = map(int, mslice.groups())
obj = obj[a:b]
Expand All @@ -75,10 +77,40 @@ def get_field(self, field_name, args, kwargs):
suffix = msuffix.groups()[0]
prefix = translate_prefix(reverse(suffix), False)
obj = reverse(re.sub(prefix, '', reverse(obj), count=1))
elif mprefixgreedy:
prefix = mprefixgreedy.groups()[0]
prefix = translate_prefix(prefix, True)
obj = re.sub(prefix, '', obj, count=1)
elif mprefix:
prefix = mprefix.groups()[0]
prefix = translate_prefix(prefix, False)
obj = re.sub(prefix, '', obj, count=1)
elif msed:
glob, dest = msed.groups()
regexp = translate(glob, True)
obj = re.sub(regexp, dest, obj, count=0)
elif mprefixjoin:
addprefix = mprefixjoin.groups()[0]
if obj:
if isinstance(obj, list):
obj = functools.reduce(lambda res, e:
res + addprefix + str(e),
obj, '')
else:
obj = addprefix + str(obj)
else:
obj = ''
elif msuffixjoin:
addsuffix = msuffixjoin.groups()[0] # no need for reverse
if obj:
if isinstance(obj, list):
obj = functools.reduce(lambda res, e:
res + str(e) + addsuffix,
obj, '')
else:
obj = str(obj) + addsuffix
else:
obj = ''
else:
obj = obj[i]

Expand Down Expand Up @@ -119,3 +151,25 @@ def test_BashLike():
assert b.format('{obj._val}', obj=Dummy(4, 12)) == ''
assert b.format('V{matrix[coq][//-/+]}', matrix={'coq': '8.12-alpha'}) == \
'V8.12+alpha'
assert b.format('{s[#*>]}', s="string->int->char") == 'int->char'
assert b.format('{s[##*>]}', s="string->int->char") == 'char'
assert b.format('{s[%-*]}', s="string->int->char") == 'string->int'
assert b.format('{s[%%-*]}', s="string->int->char") == 'string'
assert b.format('{lst[/#/;]}', lst=[]) == ''
assert b.format('{s[/#/;]}', s="dev") == ';dev'
assert b.format('{lst[/#/;]}', lst=['5.0']) == ';5.0'
assert b.format('{lst[/#/;]}', lst=[1, 2, 4]) == ';1;2;4'
assert b.format('{s[/#/;][#;]}', s="dev") == 'dev'
assert b.format('{lst[/#/;][#;]}', lst=['5.0']) == '5.0'
assert b.format('{lst[/#/;][#;]}', lst=[1, 2, 4]) == '1;2;4'
assert b.format('{lst[/#/,ocaml-][#,]}', lst=['4.14', '5.0']) == \
'ocaml-4.14,ocaml-5.0'
assert b.format('{lst[/%/;]}', lst=[]) == ''
assert b.format('{s[/%/;]}', s="dev") == 'dev;'
assert b.format('{lst[/%/;]}', lst=['5.0']) == '5.0;'
assert b.format('{lst[/%/;]}', lst=[1, 2, 4]) == '1;2;4;'
assert b.format('{s[/%/;][%;]}', s="dev") == 'dev'
assert b.format('{lst[/%/;][%;]}', lst=['5.0']) == '5.0'
assert b.format('{lst[/%/;][%;]}', lst=[1, 2, 4]) == '1;2;4'
assert b.format('{lst[/%/-flambda,][%,]}', lst=['4.14', '5.0']) == \
'4.14-flambda,5.0-flambda'

0 comments on commit 782428b

Please sign in to comment.