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), {})
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
def check_schema(self, xmlfile_path, xsdfile_path): if not LXML_AVAILABLE: global MESSAGE_SHOWN if not MESSAGE_SHOWN: msg = "The optional library 'lxml' is not available. The syntax of the configuration files will not be checked." print >> sys.stderr, msg log.log(SchemaChecker, log.level.Warning, msg) MESSAGE_SHOWN = True return xmlfile_content = self._read_xml_file(xmlfile_path) xsdfile_full_path = data_filename( os.path.join(module_directory, 'xsd', xsdfile_path)) try: xsdfile_content = self._read_xsd_file(xsdfile_full_path) except: msg = "The XSD file %s could not be loaded. The syntax of the configuration files will not be checked." % xsdfile_full_path print >> sys.stderr, msg log.log(SchemaChecker, log.level.Warning, msg) return try: sio_xsd = StringIO(xsdfile_content) xmlschema_doc = etree.parse(sio_xsd) xmlschema = etree.XMLSchema(xmlschema_doc) except Exception as e: log.log( SchemaChecker, log.level.Warning, 'Invalid syntax file configuration: File %s: %s' % (xsdfile_path, e)) log.log_exc(SchemaChecker, log.level.Info) raise LoaderErrors.InvalidSyntaxFileConfigurationError( e, xsdfile_path) try: sio_xml = StringIO(xmlfile_content) xml_doc = etree.parse(sio_xml) xmlschema.assertValid(xml_doc) except etree.DocumentInvalid as di: log.log( SchemaChecker, log.level.Warning, 'Not a valid configuration file. Check it with a XML Schema validator: File %s' % (xmlfile_path)) raise LoaderErrors.InvalidSyntaxFileConfigurationError( 'Not a valid configuration file. Check it with a XML Schema validator. %s' % di.args, xmlfile_path) except Exception as e: log.log( SchemaChecker, log.level.Warning, 'Invalid syntax file configuration: File %s: %s' % (xmlfile_path, e)) log.log_exc(SchemaChecker, log.level.Info) raise LoaderErrors.InvalidSyntaxFileConfigurationError( e, xmlfile_path)
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
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) )
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
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
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
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 find_node(file_name, root_node, node_name): nodes = find_nodes_at_least_one(file_name, root_node, node_name) if len(nodes) > 1: raise LoaderErrors.InvalidSyntaxFileConfigurationError( "Too many %s nodes in %s" % (file_name, node_name), file_name) return nodes[0]
def find_nodes_at_least_one(file_name, root_node, node_name): nodes = find_nodes(file_name, root_node, node_name) if len(nodes) == 0: raise LoaderErrors.InvalidSyntaxFileConfigurationError( "Couldn't find %s node in %s" % (file_name, node_name), file_name) return nodes
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))