def test_config_subst_cmdline(tmpconfigfile): """Use a format string in a configuration variable. Same as above, but set the referenced variable from the command line. """ args = [ "-c", tmpconfigfile.path, "-s", "example_jdoe", "-u", "jonny", "-p", "pass" ] config = icat.config.Config() config.add_variable('greeting', ("--greeting", ), dict(help="Greeting message"), subst=True) conf = config.getconfig(args) attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_'] assert attrs == [ 'auth', 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'credentials', 'greeting', 'http_proxy', 'https_proxy', 'no_proxy', 'password', 'promptPass', 'url', 'username' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.auth == "ldap" assert conf.username == "jonny" assert conf.password == "pass" assert conf.promptPass == False assert conf.greeting == "Hello jonny!" assert conf.credentials == {'username': '******', 'password': '******'}
def test_config_no_defaultvars(tmpconfigfile, monkeypatch): """Config object with no default variables. If `defaultvars=False` is passed to the constructor of Config, no default configuration variables will be defined other then `configFile` and `configSection`. The configuration mechanism is still intact. In particular, custom configuration variables may be defined and reading the configuration file still works. """ # Manipulate the default search path. monkeypatch.setenv("HOME", tmpconfigfile.home) cfgdirs = [ os.path.expanduser("~/.config/icat"), os.path.expanduser("~/.icat"), "", ] monkeypatch.setattr(icat.config, "cfgdirs", cfgdirs) monkeypatch.chdir(tmpconfigfile.home) args = ["-s", "example_root"] config = icat.config.Config(defaultvars=False, args=args) config.add_variable('url', ("-w", "--url"), dict(help="URL to the web service description")) config.add_variable('wobble', ("--wobble",), dict(help="Strange thing"), optional=True) _, conf = config.getconfig() ex = ExpectedConf(configFile=[tmpconfigfile.path], configSection="example_root", url=ex_icat, wobble=None) assert ex <= conf
def test_config_subst_cmdline(fakeClient, tmpconfigfile): """Use a format string in a configuration variable. Same as above, but set the referenced variable from the command line. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", "-u", "jonny", "-p", "pass"] config = icat.config.Config(args=args) config.add_variable('greeting', ("--greeting",), dict(help="Greeting message"), subst=True) _, conf = config.getconfig() ex = ExpectedConf(configFile=[tmpconfigfile.path], configSection="example_jdoe", url=ex_icat, auth="ldap", username="******", password="******", promptPass=False, greeting="Hello jonny!", credentials={'username': '******', 'password': '******'}) assert ex <= conf
def test_config_positional(tmpconfigfile): """Test adding a positional argument on the command line. (There used to be a bug in adding positional arguments, fixed in 7d10764.) """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", "test.dat"] config = icat.config.Config() config.add_variable( 'datafile', ("datafile", ), dict(metavar="input.dat", help="name of the input datafile")) conf = config.getconfig(args) attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_'] assert attrs == [ 'auth', 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'credentials', 'datafile', 'http_proxy', 'https_proxy', 'no_proxy', 'password', 'promptPass', 'url', 'username' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.auth == "ldap" assert conf.username == "jdoe" assert conf.password == "pass" assert conf.promptPass == False assert conf.credentials == {'username': '******', 'password': '******'} assert conf.datafile == "test.dat"
def test_config_subst_cmdline(tmpconfigfile): """Use a format string in a configuration variable. Same as above, but set the referenced variable from the command line. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", "-u", "jonny", "-p", "pass"] config = icat.config.Config() config.add_variable('greeting', ("--greeting",), dict(help="Greeting message"), subst=True) conf = config.getconfig(args) attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ] assert attrs == [ 'auth', 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'credentials', 'greeting', 'http_proxy', 'https_proxy', 'no_proxy', 'password', 'promptPass', 'url', 'username' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.auth == "ldap" assert conf.username == "jonny" assert conf.password == "pass" assert conf.promptPass == False assert conf.greeting == "Hello jonny!" assert conf.credentials == {'username': '******', 'password': '******'}
def test_config_positional(tmpconfigfile): """Test adding a positional argument on the command line. (There used to be a bug in adding positional arguments, fixed in 7d10764.) """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", "test.dat"] config = icat.config.Config() config.add_variable('datafile', ("datafile",), dict(metavar="input.dat", help="name of the input datafile")) conf = config.getconfig(args) attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ] assert attrs == [ 'auth', 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'credentials', 'datafile', 'http_proxy', 'https_proxy', 'no_proxy', 'password', 'promptPass', 'url', 'username' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.auth == "ldap" assert conf.username == "jdoe" assert conf.password == "pass" assert conf.promptPass == False assert conf.credentials == {'username': '******', 'password': '******'} assert conf.datafile == "test.dat"
def test_config_type_flag(fakeClient, tmpconfigfile, flags, ex): """Test the special configuration variable type flag. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] + flags config = icat.config.Config(needlogin=False, args=args) config.add_variable('flag1', ("--flag1",), dict(help="Flag 1"), type=icat.config.flag) config.add_variable('flag2', ("--flag2",), dict(help="Flag 2"), type=icat.config.flag) _, conf = config.getconfig() assert ex <= conf
def test_config_type_int_err(tmpconfigfile): """Read an integer variable from the configuration file. Same as last one, but have an invalid value this time. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(needlogin=False) config.add_variable('invnum', ("--invnum",), dict(help="Integer variable"), type=int) with pytest.raises(icat.exception.ConfigError) as err: conf = config.getconfig(args) assert 'invalid int value' in str(err.value)
def test_config_type_boolean(fakeClient, tmpconfigfile, flags, ex): """Test a boolean configuration variable. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] + flags config = icat.config.Config(needlogin=False, args=args) config.add_variable('flag1', ("--flag1",), dict(help="Flag 1", action='store_const', const=True), type=icat.config.boolean) config.add_variable('flag2', ("--flag2",), dict(help="Flag 2", action='store_const', const=True), type=icat.config.boolean) _, conf = config.getconfig() assert ex <= conf
def test_config_type_int_err(tmpconfigfile): """Read an integer variable from the configuration file. Same as last one, but have an invalid value this time. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(needlogin=False) config.add_variable('invnum', ("--invnum", ), dict(help="Integer variable"), type=int) with pytest.raises(icat.exception.ConfigError) as err: conf = config.getconfig(args) assert 'invalid int value' in str(err.value)
def test_config_type_int(fakeClient, tmpconfigfile): """Read an integer variable from the configuration file. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(needlogin=False, args=args) config.add_variable('num', ("--num",), dict(help="Integer variable"), type=int) _, conf = config.getconfig() ex = ExpectedConf(configFile=[tmpconfigfile.path], configSection="example_jdoe", url=ex_icat, num=42) assert ex <= conf
def test_config_type_flag(tmpconfigfile): """Test the special configuration variable type flag. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(needlogin=False) config.add_variable('flag1', ("--flag1", ), dict(help="Flag 1"), type=icat.config.flag) config.add_variable('flag2', ("--flag2", ), dict(help="Flag 2"), type=icat.config.flag) conf = config.getconfig(args) attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_'] assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'flag1', 'flag2', 'http_proxy', 'https_proxy', 'no_proxy', 'url' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.flag1 == True assert conf.flag2 == False # Now override the flags from the command line args = [ "-c", tmpconfigfile.path, "-s", "example_jdoe", "--no-flag1", "--flag2" ] conf = config.getconfig(args) attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_'] assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'flag1', 'flag2', 'http_proxy', 'https_proxy', 'no_proxy', 'url' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.flag1 == False assert conf.flag2 == True
def test_config_cfgpath_cmdline(fakeClient, tmpconfigfile, monkeypatch, tmpfiles, abspath): """Test a cfgpath configuration variable. Same as test_config_cfgpath_cwd() but override the path on the command line. """ # Manipulate the default search path. monkeypatch.setenv("HOME", tmpconfigfile.home) cfgdirs = [ os.path.expanduser("~/.config/icat"), os.path.expanduser("~/.icat"), "", ] monkeypatch.setattr(icat.config, "cfgdirs", cfgdirs) monkeypatch.chdir(tmpconfigfile.home) cpath = os.path.expanduser("~/.config/icat/control.dat") tmpfiles.addfile(cpath, "control config dir\n") hpath = os.path.join(tmpconfigfile.home, "control.dat") tmpfiles.addfile(hpath, "control home\n") if abspath: apath = os.path.expanduser("~/custom/cl.dat") cfarg = apath else: apath = os.path.expanduser("~/.config/icat/cl.dat") cfarg = "cl.dat" tmpfiles.addfile(apath, "control cmdline\n") args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", "--control", cfarg] config = icat.config.Config(args=args) config.add_variable('controlfile', ("--control",), dict(metavar="control.dat", help="control file"), default="control.dat", type=icat.config.cfgpath) _, conf = config.getconfig() ex = ExpectedConf(configFile=[tmpconfigfile.path], configSection="example_jdoe", url=ex_icat, auth="ldap", username="******", password="******", promptPass=False, credentials={'username': '******', 'password': '******'}, controlfile=apath) assert ex <= conf assert os.path.isfile(conf.controlfile)
def test_config_subst_confdir(fakeClient, tmpconfigfile): """Substitute configDir in the default of a variable. Note that configDir is deprecated and will be removed in version 1.0. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(needlogin=False, args=args) config.add_variable('extracfg', ("--extracfg",), dict(help="Extra config file"), default="%(configDir)s/extra.xml", subst=True) _, conf = config.getconfig() ex = ExpectedConf(configFile=[tmpconfigfile.path], configDir=tmpconfigfile.dir, configSection="example_jdoe", url=ex_icat) assert ex <= conf assert os.path.dirname(conf.extracfg) == tmpconfigfile.dir
def test_config_type_int(tmpconfigfile): """Read an integer variable from the configuration file. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(needlogin=False) config.add_variable('num', ("--num",), dict(help="Integer variable"), type=int) conf = config.getconfig(args) attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ] assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'http_proxy', 'https_proxy', 'no_proxy', 'num', 'url' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.num == 42
def test_config_type_boolean(tmpconfigfile): """Test a boolean configuration variable. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(needlogin=False) config.add_variable('flag1', ("--flag1",), dict(help="Flag 1", action='store_const', const=True), type=icat.config.boolean) config.add_variable('flag2', ("--flag2",), dict(help="Flag 2", action='store_const', const=True), type=icat.config.boolean) conf = config.getconfig(args) attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ] assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'flag1', 'flag2', 'http_proxy', 'https_proxy', 'no_proxy', 'url' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.flag1 == True assert conf.flag2 == False # Now override flag2 from the command line args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", "--flag2"] conf = config.getconfig(args) attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ] assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'flag1', 'flag2', 'http_proxy', 'https_proxy', 'no_proxy', 'url' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.flag1 == True assert conf.flag2 == True
def test_config_subst_confdir(tmpconfigfile): """Substitute configDir in the default of a variable. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(needlogin=False) config.add_variable('extracfg', ("--extracfg",), dict(help="Extra config file"), default="%(configDir)s/extra.xml", subst=True) conf = config.getconfig(args) attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ] assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'extracfg', 'http_proxy', 'https_proxy', 'no_proxy', 'url' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert os.path.dirname(conf.extracfg) == tmpconfigfile.dir
def test_config_custom_var(fakeClient, tmpconfigfile): """Define custom configuration variables. """ # Note that ldap_filter is not defined in the configuration file, # but we have a default value defined here, so this is ok. args = ["-c", tmpconfigfile.path, "-s", "example_root"] config = icat.config.Config(args=args) config.add_variable('ldap_uri', ("-l", "--ldap-uri"), dict(help="URL of the LDAP server"), envvar='LDAP_URI') config.add_variable('ldap_base', ("-b", "--ldap-base"), dict(help="base DN for searching the LDAP server"), envvar='LDAP_BASE') config.add_variable('ldap_filter', ("-f", "--ldap-filter"), dict(help="search filter to select the user entries"), default='(uid=*)') _, conf = config.getconfig() ex = ExpectedConf(configFile=[tmpconfigfile.path], configSection="example_root", url=ex_icat, idsurl=ex_ids, auth="simple", username="******", password="******", promptPass=False, ldap_uri="ldap://ldap.example.com", ldap_base="ou=People,dc=example,dc=com", ldap_filter="(uid=*)", credentials={'username': '******', 'password': '******'}) assert ex <= conf
def test_config_type_int(tmpconfigfile): """Read an integer variable from the configuration file. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(needlogin=False) config.add_variable('num', ("--num", ), dict(help="Integer variable"), type=int) conf = config.getconfig(args) attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_'] assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'http_proxy', 'https_proxy', 'no_proxy', 'num', 'url' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.num == 42
def test_config_subst_nosubst(fakeClient, tmpconfigfile): """Use a format string in a configuration variable. But disable the substitution. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(args=args) config.add_variable('greeting', ("--greeting",), dict(help="Greeting message"), subst=False) _, conf = config.getconfig() ex = ExpectedConf(configFile=[tmpconfigfile.path], configSection="example_jdoe", url=ex_icat, auth="ldap", username="******", password="******", promptPass=False, greeting="Hello %(username)s!", credentials={'username': '******', 'password': '******'}) assert ex <= conf
def test_config_cfgpath_default(fakeClient, tmpconfigfile, monkeypatch, tmpfiles): """Test a cfgpath configuration variable. This searches a file in the default configuration directories. Feature added in Issue #30. """ # Manipulate the default search path. monkeypatch.setenv("HOME", tmpconfigfile.home) cfgdirs = [ os.path.expanduser("~/.config/icat"), os.path.expanduser("~/.icat"), "", ] monkeypatch.setattr(icat.config, "cfgdirs", cfgdirs) monkeypatch.chdir(tmpconfigfile.home) cpath = os.path.expanduser("~/.config/icat/control.dat") tmpfiles.addfile(cpath, "control\n") args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(args=args) config.add_variable('controlfile', ("--control",), dict(metavar="control.dat", help="control file"), default="control.dat", type=icat.config.cfgpath) _, conf = config.getconfig() ex = ExpectedConf(configFile=[tmpconfigfile.path], configSection="example_jdoe", url=ex_icat, auth="ldap", username="******", password="******", promptPass=False, credentials={'username': '******', 'password': '******'}, controlfile=cpath) assert ex <= conf assert os.path.isfile(conf.controlfile)
def test_config_positional(fakeClient, tmpconfigfile): """Test adding a positional argument on the command line. (There used to be a bug in adding positional arguments, fixed in 7d10764.) """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe", "test.dat"] config = icat.config.Config(args=args) config.add_variable('datafile', ("datafile",), dict(metavar="input.dat", help="name of the input datafile")) _, conf = config.getconfig() ex = ExpectedConf(configFile=[tmpconfigfile.path], configSection="example_jdoe", url=ex_icat, auth="ldap", username="******", password="******", promptPass=False, credentials={'username': '******', 'password': '******'}, datafile="test.dat") assert ex <= conf
def test_config_subst_confdir(tmpconfigfile): """Substitute configDir in the default of a variable. """ args = ["-c", tmpconfigfile.path, "-s", "example_jdoe"] config = icat.config.Config(needlogin=False) config.add_variable('extracfg', ("--extracfg", ), dict(help="Extra config file"), default="%(configDir)s/extra.xml", subst=True) conf = config.getconfig(args) attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_'] assert attrs == [ 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'extracfg', 'http_proxy', 'https_proxy', 'no_proxy', 'url' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_jdoe" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert os.path.dirname(conf.extracfg) == tmpconfigfile.dir
def test_config_custom_var(tmpconfigfile): """Define custom configuration variables. """ # Note that ldap_filter is not defined in the configuration file, # but we have a default value defined here, so this is ok. args = ["-c", tmpconfigfile.path, "-s", "example_root"] config = icat.config.Config() config.add_variable('ldap_uri', ("-l", "--ldap-uri"), dict(help="URL of the LDAP server"), envvar='LDAP_URI') config.add_variable('ldap_base', ("-b", "--ldap-base"), dict(help="base DN for searching the LDAP server"), envvar='LDAP_BASE') config.add_variable('ldap_filter', ("-f", "--ldap-filter"), dict(help="search filter to select the user entries"), default='(uid=*)') conf = config.getconfig(args) attrs = [a for a in sorted(conf.__dict__.keys()) if a[0] != '_'] assert attrs == [ 'auth', 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'credentials', 'http_proxy', 'https_proxy', 'ldap_base', 'ldap_filter', 'ldap_uri', 'no_proxy', 'password', 'promptPass', 'url', 'username' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_root" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.auth == "simple" assert conf.username == "root" assert conf.password == "secret" assert conf.promptPass == False assert conf.ldap_uri == "ldap://ldap.example.com" assert conf.ldap_base == "ou=People,dc=example,dc=com" assert conf.ldap_filter == "(uid=*)" assert conf.credentials == {'username': '******', 'password': '******'}
def test_config_custom_var(tmpconfigfile): """Define custom configuration variables. """ # Note that ldap_filter is not defined in the configuration file, # but we have a default value defined here, so this is ok. args = ["-c", tmpconfigfile.path, "-s", "example_root"] config = icat.config.Config() config.add_variable('ldap_uri', ("-l", "--ldap-uri"), dict(help="URL of the LDAP server"), envvar='LDAP_URI') config.add_variable('ldap_base', ("-b", "--ldap-base"), dict(help="base DN for searching the LDAP server"), envvar='LDAP_BASE') config.add_variable('ldap_filter', ("-f", "--ldap-filter"), dict(help="search filter to select the user entries"), default='(uid=*)') conf = config.getconfig(args) attrs = [ a for a in sorted(conf.__dict__.keys()) if a[0] != '_' ] assert attrs == [ 'auth', 'checkCert', 'client_kwargs', 'configDir', 'configFile', 'configSection', 'credentials', 'http_proxy', 'https_proxy', 'ldap_base', 'ldap_filter', 'ldap_uri', 'no_proxy', 'password', 'promptPass', 'url', 'username' ] assert conf.configFile == [tmpconfigfile.path] assert conf.configDir == tmpconfigfile.dir assert conf.configSection == "example_root" assert conf.url == "https://icat.example.com/ICATService/ICAT?wsdl" assert conf.auth == "simple" assert conf.username == "root" assert conf.password == "secret" assert conf.promptPass == False assert conf.ldap_uri == "ldap://ldap.example.com" assert conf.ldap_base == "ou=People,dc=example,dc=com" assert conf.ldap_filter == "(uid=*)" assert conf.credentials == {'username': '******', 'password': '******'}
pass try: import icat.dumpfile_yaml except ImportError: pass logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) formats = icat.dumpfile.Backends.keys() if len(formats) == 0: raise RuntimeError("No datafile backends available.") config = icat.config.Config() config.add_variable('file', ("-o", "--outputfile"), dict(help="output file name or '-' for stdout"), default='-') config.add_variable('format', ("-f", "--format"), dict(help="output file format", choices=formats), default='YAML') conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) if client.apiversion < '4.2.99': raise RuntimeError( "Sorry, ICAT version %s is too old, need 4.3.0 or newer." % client.apiversion) client.login(conf.auth, conf.credentials) # The data is written in chunks, see the documentation of # icat.dumpfile for details why this is needed. The partition used
# run by an ICAT user having write permissions on the investigation, # e.g. a user that is in the writer group of the given investigation. # from __future__ import print_function import icat import icat.config import sys import logging import yaml logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) config = icat.config.Config() config.add_variable('skipfiles', ("--skipdatafiles",), dict(help="skip adding Datafiles", action='store_true')) config.add_variable('datafile', ("datafile",), dict(metavar="inputdata.yaml", help="name of the input datafile")) config.add_variable('investigationname', ("investigationname",), dict(help="name of the investigation to add")) client, conf = config.getconfig() client.login(conf.auth, conf.credentials) # ------------------------------------------------------------ # Helper functions # ------------------------------------------------------------ def initobj(obj, attrs): """Initialize an entity object from a dict of attributes."""
from helper import Unbuffered, DatafileBase, DatasetBase, MemorySpace logging.basicConfig(level=logging.INFO) log = logging.getLogger("upload-helper") # Report all uncaught exceptions to the log. def exchandler(type, value, traceback): log.critical("%s: %s" % (type.__name__, value)) sys.excepthook = exchandler config = icat.config.Config(ids="mandatory") config.add_variable('logfile', ("--logfile", ), dict(help="logfile name template"), optional=True) config.add_variable('fileCount', ("--fileCount", ), dict(help="number of data files in the dataset"), type=int, default=4) config.add_variable('fileSize', ("--fileSize", ), dict(help="size of the data files"), type=MemorySpace, default="20 MiB") config.add_variable('source', ("--source", ), dict(choices=["zero", "urandom", "file"], help="data source"), default="zero") config.add_variable('investigation', ("investigation", ), dict(help="name of the investigation"))
#! /usr/bin/python from __future__ import print_function import icat import icat.config import sys import logging from icat.icatcheck import * logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) #logging.getLogger('icat.icatcheck').setLevel(logging.DEBUG) config = icat.config.Config(needlogin=False) config.add_variable('test', ("-t", "--test"), dict(help="test consistency of the ICAT client with the server", action='store_true')) config.add_variable('python', ("-p", "--python"), dict(help="Generate Python source code that match the server", action='store_true')) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) checker = ICATChecker(client) retcode = 0 if conf.test: nwarn = checker.check() nwarn += checker.checkExceptions() if nwarn:
import zlib import zipfile import logging import time from icat.query import Query from icat.hzb.storage import MainFileStorage, ArchiveFileStorage from icat.hzb.proposal import ProposalNo from icat.hzb.fileformats import FormatChecker logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) log = logging.getLogger(__name__) log.setLevel(logging.DEBUG) config = icat.config.Config() config.add_variable('mainStorageBase', ("--mainStorageBase", ), dict(help="base directory of the main storage area")) config.add_variable('archiveStorageBase', ("--archiveStorageBase", ), dict(help="base directory of the archive storage area")) config.add_variable('datafileFormats', ("--datafileformats", ), dict(help="configuration file for DatafileFormats"), default="%(configDir)s/fileformats.xml", subst=True) config.add_variable('investigation', ("investigation", ), dict(help="the proposal number")) config.add_variable('dataset', ("dataset", ), dict(help="name of the dataset")) config.add_variable('files', ("files", ), dict(help="name of the files to add", nargs="+")) client, conf = config.getconfig() client.login(conf.auth, conf.credentials)
import logging import icat import icat.config from icat.query import Query from icat.dumpfile import open_dumpfile import icat.dumpfile_xml import icat.dumpfile_yaml logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) formats = icat.dumpfile.Backends.keys() config = icat.config.Config() config.add_variable('file', ("-o", "--outputfile"), dict(help="output file name or '-' for stdout"), default='-') config.add_variable('format', ("-f", "--format"), dict(help="output file format", choices=formats), default='YAML') config.add_variable('investigation', ("investigation",), dict(help="name and optionally visit id " "(separated by a colon) of the investigation")) client, conf = config.getconfig() if client.apiversion < '4.3.99': raise RuntimeError("Sorry, ICAT version %s is too old, need 4.4.0 or newer." % client.apiversion) client.login(conf.auth, conf.credentials)
# from __future__ import print_function from serializer import XMLSerializer from sys import exit from icat import config, Client from datetime import datetime, timedelta from re import sub, match from os import listdir import logging logging.basicConfig(level=logging.INFO) logging.getLogger('suds.client').setLevel(logging.CRITICAL) config = config.Config(needlogin=False) config.add_variable('sessionid', ("--sessionid", ), dict(help="ICAT session id")) conf = config.getconfig() client = Client(conf.url, **conf.client_kwargs) client.sessionId = conf.sessionid client.autoLogout = False # make dictionary from setup.properties properties = {} with open('../setup.properties', "r") as properties_file: for line in properties_file: if '=' in line: k, v = line.split('=') v = v.strip() properties[k] = sub(
# present. # # This script should be run by the ICAT user useroffice. # import icat import icat.config import ldap import logging import re logging.basicConfig(level=logging.INFO) config = icat.config.Config() config.add_variable('ldap_uri', ("-l", "--ldap-uri"), dict(help="URL of the LDAP server"), envvar='LDAP_URI') config.add_variable('ldap_base', ("-b", "--ldap-base"), dict(help="base DN for searching the LDAP server"), envvar='LDAP_BASE') config.add_variable('ldap_filter', ("-f", "--ldap-filter"), dict(help="search filter to select the user entries"), default='(uid=*)') conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) client.login(conf.auth, conf.credentials) icatuser = {u.name: u for u in client.search("User")} ldapclient = ldap.initialize(conf.ldap_uri)
import sys import os import json import re import logging import requests import icat import icat.config from icat.exception import translateError logging.basicConfig(level=logging.INFO) logging.getLogger('requests.packages.urllib3').setLevel(logging.WARNING) config = icat.config.Config() config.add_variable('resturl', ("--resturl",), dict(help="URL to the ICAT RESTful interface"), default=True) config.add_variable('file', ("-o", "--outputfile"), dict(help="output file name or '-' for stdout"), default='-') # The format argument makes in fact little sense, as there is no # choice. It's here for compatiblity with the command line interface # of icatdump.py only. config.add_variable('format', ("-f", "--format"), dict(help="output file format", choices=["ICAT"]), default='ICAT') # Additional arguments that icatdump.py does not provide: config.add_variable('query', ("--query",), dict(help="query string to select the content"), optional=True) config.add_variable('attributes', ("--attributes",),
# For "getData" and "getPreparedData", the name of the output file can # be set with the option "--outputfile". If not set the output file # from __future__ import print_function import icat import icat.config import sys import time import logging logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) config = icat.config.Config(ids="mandatory") config.add_variable('outputfile', ("--outputfile",), dict(help="name of the output file"), optional=True) config.add_variable('investigation', ("investigation",), dict(help="name and optionally visit id " "(separated by a colon) of the investigation")) config.add_variable('dataset', ("dataset",), dict(help="name of the dataset")) config.add_variable('method', ("method",), dict(choices=['getData', 'getDataUrl', 'getPreparedData', 'getPreparedDataUrl'], help="download method")) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) client.login(conf.auth, conf.credentials) client.ids.ping()
to Python. It adds an "-e" command line switch and evaluates the Python expression given as argument to it after having started an ICAT session. This allows one to run simple programs as one liners directly from the command line, as in:: # get all Dataset ids $ python -m icat.eval -e 'client.search("Dataset.id")' -s root [102284L, 102288L, 102289L, 102293L] """ from __future__ import print_function import logging import icat import icat.config if __name__ == "__main__": logging.basicConfig(level=logging.INFO) config = icat.config.Config(ids="optional") config.add_variable('expression', ("-e", "--eval"), dict(help="Python expression to evaluate")) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) client.login(conf.auth, conf.credentials) result = eval(conf.expression) if result is not None: print(result)
.. _PR #233: https://github.com/icatproject/icat.server/pull/233 """ import logging import re from timeit import default_timer as timer import icat import icat.config from icat.query import Query logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") log = logging.getLogger(__name__) config = icat.config.Config(ids=False) config.add_variable('investigation', ("investigation",), dict(help="name and optionally visit id " "(separated by a colon) of the investigation")) client, conf = config.getconfig() client.login(conf.auth, conf.credentials) have_size_attrs = ( 'investigationSize' in client.typemap['investigation'].InstAttr and 'datasetSize' in client.typemap['dataset'].InstAttr ) class Time(float): """Convenience: human readable time intervals. """ second = 1 minute = 60*second
# appropriate permissions. # from __future__ import print_function import sys import logging import yaml import icat import icat.config from icat.query import Query logging.basicConfig(level=logging.INFO) # logging.getLogger('suds.client').setLevel(logging.DEBUG) config = icat.config.Config() config.add_variable("datafile", ("datafile",), dict(metavar="inputdata.yaml", help="name of the input datafile")) config.add_variable("jobname", ("jobname",), dict(help="name of the job to add")) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) if client.apiversion < "4.3": raise RuntimeError("Sorry, ICAT version %s is too old, need 4.3.0 or newer." % client.apiversion) client.login(conf.auth, conf.credentials) # ------------------------------------------------------------ # Helper functions # ------------------------------------------------------------ def initobj(obj, attrs):
import icat.dumpfile_yaml except ImportError: pass from icat.helper import parse_attr_string logging.basicConfig(level=logging.INFO) logging.getLogger('suds.client').setLevel(logging.CRITICAL) log = logging.getLogger(__name__) formats = icat.dumpfile.Backends.keys() if len(formats) == 0: raise RuntimeError("No datafile backends available.") config = icat.config.Config(ids="optional") config.add_variable('file', ("-i", "--inputfile"), dict(help="input file name or '-' for stdin"), default='-') config.add_variable('format', ("-f", "--format"), dict(help="input file format", choices=formats), default='YAML') config.add_variable('uploadDatafiles', ("--upload-datafiles",), dict(help="upload datafiles to IDS"), type=icat.config.flag, default=False) config.add_variable('dataDir', ("--datafile-dir",), dict(help="datafile directory"), default='.') config.add_variable('duplicate', ("--duplicate",), dict(help="behavior in case of duplicate objects", choices=["THROW", "IGNORE", "CHECK", "OVERWRITE"]), default='THROW') conf = config.getconfig()
# This script should be run by a member of the samplewriter group # from __future__ import print_function import icat import icat.config import sys import logging import yaml logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) config = icat.config.Config() config.add_variable('datafile', ("datafile",), dict(metavar="inputdata.yaml", help="name of the input datafile")) config.add_variable('sampletypename', ("sampletypename",), dict(help="name of the sample type to add")) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) client.login(conf.auth, conf.credentials) # ------------------------------------------------------------ # Read input data # ------------------------------------------------------------ if conf.datafile == "-": f = sys.stdin
# datafiles in this dataset, e.g. the user must be in the writer group # of the investigation. # import icat import icat.config import sys import os.path import logging logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) config = icat.config.Config(ids="mandatory") config.add_variable( 'investigation', ("investigation", ), dict(help="name and optionally visit id " "(separated by a colon) of the investigation")) config.add_variable('dataset', ("dataset", ), dict(help="name of the dataset")) config.add_variable( 'datafileformat', ("datafileformat", ), dict(help="name and optionally version " "(separated by a colon) of the datafile format")) config.add_variable('files', ("files", ), dict(help="name of the files to upload", nargs="+")) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) client.login(conf.auth, conf.credentials) # ------------------------------------------------------------ # Get the objects that we assume to be already present in ICAT.
# import sys import logging import datetime import yaml import icat import icat.config from icat.query import Query logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) config = icat.config.Config() config.add_variable( 'datafile', ("datafile", ), dict(metavar="inputdata.yaml", help="name of the input datafile")) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) client.login(conf.auth, conf.credentials) # ------------------------------------------------------------ # Helper functions # ------------------------------------------------------------ def initobj(obj, attrs): """Initialize an entity object from a dict of attributes.""" for a in obj.InstAttr: if a != 'id' and a in attrs:
import sys import json import re import logging import requests import icat import icat.config from icat.exception import translateError logging.basicConfig(level=logging.INFO) logging.getLogger('requests.packages.urllib3').setLevel(logging.WARNING) config = icat.config.Config() config.add_variable('resturl', ("--resturl",), dict(help="URL to the ICAT RESTful interface"), default=True) config.add_variable('file', ("-i", "--inputfile"), dict(help="input file name or '-' for stdin"), default='-') # The format argument makes in fact little sense, as there is no # choice. It's here for compatiblity with the command line interface # of icatingest.py only. config.add_variable('format', ("-f", "--format"), dict(help="input file format", choices=["ICAT"]), default='ICAT') config.add_variable('duplicate', ("--duplicate",), dict(help="behavior in case of duplicate objects", choices=["THROW", "IGNORE", "CHECK", "OVERWRITE"]), default='THROW') # Additional arguments that icatdump.py does not provide:
# datafiles in this dataset, e.g. the user must be in the writer group # of the investigation. # import icat import icat.config import sys import os.path import logging logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) config = icat.config.Config(ids="mandatory") config.add_variable('investigation', ("investigation",), dict(help="name and optionally visit id " "(separated by a colon) of the investigation")) config.add_variable('dataset', ("dataset",), dict(help="name of the dataset")) config.add_variable('datafileformat', ("datafileformat",), dict(help="name and optionally version " "(separated by a colon) of the datafile format")) config.add_variable('files', ("files",), dict(help="name of the files to upload", nargs="+")) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) client.login(conf.auth, conf.credentials) # ------------------------------------------------------------
# import sys import logging import datetime import yaml import icat import icat.config from icat.query import Query logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) config = icat.config.Config() config.add_variable('datafile', ("datafile",), dict(metavar="inputdata.yaml", help="name of the input datafile")) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) client.login(conf.auth, conf.credentials) # ------------------------------------------------------------ # Helper functions # ------------------------------------------------------------ def initobj(obj, attrs): """Initialize an entity object from a dict of attributes.""" for a in obj.InstAttr: if a != 'id' and a in attrs:
# For "getData" and "getPreparedData", the name of the output file can # be set with the option "--outputfile". If not set the output file # from __future__ import print_function import icat import icat.config import sys import time import logging logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) config = icat.config.Config(ids="mandatory") config.add_variable('outputfile', ("--outputfile",), dict(help="name of the output file"), optional=True) config.add_variable('investigation', ("investigation",), dict(help="name and optionally visit id " "(separated by a colon) of the investigation")) config.add_variable('dataset', ("dataset",), dict(help="name of the dataset")) config.add_variable('method', ("method",), dict(choices=['getData', 'getDataUrl', 'getPreparedData', 'getPreparedDataUrl'], help="download method")) client, conf = config.getconfig() client.login(conf.auth, conf.credentials) client.ids.ping() # ------------------------------------------------------------
# present. # # This script should be run by the ICAT user useroffice. # import icat import icat.config import ldap import logging import re logging.basicConfig(level=logging.INFO) config = icat.config.Config() config.add_variable('ldap_uri', ("-l", "--ldap-uri"), dict(help="URL of the LDAP server"), envvar='LDAP_URI') config.add_variable('ldap_base', ("-b", "--ldap-base"), dict(help="base DN for searching the LDAP server"), envvar='LDAP_BASE') config.add_variable('ldap_filter', ("-f", "--ldap-filter"), dict(help="search filter to select the user entries"), default='(uid=*)') conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) client.login(conf.auth, conf.credentials) icatuser = { u.name:u for u in client.search("User") }
import logging import icat import icat.config from icat.query import Query from icat.dumpfile import open_dumpfile import icat.dumpfile_xml import icat.dumpfile_yaml logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) formats = icat.dumpfile.Backends.keys() config = icat.config.Config() config.add_variable('file', ("-o", "--outputfile"), dict(help="output file name or '-' for stdout"), default='-') config.add_variable('format', ("-f", "--format"), dict(help="output file format", choices=formats), default='YAML') config.add_variable( 'investigation', ("investigation", ), dict(help="name and optionally visit id " "(separated by a colon) of the investigation")) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) if client.apiversion < '4.3.99': raise RuntimeError( "Sorry, ICAT version %s is too old, need 4.4.0 or newer." % client.apiversion)
# This script should be run by the ICAT user useroffice. # from __future__ import print_function import icat import icat.config import sys import logging import yaml logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) config = icat.config.Config() config.add_variable( 'datafile', ("datafile", ), dict(metavar="inputdata.yaml", help="name of the input datafile")) config.add_variable('investigationname', ("investigationname", ), dict(help="name of the investigation to add")) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) client.login(conf.auth, conf.credentials) # ------------------------------------------------------------ # Helper functions # ------------------------------------------------------------ def initobj(obj, attrs): """Initialize an entity object from a dict of attributes."""
import icat.dumpfile_yaml except ImportError: pass from icat.dump_queries import * logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) formats = icat.dumpfile.Backends.keys() if len(formats) == 0: raise RuntimeError("No datafile backends available.") config = icat.config.Config() config.add_variable('file', ("-o", "--outputfile"), dict(help="output file name or '-' for stdout"), default='-') config.add_variable('format', ("-f", "--format"), dict(help="output file format", choices=formats), default='YAML') client, conf = config.getconfig() if client.apiversion < '4.2.99': raise RuntimeError("Sorry, ICAT version %s is too old, need 4.3.0 or newer." % client.apiversion) client.login(conf.auth, conf.credentials) with open_dumpfile(client, conf.file, conf.format, 'w') as dumpfile: dumpfile.writedata(getAuthQueries(client)) dumpfile.writedata(getStaticQueries(client))
# This script should be run by a member of the samplewriter group # from __future__ import print_function import icat import icat.config import sys import logging import yaml logging.basicConfig(level=logging.INFO) #logging.getLogger('suds.client').setLevel(logging.DEBUG) config = icat.config.Config() config.add_variable( 'datafile', ("datafile", ), dict(metavar="inputdata.yaml", help="name of the input datafile")) config.add_variable('sampletypename', ("sampletypename", ), dict(help="name of the sample type to add")) conf = config.getconfig() client = icat.Client(conf.url, **conf.client_kwargs) client.login(conf.auth, conf.credentials) # ------------------------------------------------------------ # Read input data # ------------------------------------------------------------ if conf.datafile == "-": f = sys.stdin else: