rads.config.text_parsers module

Parsers for text within a tag.

The parsers in this file should all take a string and a mapping of attributes for the tag and return a value or take one or more parser functions and return a new parsing function.

If a parser deals with generic XML then it should be rads.config.xml_parsers and if it returns a rads.config.xml_parsers.Parser but is not generic then it belongs in rads.config.grammar.

exception rads.config.text_parsers.TerminalTextParseError[source]

Bases: Exception

Raised to terminate text parsing with an error.

This error is not allowed to be handled by a text parser. It indicates that no recovery is possible.

exception rads.config.text_parsers.TextParseError[source]

Bases: rads.config.text_parsers.TerminalTextParseError

Raised to indicate that a text parsing error has occurred.

Unlike TerminalTextParseError this one is allowed to be handled by a text parser.

rads.config.text_parsers.lift(string_parser: Union[Callable[[str], _T], Type[_SupportsFromString]], *, terminal: bool = False) → Callable[[str, Mapping[str, str]], _T][source]

Lift a simple string parser to a text parser that accepts attributes.

This is very similar to lifting a plain function into a monad.

Parameters
  • string_parser – A simple parser that takes a string and returns a value. This can also by a type that can be constructed from a string.

  • terminal – Set to True to use TerminalTextParseErrors instead of TextParseErrors.

Returns

The given string_parser with an added argument to accept and ignore the attributes for the text tag.

Raises
  • TextParseError – The resulting parser throws this if the given string_parser throws a TypeError, ValueError, or KeyError and terminal was False (the default).

  • TerminalTextParseError – The resulting parser throws this if the given string_parser throws a TypeError, ValueError, or KeyError and terminal was True.

rads.config.text_parsers.list_of(parser: Callable[[str, Mapping[str, str]], _T], *, sep: Optional[str] = None, terminal: bool = False) → Callable[[str, Mapping[str, str]], List[_T]][source]

Convert parser into a parser of lists.

Parameters
  • parser – Original parser.

  • sep – Item delimiter. Default is to separate by one or more spaces.

  • terminal – If set to True it promotes any TextParseError s raised by the given parser to a TerminalTextParseError.

Returns

The new parser of delimited lists.

rads.config.text_parsers.range_of(parser: Callable[[str, Mapping[str, str]], N], *, terminal: bool = False) → Callable[[str, Mapping[str, str]], rads.config.tree.Range[~N][N]][source]

Create a range parser from a given parser for each range element.

The resulting parser will parse space separated lists of length 2 and use the given parser for both elements.

Parameters
Returns

New range parser.

Raises
  • TextParseError – Resulting parser raises this if given a string that does not contain two space separated elements and terminal was False (the default).

  • TerminalTextParseError – Resulting parser raises this if given a string that does not contain two space separated elements and terminal was True.

rads.config.text_parsers.one_of(parsers: Iterable[Callable[[str, Mapping[str, str]], Any]], *, terminal: bool = False) → Callable[[str, Mapping[str, str]], Any][source]

Convert parsers into a parser that tries each one in sequence.

Note

Each parser will be tried in sequence. The next parser will be tried if TextParseError is raised.

Parameters
Returns

The new parser which tries each of the given parsers in order until one succeeds.

Raises
  • TextParseError – Resulting parser raises this if given a string that cannot be parsed by any of the given parsers and terminal was False (the default).

  • TerminalTextParseError – Resulting parser raises this if given a string that cannot be parsed by any of the given parsers and terminal was True.

rads.config.text_parsers.compress(string: str, _: Mapping[str, str]) → rads.config.tree.Compress[source]

Parse a string into a rads.config.tree.Compress object.

Parameters
  • string

    String to parse into a rads.config.tree.Compress object. It should be in the following form:

    <type:type> [scale_factor:float] [add_offset:float]

    where only the first value is required and should be one of the following 4 character RADS data type values:

    • int1 - maps to numpy.int8

    • int2 - maps to numpy.int16

    • int4 - maps to numpy.int32

    • real - maps to numpy.float32

    • dble - maps to numpy.float64

    The attribute mapping of the tag the string came from. Not currently used by this function.

  • _ – Mapping of tag attributes. Not used by this function.

Returns

A new rads.config.tree.Compress object created from the parsed string.

Raises

TextParseError – If the <type> is not in the given string or if too many values are in the string. Also, if one of the values cannot be converted.

rads.config.text_parsers.cycles(string: str, _: Mapping[str, str]) → rads.config.tree.Cycles[source]

Parse a string into a rads.config.tree.Cycles object.

