formatting

This commit is contained in:
Istvan Ruzman
2020-08-07 16:39:49 +02:00
parent dc2a24402b
commit e8dce3e2cf
2 changed files with 26 additions and 11 deletions

View File

@@ -9,7 +9,17 @@ Classes and Types to parse and represent a RADIUS dictionary.
from enum import IntEnum, Enum, auto from enum import IntEnum, Enum, auto
from dataclasses import dataclass from dataclasses import dataclass
from os.path import dirname, isabs, join, normpath from os.path import dirname, isabs, join, normpath
from typing import cast, Dict, Generator, IO, List, Optional, Sequence, Tuple, Union from typing import (
cast,
Dict,
Generator,
IO,
List,
Optional,
Sequence,
Tuple,
Union,
)
import logging import logging
@@ -389,7 +399,10 @@ class Dictionary:
has_tag, encrypt = self._parse_attribute_flags(tokens, line_num) has_tag, encrypt = self._parse_attribute_flags(tokens, line_num)
name, attr_code, datatype = tokens[1:4] name, attr_code, datatype = tokens[1:4]
if datatype in {"concat", "extended", "evs", "long-extended" } and self.cur_vendor != self.rfc_vendor: if (
datatype in {"concat", "extended", "evs", "long-extended"}
and self.cur_vendor != self.rfc_vendor
):
raise ParseError( raise ParseError(
filename, filename,
'vendor attributes are not allowed to have the datatype "concat"', 'vendor attributes are not allowed to have the datatype "concat"',
@@ -440,7 +453,9 @@ class Dictionary:
len(codes) > 1, len(codes) > 1,
) )
attrcode: Union[int, Tuple[int, ...]] = codes[0] if len(codes) == 1 else tuple(codes) attrcode: Union[int, Tuple[int, ...]] = codes[0] if len(
codes
) == 1 else tuple(codes)
self.cur_vendor.attrs[attrcode] = attribute self.cur_vendor.attrs[attrcode] = attribute
if self.cur_vendor != self.rfc_vendor: if self.cur_vendor != self.rfc_vendor:
@@ -461,14 +476,14 @@ class Dictionary:
(attr_name, key, vvalue) = tokens[1:] (attr_name, key, vvalue) = tokens[1:]
try: try:
if '.' in vvalue: if "." in vvalue:
# quick and dirty way to make floats values an error # quick and dirty way to make floats values an error
raise ValueError raise ValueError
value = _parse_number(vvalue) value = _parse_number(vvalue)
except ValueError: except ValueError:
raise ParseError(filename, raise ParseError(
f"Invalid number {vvalue} for VALUE {key}", filename, f"Invalid number {vvalue} for VALUE {key}", line_num
line_num) )
attribute = self.attrindex[attr_name] attribute = self.attrindex[attr_name]
try: try:

View File

@@ -86,8 +86,7 @@ def test_valid_attribute_numbers(number):
@pytest.mark.parametrize( @pytest.mark.parametrize(
"invalid_number", "invalid_number", ["ABCD", "-1", "inf", "INF", "-INF", "2e4", "2.5e3"],
["ABCD", "-1", "inf", "INF", "-INF", "2e4", "2.5e3"],
) )
def test_invalid_attribute_numbers(invalid_number): def test_invalid_attribute_numbers(invalid_number):
dictionary = StringIO(f"ATTRIBUTE NAME {invalid_number} integer64") dictionary = StringIO(f"ATTRIBUTE NAME {invalid_number} integer64")
@@ -270,6 +269,7 @@ def test_invalid_datatypes_in_vendor_space(datatype):
def test_invalid_attribute_numbers(invalid_number): def test_invalid_attribute_numbers(invalid_number):
dictionary = StringIO( dictionary = StringIO(
f"ATTRIBUTE TEST-ATTRIBUTE 1 integer\n" f"ATTRIBUTE TEST-ATTRIBUTE 1 integer\n"
f"VALUE TEST-ATTRIBUTE TEST-VALUE {invalid_number}") f"VALUE TEST-ATTRIBUTE TEST-VALUE {invalid_number}"
)
with pytest.raises(ParseError): with pytest.raises(ParseError):
Dictionary("", dictionary) Dictionary("", dictionary)