0

我需要处理一个文件并立即将其上传到某处。考虑这个例子并想象我们正在做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)]

鉴于 $FILE 不是直接传递而是转义的,因此 in 中的子命令split会创建一个名为$FILE. 我怎样才能让它不逃避美元表达式,而是逐字逐句?

4

1 回答 1

0

作为临时解决方案,我决定猴子补丁plumbumshquote

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

把它放在命令执行之前为我解决了这个问题,但我欢迎另一个解决方案。

于 2019-10-17T11:22:02.890 回答