functional
This commit is contained in:
56
cbrtocbz.py
56
cbrtocbz.py
@@ -1,3 +1,59 @@
|
||||
import rarfile
|
||||
import zipfile
|
||||
import argparse
|
||||
import tempfile
|
||||
from sys import argv
|
||||
from os import curdir, remove, walk
|
||||
from os.path import splitext, join
|
||||
#from glob import glob
|
||||
from typing import Final
|
||||
|
||||
#optionally prints a message if the verbosity level is high enough
|
||||
def vprint(text, level=0):
|
||||
if vprint.verbosity >= level:
|
||||
print(text)
|
||||
vprint.verbosity = 0
|
||||
|
||||
def add_files(zip: zipfile.ZipFile, directory):
|
||||
for dirpath, dirs, files in walk(directory):
|
||||
for f in files:
|
||||
path = join(dirpath, f)
|
||||
vprint('writing ' + path, 2)
|
||||
zip.write(path)
|
||||
for d in dirs:
|
||||
path = join(dirpath, d)
|
||||
add_files(zip, path)
|
||||
|
||||
def main(arguments):
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-e', '--ext', type=str)
|
||||
parser.add_argument('-p', '--path', type=str)
|
||||
parser.add_argument('-d', '--delete', action='store_true')
|
||||
parser.add_argument('-v', action='count')
|
||||
parser.add_argument('files', nargs='+', type=str)
|
||||
args = parser.parse_args(arguments)
|
||||
files = args.files
|
||||
ext = args.ext or 'cbz'
|
||||
delete = args.delete
|
||||
path: Final = args.path or curdir
|
||||
|
||||
|
||||
for f in files:
|
||||
with tempfile.TemporaryDirectory() as dir:
|
||||
vprint('created temp dir ' + dir, 1)
|
||||
with rarfile.RarFile(f, 'r') as rar:
|
||||
vprint('extracting ' + f, 1)
|
||||
rar.extractall(dir)
|
||||
z_f = join(path, splitext(f)[0] + '.' + ext)
|
||||
with zipfile.ZipFile(z_f, 'x') as zip:
|
||||
for dirpath, dirs, files in walk(dir):
|
||||
for file in files:
|
||||
p = join(dirpath, file)
|
||||
zip.write(p)
|
||||
vprint('finished ' + z_f, 0)
|
||||
if delete:
|
||||
vprint('deleting ' + f, 0)
|
||||
remove(f)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(argv[1:])
|
||||
|
||||
Reference in New Issue
Block a user