From de35c30d0b1e12e40574fd4a9c79f6c88b896cc6 Mon Sep 17 00:00:00 2001 From: Vladimir Oprya Date: Sat, 21 Nov 2020 12:01:30 +0300 Subject: [PATCH] Adding helper _maybe_add_password where it's possible, to centralize the logic of work with password. --- patoolib/programs/arj.py | 17 +++++++++-------- patoolib/programs/p7zip.py | 31 +++++++++++++------------------ patoolib/programs/unalz.py | 11 +++++++---- patoolib/programs/unzip.py | 13 +++++++------ patoolib/programs/xdms.py | 14 ++++++++------ 5 files changed, 44 insertions(+), 42 deletions(-) diff --git a/patoolib/programs/arj.py b/patoolib/programs/arj.py index d1f5e33..9fb751f 100644 --- a/patoolib/programs/arj.py +++ b/patoolib/programs/arj.py @@ -22,11 +22,15 @@ def _get_password_switch(password): raise PatoolError("Password for ARJ can't contain spaces.") return '-g%s' % password +def _maybe_add_password(cmdlist, password): + if password: + cmdlist.append(_get_password_switch(password)) + + def extract_arj (archive, compression, cmd, verbosity, interactive, outdir, password=None): """Extract an ARJ archive.""" cmdlist = [cmd, 'x', '-r'] - if password: - cmdlist.append(_get_password_switch(password)) + _maybe_add_password(cmdlist, password) if not interactive: cmdlist.append('-y') cmdlist.extend([archive, outdir]) @@ -36,8 +40,7 @@ def extract_arj (archive, compression, cmd, verbosity, interactive, outdir, pass def list_arj (archive, compression, cmd, verbosity, interactive, password=None): """List an ARJ archive.""" cmdlist = [cmd] - if password: - cmdlist.append(_get_password_switch(password)) + _maybe_add_password(cmdlist, password) if verbosity > 1: cmdlist.append('v') else: @@ -51,8 +54,7 @@ def list_arj (archive, compression, cmd, verbosity, interactive, password=None): def test_arj (archive, compression, cmd, verbosity, interactive, password=None): """Test an ARJ archive.""" cmdlist = [cmd, 't', '-r'] - if password: - cmdlist.append(_get_password_switch(password)) + _maybe_add_password(cmdlist, password) if not interactive: cmdlist.append('-y') cmdlist.append(archive) @@ -62,8 +64,7 @@ def test_arj (archive, compression, cmd, verbosity, interactive, password=None): def create_arj (archive, compression, cmd, verbosity, interactive, filenames, password=None): """Create an ARJ archive.""" cmdlist = [cmd, 'a', '-r'] - if password: - cmdlist.append(_get_password_switch(password)) + _maybe_add_password(cmdlist, password) if not interactive: cmdlist.append('-y') cmdlist.append(archive) diff --git a/patoolib/programs/p7zip.py b/patoolib/programs/p7zip.py index 45e8483..ee1865a 100644 --- a/patoolib/programs/p7zip.py +++ b/patoolib/programs/p7zip.py @@ -15,13 +15,16 @@ # along with this program. If not, see . """Archive commands for the 7z program.""" +def _maybe_add_password(cmdlist, password): + if password: + cmdlist.append('-p%s' % password) + def extract_7z(archive, compression, cmd, verbosity, interactive, outdir, password=None): """Extract a 7z archive.""" cmdlist = [cmd, 'x'] if not interactive: cmdlist.append('-y') - if password: - cmdlist.append('-p%s' % password) + _maybe_add_password(cmdlist, password) cmdlist.extend(['-o%s' % outdir, '--', archive]) return cmdlist @@ -32,8 +35,7 @@ def extract_7z_singlefile(archive, compression, cmd, verbosity, interactive, out cmdlist = [cmd, 'e'] if not interactive: cmdlist.append('-y') - if password: - cmdlist.append('-p%s' % password) + _maybe_add_password(cmdlist, password) cmdlist.extend(['-o%s' % outdir, '--', archive]) return cmdlist @@ -60,8 +62,7 @@ def list_7z (archive, compression, cmd, verbosity, interactive, password=None): cmdlist = [cmd, 'l'] if not interactive: cmdlist.append('-y') - if password: - cmdlist.append('-p%s' % password) + _maybe_add_password(cmdlist, password) cmdlist.extend(['--', archive]) return cmdlist @@ -87,8 +88,7 @@ def test_7z (archive, compression, cmd, verbosity, interactive, password=None): cmdlist = [cmd, 't'] if not interactive: cmdlist.append('-y') - if password: - cmdlist.append('-p%s' % password) + _maybe_add_password(cmdlist, password) cmdlist.extend(['--', archive]) return cmdlist @@ -114,8 +114,7 @@ def create_7z(archive, compression, cmd, verbosity, interactive, filenames, pass cmdlist = [cmd, 'a'] if not interactive: cmdlist.append('-y') - if password: - cmdlist.append('-p%s' % password) + _maybe_add_password(cmdlist, password) cmdlist.extend(['-t7z', '-mx=9', '--', archive]) cmdlist.extend(filenames) return cmdlist @@ -126,8 +125,7 @@ def create_zip(archive, compression, cmd, verbosity, interactive, filenames, pas cmdlist = [cmd, 'a'] if not interactive: cmdlist.append('-y') - if password: - cmdlist.append('-p%s' % password) + _maybe_add_password(cmdlist, password) cmdlist.extend(['-tzip', '-mx=9', '--', archive]) cmdlist.extend(filenames) return cmdlist @@ -138,8 +136,7 @@ def create_xz(archive, compression, cmd, verbosity, interactive, filenames, pass cmdlist = [cmd, 'a'] if not interactive: cmdlist.append('-y') - if password: - cmdlist.append('-p%s' % password) + _maybe_add_password(cmdlist, password) cmdlist.extend(['-txz', '-mx=9', '--', archive]) cmdlist.extend(filenames) return cmdlist @@ -150,8 +147,7 @@ def create_gzip(archive, compression, cmd, verbosity, interactive, filenames, pa cmdlist = [cmd, 'a'] if not interactive: cmdlist.append('-y') - if password: - cmdlist.append('-p%s' % password) + _maybe_add_password(cmdlist, password) cmdlist.extend(['-tgzip', '-mx=9', '--', archive]) cmdlist.extend(filenames) return cmdlist @@ -162,8 +158,7 @@ def create_bzip2(archive, compression, cmd, verbosity, interactive, filenames, p cmdlist = [cmd, 'a'] if not interactive: cmdlist.append('-y') - if password: - cmdlist.append('-p%s' % password) + _maybe_add_password(cmdlist, password) cmdlist.extend(['-tbzip2', '-mx=9', '--', archive]) cmdlist.extend(filenames) return cmdlist diff --git a/patoolib/programs/unalz.py b/patoolib/programs/unalz.py index 9e2f69c..4bb64d2 100644 --- a/patoolib/programs/unalz.py +++ b/patoolib/programs/unalz.py @@ -15,11 +15,15 @@ # along with this program. If not, see . """Archive commands for the unalz program.""" +def _maybe_add_password(cmdlist, password): + if password: + cmdlist.extend(['-pwd', password]) + + def extract_alzip (archive, compression, cmd, verbosity, interactive, outdir, password=None): """Extract a ALZIP archive.""" cmdlist = [cmd, '-d', outdir] - if password: - cmdlist.extend(['-pwd', password]) + _maybe_add_password(cmdlist, password) cmdlist.append(archive) return cmdlist @@ -27,8 +31,7 @@ def extract_alzip (archive, compression, cmd, verbosity, interactive, outdir, pa def list_alzip (archive, compression, cmd, verbosity, interactive, password=None): """List a ALZIP archive.""" cmdlist = [cmd, '-l'] - if password: - cmdlist.extend(['-pwd', password]) + _maybe_add_password(cmdlist, password) cmdlist.append(archive) return cmdlist diff --git a/patoolib/programs/unzip.py b/patoolib/programs/unzip.py index 2b875b9..b87e497 100644 --- a/patoolib/programs/unzip.py +++ b/patoolib/programs/unzip.py @@ -15,13 +15,16 @@ # along with this program. If not, see . """Archive commands for the unzip program.""" +def _maybe_add_password(cmdlist, password): + if password: + cmdlist.extend(['-P', password]) + def extract_zip (archive, compression, cmd, verbosity, interactive, outdir, password=None): """Extract a ZIP archive.""" cmdlist = [cmd] if verbosity > 1: cmdlist.append('-v') - if password: - cmdlist.extend(['-P', password]) + _maybe_add_password(cmdlist, password) cmdlist.extend(['--', archive, '-d', outdir]) return cmdlist @@ -30,8 +33,7 @@ def list_zip (archive, compression, cmd, verbosity, interactive, password=None): cmdlist = [cmd, '-l'] if verbosity > 1: cmdlist.append('-v') - if password: - cmdlist.extend(['-P', password]) + _maybe_add_password(cmdlist, password) cmdlist.extend(['--', archive]) return cmdlist @@ -40,7 +42,6 @@ def test_zip (archive, compression, cmd, verbosity, interactive, password=None): cmdlist = [cmd, '-t'] if verbosity > 1: cmdlist.append('-v') - if password: - cmdlist.extend(['-P', password]) + _maybe_add_password(cmdlist, password) cmdlist.extend(['--', archive]) return cmdlist diff --git a/patoolib/programs/xdms.py b/patoolib/programs/xdms.py index d76048d..a05fe8f 100644 --- a/patoolib/programs/xdms.py +++ b/patoolib/programs/xdms.py @@ -17,14 +17,18 @@ from .. import util +def _maybe_add_password(cmdlist, password): + if password: + cmdlist.extend(['-p', password]) + + def extract_dms (archive, compression, cmd, verbosity, interactive, outdir, password=None): """Extract a DMS archive.""" check_archive_ext(archive) cmdlist = [cmd, '-d', outdir] if verbosity > 1: cmdlist.append('-v') - if password: - cmdlist.extend(['-p', password]) + _maybe_add_password(cmdlist, password) cmdlist.extend(['u', archive]) return cmdlist @@ -33,8 +37,7 @@ def list_dms (archive, compression, cmd, verbosity, interactive, password=None): """List a DMS archive.""" check_archive_ext(archive) cmdlist = [cmd, 'v'] - if password: - cmdlist.extend(['-p', password]) + _maybe_add_password(cmdlist, password) cmdlist.append(archive) return cmdlist @@ -43,8 +46,7 @@ def test_dms (archive, compression, cmd, verbosity, interactive, password=None): """Test a DMS archive.""" check_archive_ext(archive) cmdlist = [cmd, 't'] - if password: - cmdlist.extend(['-p', password]) + _maybe_add_password(cmdlist, password) cmdlist.append(archive) return cmdlist