<commit_before># Copyright 2016-2017 IBM Corp. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
The `zhmcclient_mock` package provides a faked HMC with all resources that are
relevant for the `zhmcclient` package. The faked HMC is implemented as a
local Python object and maintains its resource state across operations.
"""

from __future__ import absolute_import

try:
    from collections import OrderedDict
except ImportError:
    from ordereddict import OrderedDict
import six
import re
import copy

from ._idpool import IdPool
from zhmcclient._utils import repr_dict, repr_manager

__all__ = ['FakedBaseResource', 'FakedBaseManager', 'FakedHmc',
           'FakedActivationProfileManager', 'FakedActivationProfile',
           'FakedAdapterManager', 'FakedAdapter',
           'FakedCpcManager', 'FakedCpc',
           'FakedHbaManager', 'FakedHba',
           'FakedLparManager', 'FakedLpar',
           'FakedNicManager', 'FakedNic',
           'FakedPartitionManager', 'FakedPartition',
           'FakedPortManager', 'FakedPort',
           'FakedVirtualFunctionManager', 'FakedVirtualFunction',
           'FakedVirtualSwitchManager', 'FakedVirtualSwitch',
           ]


class FakedBaseResource(object):
    """
    A base class for faked resource classes in the faked HMC.
    """

    def __init__(self, manager, properties):
        self._manager = manager  # May be None
        self._properties = copy.deepcopy(properties) \
            if properties is not None else None

        if self.manager and self.properties:

            if self.manager.oid_prop not in self.properties:
                new_oid = self.manager._new_oid()
                self.properties[self.manager.oid_prop] = new_oid
            self._oid = self.properties[self.manager.oid_prop]

            if self.manager.uri_prop not in self.properties:
                new_uri = self.manager.base_uri + '/' + self.oid
                self.properties[self.manager.uri_prop] = new_uri
            self._uri = self.properties[self.manager.uri_prop]

        else:
            self._oid = None
            self._uri = None

    def __repr__(self):
        """
        Return a string with the state of this faked resource, for debug
        purposes.

        Note that the derived faked resource classes that have child resources
        have their own __repr__() methods, because only they know which child<commit_msg>Mock: Fixed FakedBaseResource init when properties are empty.<commit_after># Copyright 2016-2017 IBM Corp. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
The `zhmcclient_mock` package provides a faked HMC with all resources that are
relevant for the `zhmcclient` package. The faked HMC is implemented as a
local Python object and maintains its resource state across operations.
"""

from __future__ import absolute_import

try:
    from collections import OrderedDict
except ImportError:
    from ordereddict import OrderedDict
import six
import re
import copy

from ._idpool import IdPool
from zhmcclient._utils import repr_dict, repr_manager

__all__ = ['FakedBaseResource', 'FakedBaseManager', 'FakedHmc',
           'FakedActivationProfileManager', 'FakedActivationProfile',
           'FakedAdapterManager', 'FakedAdapter',
           'FakedCpcManager', 'FakedCpc',
           'FakedHbaManager', 'FakedHba',
           'FakedLparManager', 'FakedLpar',
           'FakedNicManager', 'FakedNic',
           'FakedPartitionManager', 'FakedPartition',
           'FakedPortManager', 'FakedPort',
           'FakedVirtualFunctionManager', 'FakedVirtualFunction',
           'FakedVirtualSwitchManager', 'FakedVirtualSwitch',
           ]


class FakedBaseResource(object):
    """
    A base class for faked resource classes in the faked HMC.
    """

    def __init__(self, manager, properties):
        self._manager = manager  # May be None
        self._properties = copy.deepcopy(properties) \
            if properties is not None else {}

        if self.manager:

            if self.manager.oid_prop not in self.properties:
                new_oid = self.manager._new_oid()
                self.properties[self.manager.oid_prop] = new_oid
            self._oid = self.properties[self.manager.oid_prop]

            if self.manager.uri_prop not in self.properties:
                new_uri = self.manager.base_uri + '/' + self.oid
                self.properties[self.manager.uri_prop] = new_uri
            self._uri = self.properties[self.manager.uri_prop]

        else:
            self._oid = None
            self._uri = None

    def __repr__(self):
        """
        Return a string with the state of this faked resource, for debug
        purposes.

        Note that the derived faked resource classes that have child resources
        have their own __repr__() methods, because only they know which child
{'fraction_non_alphanumeric': 0.07649063032367973, 'fraction_numerical': 0.0040885860306643955, 'mean_word_length': 2.903590425531915, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3591148', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1537, 'n_words': 551}