<commit_before>import sys
import time
import serial
import json
import struct
import binascii
import configparser
from datetime import datetime
from .ttyP import ttyP


class PyEfiTools:
    def __init__(self):
        self.endc = '\033[0m'  # Clear color and other terminal states

    def canPacketA(self, command=b'A'):
        """create crc32 and packet using pack"""
        packetCrc = binascii.crc32(command)
        self.packetA = struct.pack('>HcL', 1, command, packetCrc)

    # NOT USED kept cuz is only 3 lines and works - but no error checking
    def oldMethod(self):
        self.ser.write(b"A")
        print("old method: ", self.ser.read(212))

    def genByteMappingA(self, iniPath):
        config = configparser.ConfigParser()
        config.sections()
        config.read(iniPath)  # needs to suit your hardware!
        # need to implement a check for a bad config here
        byteLocs = list()  # list of bit positions, eventually sorted
        byteProps = dict()  # dict of bit properties
        self.metricNames = list()
        # copy ConfigParser object into dict, strip out garbage and
        # populate byteLocs, byteProps
        for key in config['OutputChannels']:
            props = config['OutputChannels'][key].split(",")
            props = [x.strip(' ') for x in props]
            props = [x.strip('"') for x in props]

            # if props[0] == "bits":
            # for scalar values set multiplier, bits dont have this
            # create hash from list
            if props[0] == "scalar":
                byteProps[props[2]] = {
                    "name": key,
                    "type": props[0],
                    "size": props[1],
                    "unit": props[3],
                }
                self.metricNames.append(key)
                byteLocs.append(int(props[2]))
                byteProps[props[2]]["multiplier"] = float(props[4])

        # Lets make an unpack string!
        unpkStr = ">"  # big endian!
        # returned string need to be in a precise order
        # hacky -- sorting strings returns 1, 10, 100, 2, etc,
        # so i used a sorted list as an index
        byteLocs = sorted(byteLocs)
        for bitOffset in byteLocs:
            offsetStr = "%s" % bitOffset
            if byteProps[offsetStr]["type"] == "scalar":
                bitSize = byteProps[offsetStr]["size"]
                bitLetter = ""
                # unsigned
                if bitSize == "U08": bitLetter = "B"
                if bitSize == "U16": bitLetter = "H"
                if bitSize == "U32": bitLetter = "L"
                # signed
                if bitSize == "S08": bitLetter = "b"
                if bitSize == "S16": bitLetter = "h"
                if bitSize == "S32": bitLetter = "l"
                unpkStr =+ bitLetter

        pText = "  ini : %s%s data points in %s bytes" % (
                self.endc, len(unpkStr) - 1, struct.calcsize(unpkStr))
        ttyP(4, pText)

        # make results available
        self.byteLocs = byteLocs
        self.byteProps = byteProps
        self.byteMapUnpkStr = unpkStr

    def initIni(self, iniFile):
        if iniFile:
            # initialize .ini file for parsing of A command bytes
            self.genByteMappingA(iniFile)
            self.canPacketA()  # pre-can 'A' packet up-front

    def initSerial(self, serialLoc, baud=115200):
        try:
            self.ser = serial.Serial(port=serialLoc,
                                     baudrate=baud,
                                     write_timeout=0.05,
                                     timeout=0.05)
            ttyP(4, "  serial @ " + self.endc + serialLoc)
        except serial.SerialException:
            ttyP(4, "  no serial device available or busy!")
            print("    ", sys.exc_info()[0])<commit_msg>fixed an oops<commit_after>import sys
import time
import serial
import json
import struct
import binascii
import configparser
from datetime import datetime
from .ttyP import ttyP


class PyEfiTools:
    def __init__(self):
        self.endc = '\033[0m'  # Clear color and other terminal states

    def canPacketA(self, command=b'A'):
        """create crc32 and packet using pack"""
        packetCrc = binascii.crc32(command)
        self.packetA = struct.pack('>HcL', 1, command, packetCrc)

    # NOT USED kept cuz is only 3 lines and works - but no error checking
    def oldMethod(self):
        self.ser.write(b"A")
        print("old method: ", self.ser.read(212))

    def genByteMappingA(self, iniPath):
        config = configparser.ConfigParser()
        config.sections()
        config.read(iniPath)  # needs to suit your hardware!
        # need to implement a check for a bad config here
        byteLocs = list()  # list of bit positions, eventually sorted
        byteProps = dict()  # dict of bit properties
        self.metricNames = list()
        # copy ConfigParser object into dict, strip out garbage and
        # populate byteLocs, byteProps
        for key in config['OutputChannels']:
            props = config['OutputChannels'][key].split(",")
            props = [x.strip(' ') for x in props]
            props = [x.strip('"') for x in props]

            # if props[0] == "bits":
            # for scalar values set multiplier, bits dont have this
            # create hash from list
            if props[0] == "scalar":
                byteProps[props[2]] = {
                    "name": key,
                    "type": props[0],
                    "size": props[1],
                    "unit": props[3],
                }
                self.metricNames.append(key)
                byteLocs.append(int(props[2]))
                byteProps[props[2]]["multiplier"] = float(props[4])

        # Lets make an unpack string!
        unpkStr = ">"  # big endian!
        # returned string need to be in a precise order
        # hacky -- sorting strings returns 1, 10, 100, 2, etc,
        # so i used a sorted list as an index
        byteLocs = sorted(byteLocs)
        for bitOffset in byteLocs:
            offsetStr = "%s" % bitOffset
            if byteProps[offsetStr]["type"] == "scalar":
                bitSize = byteProps[offsetStr]["size"]
                bitLetter = ""
                # unsigned
                if bitSize == "U08": bitLetter = "B"
                if bitSize == "U16": bitLetter = "H"
                if bitSize == "U32": bitLetter = "L"
                # signed
                if bitSize == "S08": bitLetter = "b"
                if bitSize == "S16": bitLetter = "h"
                if bitSize == "S32": bitLetter = "l"
                unpkStr += bitLetter

        pText = "  ini : %s%s data points in %s bytes" % (
                self.endc, len(unpkStr) - 1, struct.calcsize(unpkStr))
        ttyP(4, pText)

        # make results available
        self.byteLocs = byteLocs
        self.byteProps = byteProps
        self.byteMapUnpkStr = unpkStr

    def initIni(self, iniFile):
        if iniFile:
            # initialize .ini file for parsing of A command bytes
            self.genByteMappingA(iniFile)
            self.canPacketA()  # pre-can 'A' packet up-front

    def initSerial(self, serialLoc, baud=115200):
        try:
            self.ser = serial.Serial(port=serialLoc,
                                     baudrate=baud,
                                     write_timeout=0.05,
                                     timeout=0.05)
            ttyP(4, "  serial @ " + self.endc + serialLoc)
        except serial.SerialException:
            ttyP(4, "  no serial device available or busy!")
            print("    ", sys.exc_info()[0])
{'fraction_non_alphanumeric': 0.10453235525281634, 'fraction_numerical': 0.015195179460309143, 'mean_word_length': 1.7161152614727855, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5902968', 'n_tokens_mistral': 2277, 'n_tokens_neox': 2116, 'n_words': 645}