def test_EnumToRegex(self):
   expected = syaml.Regex('^(ADD) | (UPDATE)$')
   actual = instance_parser.EnumToRegex(
       instance_parser.EntityOperation,
       [instance_parser.EntityOperation.DELETE])
   self.assertEqual(str(expected), str(actual))
def _OrRegex(values: List[str]) -> syaml.Regex:
  """Returns a regex matching any term in the provided list."""
  r_str = '|'.join(['(' + o + ')' for o in values])
  return syaml.Regex('^' + r_str + '$')
# Exact key for the configuration metadata block
_CONFIG_METADATA_KEY = 'CONFIG_METADATA'
_CONFIG_METADATA_REGEX = f'^{_CONFIG_METADATA_KEY}:'
_CONFIG_METADATA_PATTERN = re.compile(_CONFIG_METADATA_REGEX)
# Key that marks the mode to parse file in.
_CONFIG_MODE_KEY = 'operation'

# A valid device field must match this
_FIELD_REGEX = u'^[a-z][a-z0-9]*(?:_[a-z][a-z0-9]*)*(?:_[0-9]+)*$'
"""Schema separately parses translation to account for multiple valid formats

github.com/google/digitalbuildings/blob/master/ontology/docs/building_config.md
#defining-translations
"""
_TRANSLATION_SCHEMA = syaml.MapPattern(
    syaml.Regex(_FIELD_REGEX),
    # Note: This block is somewhat permissive as the logic was difficult to
    # implement in syaml.  Additional validation occurs in EntityInstance
    syaml.Str() | syaml.Map({
        PRESENT_VALUE_KEY:
            syaml.Str(),
        syaml.Optional(STATES_KEY):
            syaml.MapPattern(
                syaml.Regex(u'^[A-Z][A-Z_]+'),
                syaml.Str() | syaml.Seq(syaml.Str())),
        syaml.Optional(UNITS_KEY):
            syaml.Map({
                UNIT_NAME_KEY: syaml.Str(),
                UNIT_VALUES_KEY: syaml.MapPattern(syaml.Str(), syaml.Str())
            }),
    }))
Exemplo n.º 4
0
# Exact key for the configuration metadata block
_CONFIG_METADATA_KEY = 'CONFIG_METADATA'
_CONFIG_METADATA_REGEX = f'^{_CONFIG_METADATA_KEY}:'
_CONFIG_METADATA_PATTERN = re.compile(_CONFIG_METADATA_REGEX)
# Key that marks the mode to parse file in.
_CONFIG_MODE_KEY = 'operation'

# A valid device field must match this
_FIELD_REGEX = u'^[a-z][a-z0-9]*(?:_[a-z][a-z0-9]*)*(?:_[0-9]+)*$'
"""Schema separately parses translation to account for multiple valid formats

github.com/google/digitalbuildings/blob/master/ontology/docs/building_config.md
#defining-translations
"""
_TRANSLATION_SCHEMA = syaml.MapPattern(
    syaml.Regex(_FIELD_REGEX),
    # Note: This block is somewhat permissive as the logic was difficult to
    # implement in syaml.  Additional validation occurs in EntityInstance
    syaml.Str() | syaml.Map({
        PRESENT_VALUE_KEY:
        syaml.Str(),
        syaml.Optional(STATES_KEY):
        syaml.MapPattern(syaml.Regex(u'^[A-Z][A-Z_]+'),
                         syaml.Str() | syaml.Seq(syaml.Str())),
        syaml.Optional(UNITS_KEY):
        syaml.Map({
            UNIT_NAME_KEY: syaml.Str(),
            UNIT_VALUES_KEY: syaml.MapPattern(syaml.Str(), syaml.Str())
        }),
    }))
Exemplo n.º 5
0
SCHEMA_RULE = strictyaml.Seq(
    strictyaml.Map({
        "process":
        strictyaml.Str(),
        strictyaml.Optional("action"):
        strictyaml.Enum(["allow", "deny", "ask"]),
        strictyaml.Optional("codeSignature"):
        strictyaml.Enum(["ignore"]),
        strictyaml.Optional("direction"):
        strictyaml.Enum(["incoming", "outgoing"]),
        strictyaml.Optional("disabled"):
        strictyaml.Bool(),
        strictyaml.Optional("notes"):
        strictyaml.Str(),
        strictyaml.Optional("ports"):
        strictyaml.Regex("^(any|\d+((\s+)?\-(\s+)?\d+)?)$"),
        strictyaml.Optional("priority"):
        strictyaml.Enum(["regular", "high"]),
        strictyaml.Optional("protocol"):
        strictyaml.Int() | strictyaml.Str(),
        strictyaml.Optional("remote"):
        strictyaml.Enum([
            "any", "local-net", "multicast", "broadcast", "bonjour",
            "dns-servers"
        ]),
        strictyaml.Optional("remote-addresses"):
        strictyaml.Seq(strictyaml.Str()),
        strictyaml.Optional("remote-domains"):
        strictyaml.Seq(strictyaml.Str()),
        strictyaml.Optional("remote-hosts"):
        strictyaml.Seq(strictyaml.Str()),
Exemplo n.º 6
0
import sys

_ENTITY_INSTANCE_REGEX = '^[A-Z][A-Z0-9\\-]+:'
_ENTITY_INSTANCE_PATTERN = re.compile(_ENTITY_INSTANCE_REGEX)

_IGNORE_PATTERN = re.compile(r'^(\W)*#|\n')
# number of entities to validate per batch
_ENTITIES_PER_BATCH = 1
_COMPLIANT_REGEX = u'^COMPLIANT$'
_TRANSLATION = 'translation'
_FIELD_REGEX = u'^[a-z]+[a-z0-9]*(?:_[a-z]+[a-z0-9]*)*(?:_[0-9]+)*$'

"""Schema separately parses translation to account for multiple valid formats
github.com/google/digitalbuildings/blob/master/ontology/docs/building_config.md
#defining-translations"""
_TRANSLATION_SCHEMA = syaml.Regex(_COMPLIANT_REGEX) | syaml.MapPattern(
    syaml.Regex(_FIELD_REGEX),
    syaml.Str() | syaml.Map({'present_value': syaml.Str(),
                             syaml.Optional('states'): syaml.MapPattern(
                                 syaml.Regex(u'^[A-Z][A-Z_]+'), syaml.Str()),
                             syaml.Optional('units'): syaml.Map(
                                 {'key': syaml.Str(),
                                  'values': syaml.MapPattern(syaml.Str(),
                                                             syaml.Str())
                                  }),
                             syaml.Optional('unit_values'): syaml.MapPattern(
                                 syaml.Str(), syaml.Str())
                             }))

"""strictyaml schema parses a YAML instance from its first level of keys
github.com/google/digitalbuildings/blob/master/ontology/docs/building_config.md