<commit_before>from hwt.hdl.constants import INTF_DIRECTION
from hwt.synthesizer.unit import Unit
from hwt.synthesizer.param import evalParam, Param


class UnitWrapper(Unit):
    """
    Class which creates wrapper around original unit instance,
    original unit will be sotred inside as subunit named baseUnit
    
    :note: This is also example of lazy loaded interfaces
        and generating of external interfaces based on internal stucture.
    """

    def __init__(self, baseUnit):
        super(UnitWrapper, self).__init__()
        self._baseUnit = baseUnit

    def _copyParamsAndInterfaces(self):
        for p in self._baseUnit._params:
            myP = Param(evalParam(p))
            self._registerParameter(p.name, myP)
            object.__setattr__(self, myP.name, myP)

        origToWrapInfMap = {}

        for intf in self.baseUnit._interfaces:
            # clone interface
            myIntf = intf._clone()
            # subinterfaces are not instanciated yet
            #myIntf._direction = intf._direction
            myIntf._direction = INTF_DIRECTION.opposite(intf._direction)

            self._registerInterface(intf._name, myIntf)
            object.__setattr__(self, intf._name, myIntf)

            origToWrapInfMap[intf] = myIntf

        for i in self._interfaces:
            self._loadInterface(i, True)

        return origToWrapInfMap

    def _getDefaultName(self):
        return self._baseUnit.__class__.__name__

    def _lazyLoadParamsAndInterfaces(self):
        self._ctx.params = self._buildParams()

        # prepare signals for interfaces
        for i in self._interfaces:
            assert i._isExtern
            signals = i._signalsForInterface(self._ctx)
            self._externInterf.extend(signals)

    def _connectBaseUnitToThisWrap(self, origToWrapInfMap):
        for baseIntf, wrapIntf in origToWrapInfMap.items():
            if baseIntf._direction is INTF_DIRECTION.MASTER:
                if isinstance(wrapIntf, list):
                    for i, _wrapIntf in enumerate(wrapIntf):
                        _wrapIntf(baseIntf[i])
                else:
                    wrapIntf(baseIntf)
            else:
                if isinstance(wrapIntf, list):
                    for i, _wrapIntf in enumerate(wrapIntf):
                        baseIntf[i](_wrapIntf)
                else:
                    baseIntf(wrapIntf)

    def _impl(self):
        self.baseUnit = self._baseUnit
        origToWrapInfMap = self._copyParamsAndInterfaces()
        self._lazyLoadParamsAndInterfaces()
        self._connectBaseUnitToThisWrap(origToWrapInfMap)
<commit_msg>codestyle<commit_after>from hwt.hdl.constants import INTF_DIRECTION
from hwt.synthesizer.unit import Unit
from hwt.synthesizer.param import evalParam, Param


class UnitWrapper(Unit):
    """
    Class which creates wrapper around original unit instance,
    original unit will be sotred inside as subunit named baseUnit

    :note: This is also example of lazy loaded interfaces
        and generating of external interfaces based on internal stucture.
    """

    def __init__(self, baseUnit):
        super(UnitWrapper, self).__init__()
        self._baseUnit = baseUnit

    def _copyParamsAndInterfaces(self):
        for p in self._baseUnit._params:
            myP = Param(evalParam(p))
            self._registerParameter(p.name, myP)
            object.__setattr__(self, myP.name, myP)

        origToWrapInfMap = {}

        for intf in self.baseUnit._interfaces:
            # clone interface
            myIntf = intf._clone()
            # subinterfaces are not instanciated yet
            # myIntf._direction = intf._direction
            myIntf._direction = INTF_DIRECTION.opposite(intf._direction)

            self._registerInterface(intf._name, myIntf)
            object.__setattr__(self, intf._name, myIntf)

            origToWrapInfMap[intf] = myIntf

        for i in self._interfaces:
            self._loadInterface(i, True)

        return origToWrapInfMap

    def _getDefaultName(self):
        return self._baseUnit.__class__.__name__

    def _lazyLoadParamsAndInterfaces(self):
        self._ctx.params = self._buildParams()

        # prepare signals for interfaces
        for i in self._interfaces:
            assert i._isExtern
            signals = i._signalsForInterface(self._ctx)
            self._externInterf.extend(signals)

    def _connectBaseUnitToThisWrap(self, origToWrapInfMap):
        for baseIntf, wrapIntf in origToWrapInfMap.items():
            if baseIntf._direction is INTF_DIRECTION.MASTER:
                if isinstance(wrapIntf, list):
                    for i, _wrapIntf in enumerate(wrapIntf):
                        _wrapIntf(baseIntf[i])
                else:
                    wrapIntf(baseIntf)
            else:
                if isinstance(wrapIntf, list):
                    for i, _wrapIntf in enumerate(wrapIntf):
                        baseIntf[i](_wrapIntf)
                else:
                    baseIntf(wrapIntf)

    def _impl(self):
        self.baseUnit = self._baseUnit
        origToWrapInfMap = self._copyParamsAndInterfaces()
        self._lazyLoadParamsAndInterfaces()
        self._connectBaseUnitToThisWrap(origToWrapInfMap)
{'fraction_non_alphanumeric': 0.07083015993907082, 'fraction_numerical': 0.0, 'mean_word_length': 2.1701870850935427, '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': '2874862', 'n_tokens_mistral': 1504, 'n_tokens_neox': 1442, 'n_words': 336}