示例#1
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.')

    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'.")

        dispatch.module = dispatch.module or dispatch.service
        dispatch.service = None
    return dispatch_info_external
示例#2
0
def LoadBackendEntry(backend_entry):
  """Parses a BackendEntry object from a string.

  Args:
    backend_entry: a backend entry, as a string

  Returns:
    A BackendEntry object.
  """
  builder = yaml_object.ObjectBuilder(BackendEntry)
  handler = yaml_builder.BuilderHandler(builder)
  listener = yaml_listener.EventListener(handler)
  listener.Parse(backend_entry)

  entries = handler.GetResults()
  if len(entries) < 1:
    raise BadConfig('Empty backend configuration.')
  if len(entries) > 1:
    raise BadConfig('Multiple backend entries were found in configuration.')

  return entries[0].Init()
示例#3
0
def parse_mapreduce_yaml(contents):
    """Parses mapreduce.yaml file contents.

  Args:
    contents: mapreduce.yaml file contents.

  Returns:
    MapReduceYaml object with all the data from original file.

  Raises:
    errors.BadYamlError: when contents is not a valid mapreduce.yaml file.
  """
    try:
        builder = yaml_object.ObjectBuilder(MapReduceYaml)
        handler = yaml_builder.BuilderHandler(builder)
        listener = yaml_listener.EventListener(handler)
        listener.Parse(contents)

        mr_info = handler.GetResults()
    except (ValueError, yaml_errors.EventError), e:
        raise errors.BadYamlError(e)
示例#4
0
def LoadSingleQueue(queue_info):
    """Load a queue.yaml file or string and return a QueueInfoExternal object.

  Args:
    queue_info: the contents of a queue.yaml file, as a string.

  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]
示例#5
0
def parse_fetcher_policy_yaml(contents):
    """Parses fetcher_policy.yaml file contents.

  Args:
    contents: fetcher_policy.yaml file contents.

  Returns:
    FetcherPolicyYaml object with all the data from original file.

  Raises:
    errors.BadYamlError: when contents is not a valid fetcher_policy.yaml file.
  """
    try:
        builder = yaml_object.ObjectBuilder(FetcherPolicyYaml)
        handler = yaml_builder.BuilderHandler(builder)
        listener = yaml_listener.EventListener(handler)
        listener.Parse(contents)

        fp_info = handler.GetResults()
    except (ValueError, yaml_errors.EventError), e:
        raise errors.BadYamlError(e)
示例#6
0
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]
def LoadSingleAppInfo(app_info):
    """Load a single AppInfo object where one and only one is expected.

  Args:
    app_info: A file-like object or string.  If it is a string, parse it as
    a configuration file.  If it is a file-like object, read in data and
    parse.

  Returns:
    An instance of AppInfoExternal as loaded from a YAML file.

  Raises:
    ValueError: if a specified service is not valid.
    EmptyConfigurationFile: when there are no documents in YAML file.
    MultipleConfigurationFile: when there is more than one document in YAML
    file.
  """
    builder = yaml_object.ObjectBuilder(AppInfoExternal)
    handler = yaml_builder.BuilderHandler(builder)
    listener = yaml_listener.EventListener(handler)
    listener.Parse(app_info)

    app_infos = handler.GetResults()
    if len(app_infos) < 1:
        raise appinfo_errors.EmptyConfigurationFile()
    if len(app_infos) > 1:
        raise appinfo_errors.MultipleConfigurationFile()

    appyaml = app_infos[0]
    if appyaml.handlers:
        for handler in appyaml.handlers:
            handler.FixSecureDefaults()
            handler.WarnReservedURLs()
            handler.ErrorOnPositionForAppInfo()
    if appyaml.builtins:
        BuiltinHandler.Validate(appyaml.builtins)

    return appyaml
def LoadAppInclude(app_include):
    """Load a single AppInclude object where one and only one is expected.

  Args:
    app_include: A file-like object or string.  If it is a string, parse it as
    a configuration file.  If it is a file-like object, read in data and
    parse.

  Returns:
    An instance of AppInclude as loaded from a YAML file.

  Raises:
    EmptyConfigurationFile: when there are no documents in YAML file.
    MultipleConfigurationFile: when there is more than one document in YAML
    file.
  """
    builder = yaml_object.ObjectBuilder(AppInclude)
    handler = yaml_builder.BuilderHandler(builder)
    listener = yaml_listener.EventListener(handler)
    listener.Parse(app_include)

    includes = handler.GetResults()
    if len(includes) < 1:
        raise appinfo_errors.EmptyConfigurationFile()
    if len(includes) > 1:
        raise appinfo_errors.MultipleConfigurationFile()

    includeyaml = includes[0]
    if includeyaml.handlers:
        for handler in includeyaml.handlers:
            handler.FixSecureDefaults()
            handler.WarnReservedURLs()
    if includeyaml.builtins:
        BuiltinHandler.Validate(includeyaml.builtins)

    return includeyaml
示例#9
0
def load_config(stream, config_globals):
    """Load a configuration file and generate importer and exporter classes.

  Args:
    stream: Stream containing config YAML.
    config_globals: Dict to use to reference globals for code in the config.

  Returns:
    BulkloaderEntry

  Raises:
    InvalidConfiguration: If the config is invalid.
  """
    builder = yaml_object.ObjectBuilder(BulkloaderEntry)
    handler = yaml_builder.BuilderHandler(builder)
    listener = yaml_listener.EventListener(handler)

    global _global_temp_globals
    _global_temp_globals = config_globals
    try:
        listener.Parse(stream)
    finally:
        _global_temp_globals = None

    bulkloader_infos = handler.GetResults()
    if len(bulkloader_infos) < 1:
        raise bulkloader_errors.InvalidConfiguration(
            'No configuration specified.')
    if len(bulkloader_infos) > 1:
        raise bulkloader_errors.InvalidConfiguration(
            'Multiple sections in configuration.')
    bulkloader_info = bulkloader_infos[0]
    if not bulkloader_info.transformers:
        raise bulkloader_errors.InvalidConfiguration(
            'No transformers specified.')
    return bulkloader_info
示例#10
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]
    dispatch_info_external.CheckInitialized()
    return dispatch_info_external
示例#11
0
def LoadBackendInfo(backend_info):
    """Parses a BackendInfoExternal object from a string.

  Args:
    backend_info: a backends stanza (list of backends) as a string

  Returns:
    A BackendInfoExternal object.
  """
    builder = yaml_object.ObjectBuilder(BackendInfoExternal)
    handler = yaml_builder.BuilderHandler(builder)
    listener = yaml_listener.EventListener(handler)
    listener.Parse(backend_info)

    backend_info = handler.GetResults()
    if len(backend_info) < 1:
        return BackendInfoExternal(backends=[])
    if len(backend_info) > 1:
        raise BadConfig("Only one 'backends' clause is allowed.")

    info = backend_info[0]
    for backend in info.backends:
        backend.Init()
    return info
示例#12
0
 def setUp(self):
     """Set up parsing frame work."""
     self.builder = yaml_object.ObjectBuilder(PetStore)
     self.handler = yaml_builder.BuilderHandler(self.builder)
     self.listener = yaml_listener.EventListener(self.handler)