Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape quotes in import command #5586

Merged
merged 2 commits into from
Nov 10, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions buildres/linux/jabrefHost.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
logging_dir = Path.home() / ".mozilla/native-messaging-hosts/"
if not logging_dir.exists():
logging_dir.mkdir(parents=True)
logging.basicConfig(filename=logging_dir / 'jabref_browser_extension.log')
logging.basicConfig(filename=logging_dir / "jabref_browser_extension.log")

# Read a message from stdin and decode it.
def get_message():
raw_length = sys.stdin.buffer.read(4)
if not raw_length:
logging.error("Raw_length \n")
sys.exit(0)
message_length = struct.unpack('=I', raw_length)[0]
message_length = struct.unpack("=I", raw_length)[0]
logging.info("Got length: {} bytes to be read\n".format(message_length))
message = sys.stdin.buffer.read(message_length).decode("utf-8")
logging.info("Got message of {} chars\n".format(len(message)))
Expand All @@ -42,25 +42,30 @@ def get_message():
# Encode a message for transmission, given its content.
def encode_message(message_content):
encoded_content = json.dumps(message_content).encode("utf-8")
encoded_length = struct.pack('=I', len(encoded_content))
return {'length': encoded_length, 'content': struct.pack(str(len(encoded_content))+"s",encoded_content)}
encoded_length = struct.pack("=I", len(encoded_content))
return {
"length": encoded_length,
"content": struct.pack(str(len(encoded_content)) + "s", encoded_content),
}


# Send an encoded message to stdout.
def send_message(message):
encoded_message = encode_message(message)
sys.stdout.buffer.write(encoded_message['length'])
sys.stdout.buffer.write(encoded_message['content'])
sys.stdout.buffer.write(encoded_message["length"])
sys.stdout.buffer.write(encoded_message["content"])
sys.stdout.buffer.flush()


def add_jabref_entry(data):
cmd = str(JABREF_PATH) + " -importBibtex " + "\"" + data + "\""
try:
cmd = f'{str(JABREF_PATH)} --importBibtex """{data}"""'
logger.error(f"Try to execute command {cmd}")
try:
response = subprocess.check_output(shlex.split(cmd), stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
logging.error("Failed to call JabRef: %s %s", exc.returncode, exc.output)
else:
logging.info(f'Called JabRef and got: {response}')
logging.info(f"Called JabRef and got: {response}")
return response


Expand All @@ -72,18 +77,17 @@ def add_jabref_entry(data):
message = str(e)
logging.info(str(message))

if 'status' in message and message["status"] == "validate":
if "status" in message and message["status"] == "validate":
cmd = str(JABREF_PATH) + " -version"
try:
try:
response = subprocess.check_output(shlex.split(cmd), stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as exc:
logging.error("Failed to call JabRef: %s %s", exc.returncode, exc.output)
send_message({"message": "jarNotFound", "path": JABREF_PATH})
else:
logging.info(f'{response}')
logging.info(f"{response}")
send_message({"message": "jarFound"})
else:
entry = message["text"]
output = add_jabref_entry(entry)
send_message({"message": "ok", "output": str(output)})