Skip to content

Commit

Permalink
GLOBAL variables changed to GLOBAL constants (#1710)
Browse files Browse the repository at this point in the history
* GLOBAL variables changed to GLOBAL constants

* Fixed linter errors

* Classes redefined

* Class and attribute names changed

* Download path and test updated

* Path and test path futher updated
  • Loading branch information
rootxrishabh authored Mar 27, 2023
1 parent 42af515 commit 3018304
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
73 changes: 43 additions & 30 deletions mslib/utils/airdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,33 @@
if not os.path.exists(os.path.join(OSDIR, "downloads", "aip")):
os.makedirs(os.path.join(OSDIR, "downloads", "aip"))

_airspaces = []
_airports = []
_airports_mtime = 0
_airspaces_mtime = {}
_airspace_url = "https://storage.googleapis.com/29f98e10-a489-4c82-ae5e-489dbcd4912f"
_airspace_download_url = "https://storage.googleapis.com/storage/v1/b/29f98e10-a489-4c82-ae5e-489dbcd4912f/o/" \
"{}_asp.xml?alt=media"
# Updated Dec 01 2022
_airspace_cache = [('ad_asp.xml', '377'), ('ae_asp.xml', '110238'), ('af_asp.xml', '377'), ('ag_asp.xml', '377'),
('ai_asp.xml', '377'), ('al_asp.xml', '11360'), ('am_asp.xml', '377'), ('ao_asp.xml', '377'),
('aq_asp.xml', '377'), ('ar_asp.xml', '975402'), ('as_asp.xml', '377'), ('at_asp.xml', '1059077'),
('au_asp.xml', '9667669'), ('aw_asp.xml', '377'), ('ax_asp.xml', '377'), ('az_asp.xml', '377'),
('ba_asp.xml', '76717'), ('bb_asp.xml', '377'), ('bd_asp.xml', '377'), ('be_asp.xml', '462667'),
('bf_asp.xml', '377'), ('bg_asp.xml', '440690'), ('bh_asp.xml', '68519'), ('bi_asp.xml', '377'),
('bj_asp.xml', '377'), ('bl_asp.xml', '377')
]

class Airspace:
"""
This class provides airspaces/airports URLs and variables
"""
airports = []

airports_mtime = 0

data = []

data_mtime = {}

data_url = "https://storage.googleapis.com/29f98e10-a489-4c82-ae5e-489dbcd4912f"

data_download_url = "https://storage.googleapis.com/storage/v1/b/29f98e10-a489-4c82-ae5e-489dbcd4912f/o/" \
"{}_asp.xml?alt=media"

data_cache = [('ad_asp.xml', '377'), ('ae_asp.xml', '110238'), ('af_asp.xml', '377'), ('ag_asp.xml', '377'),
('ai_asp.xml', '377'), ('al_asp.xml', '11360'), ('am_asp.xml', '377'), ('ao_asp.xml', '377'),
('aq_asp.xml', '377'), ('ar_asp.xml', '975402'), ('as_asp.xml', '377'),
('at_asp.xml', '1059077'),
('au_asp.xml', '9667669'), ('aw_asp.xml', '377'), ('ax_asp.xml', '377'), ('az_asp.xml', '377'),
('ba_asp.xml', '76717'), ('bb_asp.xml', '377'), ('bd_asp.xml', '377'), ('be_asp.xml', '462667'),
('bf_asp.xml', '377'), ('bg_asp.xml', '440690'), ('bh_asp.xml', '68519'), ('bi_asp.xml', '377'),
('bj_asp.xml', '377'), ('bl_asp.xml', '377')
]


def download_progress(file_path, url, progress_callback=lambda f: logging.info("%sKB Downloaded", int(f))):
Expand Down Expand Up @@ -87,7 +98,9 @@ def get_airports(force_download=False, url=None):
"""
Gets or downloads the airports.csv in ~/.config/msui/downloads/aip and returns all airports within
"""
global _airports, _airports_mtime
_airports = Airspace().airports
_airports_mtime = Airspace().airports_mtime

if url is None:
url = "https://ourairports.com/data/airports.csv"

Expand All @@ -111,9 +124,9 @@ def get_airports(force_download=False, url=None):
== QtWidgets.QMessageBox.Yes:
download_progress(os.path.join(OSDIR, "downloads", "aip", "airports.csv"), url)

if os.path.exists(os.path.join(OSDIR, "airports.csv")):
with open(os.path.join(OSDIR, "airports.csv"), "r", encoding="utf8") as file:
_airports_mtime = os.path.getmtime(os.path.join(OSDIR, "airports.csv"))
if os.path.exists(os.path.join(OSDIR, "downloads", "aip", "airports.csv")):
with open(os.path.join(OSDIR, "downloads", "aip", "airports.csv"), "r", encoding="utf8") as file:
_airports_mtime = os.path.getmtime(os.path.join(OSDIR, "downloads", "aip", "airports.csv"))
return list(csv.DictReader(file, delimiter=","))

else:
Expand All @@ -125,15 +138,15 @@ def get_available_airspaces():
Gets and returns all available airspaces and their sizes from openaip
"""
try:
directory = requests.get(_airspace_url, timeout=5)
directory = requests.get(Airspace.data_url, timeout=5)
if directory.status_code == 404:
return _airspace_cache
return Airspace.data_cache
airspaces = regex.findall(r">(.._asp\.xml)<", directory.text)
sizes = regex.findall(r".._asp.xml.*?<Size>([0-9]+)<\/Size", directory.text)
airspaces = [airspace for airspace in zip(airspaces, sizes) if airspace[-1] != "0"]
return airspaces
except requests.exceptions.RequestException:
return _airspace_cache
return Airspace.data_cache


def update_airspace(force_download=False, countries=None):
Expand All @@ -142,11 +155,10 @@ def update_airspace(force_download=False, countries=None):
"""
if countries is None:
countries = ["de"]
global _airspaces, _airspaces_mtime

for country in countries:
location = os.path.join(OSDIR, "downloads", "aip", f"{country}_asp.xml")
url = _airspace_download_url.format(country)
url = Airspace.data_download_url.format(country)
available = get_available_airspaces()
try:
data = [airspace for airspace in available if airspace[0].startswith(country)][0]
Expand Down Expand Up @@ -174,17 +186,18 @@ def get_airspaces(countries=None):
"""
if countries is None:
countries = []
global _airspaces, _airspaces_mtime
_airspaces = Airspace().data
_airspaces_mtime = Airspace().data_mtime

reload = False
files = [f"{country}_asp.xml" for country in countries]
update_airspace(countries=countries)
files = [file for file in files if os.path.exists(os.path.join(OSDIR, file))]
files = [file for file in files if os.path.exists(os.path.join(OSDIR, "downloads", "aip", file))]

if _airspaces and len(files) == len(_airspaces_mtime):
for file in files:
if file not in _airspaces_mtime or \
os.path.getmtime(os.path.join(OSDIR, file)) != _airspaces_mtime[file]:
os.path.getmtime(os.path.join(OSDIR, "downloads", "aip", file)) != _airspaces_mtime[file]:
reload = True
break
if not reload:
Expand All @@ -193,7 +206,7 @@ def get_airspaces(countries=None):
_airspaces_mtime = {}
_airspaces = []
for file in files:
fpath = os.path.join(OSDIR, file)
fpath = os.path.join(OSDIR, "downloads", "aip", file)
root = etree.parse(fpath).getroot()
valid_file = len(set([elem.tag for elem in root.iter()])) == 12
if valid_file:
Expand Down Expand Up @@ -238,7 +251,7 @@ def get_airspaces(countries=None):
airspace_data["polygon"] = [(float(data.split()[0]), float(data.split()[-1]))
for data in airspace_data["polygon"].split(",")]
_airspaces.append(airspace_data)
_airspaces_mtime[file] = os.path.getmtime(os.path.join(OSDIR, file))
_airspaces_mtime[file] = os.path.getmtime(os.path.join(OSDIR, "downloads", "aip", file))
else:
QtWidgets.QMessageBox.information(None, "No Airspaces data in file:", f"{file}")

Expand Down
14 changes: 7 additions & 7 deletions tests/_test_utils/test_airdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def _download_progress_airports(path, url):
"US-PA","Bensalem","no","00A",,"00A",,,\
323361,"00AA","small_airport","Aero B Ranch Airport",38.704022,-101.473911,3435,"NA",\
"US","US-KS","Leoti","no","00AA",,"00AA",,,'''
file_path = os.path.join(ROOT_DIR, "airports.csv")
file_path = os.path.join(ROOT_DIR, "downloads", "aip", "airports.csv")
with open(file_path, "w") as f:
f.write(text)

Expand Down Expand Up @@ -75,7 +75,7 @@ def _download_progress_airspace(path, url):
</POLYGON></GEOMETRY></ASP></AIRSPACES>
</OPENAIP>
'''
file_path = os.path.join(ROOT_DIR, "bg_asp.xml")
file_path = os.path.join(ROOT_DIR, "downloads", "aip", "bg_asp.xml")
with open(file_path, "w") as f:
f.write(text)

Expand All @@ -93,24 +93,24 @@ def _download_incomplete_airspace(path, url):
<AIRSPACES></AIRSPACES>
</OPENAIP>
'''
file_path = os.path.join(ROOT_DIR, "bg_asp.xml")
file_path = os.path.join(ROOT_DIR, "downloads", "aip", "bg_asp.xml")
with open(file_path, "w") as f:
f.write(text)


def _cleanup_test_files():
file_path = os.path.join(ROOT_DIR, "bg_asp.xml")
file_path = os.path.join(ROOT_DIR, "downloads", "aip", "bg_asp.xml")
if "tmp" in file_path:
if os.path.exists(file_path):
os.remove(file_path)
file_path = os.path.join(ROOT_DIR, "airports.csv")
file_path = os.path.join(ROOT_DIR, "downloads", "aip", "airports.csv")
if "tmp" in file_path:
if os.path.exists(file_path):
os.remove(file_path)


def test_download_progress():
file_path = os.path.join(ROOT_DIR, "airdata")
file_path = os.path.join(ROOT_DIR, "downloads", "aip", "airdata")
download_progress(file_path, 'http://speedtest.ftp.otenet.gr/files/test100k.db')
assert os.path.exists(file_path)

Expand Down Expand Up @@ -141,7 +141,7 @@ def test_get_available_airspaces():
def test_update_airspace(mockbox):
with mock.patch("mslib.utils.airdata.download_progress", _download_progress_airspace):
update_airspace(force_download=True, countries=["bg"])
example_file = os.path.join(ROOT_DIR, "bg_asp.xml")
example_file = os.path.join(ROOT_DIR, "downloads", "aip", "bg_asp.xml")
os.path.exists(example_file)
with open(example_file, 'r') as f:
text = f.read()
Expand Down

0 comments on commit 3018304

Please sign in to comment.