harbor run -t openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0446harbor download openthoughts/tasktrove-exp-rpt-curriculum-medium-curriculum-medium-0446Medium difficulty task: Implement jack_compiler fs_utils utilities (single-file)
Location
- Implement the solution in /app/
- The tests import the module via: from jack_compiler import fs_utils
- Therefore, jack_compiler must expose a submodule named fs_utils (accessible as jack_compiler.fs_utils)
What to implement
Create a Python module (single file) that provides a virtual submodule fs_utils with the following API and behavior:
1) validateInputPath(path)
- Signature: def validateInputPath(path) -> Path
- Behavior:
- Accept both str and Path inputs.
- Convert to a Path.
- If the path does not exist on disk, raise an Exception (any exception type is acceptable for tests).
- If the path exists (as a file or directory), return the Path object.
- Examples that should succeed:
- "tests/jackFiles"
- "tests/jackFiles/"
- "tests/jackFiles/Main.jack"
- "./tests/jackFiles"
- "./tests/jackFiles/Main.jack"
- Examples that should fail (raise an exception):
- "invalid"
- "./invalid"
- "tests/invalid"
- "./tests/invalid"
2) generateOutputPath(input_path)
- Signature: def generateOutputPath(input_path) -> Path
- Behavior:
- Accept both str and Path inputs.
- If the input_path represents a file with a ".jack" suffix (case-insensitive), return a Path with the same location but the suffix replaced by ".xml" (e.g., "Main.jack" -> "Main.xml", "/Main.jack" -> "/Main.xml").
- If the input_path represents a directory (no ".jack" suffix implied), return a Path that appends "<directory_name>.xml" in the same parent directory (e.g., "tests/jackFiles" -> "tests/jackFiles/jackFiles.xml"). The tests only inspect the .name property, so returning any parent path is acceptable as long as the name matches.
- Examples:
- Path("Main.jack").name -> "Main.xml"
- Path("./Main.jack").name -> "Main.xml"
- Path("/Main.jack").name -> "Main.xml"
- Path("tests/Main.jack").name -> "Main.xml"
- Path("tests/jackFiles").name -> "jackFiles.xml"
- Path("tests/jackFiles/").name -> "jackFiles.xml"
3) isJackFile(path)
- Signature: def isJackFile(path) -> bool
- Behavior:
- Accept both str and Path inputs.
- Return True if the path’s final component ends with the ".jack" suffix (case-insensitive), regardless of whether the path exists.
- Return False otherwise (e.g., paths like "tests", "tests/Main.abc" should be False).
- True for:
- Main.jack, ./Main.jack, /Main.jack, tests/Main.jack, ./tests/Main.jack, /tests/Main.jack
- False for:
- tests, ./tests, /tests, tests/Main.abc, ./tests/Main.abc, /tests/Main.abc
4) getJackFilesInDirectory(directory)
- Signature: def getJackFilesInDirectory(directory) -> list[Path]
- Behavior:
- Accept directory path as str or Path.
- If the directory does not exist or is not a directory, return an empty list.
- Return a sorted list (lexicographically) of Path objects for immediate children that are files with a ".jack" extension (case-insensitive).
- Tests require:
- getJackFilesInDirectory(Path("tests/jackFiles")) returns [Path("tests/jackFiles/Main.jack")]
- getJackFilesInDirectory(Path("tests")) returns []
Implementation notes
- All functions should work with both Path and str inputs, converting to Path where appropriate.
- Do not rely on filesystem side effects for extension checks (except getJackFilesInDirectory, which should inspect the filesystem).
- Return types should be Path for path-related functions and bool for isJackFile.
- Include clear docstrings and type hints.
- The module should be importable as fs_utils via from jack_compiler import fs_utils.
Design and packaging guidance (single-file task)
- Deliver a single Python file placed at /app/ named jack_compiler.py.
- Since tests import fs_utils as a submodule, the jack_compiler.py file must expose a submodule object named fs_utils:
- Implement the functions above in this file.
- Create a lightweight in-file submodule object (fs_utils) using types.ModuleType and attach it to jack_compiler so that:
- from jack_compiler import fs_utils works
- fs_utils.validateInputPath, fs_utils.generateOutputPath, fs_utils.isJackFile, fs_utils.getJackFilesInDirectory are available
- Optionally also insert the submodule into sys.modules as "jack_compiler.fs_utils" to mirror a real package structure.
- Include clear docstrings for all functions.
- Use pathlib.Path for path handling and provide robust input validation.
Hints
- Use pathlib.Path for all path operations; remember to normalize inputs (convert str to Path) where appropriate.
- For isJackFile, normalize extension checks with .lower() to ensure case-insensitive matching.
- For getJackFilesInDirectory, guard against non-existent or non-directory inputs by returning an empty list early.
- When listing directory contents, sort the results by name to ensure deterministic ordering.
Acceptance criteria
- A Python module/package is created at /app/ exposing a submodule fs_utils with the exact APIs above.
- All tests referencing fs_utils.validateInputPath, fs_utils.generateOutputPath, fs_utils.isJackFile, and fs_utils.getJackFilesInDirectory pass with the described behaviors.
Notes
- The solution must be importable as a single-file module under /app/, with a submodule fs_utils accessible via from jack_compiler import fs_utils.
- Maintain type hints and docstrings to aid readability and static checks.