d33tah
d33tah

Reputation: 11628

Avoid escaping dollar sign in a subcommand?

I need to process a file and immediately upload it somewhere. Consider the example and imagine we're doing aws s3 cp - s3://some-path/$FILE instead of the dd call:

from plumbum.cmd import split, seq, rev, dd
my_filter = (rev | dd['of=$FILE'])
cmd = seq['1', '10'] | split['--filter', str(my_filter)]

Given that $FILE is not passed directly but escaped, the subcommand in split creates a file named $FILE. How can I make it NOT escape the dollar expression, but take it verbatim?

Upvotes: 1

Views: 237

Answers (1)

d33tah
d33tah

Reputation: 11628

As a temporary solution, I decided to monkey-patch plumbum's shquote:

from plumbum.cmd import split, seq, rev, dd

import plumbum
import unittest.mock as mock
# HACK: disable quoting of every argument in shquote
# otherwise we'd get --filter="dd 'of=$FILE'"
# which would create a file named $FILE anyway
with mock.patch('plumbum.commands.base.shquote', lambda x: x):
    my_filter = str(rev | dd['of=$FILE'])

funnychars_new = plumbum.commands.base._funnychars.replace('$', '')
# HACK: don't treat dollar sign as an escapeable character
with mock.patch('plumbum.commands.base._funnychars', funnychars_new):
    cmd = seq['1', '10'] | split['--filter', my_filter]
    print(cmd)
    cmd & plumbum.FG

Putting this before the command execution solved the problem for me, but I would welcome another solution.

Upvotes: 0

Related Questions