zephyr_mirror: Clean up some code duplication.

(imported from commit ab21fc28d167d6f74e9d93aa8a7ccb252b5664c2)
This commit is contained in:
Tim Abbott 2012-10-29 18:53:36 -04:00
parent 13d3fd46e5
commit 36eab0e874

View file

@ -273,13 +273,7 @@ def process_notice(notice, log):
if zeph['type'] == "personal" and instance != "personal": if zeph['type'] == "personal" and instance != "personal":
zeph["content"] = "[-i %s]" % (instance,) + "\n" + zeph["content"] zeph["content"] = "[-i %s]" % (instance,) + "\n" + zeph["content"]
for field in zeph.keys(): zeph = decode_unicode_byte_strings(zeph)
if isinstance(zeph[field], str):
try:
decoded = zeph[field].decode("utf-8")
except:
decoded = zeph[field].decode("iso-8859-1")
zeph[field] = decoded
print "%s: zephyr=>humbug: received a message on %s/%s from %s..." % \ print "%s: zephyr=>humbug: received a message on %s/%s from %s..." % \
(datetime.datetime.now(), zephyr_class, instance, notice.sender) (datetime.datetime.now(), zephyr_class, instance, notice.sender)
@ -292,6 +286,15 @@ def process_notice(notice, log):
print zeph print zeph
print res print res
def decode_unicode_byte_strings(zeph):
for field in zeph.keys():
if isinstance(zeph[field], str):
try:
decoded = zeph[field].decode("utf-8")
except:
decoded = zeph[field].decode("iso-8859-1")
zeph[field] = decoded
return zeph
def zephyr_to_humbug(options): def zephyr_to_humbug(options):
if options.forward_class_messages: if options.forward_class_messages:
@ -311,18 +314,12 @@ def zephyr_to_humbug(options):
# unicode), but older messages in the log are # unicode), but older messages in the log are
# still of type str, so convert them before we # still of type str, so convert them before we
# send the message # send the message
for field in zeph.keys(): zeph = decode_unicode_byte_strings(zeph)
if isinstance(zeph[field], str):
try:
decoded = zeph[field].decode("utf-8")
except:
decoded = zeph[field].decode("iso-8859-1")
zeph[field] = decoded
# Handle importing older zephyrs in the logs # Handle importing older zephyrs in the logs
# where it isn't called a "stream" yet # where it isn't called a "stream" yet
if field == "class": if "class" in zeph:
zeph["stream"] = zeph["class"] zeph["stream"] = zeph["class"]
if field == "instance": if "instance" in zeph:
zeph["subject"] = zeph["instance"] zeph["subject"] = zeph["instance"]
print "%s: zephyr=>humbug: sending saved message to %s from %s..." % \ print "%s: zephyr=>humbug: sending saved message to %s from %s..." % \
(datetime.datetime.now(), zeph.get('stream', zeph.get('recipient')), (datetime.datetime.now(), zeph.get('stream', zeph.get('recipient')),