Add normalize_compression.

This commit is contained in:
voussoir 2021-09-16 20:58:24 -07:00
parent 7efdcdcdbe
commit d4ad110ff1
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -175,6 +175,26 @@ def move(pattern, directory):
print(file) print(file)
shutil.move(file, directory) shutil.move(file, directory)
def normalize_compression(compression):
if compression is None:
return None
if isinstance(compression, str):
compression = compression.lower()
if compression == 'max':
return COMPRESSION_MAX
elif compression == 'store':
return COMPRESSION_STORE
else:
compression = int(compression)
if isinstance(compression, int):
if compression < COMPRESSION_STORE or compression > COMPRESSION_MAX:
raise ValueError(f'Compression level {compression} is invalid.')
return compression
raise TypeError(f'Compression should be an integer or max or store, not {type(compression)}.')
def normalize_dictionary_size(dictionary): def normalize_dictionary_size(dictionary):
if dictionary is None: if dictionary is None:
return None return None
@ -347,8 +367,7 @@ def rarpar(
moveto = pathclass.Path(moveto) moveto = pathclass.Path(moveto)
moveto.assert_is_directory() moveto.assert_is_directory()
if compression not in [None, 0, 1, 2, 3, 4, 5]: compression = normalize_compression(compression)
raise ValueError(f'compression must be 0-5 or None, not {compression}.')
dictionary_size = normalize_dictionary_size(dictionary_size) dictionary_size = normalize_dictionary_size(dictionary_size)
@ -510,21 +529,13 @@ path:
''' '''
def rarpar_argparse(args): def rarpar_argparse(args):
compression = args.compression.lower() if args.compression is not None else None
if compression == 'max':
compression = COMPRESSION_MAX
elif compression == 'store':
compression = COMPRESSION_STORE
else:
compression = int(compression)
status = 0 status = 0
try: try:
return rarpar( return rarpar(
path=args.path, path=args.path,
volume=args.volume, volume=args.volume,
basename=args.basename, basename=args.basename,
compression=compression, compression=args.compression,
dictionary_size=args.dictionary_size, dictionary_size=args.dictionary_size,
dry=args.dry, dry=args.dry,
moveto=args.moveto, moveto=args.moveto,