Skip to content

Add input file validation before command-line tool execution #51

@coderabbitai

Description

@coderabbitai

Enhancement Request

Currently, command-line tools in mdx2 load input files using loadobj and nxload, which can raise NeXusError when files don't exist or have invalid paths. However, these errors occur after the tool has started execution and created log files, leading to delays before the issue is detected.

Proposed Enhancement

Check the existence and validity of input files before a command-line tool executes its main logic. This would:

  • Avoid creating log files when input files are invalid
  • Provide immediate feedback to users about file issues
  • Not require major changes to existing command-line tools
  • Not break the testing framework (which uses fake filenames and mocking)

Suggested Implementation

Implement the validation inside the with_logging decorator using a mechanism to auto-detect whether a parameter is an input file. One approach could be:

  • Define a custom type hint class (e.g., InputFile) instead of str for file parameters
  • The decorator can introspect the Parameters dataclass to identify fields with this type
  • Validate file existence before executing the wrapped function
  • Skip validation during testing (potentially via an environment variable or detecting test context)

Example

@dataclass
class Parameters:
    geom: InputFile  # Auto-detected as input file
    data: InputFile  # Auto-detected as input file
    outfile: str     # Regular string, not validated

The with_logging decorator would check that geom and data files exist before proceeding.

Context

Current Behavior

m = loadobj('sandbox/map2.nxs','intensity')  # File doesn't exist
# NeXusError: '/Users/steve/dev/mdx2/sandbox/map2.nxs' does not exist

The error is informative but occurs after log file creation and tool initialization.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions