Exemple #1
0
def obtain_from_python_path(name):
    """ obtain_from_python_path(name) -> whatever :-)

    Given a full str name like 'os.path.sep', this function returns whatever
    it is in that path (for example, os.path.sep is a str, so this function
    will return "the value of that variable").
    """
    the_module = obtain_module(name)
    #If the_module is still None, the path is wrong
    if the_module == None:
        raise LoaderErrors.InvalidConfigurationError(
            """I can't import any module from "%s" """ % name)

    sequence_name_without = name[len(str(the_module).split("'")[1]) + 1:]
    current_block = the_module
    for i in sequence_name_without.split('.'):
        module_name = current_block.__name__ + '.' + i
        try:
            __import__(module_name, globals(), locals())
        except ImportError:
            pass  # foo.bar.MyClass will fail, but foo.bar will work

        try:
            current_block = getattr(current_block, i)
        except AttributeError:
            raise LoaderErrors.InvalidConfigurationError(
                """Couldn't find %s in module: %s""" % (i, name))
    return current_block
Exemple #2
0
def fill_creation(protocol_configuration, coord_address):
    creation_configuration = protocol_configuration.creation
    if len(creation_configuration.parameters) != 2:
        raise LoaderErrors.InvalidConfigurationError(
            "Unexpected number of parameters for InternetSocket creation, expected 2 and received %s"
            % len(creation_configuration.parameters))

    if creation_configuration.parameters[0].name == 'address':
        address = creation_configuration.parameters[0].value
    elif creation_configuration.parameters[1].name == 'address':
        address = creation_configuration.parameters[1].value
    else:
        raise LoaderErrors.InvalidConfigurationError(
            "Parameter 'address' not found in InternetSocket creation")
    if creation_configuration.parameters[0].name == 'port':
        port_value = creation_configuration.parameters[0].value
    elif creation_configuration.parameters[1].name == 'port':
        port_value = creation_configuration.parameters[1].value
    else:
        raise LoaderErrors.InvalidConfigurationError(
            "Parameter 'port' not found in InternetSocket creation")

    try:
        port_number = int(port_value)
    except ValueError:
        raise LoaderErrors.InvalidConfigurationError('Invalid port: %s' %
                                                     port_value)
    else:
        if port_number > 65536 or port_number < 0:
            raise LoaderErrors.InvalidConfigurationError('Invalid port: %s' %
                                                         port_value)

    protocol_configuration.filled_creation = ('InternetSocket',
                                              (address, port_number), {})
Exemple #3
0
def fill_coordinations(coordinations_configuration, address):
    if len(coordinations_configuration.coordinations) != 1:
        raise LoaderErrors.InvalidConfigurationError(
            "Unexpected number of coordinations for Direct, expected 1 and received %s"
            % len(coordinations_configuration.parameters))

    if len(coordinations_configuration.coordinations[0].parameters) != 0:
        raise LoaderErrors.InvalidConfigurationError(
            "Unexpected number of parameters for Direct coordinations, expected 0 and received %s"
            % len(coordinations_configuration[0].parameters))
    coordinations_configuration.filled_coordinations = [
        Address.from_coord_address(address)
    ]
    coordinations_configuration.filled_level = AccessLevel.instance
Exemple #4
0
def fill_creation(protocol_configuration, coord_address):
    creation_configuration = protocol_configuration.creation
    if len(creation_configuration.parameters) != 1:
        raise LoaderErrors.InvalidConfigurationError(
            "Unexpected number of parameters for UnixSocket creation, expected 1 and received %s"
            % len(creation_configuration.parameters))

    if creation_configuration.parameters[0].name == 'socketpath':
        socket_path = creation_configuration.parameters[0].value
    else:
        raise LoaderErrors.InvalidConfigurationError(
            "Parameter 'socketpath' not found in UnixSocket creation")

    protocol_configuration.filled_creation = ('UnixSocket', (socket_path, ),
                                              {})
    def _retrieve_variable(self, format_node):
        text_value = LoaderUtilities.obtain_text_safe(format_node)
        if text_value.count('::') != 1:
            raise LoaderErrors.InvalidConfigurationError(
                        'Unknown format: %s. module::variable expected' % text_value
                    )

        module_name, variable = text_value.split('::')
        module_inst = LoaderUtilities.obtain_from_python_path(module_name)
        try:
            return getattr(module_inst, variable)
        except AttributeError:
            raise LoaderErrors.InvalidConfigurationError(
                    'Unknown format: couldn\'t find %s in %s' % (variable, module_name)
                )
Exemple #6
0
def fill_creation(protocol_configuration, address):
    creation_configuration = protocol_configuration.creation
    if len(creation_configuration.parameters) != 0:
        raise LoaderErrors.InvalidConfigurationError(
            "Unexpected number of parameters for Direct creation, expected 0 and received %s"
            % len(creation_configuration.parameters))

    protocol_configuration.filled_creation = ('Direct', (address.address, ),
                                              {})
    def _parse_server_type_module(self, server_type_node):
        text_value = LoaderUtilities.obtain_text_safe(server_type_node)
        if text_value.count('::') != 1:
            raise LoaderErrors.InvalidConfigurationError(
                        'Unknown format: %s. module::variable expected' % text_value
                    )

        module_name, _ = text_value.split('::')
        module_inst = LoaderUtilities.obtain_from_python_path(module_name)
        return module_inst
Exemple #8
0
def fill_coordinations(coordinations_configuration, address):
    coordinations = []
    for coordination_configuration in coordinations_configuration.coordinations:
        if len(coordination_configuration.parameters) != 1:
            raise LoaderErrors.InvalidConfigurationError(
                "Unexpected number of parameters for SOAP coordinations, expected 1 and received %s"
                % len(coordination_configuration.parameters))
        parameter = coordination_configuration.parameters[0]
        if parameter.name != 'address':
            raise LoaderErrors.InvalidConfigurationError(
                "Parameter for SOAP coordinations should be address, found %s"
                % parameter.name)
        try:
            address = Address.Address(parameter.value)
        except Exception as e:
            raise LoaderErrors.InvalidConfigurationError(
                "Invalid address format for SOAP coordinations: %s" % e)
        coordinations.append(address)
    coordinations_configuration.filled_coordinations = coordinations
    coordinations_configuration.filled_level = AccessLevel.network
Exemple #9
0
def fill_coordinations(coordinations_configuration, address):
    if len(coordinations_configuration.coordinations) != 1:
        raise LoaderErrors.InvalidConfigurationError(
            "Unexpected number of coordinations for UnixSocket, expected 1 and received %s"
            % len(coordinations_configuration.parameters))

    if len(coordinations_configuration.coordinations[0].parameters) != 1:
        raise LoaderErrors.InvalidConfigurationError(
            "Unexpected number of parameters for UnixSocket coordinations, expected 1 and received %s"
            % len(coordinations_configuration[0].parameters))

    parameter = coordinations_configuration.coordinations[0].parameters[0]
    if parameter.name != 'sockpath':
        raise LoaderErrors.InvalidConfigurationError(
            "Unexpected parameter: expected 'sockpath' and found:" %
            parameter.name)

    coordinations_configuration.filled_coordinations = [
        Address.Address(address.machine_id, parameter.value)
    ]

    coordinations_configuration.filled_level = AccessLevel.machine
Exemple #10
0
def obtain_text_safe(text_node):
    text = [i for i in text_node.childNodes if isinstance(i, minidom.Text)]
    if len(text) == 0:
        raise LoaderErrors.InvalidConfigurationError("Empty Text Node")
    else:
        return text[0].nodeValue
 def _parse_dom(self, stream, file_path):
     try:
         return minidom.parse(stream)
     except Exception as e:
         raise LoaderErrors.InvalidConfigurationError("Couldn't load xml file %s: %s" % (file_path, e))
 def parse(self, directory, address = None):
     if not isinstance(self, GlobalParser) and address is None:
         raise LoaderErrors.InvalidConfigurationError( "Missing address parameter" )
     stream, file_path = self._retrieve_stream(directory)
     return self._parse_from_stream(stream, directory, file_path, address)
 def _retrieve_stream(self, directory):
     file_path = os.sep.join((directory,'configuration.xml'))
     try:
         return open(file_path), file_path
     except Exception as e:
         raise LoaderErrors.InvalidConfigurationError("Couldn't parse '%s': %s" % (file_path,e))