Skip to content

Commit

Permalink
Plugins now live in the BIDScoin configuration directory
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelzwiers committed Sep 26, 2024
1 parent f35c7c5 commit 6e8891a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
17 changes: 11 additions & 6 deletions bidscoin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,26 @@
tutorialurl = 'https://surfdrive.surf.nl/files/index.php/s/HTxdUbykBZm2cYM/download'
bidscoinroot = Path(__file__).parent
schemafolder = bidscoinroot/'schema'
pluginfolder = bidscoinroot/'plugins'

# Get the BIDSCOIN_DEBUG environment variable to set the log-messages and logging level, etc
DEBUG = os.getenv('BIDSCOIN_DEBUG','').upper() in ('1', 'TRUE', 'Y', 'YES')

# Create a BIDScoin user configuration directory if needed and load the BIDScoin user settings
configfile = Path(os.getenv('BIDSCOIN_CONFIGDIR') or
(Path.home() if os.access(Path.home(),os.W_OK) else Path(tempfile.gettempdir()))/'.bidscoin')/__version__/'config.toml'
templatefolder = configfile.parent/'templates'
configdir = Path(os.getenv('BIDSCOIN_CONFIGDIR') or (Path.home() if os.access(Path.home(),os.W_OK) else Path(tempfile.gettempdir()))/'.bidscoin')/__version__
configfile = configdir/'config.toml'
pluginfolder = configdir/'plugins'
pluginfolder.mkdir(parents=True, exist_ok=True)
templatefolder = configdir/'templates'
templatefolder.mkdir(parents=True, exist_ok=True)
if not configfile.is_file():
print(f"Creating BIDScoin configuration:\n-> {configfile}")
print(f"Creating BIDScoin configuration:\n-> {configdir}")
configfile.write_text(f"[bidscoin]\n"
f"bidsmap_template = '{templatefolder}/bidsmap_dccn.yaml' # The default template bidsmap (change to use a different default)\n"
f"trackusage = 'yes' # Upload anonymous usage data if 'yes' (maximally 1 upload every {tracking['sleep']} hour) (see `bidscoin --tracking show`)\n")
for plugin in (bidscoinroot/'plugins').glob('*.py'):
if not (pluginfolder/plugin.name).is_file():
print(f"-> {pluginfolder/plugin.name}")
shutil.copyfile(plugin, pluginfolder/plugin.name)
for template in list((bidscoinroot/'heuristics').glob('*.yaml')) + [bidscoinroot/'heuristics'/'schema.json']:
if not (templatefolder/template.name).is_file():
print(f"-> {templatefolder/template.name}")
Expand Down Expand Up @@ -156,7 +161,7 @@ def trackusage(event: str, dryrun: bool=False) -> dict:

# Check if we are not asleep
try:
trackfile = configfile.parent/'usage'/f"bidscoin_{data['userid']}"
trackfile = configdir/'usage'/f"bidscoin_{data['userid']}"
trackfile.parent.mkdir(parents=True, exist_ok=True)
with shelve.open(str(trackfile), 'c', writeback=True) as tracked:
now = datetime.datetime.now()
Expand Down
5 changes: 3 additions & 2 deletions bidscoin/bcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"""

import os
import types
import coloredlogs
import inspect
import logging
Expand Down Expand Up @@ -392,7 +393,7 @@ def uninstall_plugins(filenames: List[str]=(), wipe: bool=True) -> None:


@lru_cache()
def import_plugin(plugin: Union[Path,str], functions: tuple=()) -> module_from_spec:
def import_plugin(plugin: Union[Path,str], functions: tuple=()) -> Union[types.ModuleType, None]:
"""
Imports the plugin if it contains any of the specified functions
Expand All @@ -416,7 +417,7 @@ def import_plugin(plugin: Union[Path,str], functions: tuple=()) -> module_from_s
# Load the plugin-module
LOGGER.bcdebug(f"Importing plugin: '{plugin}'")
try:
spec = spec_from_file_location('bidscoin.plugin.' + plugin.stem, plugin)
spec = spec_from_file_location(f"bidscoin.{pluginfolder.name}." + plugin.stem, plugin)
module = module_from_spec(spec)
spec.loader.exec_module(module)

Expand Down
4 changes: 2 additions & 2 deletions bidscoin/cli/_bcoin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
$ bidsmapper sourcefolder bidsfolder # This produces a study bidsmap and launches a GUI
$ bidscoiner sourcefolder bidsfolder # This converts your data to BIDS according to the study bidsmap
Default settings and template bidsmaps are stored in the `.bidscoin` configuration folder in your home
directory (you can modify the configuration files to your needs with any plain text editor)
Default settings, plugins and template bidsmaps are stored in the `.bidscoin` configuration folder in your
home directory (you can modify the configuration files to your needs with any plain text editor)
Set the environment variable `BIDSCOIN_DEBUG=TRUE` to run BIDScoin in a more verbose logging mode and
`BIDSCOIN_CONFIGDIR=/writable/path/to/configdir` for using a different configuration (root) directory.
Expand Down
1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
### Changed
- BIDScoin's main API, which now includes new classes to separate the logic from the data and make the code cleaner, better organized and maintainable
- Dropped support for Qt5
- Plugins are now installed in the BIDScoin configuration directory

## [4.3.3] - 2024-07-12

Expand Down

0 comments on commit 6e8891a

Please sign in to comment.