Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get_path_target_type_stmt(self, type_stmt):
"""Return target pyang statement."""
type_spec = type_stmt.i_type_spec
while all([isinstance(type_spec, ptypes.PathTypeSpec),
hasattr(type_spec, 'i_target_node')]):
type_stmt = type_spec.i_target_node.search_one('type')
type_spec = type_stmt.i_type_spec
return type_stmt
def _get_union_type_spec(self, orig_type_spec):
"""Return union type_spec and type_stmt."""
type_spec = orig_type_spec
while isinstance(type_spec, ptypes.UnionTypeSpec):
type_stmt = choice(type_spec.types)
type_spec = self.types_extractor.get_property_type(type_stmt)
if hasattr(type_spec, 'i_type_spec'):
type_spec = type_spec.i_type_spec
return type_spec, type_stmt
def _set_prop_type(self, prop):
"""Set self.type_spec and self.type_stmt:
- Trace path statement and set destination statement's
pyang statement as self.type_stmt, pyang TypeSpec as
self.type_spec.
- If prop represents union property, chose one.
"""
type_spec = prop.property_type
type_stmt = prop.stmt.search_one('type')
if isinstance(type_spec, ptypes.PathTypeSpec):
type_stmt = self._get_path_target_type_stmt(type_stmt)
type_spec = type_stmt.i_type_spec
if isinstance(type_spec, ptypes.UnionTypeSpec):
type_spec, type_stmt = self._get_union_type_spec(type_spec)
self.type_spec = type_spec
self.type_stmt = type_stmt
if stmt.i_type_spec is None:
# an error has been added already; skip further validation
return
# check the fraction-digits - only applicable when the type is the builtin
# decimal64
frac = stmt.search_one('fraction-digits')
if frac is not None and stmt.arg != 'decimal64':
err_add(ctx.errors, frac.pos, 'BAD_RESTRICTION', 'fraction_digits')
elif stmt.arg == 'decimal64' and frac is None:
err_add(ctx.errors, stmt.pos, 'MISSING_TYPE_SPEC_1',
('decimal64', 'fraction-digits'))
elif stmt.arg == 'decimal64' and frac.is_grammatically_valid:
stmt.i_is_derived = True
stmt.i_type_spec = types.Decimal64TypeSpec(frac)
# check the range restriction
stmt.i_ranges = []
rangestmt = stmt.search_one('range')
if rangestmt is not None:
if 'range' not in stmt.i_type_spec.restrictions():
err_add(ctx.errors, rangestmt.pos, 'BAD_RESTRICTION', 'range')
else:
stmt.i_is_derived = True
ranges_spec = types.validate_range_expr(ctx.errors, rangestmt, stmt)
if ranges_spec is not None:
stmt.i_ranges = ranges_spec[0]
stmt.i_type_spec = types.RangeTypeSpec(stmt.i_type_spec,
ranges_spec)
# check the length restriction
def v_grammar_typedef(ctx, stmt):
if types.is_base_type(stmt.arg):
err_add(ctx.errors, stmt.pos, 'BAD_TYPE_NAME', stmt.arg)
def is_leafref_prop(prop):
return (isinstance(prop.property_type, ptypes.PathTypeSpec) and
prop.stmt.i_leafref_ptr is not None)
stmt.i_lengths = lengths_spec[0]
stmt.i_type_spec = types.LengthTypeSpec(stmt.i_type_spec,
lengths_spec)
# check the pattern restrictions
patterns = stmt.search('pattern')
if (patterns and
'pattern' not in stmt.i_type_spec.restrictions()):
err_add(ctx.errors, patterns[0].pos, 'BAD_RESTRICTION', 'pattern')
elif patterns:
stmt.i_is_derived = True
pattern_specs = [types.validate_pattern_expr(ctx.errors, p)
for p in patterns]
if None not in pattern_specs:
# all patterns valid
stmt.i_type_spec = types.PatternTypeSpec(stmt.i_type_spec,
pattern_specs)
# check the path restriction
path = stmt.search_one('path')
if path is not None and stmt.arg != 'leafref':
err_add(ctx.errors, path.pos, 'BAD_RESTRICTION', 'path')
elif stmt.arg == 'leafref' and path is None:
err_add(ctx.errors, stmt.pos, 'MISSING_TYPE_SPEC_1',
('leafref', 'path'))
elif path is not None:
stmt.i_is_derived = True
if path.is_grammatically_valid is True:
path_spec = types.validate_path_expr(ctx.errors, path)
if path_spec is not None:
stmt.i_type_spec = types.PathTypeSpec(stmt.i_type_spec,
path_spec, path, path.pos)
stmt.i_is_derived = True
ranges_spec = types.validate_range_expr(ctx.errors, rangestmt, stmt)
if ranges_spec is not None:
stmt.i_ranges = ranges_spec[0]
stmt.i_type_spec = types.RangeTypeSpec(stmt.i_type_spec,
ranges_spec)
# check the length restriction
stmt.i_lengths = []
length = stmt.search_one('length')
if (length is not None and
'length' not in stmt.i_type_spec.restrictions()):
err_add(ctx.errors, length.pos, 'BAD_RESTRICTION', 'length')
elif length is not None:
stmt.i_is_derived = True
lengths_spec = types.validate_length_expr(ctx.errors, length, stmt)
if lengths_spec is not None:
stmt.i_lengths = lengths_spec[0]
stmt.i_type_spec = types.LengthTypeSpec(stmt.i_type_spec,
lengths_spec)
# check the pattern restrictions
patterns = stmt.search('pattern')
if (patterns and
'pattern' not in stmt.i_type_spec.restrictions()):
err_add(ctx.errors, patterns[0].pos, 'BAD_RESTRICTION', 'pattern')
elif patterns:
stmt.i_is_derived = True
pattern_specs = [types.validate_pattern_expr(ctx.errors, p)
for p in patterns]
if None not in pattern_specs:
# all patterns valid
if stmt.i_type_spec is None:
# an error has been added already; skip further validation
return
# check the fraction-digits - only applicable when the type is the builtin
# decimal64
frac = stmt.search_one('fraction-digits')
if frac is not None and stmt.arg != 'decimal64':
err_add(ctx.errors, frac.pos, 'BAD_RESTRICTION', 'fraction_digits')
elif stmt.arg == 'decimal64' and frac is None:
err_add(ctx.errors, stmt.pos, 'MISSING_TYPE_SPEC_1',
('decimal64', 'fraction-digits'))
elif stmt.arg == 'decimal64' and frac.is_grammatically_valid:
stmt.i_is_derived = True
stmt.i_type_spec = types.Decimal64TypeSpec(frac)
# check the range restriction
stmt.i_ranges = []
range = stmt.search_one('range')
if (range is not None and
'range' not in stmt.i_type_spec.restrictions()):
err_add(ctx.errors, range.pos, 'BAD_RESTRICTION', 'range')
elif range is not None:
stmt.i_is_derived = True
ranges_spec = types.validate_range_expr(ctx.errors, range, stmt)
if ranges_spec is not None:
stmt.i_ranges = ranges_spec[0]
stmt.i_type_spec = types.RangeTypeSpec(stmt.i_type_spec,
ranges_spec)
# check the length restriction