Skip to content

Instantly share code, notes, and snippets.

@eddie-atkinson
Last active October 7, 2022 07:44
Show Gist options
  • Save eddie-atkinson/701cf76607dcc8fa5a4bb8fcc4ca1bb7 to your computer and use it in GitHub Desktop.
Save eddie-atkinson/701cf76607dcc8fa5a4bb8fcc4ca1bb7 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# (c) 2022 - Eddie Atkinson
# This code is licensed under the MIT license
import argparse
from typing import Union
from pathlib import Path
from zipfile import ZipFile
import textwrap
def check_path(path: str, permissive=False):
input_path = Path(path)
if not input_path.exists() and not permissive:
raise argparse.ArgumentTypeError(f"Path does not exist: {path}")
return input_path.resolve()
def check_path_permissive(path: str):
return check_path(path, permissive=True)
def is_target_file(file_path: str):
# Pretty rudimentary test but it does the job
return file_path.endswith(".zip")
def process_files(input_path: Path, output_path: Union[None, Path]):
if not output_path:
output_path = input_path
output_path.mkdir(exist_ok=True, parents=True)
for entry in input_path.iterdir():
if not is_target_file(str(entry)) or entry.is_dir():
continue
file_name = str(entry)
with ZipFile(file_name, "r") as zf:
print(f"Extracting {entry.name} to {output_path}")
zf.extractall(path=output_path)
def parseargs():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent(
"""\
Simple utility for unzipping MMS data zips
"""
),
)
parser.add_argument(
"-i",
"--input-path",
default=None,
required=True,
metavar="PATH",
type=check_path,
help="Source path to look for files",
)
parser.add_argument(
"-o",
"--output-path",
type=check_path_permissive,
metavar="OUTPUT_PATH",
help=textwrap.dedent(
"""Path where output files are dumped.
This directory and its parents are automatically created if they don't exist"""
),
)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parseargs()
process_files(args.input_path, args.output_path)
# from zipfile import ZipFile
# file_name = "MMSDM_2020_07.zip"
# with ZipFile(file_name, "r") as zip_file:
# zip_file.extractall(path=".")
@eddie-atkinson
Copy link
Author

To run open a terminal in the directory where you download this script and run:

python leemon.py -i <input directory containing your files> -o <optional-output-directory>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment