# Path: media_nommer/utils/mod_importing.py
# def import_class_from_module_string(fqpn_str):
#     """
#     Given a FQPN for a class, import and return the class.
#     
#     :param str fqpn_str: The FQPN to the class to import.
#     :returns: The class given in the FQPN.
#     """
#     components = fqpn_str.split('.')
#     # Generate a FQPN with everything but the class name.
#     nom_module_str = '.'.join(components[:-1])
#     # Just the target module to import.
#     class_name = components[-1]
#     # Import the class's module and the class itself.
#     nommer = __import__(nom_module_str, globals(), locals(), [class_name])
# 
#     try:
#         return getattr(nommer, class_name)
#     except AttributeError:
#         raise ImportError('No class named %s' % class_name)
# 
# Path: media_nommer/utils/uri_parsing.py
# def get_values_from_media_uri(uri_str):
#     """
#     Given a valid URI string, break it up into its component pieces like
#     protocol, hostname, and path, along with some optional elements like 
#     username, password, and port. All of these values are returned in a dict.
#     
#     The following keys should always be in the dict:
#     * protocol
#     * host
#     * path
#     
#     The following are set when present, but absent when not:
#     * port
#     * username
#     * password
#     
#     :param str uri_str: A valid URI string.
#     :returns: A dict of strings.
#     """
#     # The dict that will be later returned.
#     values = {}
# 
#     dslash_split = uri_str.split('://', 1)
#     values['protocol'] = dslash_split[0]
#     try:
#         url_str = dslash_split[1]
#     except IndexError:
#         # If this split fails, we've definitely forgotten the protocol.
#         msg = 'No protocol specified. Pre-pend "proto://" to your current ' \
#               'value of "%s", where "proto" is your choice of protocol.' % uri_str
#         raise InvalidUri(msg)
# 
#     # An '@' symbol indicates the presence of a username and/or username+pass.
#     has_auth_details = '@' in url_str
#     if has_auth_details:
#         auth_host_split = url_str.split('@', 1)
#         # Should be just the username or username@pass.
#         auth_details = auth_host_split[0]
# 
#         # A colon is used to separate username from passwords.
#         has_password = ':' in auth_details
#         if has_password:
#             user_pass_split = auth_details.split(':', 1)
#             values['username'] = user_pass_split[0]
#             values['password'] = user_pass_split[1]
#         else:
#             values['username'] = auth_details
# 
#         # Seperated the host/path from auth details. 
#         host_and_path = auth_host_split[1]
#     else:
#         host_and_path = url_str
# 
#     # Split the host and path up.
#     host_path_split = host_and_path.split('/', 1)
#     values['host'] = host_path_split[0]
# 
#     try:
#         values['path'] = host_path_split[1]
#     except IndexError:
#         # This means that no trailing slash was present in the URI str after
#         # the host was specified. Assume they mean the root path.
#         values['path'] = '/'
# 
#     if ':' in values['host']:
#         # Looks like they specified a port.
#         host_port_split = values['host'].split(':')
#         values['host'] = host_port_split[0]
#         values['port'] = host_port_split[1]
# 
#     return values
# 
# class InvalidUri(BaseException):
#     """
#     Raised when an invalid URI is passed to a function that consumes URIs.
#     Make sure to instantiate with a helpful error message.
#     """
#     pass


import unittest
from media_nommer.utils.conf import SettingsStore
from media_nommer.utils.mod_importing import import_class_from_module_string
from media_nommer.utils.uri_parsing import get_values_from_media_uri, InvalidUri
        self.fake_defaults = FakeSettingsObj()
        self.settings = SettingsStore(self.fake_defaults)

    def test_overriding(self):
        """
        Tests overriding default settings with the user-specified settings.
        """
        # Pretend that this is the global default for some arbitrary setting.
        self.settings.SOME_SETTING = 1
        # Pretend like this is the user's settings module.
        user_vals = FakeSettingsObj()
        # The user has changed some setting to a non-default value.
        user_vals.SOME_SETTING = 2
        user_settings = SettingsStore(user_vals)
        # Now load the user's settings over the defaults, like the daemon does.
        self.settings.update_settings_from_module(user_settings)
        # Make sure the new setting matches the user's values.
        self.assert_(self.settings.SOME_SETTING == user_settings.SOME_SETTING)

class ModImportingTests(unittest.TestCase):
    def test_valid_import(self):
        """
        Test an import that should be valid.
        """
        import_class_from_module_string('media_nommer.client.api.APIConnection')

    def test_invalid_import(self):
        """
        Test some invalid imports to make sure exception raising is good.
        """

