From 7bf51466431ba69a112732f737fcba847f01a729 Mon Sep 17 00:00:00 2001
From: Walter Purcaro <vuolter@users.noreply.github.com>
Date: Sun, 18 Oct 2015 18:48:58 +0200
Subject: [Extractor] Code cleanup

---
 module/plugins/hooks/ExtractArchive.py | 25 +++++++++++--------------
 module/plugins/internal/Extractor.py   | 22 ++++++++++++++++------
 module/plugins/internal/SevenZip.py    |  7 ++++---
 module/plugins/internal/UnRar.py       | 31 ++++++++++---------------------
 4 files changed, 41 insertions(+), 44 deletions(-)

diff --git a/module/plugins/hooks/ExtractArchive.py b/module/plugins/hooks/ExtractArchive.py
index 034a4b81a..1114d2ea5 100644
--- a/module/plugins/hooks/ExtractArchive.py
+++ b/module/plugins/hooks/ExtractArchive.py
@@ -50,9 +50,8 @@ except ImportError:
     pass
 
 from module.plugins.internal.Addon import Addon, Expose, threaded
-from module.plugins.internal.Plugin import exists, replace_patterns
 from module.plugins.internal.Extractor import ArchiveError, CRCError, PasswordError
-from module.utils import fs_encode, save_join as fs_join, uniqify
+from module.plugins.internal.utils import encode, exists, fs_join, replace_patterns, uniqify
 
 
 class ArchiveQueue(object):
@@ -117,7 +116,7 @@ class ExtractArchive(Addon):
                   ("excludefiles"   , "str"   , "Don't extract the following files"     , "*.nfo,*.DS_Store,index.dat,thumb.db"                                    ),
                   ("recursive"      , "bool"  , "Extract archives in archives"          , True                                                                     ),
                   ("waitall"        , "bool"  , "Run after all downloads was processed" , False                                                                    ),
-                  ("renice"         , "int"   , "CPU priority"                          , 0                                                                        )]
+                  ("priority"       , "int"   , "Process priority"                      , 0                                                                        )]
 
     __description__ = """Extract different kind of archives"""
     __license__     = "GPLv3"
@@ -242,9 +241,8 @@ class ExtractArchive(Addon):
         subfolder   = self.get_config('subfolder')
         fullpath    = self.get_config('fullpath')
         overwrite   = self.get_config('overwrite')
-        renice      = self.get_config('renice')
+        priority    = self.get_config('priority')
         recursive   = self.get_config('recursive')
-        delete      = self.get_config('delete')
         keepbroken  = self.get_config('keepbroken')
 
         extensions   = [x.lstrip('.').lower() for x in toList(self.get_config('extensions'))]
@@ -256,7 +254,7 @@ class ExtractArchive(Addon):
         #: Reload from txt file
         self.reload_passwords()
 
-        download_folder = self.pyload.config.get("general", "download_folder")
+        dl_folder = self.pyload.config.get("general", "download_folder")
 
         #: Iterate packages -> extractors -> targets
         for pid in ids:
@@ -269,7 +267,7 @@ class ExtractArchive(Addon):
             self.log_info(_("Check package: %s") % pypack.name)
 
             #: Determine output folder
-            out = fs_join(download_folder, pypack.folder, destination, "")  #: Force trailing slash
+            out = fs_join(dl_folder, pypack.folder, destination, "")  #: Force trailing slash
 
             if subfolder:
                 out = fs_join(out, pypack.folder)
@@ -279,7 +277,7 @@ class ExtractArchive(Addon):
 
             matched   = False
             success   = True
-            files_ids = dict((pylink['name'], ((fs_join(download_folder, pypack.folder, pylink['name'])), pylink['id'], out)) for pylink \
+            files_ids = dict((pylink['name'], ((fs_join(dl_folder, pypack.folder, pylink['name'])), pylink['id'], out)) for pylink \
                         in sorted(pypack.getChildren().values(), key=lambda k: k['name'])).values()  #: Remove duplicates
 
             #: Check as long there are unseen files
@@ -312,8 +310,7 @@ class ExtractArchive(Addon):
                                                 fullpath,
                                                 overwrite,
                                                 excludefiles,
-                                                renice,
-                                                delete,
+                                                priority,
                                                 keepbroken,
                                                 fid)
 
@@ -341,7 +338,7 @@ class ExtractArchive(Addon):
                             self.set_permissions(file)
 
                         for filename in new_files:
-                            file = fs_encode(fs_join(os.path.dirname(archive.filename), filename))
+                            file = encode(fs_join(os.path.dirname(archive.filename), filename))
                             if not exists(file):
                                 self.log_debug("New file %s does not exists" % filename)
                                 continue
@@ -458,7 +455,7 @@ class ExtractArchive(Addon):
 
                 deltotrash = self.get_config('deltotrash')
                 for f in delfiles:
-                    file = fs_encode(f)
+                    file = encode(f)
                     if not exists(file):
                         continue
 
@@ -526,7 +523,7 @@ class ExtractArchive(Addon):
         try:
             passwords = []
 
-            file = fs_encode(self.get_config('passwordfile'))
+            file = encode(self.get_config('passwordfile'))
             with open(file) as f:
                 for pw in f.read().splitlines():
                     passwords.append(pw)
@@ -555,7 +552,7 @@ class ExtractArchive(Addon):
         try:
             self.passwords = uniqify([password] + self.passwords)
 
