-
Notifications
You must be signed in to change notification settings - Fork 0
/
magneticfielddb.py
45 lines (38 loc) · 1.67 KB
/
magneticfielddb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""Storage and Access to magnetic field coefficients for Feltor """
import json
import os.path
# MW: After two days of trying to get importlib_resources to run I give up on it
# even though it is supposed to be the faster and more modern option
# import importlib_resources as res
import pkg_resources as res
def select( path, *paths) :
""" Select a file in the data folder
The file is then opened and its contents returned as a dictionary
Parameters:
path, *paths (pathlike): one or more path indicators to a file
relative to path/to/magneticfielddb/data (forwarded to
os.path.join(path,*paths) )
Return:
dict : A dictionary containing the contents of the chosen file
"""
ref = res.resource_string( "magneticfielddb",
os.path.join( "data", path, *paths)).decode("utf-8")
# resrouce_string returns a string of bytes (not an actual string)
return json.loads( ref)
def files() :
""" Create a list of available files
Return:
list: A list of file paths relative to path/to/magneticfielddb/data
of all files in the data repository
each item in the list can be passed to the select function
"""
base = "magneticfielddb"
file_list = list()
def inner_list_files( directory_name, file_list) :
for f in res.resource_listdir(base,directory_name) :
if res.resource_isdir(base, os.path.join( directory_name, f) ) :
inner_list_files( os.path.join( directory_name, f), file_list)
else :
file_list.append( os.path.join( os.path.relpath( directory_name, "data"), f) )
inner_list_files( "data", file_list)
return file_list