diff options
| author | Sam Scholten | 2025-09-14 18:45:24 +1000 |
|---|---|---|
| committer | Sam Scholten | 2025-09-14 18:50:51 +1000 |
| commit | cda3207e85f042541c044139a3657b40778fa39a (patch) | |
| tree | 661cb267869951796a66ec3bef9bbbd6db11ae78 | |
| parent | 20faf71815922e6e79ef81fbee4e3c28c75fd891 (diff) | |
| download | picostream-cda3207e85f042541c044139a3657b40778fa39a.tar.gz picostream-cda3207e85f042541c044139a3657b40778fa39a.zip | |
add automatic output filepath & force overwrite (-f) option
| -rw-r--r-- | picostream/main.py | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/picostream/main.py b/picostream/main.py index 8fc7f78..fb56d87 100644 --- a/picostream/main.py +++ b/picostream/main.py @@ -406,6 +406,30 @@ VOLTAGE_RANGE_MAP = { } +def generate_unique_filename(base_path: str) -> str: + """Generate a unique filename by appending timestamp if file exists.""" + import os + from pathlib import Path + + if not os.path.exists(base_path): + return base_path + + # Split the path into parts + path = Path(base_path) + stem = path.stem + suffix = path.suffix + parent = path.parent + + # Generate timestamp + timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") + + # Create new filename with timestamp + new_filename = f"{stem}_{timestamp}{suffix}" + new_path = parent / new_filename + + return str(new_path) + + @click.command() @click.option( "--sample-rate", @@ -483,6 +507,13 @@ VOLTAGE_RANGE_MAP = { type=float, help="Maximum buffer duration in seconds for live-only mode (limits file size).", ) +@click.option( + "--force", + "-f", + is_flag=True, + default=False, + help="Overwrite existing output file.", +) def main( sample_rate: float, resolution: str, @@ -496,6 +527,7 @@ def main( downsample_mode: str, offset: float, max_buff_sec: Optional[float], + force: bool, ) -> None: """High-speed data acquisition tool for Picoscope 5000a series.""" # --- Argument Validation and Processing --- @@ -526,6 +558,14 @@ def main( if not output: timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") output = f"./output_{timestamp}.hdf5" + else: + # Check if file exists and handle accordingly + if not force: + original_output = output + output = generate_unique_filename(output) + if output != original_output: + logger.info(f"File '{original_output}' exists. Using '{output}' instead.") + logger.info("Use --force/-f to overwrite existing files.") logger.info(f"Output file: {output}") logger.info(f"Selected voltage range: {rangev}V -> {channel_range_str}") |
