Skip to content

Commit

Permalink
change the default config_file location
Browse files Browse the repository at this point in the history
it will now look into the home directory first, then in the default file in the module
  • Loading branch information
benoitberanger committed May 3, 2022
1 parent b70d7fc commit 048c799
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 20 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ Optional arguments:
--logfile Write logfile (default)
--no-logfile Disable writing logfile
-c FILE, --config_file FILE
If you want to use non-coded sequences such as WIP or C2P,
If you want to use non-coded sequences such as new Products, WIP or C2P,
you can provide a config file.
Default file is located in [niix2bids]/config_file/siemens.py
Default location is ~/niix2bids_config_file/siemens.py
If default location is not present, try to use the template file
located in [niix2bids]/config_file/siemens.py
-v, --version show program's version number and exit
niix2bids_version==0.0.1 + bids_version==1.6.0
niix2bids_version==v1.2.0 + bids_version==v1.6.0
```

## Limitations
Expand Down
11 changes: 9 additions & 2 deletions niix2bids/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,18 @@ def get_parser() -> argparse.ArgumentParser:
help=(
"If you want to use non-coded sequences such as new Products, WIP or C2P,\n"
"you can provide a config file.\n"
"Default file is located in [niix2bids]/config_file/siemens.py"
"Default location is ~/niix2bids_config_file/siemens.py\n"
"If default location is not present, try to use the template file \n"
"located in [niix2bids]/config_file/siemens.py"
),
dest="config_file",
metavar='FILE',
default=os.path.join( niix2bids.__path__[0], 'config_file', 'siemens.py'))
# default=os.path.join( niix2bids.__path__[0], 'config_file', 'siemens.py'))
default=[
os.path.join( os.path.expanduser('~'), 'niix2bids_config_file', 'siemens.py'),
os.path.join( niix2bids.__path__[0], 'config_file', 'siemens.py')
]
)

optional.add_argument("-v", "--version",
action="version",
Expand Down
2 changes: 1 addition & 1 deletion niix2bids/metadata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = 'v1.0.0'
__version__ = 'v1.2.0'


def get_niix2bids_version() -> str:
Expand Down
35 changes: 21 additions & 14 deletions niix2bids/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,28 @@ def wrapper(*args, **kwargs):
def load_config_file(config_file: str) -> list:
log = get_logger()

if os.path.exists(config_file):
if os.path.isfile(config_file):
script_content = runpy.run_path(config_file)
if "config" in script_content:
config = script_content['config']
log.info(f"using config_file : {config_file}")
return config
else:
log.critical(f"config_file incorrect (no 'config' variable inside) : {config_file}")
sys.exit(1)
else:
log.critical(f"config_file is not a file : {config_file}")
sys.exit(1)
fpath = ''
if type(config_file) is list:
for idx, file in enumerate(config_file):
log.info(f"Trying config_file location : {file}")
if os.path.exists(file):
fpath = file
else:
log.info(f"Trying config_file location : {config_file}")
if os.path.exists(config_file):
fpath = config_file

if fpath == '':
log.critical(f"No config file found")
sys.exit(1)

script_content = runpy.run_path(fpath)
if "config" in script_content:
config = script_content['config']
log.info(f"Using config_file : {fpath}")
return config
else:
log.critical(f"config_file does not exist : {config_file}")
log.critical(f"Config_file incorrect (no 'config' variable inside) : {fpath}")
sys.exit(1)


Expand Down

0 comments on commit 048c799

Please sign in to comment.