Commit 4cfe14da authored by Marcus's avatar Marcus
Browse files

new

parent 7aabbe9b
# pylint
# vim: tw=100 foldmethod=indent
# pylint: disable=bad-continuation, invalid-name, superfluous-parens
# pylint: disable=bad-whitespace, mixed-indentation
# pylint: disable=redefined-outer-name, logging-not-lazy, logging-format-interpolation
# pylint: disable=missing-docstring, trailing-whitespace, trailing-newlines, too-few-public-methods
import logging
import sys
from pathlib import Path
from configparser import ConfigParser
from configparser import ExtendedInterpolation
from .parse_args import args
logger = logging.getLogger(__name__)
CONFIG = ConfigParser(interpolation=ExtendedInterpolation())
CONFIG.optionxform = lambda option: option
def set_defaults():
CONFIG.read_dict ({'main': {
'basename' : args.basename,
'logfile' : args.logfile,
'config' : args.config,
# 'port' : args.port,
'loglevel' : args.loglevel,
'verbose' : args.verbose,
'debug' : args.debug,
}})
def load_config():
"""Reload configuration from disk.
Config locations, by priority (first one wins)
"""
files = []
files += [
Path(F'/etc/{args.basename}.conf'),
Path(Path.home(),'.config',F'.{args.basename}conf'),
Path(F'./{args.basename}.conf')
]
try:
files += [ Path(args.config) ]
except FileNotFoundError:
pass
except TypeError:
pass
read_a_config = False
for f in files:
try:
if f.exists():
logger.info("Using this config file: {}".format(f))
CONFIG.read(f)
read_a_config = True
break
except PermissionError:
pass
if not read_a_config:
filelist = [str(f) for f in files]
filestring = "\n ".join(filelist)
logger.warning(F"Warning: Could not read any config file from \n"\
F" {filestring}")
# sys.exit(4)
def test_config():
try:
if CONFIG['main']['basename'] != 'yahapd':
logging.error(F"Config does not appear to be for this program, but for"
F"'{CONFIG['main']['basename']}'")
print (F"Config does not appear to be for this program, but for"
F"'{CONFIG['main']['basename']}'")
sys.exit(1)
delme = CONFIG['main']['logfile']
delme = CONFIG['main']['loglevel']
delme = CONFIG['main']['verbose']
except KeyError as e:
logging.error(F"Cannot find required config entry: {e}")
sys.exit(3)
set_defaults()
load_config()
# test_config()
# pylint
# vim: tw=100 foldmethod=indent
# pylint: disable=bad-continuation, invalid-name, superfluous-parens
# pylint: disable=bad-whitespace, mixed-indentation
# pylint: disable=redefined-outer-name, logging-not-lazy, logging-format-interpolation
# pylint: disable=missing-docstring, trailing-whitespace, trailing-newlines, too-few-public-methods
import os
import sys
import logging
import argparse
logger = logging.getLogger(__name__)
def parseOptions():
'''Parse the commandline options'''
folder_of_executable = os.path.split(sys.argv[0])[0]
basename = os.path.basename(sys.argv[0]).rstrip('.py')
config_dir = os.environ['HOME']+F'/.config/{basename}'
config_file = os.environ['HOME']+F'/.config/{basename}.conf'
# log_file = os.environ['HOME']+F'/.cache/{basename}.log'
log_file = folder_of_executable+F'/{basename}.log'
if not os.path.exists(config_dir):
os.mkdir(config_dir)
parser = argparse.ArgumentParser(description='''inaplot''')
parser.add_argument('--debug', '-d', default=False, action="store_true",
help='Do Debug')
parser.add_argument('--verbose', '-v', default=False, action="store_true",
help='Verbosity')
parser.add_argument('--serial', '-s', default='/dev/ttyUSB0')
parser.add_argument('--baudrate', '--baud', '-b', default=115200)
#### These are needed for internal stuff
parser.add_argument('--config', '-c', default=config_file)
parser.add_argument('--basename', default=basename)
parser.add_argument('--loglevel', default=os.environ.get("LOG", "WARNING").upper())
parser.add_argument('--logfile', default=log_file, help='logfile' )
args = parser.parse_args()
# sanitise args
args.loglevel = args.loglevel.upper()
return args
# reparse args on import
args = parseOptions()
#!/usr/bin/env python3
# pylint: disable=bad-whitespace, invalid-name, missing-docstring
import logging
from mteam_python3_template.config import CONFIG
from mteam_python3_template.parse_args import args
def entrypoint_one():
print("Hello world")
print("There is nothing here")
logging.debug("fine")
# pylint: disable=bad-whitespace, invalid-name, missing-docstring
import pytest
from mteam_python3_template import Aarc_g002_entitlement
class TestAarc_g002_entitlement:
def test_equality(self):
required_group = "urn:geant:h-df.de:group:aai-admin:role = member#unity.helmholtz-data-federation.de"
actual_group = "urn:geant:h-df.de:group:aai-admin:role = member#unity.helmholtz-data-federation.de"
req_entitlement = Aarc_g002_entitlement(required_group)
act_entitlement = Aarc_g002_entitlement(actual_group)
assert act_entitlement == req_entitlement
assert req_entitlement.is_contained_in(act_entitlement)
def test_simple(self):
required_group = "urn:geant:h-df.de:group:aai-admin:role=member#unity.helmholtz-data-federation.de"
actual_group = "urn:geant:h-df.de:group:aai-admin:role=member#backupserver.used.for.developmt.de"
req_entitlement = Aarc_g002_entitlement(required_group)
act_entitlement = Aarc_g002_entitlement(actual_group)
assert req_entitlement.is_contained_in(act_entitlement)
def test_role_not_required(self):
required_group = "urn:geant:h-df.de:group:aai-admin#unity.helmholtz-data-federation.de"
actual_group = "urn:geant:h-df.de:group:aai-admin:role=member#backupserver.used.for.developmt.de"
req_entitlement = Aarc_g002_entitlement(required_group)
act_entitlement = Aarc_g002_entitlement(actual_group)
assert req_entitlement.is_contained_in(act_entitlement)
def test_role_required(self):
required_group = "urn:geant:h-df.de:group:aai-admin:role=member#unity.helmholtz-data-federation.de"
actual_group = "urn:geant:h-df.de:group:aai-admin#backupserver.used.for.developmt.de"
req_entitlement = Aarc_g002_entitlement(required_group)
act_entitlement = Aarc_g002_entitlement(actual_group)
assert not req_entitlement.is_contained_in(act_entitlement)
def test_subgroup_required(self):
required_group = "urn:geant:h-df.de:group:aai-admin:special-admins#unity.helmholtz-data-federation.de"
actual_group = (
"urn:geant:h-df.de:group:aai-admin#backupserver.used.for.developmt.de"
)
req_entitlement = Aarc_g002_entitlement(required_group)
act_entitlement = Aarc_g002_entitlement(actual_group)
assert not req_entitlement.is_contained_in(act_entitlement)
def test_user_in_subgroup(self):
required_group = "urn:geant:h-df.de:group:aai-admin"
actual_group = "urn:geant:h-df.de:group:aai-admin:special-admins#backupserver.used.for.developmt.de"
req_entitlement = Aarc_g002_entitlement(required_group, strict=False)
act_entitlement = Aarc_g002_entitlement(actual_group)
assert req_entitlement.is_contained_in(act_entitlement)
def test_role_required_for_supergroup(self):
required_group = "urn:geant:h-df.de:group:aai-admin:role=admin#unity.helmholtz-data-federation.de"
actual_group = "urn:geant:h-df.de:group:aai-admin:special-admins:role=admin#backupserver.used.for.developmt.de"
req_entitlement = Aarc_g002_entitlement(required_group)
act_entitlement = Aarc_g002_entitlement(actual_group)
assert not req_entitlement.is_contained_in(act_entitlement)
@pytest.mark.parametrize(
'required_group,actual_group',
[
("urn:geant:h-df.de:group:aai-admin", "urn:geant:kit.edu:group:bwUniCluster"),
("urn:geant:h-df.de:group:myExampleColab#unity.helmholtz-data-federation.de", "urn:geant:kit.edu:group:bwUniCluster"),
("urn:geant:h-df.de:group:aai-admin", "urn:geant:kit.edu:group:aai-admin"),
]
)
def test_foreign_entitlement(self, required_group, actual_group):
actual_group = "urn:geant:kit.edu:group:bwUniCluster"
req_entitlement = Aarc_g002_entitlement(required_group, strict=False)
act_entitlement = Aarc_g002_entitlement(actual_group, strict=False)
assert not req_entitlement.is_contained_in(act_entitlement)
# #
@pytest.mark.parametrize(
'actual_group',
[
"urn:mace:dir:entitlement:common-lib-terms",
"urn:geant:kit.edu:group:DFN-SLCS",
]
)
def test_non_aarc_entitlement(self, actual_group):
required_group = "urn:geant:h-df.de:group:aai-admin"
req_entitlement = Aarc_g002_entitlement(required_group, strict=False)
act_entitlement = Aarc_g002_entitlement(actual_group, strict=False, force=False)
assert not req_entitlement.is_contained_in(act_entitlement)
@pytest.mark.parametrize(
'required_group',
[
"urn:geant:h-df.de:group:aai-admin:role=admin",
"urn:geant:h-df.de:group:aai-admin",
]
)
def test_failure_incomplete_but_valid_entitlement(self, required_group):
Aarc_g002_entitlement(required_group, strict=False)
def test_failure_incomplete_invalid_entitlement(self):
required_group = "urn:geant:h-df.de"
with pytest.raises(ValueError):
Aarc_g002_entitlement(required_group)
Supports Markdown
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