Passed
Push — master ( b71dca...46f9a9 )
by Stefan
04:13
created

spaceweather.core._resource_filepath()   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nop 2
dl 0
loc 11
rs 9.85
c 0
b 0
f 0
1
# Copyright (c) 2020--2022 Stefan Bender
2
#
3
# This module is part of pyspaceweather.
4
# pyspaceweather is free software: you can redistribute it or modify
5
# it under the terms of the GNU General Public License as published
6
# by the Free Software Foundation, version 2.
7
# See accompanying COPYING.GPLv2 file or http://www.gnu.org/licenses/gpl-2.0.html.
8
"""Python interface for space weather indices
9
10
General file handling functions for space weather data
11
"""
12
import errno
13
import os
14
15
import requests
16
17
18
def _assert_file_exists(f):
19
	if not os.path.exists(f):
20
		raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), f)
21
22
23
def _dl_file(swpath, url):
24
	with requests.get(url, stream=True) as r:
25
		if r.status_code != requests.codes.ok:
26
			return
27
		with open(swpath, 'wb') as fd:
28
			for chunk in r.iter_content(chunk_size=1024):
29
				fd.write(chunk)
30
31
32
def _resource_filepath(file, subdir="data"):
33
	try:
34
		from contextlib import ExitStack
35
		from importlib import resources
36
		file_manager = ExitStack()
37
		ref = resources.files(__package__) / subdir / file
38
		filepath = file_manager.enter_context(resources.as_file(ref))
39
	except (AttributeError, ImportError):
40
		from pkg_resources import resource_filename
41
		filepath = resource_filename(__package__, os.path.join(subdir, file))
42
	return filepath
43