Skip to content

Commit

Permalink
Add flag for 'exact match' (#44)
Browse files Browse the repository at this point in the history
* Add comments for better understanding
* Add exact match flag
* fix problems with copied data
* Change creation of new dict to deepcopy

Co-authored-by: classabbyamp <[email protected]>
  • Loading branch information
obi134 and classabbyamp authored Jan 25, 2021
1 parent 11c6a63 commit 30e7156
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions ctyparser/bigcty.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import re
import os
import collections
import copy
from datetime import datetime

import requests
Expand Down Expand Up @@ -92,32 +93,43 @@ def import_dat(self, dat_file: Union[str, os.PathLike]) -> None:
with dat_file.open("r") as file:
cty_dict = dict()

# get the version from the file
ver_match = re.search(self.regex_version_entry, file.read())
self._version = ver_match.group(1) if ver_match is not None else ""
file.seek(0)

# stores the previous prefix for the next iteration
last = ''

while True:
line = file.readline().rstrip('\x0D').strip(':')
line = file.readline().rstrip('\r').strip(':') # remove unnecessary carriage returns and colons
if not line:
break
# check if the line introduces new DXCC
if line != '' and line[0].isalpha():
# split line into fields at delimiters
segments = [x.strip() for x in line.split(':')]
# check if this entity is not a DXCC
if segments[7][0] == '*':
segments[7] = segments[7][1:]
segments[0] += ' (not DXCC)'
cty_dict[segments[7]] = {'entity': segments[0], 'cq': int(segments[1]),
'itu': int(segments[2]), 'continent': segments[3],
'lat': float(segments[4]), 'long': float(segments[5]),
'tz': -1*float(segments[6]), 'len': len(segments[7]),
'primary_pfx': segments[7]}
'primary_pfx': segments[7], 'exact_match': False}
# store the current prefix for the next iteration
last = segments[7]

# check if the line continues a DXCC
elif line != '' and line[0].isspace():
overrides = line.strip().rstrip(';').rstrip(',').split(',')

for item in overrides:
if item not in cty_dict.keys():
data = cty_dict[last]
# get the already stored data from primary prefix
data = copy.deepcopy(cty_dict[last])
# apply regex to extract the prefix and overrides
match = re.search(self.regex_dat, item)
if match is None:
continue
Expand All @@ -132,6 +144,8 @@ def import_dat(self, dat_file: Union[str, os.PathLike]) -> None:
data['continent'] = match.group("continent")
if match.group("tz"):
data['tz'] = -1 * float(match.group("tz"))
if item.startswith('='):
data['exact_match'] = True
prefix = match.group("prefix")
cty_dict[prefix] = data
self._data = cty_dict
Expand Down

0 comments on commit 30e7156

Please sign in to comment.