Commit 46a886ac authored by Maximilian Schanner's avatar Maximilian Schanner
Browse files

Sketch for local models path handling.

parent eb1936ae
......@@ -22,6 +22,9 @@ command line interface and when using pythons -m switch.
'''
import sys
import os
from pathlib import Path
import argparse
from pymagglobal import _commands
......@@ -62,6 +65,8 @@ _models = built_in_models()
listing names and paths to the included models.
'''
_local_models_fname = ".additional_model_folders.pth"
class ListModelsAction(argparse.Action):
'''The action class that will produce a list of available models.
......@@ -85,6 +90,65 @@ class ListModelsAction(argparse.Action):
parser.exit()
class ResetPathAction(argparse.Action):
def __init__(self, option_strings, dest=argparse.SUPPRESS,
default=argparse.SUPPRESS, help=help):
super().__init__(option_strings=option_strings,
dest=dest,
default=default,
nargs=0,
help=help)
def __call__(self, parser, namespace, values, option_string=None):
path = Path(__file__).parent.parent
if os.path.isfile(path / _local_models_fname):
os.remove(path / _local_models_fname)
parser.exit()
class AddPathAction(argparse.Action):
def __init__(self, option_strings, dest=argparse.SUPPRESS,
default=argparse.SUPPRESS, help=help, **kwargs):
super().__init__(option_strings=option_strings,
dest=dest,
default=default,
nargs=1,
help=help,
**kwargs)
def __call__(self, parser, namespace, values, option_string=None):
path = Path(__file__).parent.parent
pth = os.path.realpath(os.getcwd() + '/' + values[0])
if not os.path.isdir(pth):
raise ValueError("Path to be appended has to be a folder!")
if os.path.isfile(path / _local_models_fname):
pth = '\n' + pth
with open(path / _local_models_fname, 'a') as fh:
fh.write(pth)
parser.exit()
class ListPathAction(argparse.Action):
def __init__(self, option_strings, dest=argparse.SUPPRESS,
default=argparse.SUPPRESS, help=help, **kwargs):
super().__init__(option_strings=option_strings,
dest=dest,
default=default,
nargs=0,
help=help)
def __call__(self, parser, namespace, values, option_string=None):
path = Path(__file__).parent.parent
if os.path.isfile(path / _local_models_fname):
with open(path / _local_models_fname, 'r') as fh:
for line in fh.readlines():
print(line, end='')
print(os.path.isdir(line[:-1]))
else:
print(None)
parser.exit()
class ModelAction(argparse.Action):
'''Custom action for parsing the positional argument `model`.
'''
......@@ -128,6 +192,15 @@ def argument_parser():
version=f'pymagglobal {__version__}')
parser.add_argument('--list-models', help=f'List available models and '
f'exit.', action=ListModelsAction)
parser.add_argument('--add-path', metavar='<path/to/folder>', type=str,
help=f'Add a local folder with models, '
f'that will be searched for models.',
action=AddPathAction)
parser.add_argument('--reset-path', help=f'Reset the model-folder path.',
action=ResetPathAction)
parser.add_argument('--list-path', help=f'List all user-defined '
f'model-folder paths.',
action=ListPathAction)
# parser for arguments that are shared between all commands
base_parser = argparse.ArgumentParser(add_help=False)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment