Skip to content

Commit

Permalink
Python: Remove one of three filesystem structures
Browse files Browse the repository at this point in the history
For commands where the entry in python/mrtrix3/commands/ is not a standalone .py file but a directory, previously there were two possible filesystem contents. One was for that sub-directory to contain a .py file with the same name as the command, as determined by the name of the python/commands/ sub-directory, and that file is expected to contain functions called uage() and execute(). The second is for such a file to be absent, but for there to instead exist two files called usage.py and execute.py, which would provide those relevant functions. This commit removes support for the latter.
  • Loading branch information
Lestropie committed Aug 20, 2024
1 parent d533879 commit 5ff0868
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
17 changes: 6 additions & 11 deletions cmake/MakePythonExecutable.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,28 @@ set(BINPATH_CONTENTS
"\n"
)

# Three possible interfaces:
# Two possible interfaces:
# 1. Standalone file residing in commands/
# 2. File stored in location commands/<cmdname>/<cmdname>.py, which will contain usage() and execute() functions
# 3. Two files stored at commands/<cmdname>/usage.py and commands/<cmdname>/execute.py, defining the two corresponding functions
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/__init__.py")
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/usage.py" AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/execute.py")
string(APPEND BINPATH_CONTENTS
"module_usage = importlib.import_module('.usage', 'mrtrix3.commands.${CMDNAME}')\n"
"module_execute = importlib.import_module('.execute', 'mrtrix3.commands.${CMDNAME}')\n"
"_execute(module_usage.usage, module_execute.execute)\n"
)
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/${CMDNAME}.py")
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}/${CMDNAME}.py")
string(APPEND BINPATH_CONTENTS
"module = importlib.import_module('.${CMDNAME}', 'mrtrix3.commands.${CMDNAME}')\n"
"_execute(module.usage, module.execute)\n"
)
else()
message(FATAL_ERROR "Malformed filesystem structure for Python command ${CMDNAME}")
endif()
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${CMDNAME}.py")
string(APPEND BINPATH_CONTENTS
"module = importlib.import_module('.${CMDNAME}', 'mrtrix3.commands')\n"
"_execute(module.usage, module.execute)\n"
)
else()
message(FATAL_ERROR "Malformed filesystem structure for Python command ${CMDNAME}")
endif()
string(APPEND BINPATH_CONTENTS
"_execute(module.usage, module.execute)\n"
)


if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.19)
file(WRITE ${OUTPUT_DIR}/${CMDNAME} ${BINPATH_CONTENTS})
Expand Down
23 changes: 23 additions & 0 deletions python/mrtrix3/commands/population_template/population_template.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2008-2024 the MRtrix3 contributors.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Covered Software is provided under this License on an "as is"
# basis, without warranty of any kind, either expressed, implied, or
# statutory, including, without limitation, warranties that the
# Covered Software is free of defects, merchantable, fit for a
# particular purpose or non-infringing.
# See the Mozilla Public License v. 2.0 for more details.
#
# For more details, see http://www.mrtrix.org/.

from .usage import usage as my_usage
from .execute import execute as my_execute

def usage(cmdline): #pylint: disable=unused-variable
return my_usage(cmdline)

def execute(): #pylint: disable=unused-variable
return my_execute()

0 comments on commit 5ff0868

Please sign in to comment.