[英]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:考虑这个例子,想象我们正在做aws s3 cp - s3://some-path/$FILE
而不是dd
调用:
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
.鉴于 $FILE 不是直接传递而是转义的,因此split
中的子命令会创建一个名为$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
:作为一个临时解决方案,我决定对plumbum
的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.把它放在命令执行之前为我解决了这个问题,但我欢迎另一个解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.