Files
ETPlot/gallery/cli.py
T
Kylian Schmidt 6f0cf4521b Add support for incremental updates in gallery generation
- Introduced `source_to_update` parameter to selectively regenerate specific sources.
- Updated documentation to reflect new behavior for `clean_first` and `source_to_update`.
- Modified CLI and legacy CLI to handle source overrides appropriately.
2026-04-23 13:09:26 +02:00

122 lines
3.4 KiB
Python

"""
Command-line interface for gallery generation.
Provides a CLI entry point for gallery generation when using git clone setup.
"""
import argparse
import sys
from pathlib import Path
from gallery import generate
from gallery.config import GalleryConfig
def main():
"""
Main CLI entry point for gallery generation.
Supports:
- Loading config from YAML file (default: config.yaml)
- Clean gallery directory before generation
- Override to process only a specific source directory
"""
parser = argparse.ArgumentParser(
description='Generate scientific gallery from plot collections',
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
gallery # Generate from config.yaml
gallery --config myconfig.yaml # Use custom config file
gallery --clean # Clean and regenerate
gallery --source /path/to/plots # Generate only specific source
"""
)
parser.add_argument(
'--config',
type=str,
default='config.yaml',
help='Path to config.yaml file (default: config.yaml)'
)
parser.add_argument(
'--clean',
action='store_true',
help='Clean gallery directory before generation'
)
parser.add_argument(
'--source',
type=str,
default=None,
help='Override to only recompute a specific source directory. '
'If the directory is not in config, it will be added '
'temporarily.'
)
parser.add_argument(
'-v', '--verbose',
action='store_true',
help='Print verbose output'
)
args = parser.parse_args()
try:
# Load config from file
config = GalleryConfig.from_yaml(args.config)
# Handle source override
source_to_update = None
if args.source:
from gallery.config import GallerySource
source_path = Path(args.source).resolve()
# Check if source is in config
matching_source = None
for source in config.sources:
if Path(source.path).resolve() == source_path:
matching_source = source
break
# If not in config, create a temporary source entry
if matching_source is None:
source_name = source_path.name
source_to_update = GallerySource(
name=source_name,
path=source_path
)
config.sources.append(source_to_update)
if args.verbose:
print(
f"Source {args.source} not in config. "
f"Adding temporarily as '{source_name}'"
)
else:
source_to_update = matching_source
# Generate gallery
success = generate(
config=config,
clean_first=args.clean,
verbose=args.verbose,
source_to_update=source_to_update
)
sys.exit(0 if success else 1)
except FileNotFoundError as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
except ValueError as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()