11from __future__ import annotations
22
33import array
4- import contextlib
54import itertools
6- import re
75import sys
86from enum import Enum , auto
97from typing import cast , overload
1311else :
1412 from typing import Mapping , Sequence
1513
14+ from ._parsing import DATA , KEYWORD
1615from ._types import Data , DataEntry , Dimensioned , DimensionSet
1716from ._util import is_sequence
1817
@@ -34,9 +33,6 @@ class Kind(Enum):
3433 DIMENSIONS = auto ()
3534
3635
37- _TOKENS = re .compile (r'(?:[^\s"]|"(?:[^"])*")+' )
38-
39-
4036@overload
4137def normalize (data : DataEntry , * , kind : Kind = Kind .DEFAULT ) -> DataEntry : ...
4238
@@ -69,46 +65,19 @@ def normalize(data: Data, *, kind: Kind = Kind.DEFAULT) -> Data:
6965
7066 return [normalize (d , kind = Kind .SINGLE_ENTRY ) for d in data ]
7167
72- if isinstance (data , str ):
73- data = data .strip ()
74-
75- if data .startswith ("(" ) and data .endswith (")" ):
76- data = data [1 :- 1 ].split ()
77- if kind == Kind .KEYWORD :
78- return "(" + " " .join (data ) + ")"
79- return [normalize (d , kind = Kind .SINGLE_ENTRY ) for d in data ]
80-
81- if data .startswith ("[" ) and data .endswith ("]" ):
82- data = data [1 :- 1 ].split ()
83- return normalize (data , kind = Kind .DIMENSIONS )
84-
85- with contextlib .suppress (ValueError ):
86- return int (data )
87-
88- with contextlib .suppress (ValueError ):
89- return float (data )
90-
91- if kind != Kind .KEYWORD :
92- if data in ("yes" , "true" , "on" , "y" , "t" ):
93- return True
94- if data in ("no" , "false" , "off" , "n" , "f" ):
95- return False
96-
97- tokens : list [str ] = re .findall (_TOKENS , data )
98-
99- if len (tokens ) == 1 :
100- return tokens [0 ]
101-
102- if kind == Kind .KEYWORD :
103- return " " .join (tokens )
104-
105- return tuple (tokens )
106-
10768 if isinstance (data , Dimensioned ):
10869 value = normalize (data .value , kind = Kind .SINGLE_ENTRY )
10970 assert isinstance (value , (int , float , list ))
11071 return Dimensioned (value , data .dimensions , data .name )
11172
73+ if isinstance (data , str ):
74+ if kind == Kind .KEYWORD :
75+ data = KEYWORD .parse_string (data )[0 ]
76+ assert isinstance (data , str )
77+ return data
78+
79+ return cast (DataEntry , DATA .parse_string (data )[0 ])
80+
11281 if isinstance (
11382 data ,
11483 (int , float , bool , tuple , DimensionSet ),
0 commit comments