Parameters
  • string

    String to parse into a rads.config.tree.Cycles object. It should be in the following form:

    <first cycle in phase> <last cycle in phase>
    

  • _ – Mapping of tag attributes. Not used by this function.

Returns

A new rads.config.tree.Cycles object created from the parsed string.

Raises

TextParseError – If the wrong number of values are given in the string or one of the values is not parsable to an integer.

rads.config.text_parsers.data(string: str, attr: Mapping[str, str]) → Any[source]

Parse a string into one of the data objects list below.

The parsing is done based on both the given string and the ‘source’ value in attr if it exists.

Note

This is a terminal parser, it will either succeed or raise TerminalTextParseError.

Parameters
  • string – String to parse into a data object.

  • attr

    Mapping of tag attributes. This parser can make use of the following key/value pairs if they exist:

    • ”source” - explicitly specify the data source, this can be any of the following:

      • ”flags”

      • ”constant”

      • ”grid”

      • ”grid_l”

      • ”grid_s”

      • ”grid_c”

      • ”grid_q”

      • ”grid_n”

      • ”nc”

      • ”netcdf”

      • ”math”

      • ”branch” - used by some sources to specify an alternate directory

    • ”x” - used by the grid sources to set the x dimension

    • ”y” - used by the grid sources to set the y dimension

Returns

A new data object representing the given string.

Raises

TerminalTextParseError – If for any reason the given string and attr cannot be parsed into one of the data objects listed above.

rads.config.text_parsers.nop(string: str, _: Mapping[str, str]) → str[source]

No operation parser, returns given string unchanged.

This exists primarily as a default for when no parser is given as the use of lift(str) is recommended when the parsed value is supposed to be a string.

Parameters
  • string – String to return.

  • _ – Mapping of tag attributes. Not used by this function.

Returns

The given string.

rads.config.text_parsers.ref_pass(string: str, _: Mapping[str, str]) → rads.config.tree.ReferencePass[source]

Parse a string into a rads.config.tree.ReferencePass object.

Parameters
  • string

    String to parse into a:class:rads.config.tree.ReferencePass object. It should be in the following form:

    <yyyy-mm-ddTHH:MM:SS> <lon> <cycle> <pass> [absolute orbit number]
    

    where the last element is optional and defaults to 1. The date can also be missing seconds, minutes, and hours.

  • _ – Mapping of tag attributes. Not used by this function.

Returns

A new rads.config.tree.ReferencePass object created from the parsed string.

Raises

TextParseError – If the wrong number of values are given in the string or one of the values is not parsable.

rads.config.text_parsers.repeat(string: str, _: Mapping[str, str]) → rads.config.tree.Repeat[source]

Parse a string into a rads.config.tree.Repeat object.

Parameters
  • string

    String to parse into a rads.config.tree.Repeat object. It should be in the following form:

    <days:float> <passes:int> [longitude drift per cycle:float]
    

    where the last value is optional.

  • _ – Mapping of tag attributes. Not used by this function.

Returns

A new rads.config.tree.Repeat object created from the parsed string.

Raises

TextParseError – If the wrong number of values are given in the string or one of the values is not parsable.

rads.config.text_parsers.time(string: str, _: Mapping[str, str]) → datetime.datetime[source]

Parse a string into a datetime.datetime object.

Parameters
  • string

    String to parse into a datetime.datetime object. It should be in one of the following forms:

    • yyyy-mm-ddTHH:MM:SS

    • yyyy-mm-ddTHH:MM

    • yyyy-mm-ddTHH

    • yyyy-mm-ddT

    • yyyy-mm-dd

  • _ – Mapping of tag attributes. Not used by this function.

Returns

A new datetime.datetime object created from the parsed string.

Raises

TextParseError – If the date/time string cannot be parsed.

rads.config.text_parsers.unit(string: str, _: Mapping[str, str]) → cf_units.Unit[source]

Parse a string into a cf_units.Unit object.

Parameters
  • string – String to parse into a cf_units.Unit object. See the cf_units package for supported units. If given ‘dB’ or ‘decibel’ a no_unit object will be returned and if given ‘yymmddhhmmss’ an unknown unit will be returned.

  • _ – Mapping of tag attributes. Not used by this function.

Returns

A new cf_units.Unit object created from the parsed string. In the case of ‘dB’ and ‘decibel’ this will be a no_unit and in the case of ‘yymmddhhmmss’ it will be ‘unknown’. See issue 30.

Raises

ValueError – If the given string does not represent a valid unit.