def LoadSingleDispatch(dispatch_info, open_fn=None): """Load a dispatch.yaml file or string and return a DispatchInfoExternal. Args: dispatch_info: The contents of a dispatch.yaml file as a string, or an open file object. open_fn: Function for opening files. Unused here, needed to provide a polymorphic API used by appcfg.py yaml parsing. Returns: A DispatchInfoExternal instance which represents the contents of the parsed yaml file. Raises: MalformedDispatchConfigurationError: The yaml file contains multiple dispatch sections. yaml_errors.EventError: An error occured while parsing the yaml file. """ builder = yaml_object.ObjectBuilder(DispatchInfoExternal) handler = yaml_builder.BuilderHandler(builder) listener = yaml_listener.EventListener(handler) listener.Parse(dispatch_info) parsed_yaml = handler.GetResults() if not parsed_yaml: return DispatchInfoExternal() if len(parsed_yaml) > 1: raise MalformedDispatchConfigurationError( 'Multiple dispatch: sections ' 'in configuration.') return parsed_yaml[0]
def LoadPagespeedEntry(pagespeed_entry, open_fn=None): """Load a yaml file or string and return a PagespeedEntry. Args: pagespeed_entry: The contents of a pagespeed entry from a yaml file as a string, or an open file object. open_fn: Function for opening files. Unused. Returns: A PagespeedEntry instance which represents the contents of the parsed yaml. Raises: yaml_errors.EventError: An error occured while parsing the yaml. MalformedPagespeedConfiguration: The configuration is parseable but invalid. """ builder = yaml_object.ObjectBuilder(PagespeedEntry) handler = yaml_builder.BuilderHandler(builder) listener = yaml_listener.EventListener(handler) listener.Parse(pagespeed_entry) parsed_yaml = handler.GetResults() if not parsed_yaml: return PagespeedEntry() if len(parsed_yaml) > 1: raise MalformedPagespeedConfiguration( 'Multiple configuration sections in the yaml') return parsed_yaml[0]
def LoadSingleDos(dos_info, open_fn=None): """Load a dos.yaml file or string and return a DosInfoExternal object. Args: dos_info: The contents of a dos.yaml file as a string, or an open file object. open_fn: Function for opening files. Unused. Returns: A DosInfoExternal instance which represents the contents of the parsed yaml file. Raises: MalformedDosConfiguration: The yaml file contains multiple blacklist sections. yaml_errors.EventError: An error occured while parsing the yaml file. """ builder = yaml_object.ObjectBuilder(DosInfoExternal) handler = yaml_builder.BuilderHandler(builder) listener = yaml_listener.EventListener(handler) listener.Parse(dos_info) parsed_yaml = handler.GetResults() if not parsed_yaml: return DosInfoExternal() if len(parsed_yaml) > 1: raise MalformedDosConfiguration('Multiple blacklist: sections ' 'in configuration.') return parsed_yaml[0]
def LoadSingleClientDeployInfo(client_deploy_info): """Returns a ClientDeployInfoExternal from a deploy_info.yaml file or string. Args: client_deploy_info: The contents of a client_deploy_info.yaml file or string, or an open file object. Returns: A ClientDeployInfoExternal instance which represents the contents of the parsed yaml. Raises: EmptyYaml: when there are no documents in yaml. MultipleClientDeployInfo: when there are multiple documents in yaml. yaml_errors.EventError: when an error occurs while parsing the yaml. """ builder = yaml_object.ObjectBuilder(ClientDeployInfoExternal) handler = yaml_builder.BuilderHandler(builder) listener = yaml_listener.EventListener(handler) listener.Parse(client_deploy_info) parsed_yaml = handler.GetResults() if not parsed_yaml: raise EmptyYaml() if len(parsed_yaml) > 1: raise MultipleClientDeployInfo() return parsed_yaml[0]
def LoadSingleCron(cron_info, open_fn=None): """Load a cron.yaml file or string and return a CronInfoExternal object.""" builder = yaml_object.ObjectBuilder(CronInfoExternal) handler = yaml_builder.BuilderHandler(builder) listener = yaml_listener.EventListener(handler) listener.Parse(cron_info) cron_info_result = handler.GetResults() if len(cron_info_result) < 1: raise MalformedCronfigurationFile('Empty cron configuration.') if len(cron_info_result) > 1: raise MalformedCronfigurationFile('Multiple cron sections ' 'in configuration.') return cron_info_result[0]
def LoadSingleDispatch(dispatch_info, open_fn=None): """Load a dispatch.yaml file or string and return a DispatchInfoExternal. Args: dispatch_info: The contents of a dispatch.yaml file as a string, or an open file object. open_fn: Function for opening files. Unused here, needed to provide a polymorphic API used by appcfg.py yaml parsing. Returns: A DispatchInfoExternal instance which represents the contents of the parsed yaml file. Raises: MalformedDispatchConfigurationError: The yaml file contains multiple dispatch sections or is missing a required value. yaml_errors.EventError: An error occured while parsing the yaml file. """ builder = yaml_object.ObjectBuilder(DispatchInfoExternal) handler = yaml_builder.BuilderHandler(builder) listener = yaml_listener.EventListener(handler) listener.Parse(dispatch_info) parsed_yaml = handler.GetResults() if not parsed_yaml: return DispatchInfoExternal() if len(parsed_yaml) > 1: raise MalformedDispatchConfigurationError( 'Multiple dispatch: sections ' 'in configuration.') # The validation framework doesn't allow validating multiple fields at once, # so we have to check here. dispatch_info_external = parsed_yaml[0] for dispatch in getattr(dispatch_info_external, DISPATCH) or []: if dispatch.module and dispatch.service: raise MalformedDispatchConfigurationError( 'Both module: and service: in dispatch entry. Please use only one.' ) if not (dispatch.module or dispatch.service): raise MalformedDispatchConfigurationError( "Missing required value 'service'.") # The important part is that just 'module' is sent to the API for now; # eventually, we'll switch this to just check service. dispatch.module = dispatch.module or dispatch.service dispatch.service = None return dispatch_info_external
def BuildObjects(default_class, stream, loader=yaml.loader.SafeLoader): """Build objects from stream. Handles the basic case of loading all the objects from a stream. Args: default_class: Class that is instantiated upon the detection of a new document. An instance of this class will act as the document itself. stream: String document or open file object to process as per the yaml.parse method. Any object that implements a 'read()' method which returns a string document will work with the YAML parser. loader_class: Used for dependency injection. Returns: List of default_class instances parsed from the stream. """ builder = ObjectBuilder(default_class) handler = yaml_builder.BuilderHandler(builder) listener = yaml_listener.EventListener(handler) listener.Parse(stream, loader) return handler.GetResults()
def LoadSingleQueue(queue_info, open_fn=None): """Loads a `queue.yaml` file/string and returns a `QueueInfoExternal` object. Args: queue_info: The contents of a `queue.yaml` file, as a string. open_fn: Function for opening files. Unused. Returns: A `QueueInfoExternal` object. """ builder = yaml_object.ObjectBuilder(QueueInfoExternal) handler = yaml_builder.BuilderHandler(builder) listener = yaml_listener.EventListener(handler) listener.Parse(queue_info) queue_info = handler.GetResults() if len(queue_info) < 1: raise MalformedQueueConfiguration('Empty queue configuration.') if len(queue_info) > 1: raise MalformedQueueConfiguration('Multiple queue: sections ' 'in configuration.') return queue_info[0]