Created
August 8, 2018 05:53
-
-
Save jhavard/32ec725462d256ecc71ec5bf3343d201 to your computer and use it in GitHub Desktop.
takes a telegram as specified in ITU-T Recommendations F.1 and F.31 and turns into an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Effwon: | |
def __init__(self, message): | |
self.msg = message | |
self.split() | |
self.words = self.bodycount() + self.addrcount() | |
self.headerparse() | |
self._stringify_blocks() | |
def split(self): | |
states = ['START', 'HEADING', 'ADDRESS', 'BODY', 'TAIL'] | |
state = 0 | |
blocks = {'START': [], 'HEADING' : [], 'ADDRESS': [], 'BODY': [], 'TAIL': []} | |
for x in self.msg.splitlines(): | |
if (x.find('NNNN')+1): | |
state = 4 | |
#print("TRANSITIONED TO %s(%u)" % (states[state],state)) | |
blocks[states[state]].append(x) | |
self.blocks = blocks | |
return blocks | |
if (x.find('ZCZC')+1): | |
state = 1 | |
#print("TRANSITIONED TO %s(%u)" % (states[state],state)) | |
if (len(blocks[states[state]])) and (x == '') and (state < 3): | |
state += 1 | |
if (not len(x)) and (not len(blocks[states[state]])): | |
None | |
else: | |
blocks[states[state]].append(x) | |
def bodycount(self): | |
"Counts non-chargeable words in the body" | |
self.bwords = 0 | |
for x in self.blocks['BODY']: | |
self.bwords += len([z for z in x.split(' ') if z != '']) | |
return self.bwords | |
def addrcount(self): | |
"Counts non-chargeable words in the address block" | |
self.awords = 0 | |
for x in self.blocks['ADDRESS']: | |
self.awords += len([z for z in x.split(' ') if z != '']) | |
return self.awords | |
def headerparse(self): | |
"Parses the F.1/F.31 heading block as seen between Net Intex/HXTP/Havardgram systems" | |
None | |
def _stringify_blocks(self): | |
"Takes the blocks that have been split, then reformats them into a blobs of text" | |
body = "" | |
head = "" | |
addr = "" | |
tail = "" | |
for x in self.blocks['BODY']: | |
body += "%s\r\n" % x | |
for x in self.blocks['HEADING']: | |
head += "%s\r\n" % x | |
for x in self.blocks['ADDRESS']: | |
addr += "%s\r\n" % x | |
for x in self.blocks['TAIL']: | |
tail += "%s\r\n" % x | |
self.head = head | |
self.addr = addr | |
self.body = body | |
self.tail = tail |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment