<commit_before>from collections import OrderedDict
import itertools
import json
import os

from opentrons_sdk import containers
from opentrons_sdk.labware import instruments
from opentrons_sdk.robot import Robot
from opentrons_sdk.util import vector


class JSONProcessorValidationError(Exception):
    pass


class JSONProcessorRuntimeError(Exception):
    pass


class BaseHandler(object):
    def __init__(self):
        self.errors = []
        self.warnings = []

    def validate(self):
        raise NotImplementedError

    def perform(self):
        raise NotImplementedError

    def submit(self):
        self.validate()
        return self.perform()


class JSONProtocolProcessor(BaseHandler):
    def __init__(self, protocol: (str, OrderedDict)):
        super().__init__()
        self._protocol_input = protocol
        self.protocol = self.read_protocol(protocol)

        self.head = None
        self.deck = None

    def get_protocol_from_file(self, path):
        with open(path) as f:
            return json.load(f, object_pairs_hook=OrderedDict)

    def read_protocol(self, protocol):
        if isinstance(protocol, str):
            if os.path.isfile(protocol):
                return self.get_protocol_from_file(protocol)
            return json.loads(protocol, object_pairs_hook=OrderedDict)
        elif isinstance(protocol, OrderedDict):
            return protocol
        raise Exception('Protocol must be a file, json string, or OrderedDict')


    def validate(self):
        errors = []
        warnings = []

        # Process errors
        if 'head' not in self.protocol:
            errors.append('JSON Protocol is missing "HEAD" section')
        if 'deck' not in self.protocol:
            errors.append('JSON Protocol is missing "DECK" section')
        if 'instructions' not in self.protocol:
            errors.append('JSON Protocol is missing "INSTRUCTIONS" section')

        # Process warnings
        if 'ingredients' not in self.protocol:
            warnings.append(
                'JSON Protocol section "Ingredients" will not be used'
            )

        self.warnings.extend(warnings)
        self.errors.extend(errors)

        if errors:
            raise JSONProcessorValidationError('Errors encountered compiling JSON')


    def perform(self):
        self.process_deck()

        # deck, head, instructions = None
        # try:
        #     deck_processor = JSONDeckProcessor(self.protocol['deck'])
        #     deck = deck_processor.submit()
        # except JSONProcessorValidationError:
        #     self.errors.extend(deck_processor.errors)
        #     self.warnings.extend(deck_processor.warnings)

    def process_deck(self):
        """
        "deck": {
            "p200-rack": {
                "labware": "tiprack-200ul",
                "slot" : "A1"
            },
            ".75 mL Tube Rack": {
                "labware": "tube-rack-.75ml",
                "slot" : "C1"
            },
            "trash": {
                "labware": "point",
                "slot" : "B2"
            }
        }
        :param protocol:
        :param deck_info:
        :return:
        """

        deck_info = self.protocol['deck']

        deck_data = {}
        for container_label, definition in deck_info.items():
            try:
                container_type = definition.get('labware')<commit_msg>clean up<commit_after>from collections import OrderedDict
import itertools
import json
import os

from opentrons_sdk import containers
from opentrons_sdk.labware import instruments
from opentrons_sdk.robot import Robot
from opentrons_sdk.util import vector


class JSONProcessorValidationError(Exception):
    pass


class JSONProcessorRuntimeError(Exception):
    pass


class JSONProtocolProcessor(object):
    def __init__(self, protocol: (str, OrderedDict)):
        self.errors = []
        self.warnings = []
        self.deck = None
        self.head = None
        self.protocol = self.read_protocol(protocol)

    def get_protocol_from_file(self, path):
        with open(path) as f:
            return json.load(f, object_pairs_hook=OrderedDict)

    def read_protocol(self, protocol):
        if isinstance(protocol, str):
            if os.path.isfile(protocol):
                return self.get_protocol_from_file(protocol)
            return json.loads(protocol, object_pairs_hook=OrderedDict)
        elif isinstance(protocol, OrderedDict):
            return protocol
        raise Exception('Protocol must be a file, json string, or OrderedDict')

    def validate(self):
        errors = []
        warnings = []

        # Process errors
        if 'head' not in self.protocol:
            errors.append('JSON Protocol is missing "HEAD" section')
        if 'deck' not in self.protocol:
            errors.append('JSON Protocol is missing "DECK" section')
        if 'instructions' not in self.protocol:
            errors.append('JSON Protocol is missing "INSTRUCTIONS" section')

        # Process warnings
        if 'ingredients' not in self.protocol:
            warnings.append(
                'JSON Protocol section "Ingredients" will not be used'
            )

        self.warnings.extend(warnings)
        self.errors.extend(errors)

        if errors:
            raise JSONProcessorValidationError('Errors encountered compiling JSON')

    def process(self):
        try:
            self.process_deck()
        except JSONProcessorRuntimeError as e:
            self.errors.extend(
                'Failed to process protocol "deck". Error: {}'
                .format(str(e))
            )

        try:
                self.process_head()
        except JSONProcessorRuntimeError as e:
            self.errors.extend(
                'Failed to process protocol "head". Error: {}'
                .format(str(e))
            )

        try:
            self.process_instructions()
        except JSONProcessorRuntimeError as e:
            self.errors.extend(
                'Failed to process protocol "instructions". Error: {}'
                .format(str(e))
            )

    def process_deck(self):
        """
        "deck": {
            "p200-rack": {
                "labware": "tiprack-200ul",
                "slot" : "A1"
            },
            ".75 mL Tube Rack": {
                "labware": "tube-rack-.75ml",
                "slot" : "C1"
            },
            "trash": {
                "labware": "point",
                "slot" : "B2"
            }
        }
        :param protocol:
        :param deck_info:
        :return:
        """

        deck_info = self.protocol['deck']

        deck_data = {}
        for container_label, definition in deck_info.items():
            try:
                container_type = definition.get('labware')
{'fraction_non_alphanumeric': 0.09322533136966127, 'fraction_numerical': 0.003829160530191458, 'mean_word_length': 1.9322107081174438, 'pattern_counts': {'":': 14, '<': 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': '243200', 'n_tokens_mistral': 1852, 'n_tokens_neox': 1766, 'n_words': 452}