mirror of
https://github.com/mdn/webextensions-examples.git
synced 2026-04-17 23:08:33 +02:00
Remove Python 2 support
This commit is contained in:
@@ -4,10 +4,8 @@ import sys
|
|||||||
import json
|
import json
|
||||||
import struct
|
import struct
|
||||||
|
|
||||||
try:
|
# Read a message from stdin and decode it.
|
||||||
# Python 3.x version
|
def getMessage():
|
||||||
# Read a message from stdin and decode it.
|
|
||||||
def getMessage():
|
|
||||||
rawLength = sys.stdin.buffer.read(4)
|
rawLength = sys.stdin.buffer.read(4)
|
||||||
if len(rawLength) == 0:
|
if len(rawLength) == 0:
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
@@ -15,9 +13,8 @@ try:
|
|||||||
message = sys.stdin.buffer.read(messageLength).decode('utf-8')
|
message = sys.stdin.buffer.read(messageLength).decode('utf-8')
|
||||||
return json.loads(message)
|
return json.loads(message)
|
||||||
|
|
||||||
# Encode a message for transmission,
|
# Encode a message for transmission, given its content.
|
||||||
# given its content.
|
def encodeMessage(messageContent):
|
||||||
def encodeMessage(messageContent):
|
|
||||||
# https://docs.python.org/3/library/json.html#basic-usage
|
# https://docs.python.org/3/library/json.html#basic-usage
|
||||||
# To get the most compact JSON representation, you should specify
|
# To get the most compact JSON representation, you should specify
|
||||||
# (',', ':') to eliminate whitespace.
|
# (',', ':') to eliminate whitespace.
|
||||||
@@ -27,47 +24,13 @@ try:
|
|||||||
encodedLength = struct.pack('@I', len(encodedContent))
|
encodedLength = struct.pack('@I', len(encodedContent))
|
||||||
return {'length': encodedLength, 'content': encodedContent}
|
return {'length': encodedLength, 'content': encodedContent}
|
||||||
|
|
||||||
# Send an encoded message to stdout
|
# Send an encoded message to stdout
|
||||||
def sendMessage(encodedMessage):
|
def sendMessage(encodedMessage):
|
||||||
sys.stdout.buffer.write(encodedMessage['length'])
|
sys.stdout.buffer.write(encodedMessage['length'])
|
||||||
sys.stdout.buffer.write(encodedMessage['content'])
|
sys.stdout.buffer.write(encodedMessage['content'])
|
||||||
sys.stdout.buffer.flush()
|
sys.stdout.buffer.flush()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
receivedMessage = getMessage()
|
receivedMessage = getMessage()
|
||||||
if receivedMessage == "ping":
|
if receivedMessage == "ping":
|
||||||
sendMessage(encodeMessage("pong3"))
|
sendMessage(encodeMessage("pong"))
|
||||||
|
|
||||||
except AttributeError:
|
|
||||||
# Python 2.x version (if sys.stdin.buffer is not defined)
|
|
||||||
# Read a message from stdin and decode it.
|
|
||||||
def getMessage():
|
|
||||||
rawLength = sys.stdin.read(4)
|
|
||||||
if len(rawLength) == 0:
|
|
||||||
sys.exit(0)
|
|
||||||
messageLength = struct.unpack('@I', rawLength)[0]
|
|
||||||
message = sys.stdin.read(messageLength)
|
|
||||||
return json.loads(message)
|
|
||||||
|
|
||||||
# Encode a message for transmission,
|
|
||||||
# given its content.
|
|
||||||
def encodeMessage(messageContent):
|
|
||||||
# https://docs.python.org/3/library/json.html#basic-usage
|
|
||||||
# To get the most compact JSON representation, you should specify
|
|
||||||
# (',', ':') to eliminate whitespace.
|
|
||||||
# We want the most compact representation because the browser rejects
|
|
||||||
# messages that exceed 1 MB.
|
|
||||||
encodedContent = json.dumps(messageContent, separators=(',', ':'))
|
|
||||||
encodedLength = struct.pack('@I', len(encodedContent))
|
|
||||||
return {'length': encodedLength, 'content': encodedContent}
|
|
||||||
|
|
||||||
# Send an encoded message to stdout
|
|
||||||
def sendMessage(encodedMessage):
|
|
||||||
sys.stdout.write(encodedMessage['length'])
|
|
||||||
sys.stdout.write(encodedMessage['content'])
|
|
||||||
sys.stdout.flush()
|
|
||||||
|
|
||||||
while True:
|
|
||||||
receivedMessage = getMessage()
|
|
||||||
if receivedMessage == "ping":
|
|
||||||
sendMessage(encodeMessage("pong2"))
|
|
||||||
|
|||||||
Reference in New Issue
Block a user