Skip to content

Commit

Permalink
WIP: Commander Python API
Browse files Browse the repository at this point in the history
  • Loading branch information
tommydeboer committed Jun 17, 2022
1 parent fc51012 commit eac721c
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 3 deletions.
8 changes: 8 additions & 0 deletions mcmd/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ def start(argv, context: Context):
return 0


def start_detached(argv, context: Context):
with context:
args = _parse_args(argv)
setattr(args, 'arg_string', ' '.join(argv[1:]))
args.func(args)
return 0


def _parse_args(argv):
try:
return parse_args(argv[1:])
Expand Down
77 changes: 77 additions & 0 deletions mcmd/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from pathlib import Path
from typing import List

from mcmd.__main__ import start_detached
from mcmd.config import config
from mcmd.core.context import Context

from mcmd.core.errors import ApiError


class EmptyContext(Context):
def get_scripts_folder(self) -> Path:
self._raise()

def get_backups_folder(self) -> Path:
self._raise()

def get_issues_folder(self) -> Path:
self._raise()

def get_history_file(self) -> Path:
return None

def get_dataset_folders(self) -> List[Path]:
self._raise()

def get_resource_folders(self) -> List[Path]:
self._raise()

def get_git_folders(self) -> List[Path]:
self._raise()

def get_properties_file(self) -> Path:
self._raise()

def get_storage_file(self) -> Path:
self._raise()

def _raise(self):
raise ApiError("Can't access context paths using the API.")


class Commander:

# TODO pass token, or better: a molgenis.client.Session
def __init__(self):
config.set_config(
{
"host":
{
"selected": "http://localhost",
"auth": [
{
"url": "http://localhost",
"username": "xxx",
"password": "xxx",
"token": "xxx"
}
]
},
"settings":
{
"import_action": "add_update_existing",
"non_interactive": True
}
},
None)
pass

@staticmethod
def execute(arg_string):
exit_code = start_detached(['mcmd'] + arg_string.split(' '), EmptyContext())
assert exit_code == 0


commander = Commander()
commander.execute("import --from-path it_emx_autoid")
7 changes: 5 additions & 2 deletions mcmd/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
_config = None
_properties_file: Optional[Path] = None

# TODO make Config an object similar to Context so you can have multiple types:
# TODO PersistedConfig / TestConfig / ApiConfig

def set_config(config, properties_file):
def set_config(config: dict, properties_file):
"""The config module must not have dependencies on other modules in the config package so the necessary information
should be passed here."""
global _config
Expand All @@ -33,7 +35,8 @@ def set_config(config, properties_file):

def _persist():
"""Writes the config to disk."""
YAML().dump(_config, _properties_file)
if _properties_file:
YAML().dump(_config, _properties_file)


def get(*args):
Expand Down
5 changes: 5 additions & 0 deletions mcmd/core/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ def wrapper(args, nested=False):
:return: a decorated function
"""

# TODO detect when running via Api and:
# TODO - don't write history
# TODO - don't catch the McmdErrors and just let them be raised

success = True
try:
_set_authentication(args)
Expand Down Expand Up @@ -60,6 +64,7 @@ def wrapper(args, nested=False):

def _set_authentication(args):
if args.as_user:
# TODO create a temporary alternative molgenis.client.Session
if args.with_password:
auth.set_(username=args.as_user, password=args.with_password, as_user=True)
else:
Expand Down
4 changes: 4 additions & 0 deletions mcmd/core/errors.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from mcmd.config import config


class ApiError(Exception):
pass


class McmdError(Exception):
def __init__(self, message, info: str = None):
"""
Expand Down
7 changes: 6 additions & 1 deletion mcmd/core/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@

def write(arg_string, success):
try:
history = open(str(context().get_history_file()), 'a')
# TODO handle in command.py
file = context().get_history_file()
if not file:
return

history = open(str(file), 'a')

indicator = _INDICATOR_SUCCESS
if not success:
Expand Down
1 change: 1 addition & 0 deletions mcmd/molgenis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from mcmd.molgenis import auth
from mcmd.molgenis.request_handler import request

# TODO use a molgenis.client.Session

@request
def get(url, params=None) -> Response:
Expand Down

0 comments on commit eac721c

Please sign in to comment.