简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM