<commit_before># -*- coding: utf-8 -*-

"""
Packet handling helper functions for Telehash.

This will probably need to be refactored to use BytesIO for speed.
"""

import time
import struct

from tomcrypt import ecc
from tomcrypt.hash import sha256
from tomcrypt.cipher import aes

from .log import log
from .identity import SwitchID

try:
    import simplejson as json
except ImportError:
    import json


# Maybe make this global...
class PacketException(ValueError):
    """There was an error encoding or decoding your packet."""


class Packet(object):
    """TeleHash Packet validation / handling"""

    # TeleHash length header: !H == network order, unsigned short
    hdr = struct.Struct('!H')

    def __init__(self, data):
        """Basic validation to start

        TODO: Look at enum or namedtuple / similar structures.
        """
        wrapper, self.payload = self.decode(data)
        if 'type' not in wrapper:
            raise PacketException('Missing type')
        t = wrapper['type']
        if 'iv' not in wrapper:
            raise PacketException('Missing iv')
        self.iv = wrapper['iv'].decode('hex')
        if t not in ('line', 'open'):
            raise PacketException('Unknown packet type')
        if t not in wrapper:
            raise PacketException('Missing open/line value')
        elif t == 'line':
            self.line = wrapper[t]
            self.open = False
        elif t == 'open':
            self.open = wrapper[t]<commit_msg>wait, I *did* make this global<commit_after># -*- coding: utf-8 -*-

"""
Packet handling helper functions for Telehash.

This will probably need to be refactored to use BytesIO for speed.
"""

import time
import struct

from tomcrypt import ecc
from tomcrypt.hash import sha256
from tomcrypt.cipher import aes

from .log import log
from .identity import SwitchID
from .exceptions import *

try:
    import simplejson as json
except ImportError:
    import json


class Packet(object):
    """TeleHash Packet validation / handling"""

    # TeleHash length header: !H == network order, unsigned short
    hdr = struct.Struct('!H')

    def __init__(self, data):
        """Basic validation to start

        TODO: Look at enum or namedtuple / similar structures.
        """
        wrapper, self.payload = self.decode(data)
        if 'type' not in wrapper:
            raise PacketException('Missing type')
        t = wrapper['type']
        if 'iv' not in wrapper:
            raise PacketException('Missing iv')
        self.iv = wrapper['iv'].decode('hex')
        if t not in ('line', 'open'):
            raise PacketException('Unknown packet type')
        if t not in wrapper:
            raise PacketException('Missing open/line value')
        elif t == 'line':
            self.line = wrapper[t]
            self.open = False
        elif t == 'open':
            self.open = wrapper[t]
{'fraction_non_alphanumeric': 0.10100659493231517, 'fraction_numerical': 0.0027768136063866713, 'mean_word_length': 2.493333333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6048208', 'n_tokens_mistral': 785, 'n_tokens_neox': 749, 'n_words': 300}