-            file = fs_encode(self.get_config('passwordfile'))
+            file = encode(self.get_config('passwordfile'))
             with open(file, "wb") as f:
                 for pw in self.passwords:
                     f.write(pw + '\n')
diff --git a/module/plugins/internal/Extractor.py b/module/plugins/internal/Extractor.py
index 3ab5d6a0d..6bc1ddc71 100644
--- a/module/plugins/internal/Extractor.py
+++ b/module/plugins/internal/Extractor.py
@@ -5,7 +5,19 @@ import re
 
 from module.PyFile import PyFile
 from module.plugins.internal.Plugin import Plugin
-from module.utils import fs_encode
+
+
+def renice(pid, value):
+    if not value or os.name is "nt":
+        return
+
+    try:
+        subprocess.Popen(["renice", str(value), str(pid)],
+                         stdout=subprocess.PIPE,
+                         stderr=subprocess.PIPE,
+                         bufsize=-1)
+    except Exception:
+        pass
 
 
 class ArchiveError(Exception):
@@ -73,15 +85,14 @@ class Extractor(Plugin):
 
     @property
     def target(self):
-        return fs_encode(self.filename)
+        return encode(self.filename)
 
 
     def __init__(self, plugin, filename, out,
                  fullpath=True,
                  overwrite=False,
                  excludefiles=[],
-                 renice=0,
-                 delete='No',
+                 renice=False,
                  keepbroken=False,
                  fid=None):
         """
@@ -95,8 +106,7 @@ class Extractor(Plugin):
         self.fullpath     = fullpath
         self.overwrite    = overwrite
         self.excludefiles = excludefiles
-        self.renice       = renice
-        self.delete       = delete
+        self.priority     = int(priority)
         self.keepbroken   = keepbroken
         self.files        = []  #: Store extracted files here
 
diff --git a/module/plugins/internal/SevenZip.py b/module/plugins/internal/SevenZip.py
index f73e935e8..db22dfc4e 100644
--- a/module/plugins/internal/SevenZip.py
+++ b/module/plugins/internal/SevenZip.py
@@ -5,7 +5,7 @@ import re
 import subprocess
 
 from module.plugins.internal.UnRar import ArchiveError, CRCError, PasswordError, UnRar, renice
-from module.utils import save_join as fs_join
+from module.plugins.internal.utils import fs_join
 
 
 class SevenZip(UnRar):
@@ -78,8 +78,6 @@ class SevenZip(UnRar):
 
         p = self.call_cmd(command, '-o' + self.out, self.target, password=password)
 
-        renice(p.pid, self.renice)
-
         #: Communicate and retrieve stderr
         self._progress(p)
         err = p.stderr.read().strip()
@@ -139,4 +137,7 @@ class SevenZip(UnRar):
         self.log_debug(" ".join(call))
 
         p = subprocess.Popen(call, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+        renice(p.pid, self.priority)
+
         return p
diff --git a/module/plugins/internal/UnRar.py b/module/plugins/internal/UnRar.py
index 6f85c286a..6fe02a51d 100644
--- a/module/plugins/internal/UnRar.py
+++ b/module/plugins/internal/UnRar.py
@@ -2,22 +2,11 @@
 
 import os
 import re
+import string
 import subprocess
 
-from glob import glob
-from string import digits
-
-from module.plugins.internal.Extractor import Extractor, ArchiveError, CRCError, PasswordError
-from module.utils import fs_decode, save_join as fs_join
-
-
-def renice(pid, value):
-    if value and os.name is not "nt":
-        try:
-            subprocess.Popen(["renice", str(value), str(pid)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=-1)
-
-        except Exception:
-            pass
+from module.plugins.internal.Extractor import Extractor, ArchiveError, CRCError, PasswordError, renice
+from module.plugins.internal.utils import decode, fs_join
 
 
 class UnRar(Extractor):
@@ -138,7 +127,7 @@ class UnRar(Extractor):
                 self.notify_progress(int(s))
                 s = ""
             #: Not reading a digit -> therefore restart
-            elif c not in digits:
+            elif c not in string.digits:
                 s = ""
             #: Add digit to progressstring
             else:
@@ -150,8 +139,6 @@ class UnRar(Extractor):
 
         p = self.call_cmd(command, self.target, self.out, password=password)
 
-        renice(p.pid, self.renice)
-
         #: Communicate and retrieve stderr
         self._progress(p)
         err = p.stderr.read().strip()
@@ -200,12 +187,12 @@ class UnRar(Extractor):
         result = set()
         if not self.fullpath and self.VERSION.startswith('5'):
             #@NOTE: Unrar 5 always list full path
-            for f in fs_decode(out).splitlines():
+            for f in decode(out).splitlines():
                 f = fs_join(self.out, os.path.basename(f.strip()))
                 if os.path.isfile(f):
                     result.add(fs_join(self.out, os.path.basename(f)))
         else:
-            for f in fs_decode(out).splitlines():
+            for f in decode(out).splitlines():
                 result.add(fs_join(self.out, f.strip()))
 
         return list(result)
@@ -219,8 +206,7 @@ class UnRar(Extractor):
             args.append("-o+")
         else:
             args.append("-o-")
-            if self.delete != 'No':
-                args.append("-or")
+            args.append("-or")
 
         for word in self.excludefiles:
             args.append("-x'%s'" % word.strip())
@@ -243,4 +229,7 @@ class UnRar(Extractor):
         self.log_debug(" ".join(call))
 
         p = subprocess.Popen(call, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+
+        renice(p.pid, self.priority)
+
         return p
-- 
cgit v1.2.3