<commit_before>"""Contains all the code for defining a configuration or component"""
from collections import OrderedDict

from BlockServer.config.containers import Group, Block, IOC, MetaData
from BlockServer.core.constants import GRP_NONE
from BlockServer.core.macros import PVPREFIX_MACRO


class Configuration(object):
    """The configuration class.

    Attributes:
        blocks (OrderedDict) : The blocks for the configuration
        macros (dict) : The EPICS/BlockServer related macros
        groups (OrderedDict) : The groups for the configuration
        iocs (OrderedDict) : The IOCs for the configuration
        meta (MetaData) : The meta-data for the configuration
        subconfigs (OrderedDict) : The components which are part of the configuration
        is_component (bool) : Whether it is actually a component
    """
    def __init__(self, macros):
        """Constructor.

        Args:
            macros (dict) : The dictionary containing the macros
        """
        # All dictionary keys are lowercase except iocs which is uppercase
        self.blocks = OrderedDict()
        self.macros = macros
        self.groups = OrderedDict()
        self.iocs = OrderedDict()
        self.meta = MetaData("")
        self.subconfigs = OrderedDict()
        self.is_component = False

    def add_block(self, name, pv, group=GRP_NONE, local=True, **kwargs):
        """Add a block to the configuration.

        Args:
            name (string) : The name for the new block
            pv (string) : The PV that is aliased
            group (string) : The group that the block belongs to [optional]
            local (bool) : Is the block local [optional]
            kwargs (dict) : Keyword arguments for the other parameters
        """
        # Check block name is unique
        if name.lower() in self.blocks.keys():
            raise Exception("Failed to add block as name is not unique")

        if local:
            # Strip off the MYPVPREFIX in the PV name (not the block name!)
            pv = pv.replace(self.macros[PVPREFIX_MACRO], "")
        self.blocks[name.lower()] = Block(name, pv, local, **kwargs)

        if group is not None:
            # If group does not exists then add it
            if not group.lower() in self.groups.keys():
                self.groups[group.lower()] = Group(group)
            self.groups[group.lower()].blocks.append(name)

    def add_ioc(self, name, subconfig=None, autostart=None, restart=None, macros=None, pvs=None, pvsets=None,
                simlevel=None):
        """Add an IOC to the configuration.

        Args:
            name (string) : The name of the IOC to add
            subconfig (string) : The component that the IOC belongs to [optional]
            autorestart (bool) : Should the IOC auto-restart [optional]
            restart (bool) :
            macros (dict) : The macro sets relating to the IOC [optional]
            pvs () :
            pvsets () : Any PV values that should be set at start up [optional]
            simlevel () : Sets the simulation level [optional]

        """
        # Only add it if it has not been added before
        if not name.upper() in self.iocs.keys():
            self.iocs[name.upper()] = IOC(name, autostart, restart, subconfig, macros, pvs, pvsets, simlevel)<commit_msg>Refs #667. Mistyped a parameter name in the docstring.<commit_after>"""Contains all the code for defining a configuration or component"""
from collections import OrderedDict

from BlockServer.config.containers import Group, Block, IOC, MetaData
from BlockServer.core.constants import GRP_NONE
from BlockServer.core.macros import PVPREFIX_MACRO


class Configuration(object):
    """The configuration class.

    Attributes:
        blocks (OrderedDict) : The blocks for the configuration
        macros (dict) : The EPICS/BlockServer related macros
        groups (OrderedDict) : The groups for the configuration
        iocs (OrderedDict) : The IOCs for the configuration
        meta (MetaData) : The meta-data for the configuration
        subconfigs (OrderedDict) : The components which are part of the configuration
        is_component (bool) : Whether it is actually a component
    """
    def __init__(self, macros):
        """Constructor.

        Args:
            macros (dict) : The dictionary containing the macros
        """
        # All dictionary keys are lowercase except iocs which is uppercase
        self.blocks = OrderedDict()
        self.macros = macros
        self.groups = OrderedDict()
        self.iocs = OrderedDict()
        self.meta = MetaData("")
        self.subconfigs = OrderedDict()
        self.is_component = False

    def add_block(self, name, pv, group=GRP_NONE, local=True, **kwargs):
        """Add a block to the configuration.

        Args:
            name (string) : The name for the new block
            pv (string) : The PV that is aliased
            group (string) : The group that the block belongs to [optional]
            local (bool) : Is the block local [optional]
            kwargs (dict) : Keyword arguments for the other parameters
        """
        # Check block name is unique
        if name.lower() in self.blocks.keys():
            raise Exception("Failed to add block as name is not unique")

        if local:
            # Strip off the MYPVPREFIX in the PV name (not the block name!)
            pv = pv.replace(self.macros[PVPREFIX_MACRO], "")
        self.blocks[name.lower()] = Block(name, pv, local, **kwargs)

        if group is not None:
            # If group does not exists then add it
            if not group.lower() in self.groups.keys():
                self.groups[group.lower()] = Group(group)
            self.groups[group.lower()].blocks.append(name)

    def add_ioc(self, name, subconfig=None, autostart=None, restart=None, macros=None, pvs=None, pvsets=None,
                simlevel=None):
        """Add an IOC to the configuration.

        Args:
            name (string) : The name of the IOC to add
            subconfig (string) : The component that the IOC belongs to [optional]
            autostart (bool) : Should the IOC automatically start [optional]
            restart (bool) : Should the IOC automatically restart [optional]
            macros (dict) : The macro sets relating to the IOC [optional]
            pvs () :
            pvsets () : Any PV values that should be set at start up [optional]
            simlevel () : Sets the simulation level [optional]

        """
        # Only add it if it has not been added before
        if not name.upper() in self.iocs.keys():
            self.iocs[name.upper()] = IOC(name, autostart, restart, subconfig, macros, pvs, pvsets, simlevel)
{'fraction_non_alphanumeric': 0.08693063411729682, 'fraction_numerical': 0.00044656147662994937, 'mean_word_length': 2.367919799498747, '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': '2345542', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1760, 'n_words': 686}