<commit_before>#
# public domain
#
__doc__ = """
simple sam i2p socket implemenation
"""

from i2p import datatypes
from enum import Enum

import collections
import errno
import logging
import os
import string
import random
import socket as pysocket

from contextlib import wraps

# Address family for i2p
# TODO: fix this
AF_I2P = pysocket.AF_INET

_defaultSAMAddr = ('127.0.0.1', 7656)

class State(Enum):
    """
    state of a sam socket
    """
    
    Initial = 0
    """
    we are not connected to anything, initial state
    """
    
    Established = 1
    """
    we have established a session with sam
    """
    
    Ready = 2
    """
    we are ready to do stuff
    """
    
    Connecting = 3
    """
    we are connecting or binding
    """
    
    Running = 4
    """
    we are connected or bound
    """
    
    Closing = 5
    """
    closing all connections
    """
    
    Closed = 6
    """
    sam connection closed entirely
    """    
    
    Error = 7
    """
    an error occured
    """
    
class SAM:
    """
    decorator constants
    """
    SOCK_STREAM = pysocket.SOCK_STREAM #datatypes.I2CPProtocol.STREAMING
    SOCK_DGRAM = pysocket.SOCK_DGRAM #datatypes.I2CPProtocol.DGRAM
    SOCK_RAW = pysocket.SOCK_RAW #datatypes.I2CPProtocol.RAW

def randnick(l):
    nick = ''
    for n in range(l):
        nick += random.choice(string.ascii_letters)
    return nick
    
def _sam_readline(sock, partial=''):
    """
    read a line from a sam control socket
    """
    response = bytearray()
    finished = True
    while True:
        try:
            c = sock.recv(1)
            if c:
                if c == b'\n':
                    break
                response += c
            else:
                break
        except pysocket.error as err:
            if ex.args[0] != errno.EWOULDBLOCK:
                raise
            finished = False
            break
    return partial+response.decode('ascii'), finished

SamReply = collections.namedtuple('SamReply', ['cmd', 'opts'])


def _sam_parse_reply(line):
    """
    parse a reply line into a dict
    """
    opts = dict()
    parts = line.split(' ')

    for part in parts[2:]:
        if '=' in part:
            idx = part.index('=')
            k = part[:idx]
            v = part[1+idx:]
            opts[k] = v<commit_msg>Fix bug in non-blocking _sam_readline<commit_after>#
# public domain
#
__doc__ = """
simple sam i2p socket implemenation
"""

from i2p import datatypes
from enum import Enum

import collections
import errno
import logging
import os
import string
import random
import socket as pysocket

from contextlib import wraps

# Address family for i2p
# TODO: fix this
AF_I2P = pysocket.AF_INET

_defaultSAMAddr = ('127.0.0.1', 7656)

class State(Enum):
    """
    state of a sam socket
    """
    
    Initial = 0
    """
    we are not connected to anything, initial state
    """
    
    Established = 1
    """
    we have established a session with sam
    """
    
    Ready = 2
    """
    we are ready to do stuff
    """
    
    Connecting = 3
    """
    we are connecting or binding
    """
    
    Running = 4
    """
    we are connected or bound
    """
    
    Closing = 5
    """
    closing all connections
    """
    
    Closed = 6
    """
    sam connection closed entirely
    """    
    
    Error = 7
    """
    an error occured
    """
    
class SAM:
    """
    decorator constants
    """
    SOCK_STREAM = pysocket.SOCK_STREAM #datatypes.I2CPProtocol.STREAMING
    SOCK_DGRAM = pysocket.SOCK_DGRAM #datatypes.I2CPProtocol.DGRAM
    SOCK_RAW = pysocket.SOCK_RAW #datatypes.I2CPProtocol.RAW

def randnick(l):
    nick = ''
    for n in range(l):
        nick += random.choice(string.ascii_letters)
    return nick
    
def _sam_readline(sock, partial=''):
    """
    read a line from a sam control socket
    """
    response = bytearray()
    finished = True
    while True:
        try:
            c = sock.recv(1)
            if c:
                if c == b'\n':
                    break
                response += c
            else:
                break
        except pysocket.error as err:
            if err.args[0] != errno.EWOULDBLOCK:
                raise
            finished = False
            break
    return partial+response.decode('ascii'), finished

SamReply = collections.namedtuple('SamReply', ['cmd', 'opts'])


def _sam_parse_reply(line):
    """
    parse a reply line into a dict
    """
    opts = dict()
    parts = line.split(' ')

    for part in parts[2:]:
        if '=' in part:
            idx = part.index('=')
            k = part[:idx]
            v = part[1+idx:]
            opts[k] = v
{'fraction_non_alphanumeric': 0.10224119530416222, 'fraction_numerical': 0.012379935965848453, 'mean_word_length': 1.8194945848375452, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2098358', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1434, 'n_words': 429}