###############################################################################
######################### End preferences section #############################
###############################################################################
def getMessage():
""" Read a message from stdin and decode it. """
rawLength = sys.stdin.buffer.read(4)
if len(rawLength) == 0:
sys.exit(0)
messageLength = struct.unpack('@I', rawLength)[0]
message = sys.stdin.buffer.read(messageLength).decode("utf-8")
return json.loads(message)
def encodeMessage(messageContent):
""" Encode a message for transmission, given its content. """
encodedContent = json.dumps(messageContent)
encodedLength = struct.pack('@I', len(encodedContent))
return {'length': encodedLength, 'content': encodedContent}
def sendMessage(encodedMessage):
""" Send an encoded message to stdout. """
sys.stdout.buffer.write(encodedMessage['length'])
sys.stdout.write(encodedMessage['content'])
sys.stdout.flush()
def setPassGpgOpts(env, opts_dict):
""" Add arguments to PASSWORD_STORE_GPG_OPTS. """
opts = env.get('PASSWORD_STORE_GPG_OPTS', '')
for opt, value in opts_dict.items():
re_opt = new_opt = opt
if value is not None:
re_opt = rf"{opt}(?:=|\s+)\S*"
new_opt = (
f"{opt}={shlex.quote(value)}"
if opt.startswith("--") else
f"{opt} {shlex.quote(value)}"
)
# If the user's environment sets this opt, remove it.
opts = re.sub(re_opt, '', opts)
opts = f"{new_opt} {opts}"
env['PASSWORD_STORE_GPG_OPTS'] = opts.strip()
if __name__ == "__main__":
# Read message from standard input
receivedMessage = getMessage()
opt_args = []
pos_args = []
std_input = None
# Set up (modified) command environment
env = dict(os.environ)
if "HOME" not in env:
env["HOME"] = os.path.expanduser('~')
for key, val in COMMAND_ENV.items():
env[key] = val
setPassGpgOpts(env, {'--status-fd': '2', '--debug': 'ipc'})