# Path: media_nommer/utils/mod_importing.py
# def import_class_from_module_string(fqpn_str):
#     """
#     Given a FQPN for a class, import and return the class.
#     
#     :param str fqpn_str: The FQPN to the class to import.
#     :returns: The class given in the FQPN.
#     """
#     components = fqpn_str.split('.')
#     # Generate a FQPN with everything but the class name.
#     nom_module_str = '.'.join(components[:-1])
#     # Just the target module to import.
#     class_name = components[-1]
#     # Import the class's module and the class itself.
#     nommer = __import__(nom_module_str, globals(), locals(), [class_name])
# 
#     try:
#         return getattr(nommer, class_name)
#     except AttributeError:
#         raise ImportError('No class named %s' % class_name)
# 
# Path: media_nommer/utils/uri_parsing.py
# def get_values_from_media_uri(uri_str):
#     """
#     Given a valid URI string, break it up into its component pieces like
#     protocol, hostname, and path, along with some optional elements like 
#     username, password, and port. All of these values are returned in a dict.
#     
#     The following keys should always be in the dict:
#     * protocol
#     * host
#     * path
#     
#     The following are set when present, but absent when not:
#     * port
#     * username
#     * password
#     
#     :param str uri_str: A valid URI string.
#     :returns: A dict of strings.
#     """
#     # The dict that will be later returned.
#     values = {}
# 
#     dslash_split = uri_str.split('://', 1)
#     values['protocol'] = dslash_split[0]
#     try:
#         url_str = dslash_split[1]
#     except IndexError:
#         # If this split fails, we've definitely forgotten the protocol.
#         msg = 'No protocol specified. Pre-pend "proto://" to your current ' \
#               'value of "%s", where "proto" is your choice of protocol.' % uri_str
#         raise InvalidUri(msg)
# 
#     # An '@' symbol indicates the presence of a username and/or username+pass.
#     has_auth_details = '@' in url_str
#     if has_auth_details:
#         auth_host_split = url_str.split('@', 1)
#         # Should be just the username or username@pass.
#         auth_details = auth_host_split[0]
# 
#         # A colon is used to separate username from passwords.
#         has_password = ':' in auth_details
#         if has_password:
#             user_pass_split = auth_details.split(':', 1)
#             values['username'] = user_pass_split[0]
#             values['password'] = user_pass_split[1]
#         else:
#             values['username'] = auth_details
# 
#         # Seperated the host/path from auth details. 
#         host_and_path = auth_host_split[1]
#     else:
#         host_and_path = url_str
# 
#     # Split the host and path up.
#     host_path_split = host_and_path.split('/', 1)
#     values['host'] = host_path_split[0]
# 
#     try:
#         values['path'] = host_path_split[1]
#     except IndexError:
#         # This means that no trailing slash was present in the URI str after
#         # the host was specified. Assume they mean the root path.
#         values['path'] = '/'
# 
#     if ':' in values['host']:
#         # Looks like they specified a port.
#         host_port_split = values['host'].split(':')
#         values['host'] = host_port_split[0]
#         values['port'] = host_port_split[1]
# 
#     return values
# 
# class InvalidUri(BaseException):
#     """
#     Raised when an invalid URI is passed to a function that consumes URIs.
#     Make sure to instantiate with a helpful error message.
#     """
#     pass


# Path: media_nommer/utils/tests.py
import unittest
from media_nommer.utils.conf import SettingsStore
from media_nommer.utils.mod_importing import import_class_from_module_string
from media_nommer.utils.uri_parsing import get_values_from_media_uri, InvalidUri

        self.fake_defaults = FakeSettingsObj()
        self.settings = SettingsStore(self.fake_defaults)

    def test_overriding(self):
        """
        Tests overriding default settings with the user-specified settings.
        """
        # Pretend that this is the global default for some arbitrary setting.
        self.settings.SOME_SETTING = 1
        # Pretend like this is the user's settings module.
        user_vals = FakeSettingsObj()
        # The user has changed some setting to a non-default value.
        user_vals.SOME_SETTING = 2
        user_settings = SettingsStore(user_vals)
        # Now load the user's settings over the defaults, like the daemon does.
        self.settings.update_settings_from_module(user_settings)
        # Make sure the new setting matches the user's values.
        self.assert_(self.settings.SOME_SETTING == user_settings.SOME_SETTING)

class ModImportingTests(unittest.TestCase):
    def test_valid_import(self):
        """
        Test an import that should be valid.
        """
        import_class_from_module_string('media_nommer.client.api.APIConnection')

    def test_invalid_import(self):
        """
        Test some invalid imports to make sure exception raising is good.
        """

        self.assertRaises(ImportError, import_class_from_module_string,