@@ -35,9 +35,12 @@ class IllegalDataTypeException(Exception):
3535REGEX_PARAM = re .compile (r'[a-zA-Z][a-zA-Z_0-9]*' )
3636
3737
38- def unpack (data : str ) -> dict [str , str ]:
38+ def unpack (data : str , strip_tags : bool = True ) -> dict [str , str ]:
3939 """Unpack header or record part to dictionary
40- The parameters are converted to uppercase"""
40+ The parameters are converted to uppercase
41+ :param data: string with multiple ADIF tag and value for a whole record
42+ :param strip_tags: remove any leading or trailing whitespaces in tag names (default: True)
43+ :return: dictionary of ADIF tag and value"""
4144
4245 unpacked = {}
4346
@@ -56,7 +59,7 @@ def unpack(data: str) -> dict[str, str]:
5659 dtype = None
5760 try :
5861 tag_def = tag .split (':' )
59- param = tag_def [0 ]
62+ param = tag_def [0 ]. strip () if strip_tags else tag_def [ 0 ]
6063 length = tag_def [1 ]
6164 if len (tag_def ) == 3 :
6265 dtype = tag_def [2 ]
@@ -80,12 +83,13 @@ def unpack(data: str) -> dict[str, str]:
8083 return unpacked
8184
8285
83- def loadi (adi : str , skip : int = 0 ) -> Iterator [dict [str , str ]]:
86+ def loadi (adi : str , skip : int = 0 , strip_tags : bool = True ) -> Iterator [dict [str , str ]]:
8487 """Turn ADI formated string to header/records as an iterator over dict
8588 The skip option is useful if you want to watch a file for new records only. This saves processing time.
8689
8790 :param adi: the ADI data
8891 :param skip: skip first number of records (does not apply for header)
92+ :param strip_tags: remove any leading or trailing whitespaces in tag names (default: True)
8993 :return: an iterator of records (first record is the header even if not available)
9094 """
9195
@@ -96,15 +100,15 @@ def loadi(adi: str, skip: int = 0) -> Iterator[dict[str, str]]:
96100 elif len (hr_list ) > 2 : # More than one header
97101 raise TooMuchHeadersException ()
98102 else : # One header and the records
99- yield unpack (hr_list [0 ])
103+ yield unpack (hr_list [0 ], strip_tags )
100104 record_data = hr_list [1 ]
101105
102106 for i , rec in enumerate (re .finditer (r'(.*?)<[eE][oO][rR]>' , record_data , re .S )):
103107 if i >= skip :
104- yield unpack (rec .groups ()[0 ])
108+ yield unpack (rec .groups ()[0 ], strip_tags )
105109
106110
107- def loads (adi : str , skip : int = 0 ) -> dict :
111+ def loads (adi : str , skip : int = 0 , strip_tags : bool = True ) -> dict :
108112 """Turn ADI formated string to dictionary
109113 The parameters are converted to uppercase
110114
@@ -118,6 +122,7 @@ def loads(adi: str, skip: int = 0) -> dict:
118122
119123 :param adi: the ADI data
120124 :param skip: skip first number of records (does not apply for header)
125+ :param strip_tags: remove any leading or trailing whitespaces in tag names (default: True)
121126 :return: the ADI as a dict
122127 """
123128
@@ -126,7 +131,7 @@ def loads(adi: str, skip: int = 0) -> dict:
126131 }
127132
128133 first = True
129- for rec in loadi (adi , skip ):
134+ for rec in loadi (adi , skip , strip_tags ):
130135 if first :
131136 doc ['HEADER' ] = rec
132137 first = False
@@ -136,7 +141,7 @@ def loads(adi: str, skip: int = 0) -> dict:
136141 return doc
137142
138143
139- def load (file_name : str , skip : int = 0 , encoding = None ) -> dict :
144+ def load (file_name : str , skip : int = 0 , encoding = None , strip_tags : bool = True ) -> dict :
140145 """Load ADI formated file to dictionary
141146 The parameters are converted to uppercase
142147
@@ -151,13 +156,14 @@ def load(file_name: str, skip: int = 0, encoding=None) -> dict:
151156 :param file_name: the file name where the ADI data is stored
152157 :param skip: skip first number of records (does not apply for header)
153158 :param encoding: the file encoding
159+ :param strip_tags: remove any leading or trailing whitespaces in tag names (default: True)
154160 :return: the ADI as a dict
155161 """
156162
157163 with open (file_name , encoding = encoding ) as af :
158164 data = af .read ()
159165
160- return loads (data , skip )
166+ return loads (data , skip , strip_tags )
161167
162168
163169def pack (param : str , value : str , dtype : str = None ) -> str :
0 commit comments