From 2c77bd28af4898873f182f7618fa473bf7cdb7e3 Mon Sep 17 00:00:00 2001 From: croneter Date: Sat, 2 Feb 2019 20:22:06 +0100 Subject: [PATCH] Implement Codacy suggestions --- resources/lib/artwork.py | 2 +- resources/lib/backgroundthread.py | 24 +++++++++--------- resources/lib/downloadutils.py | 16 ++++++------ resources/lib/kodi_db/video.py | 2 +- resources/lib/library_sync/fanart.py | 2 +- resources/lib/library_sync/full_sync.py | 9 ++++--- resources/lib/library_sync/videonodes.py | 21 ++++++---------- resources/lib/plex_api.py | 8 +----- resources/lib/plex_companion.py | 4 +-- resources/lib/plexbmchelper/httppersist.py | 2 +- resources/lib/plexbmchelper/listener.py | 2 +- resources/lib/plexbmchelper/plexgdm.py | 10 ++++---- resources/lib/sync.py | 2 +- resources/lib/utils.py | 29 ++++++++++++---------- resources/lib/websocket.py | 2 +- resources/lib/websocket_client.py | 2 +- 16 files changed, 65 insertions(+), 72 deletions(-) diff --git a/resources/lib/artwork.py b/resources/lib/artwork.py index 539a9c45..555cb70c 100644 --- a/resources/lib/artwork.py +++ b/resources/lib/artwork.py @@ -62,7 +62,7 @@ class ImageCachingThread(backgroundthread.KillableThread): LOG.info("---===### Starting ImageCachingThread ###===---") try: self._run() - except: + except Exception: utils.ERROR() finally: LOG.info("---===### Stopped ImageCachingThread ###===---") diff --git a/resources/lib/backgroundthread.py b/resources/lib/backgroundthread.py index 46d8e240..7e9ac465 100644 --- a/resources/lib/backgroundthread.py +++ b/resources/lib/backgroundthread.py @@ -131,12 +131,12 @@ class Tasks(list): class Task(object): def __init__(self, priority=None): - self._priority = priority + self.priority = priority self._canceled = False self.finished = False def __cmp__(self, other): - return self._priority - other._priority + return self.priority - other.priority def start(self): BGThreader.addTask(self) @@ -182,7 +182,7 @@ class MutablePriorityQueue(Queue.PriorityQueue): self.mutex.acquire() try: lowest = self.queue and min(self.queue) or None - except: + except Exception: lowest = None utils.ERROR() finally: @@ -203,7 +203,7 @@ class BackgroundWorker(object): return try: task._run() - except: + except Exception: utils.ERROR() def abort(self): @@ -275,12 +275,12 @@ class BackgroundThreader: self.name = name self._queue = MutablePriorityQueue() self._abort = False - self._priority = -1 + self.priority = -1 self.workers = [worker(self._queue, 'queue.{0}:worker.{1}'.format(self.name, x)) for x in range(worker_count)] def _nextPriority(self): - self._priority += 1 - return self._priority + self.priority += 1 + return self.priority def abort(self): self._abort = True @@ -298,13 +298,13 @@ class BackgroundThreader: w.shutdown() def addTask(self, task): - task._priority = self._nextPriority() + task.priority = self._nextPriority() self._queue.put(task) self.startWorkers() def addTasks(self, tasks): for t in tasks: - t._priority = self._nextPriority() + t.priority = self._nextPriority() self._queue.put(t) self.startWorkers() @@ -316,7 +316,7 @@ class BackgroundThreader: p = lowest - len(tasks) for t in tasks: - t._priority = p + t.priority = p self._queue.put(t) p += 1 @@ -337,14 +337,14 @@ class BackgroundThreader: if not lowest: return None - return lowest._priority + return lowest.priority def moveToFront(self, qitem): lowest = self.getLowestPrority() if lowest is None: return - qitem._priority = lowest - 1 + qitem.priority = lowest - 1 class ThreaderManager: diff --git a/resources/lib/downloadutils.py b/resources/lib/downloadutils.py index b4bc2562..746c11a0 100644 --- a/resources/lib/downloadutils.py +++ b/resources/lib/downloadutils.py @@ -84,21 +84,23 @@ class DownloadUtils(): def stopSession(self): try: self.s.close() - except: + except Exception: LOG.info("Requests session already closed") try: del self.s - except: + except AttributeError: pass LOG.info('Request session stopped') - def getHeader(self, options=None): + @staticmethod + def getHeader(options=None): header = clientinfo.getXArgsDeviceInfo() if options is not None: header.update(options) return header - def _doDownload(self, s, action_type, **kwargs): + @staticmethod + def _doDownload(s, action_type, **kwargs): if action_type == "GET": r = s.get(**kwargs) elif action_type == "POST": @@ -201,7 +203,7 @@ class DownloadUtils(): LOG.info('SystemExit detected, aborting download') self.stopSession() - except: + except Exception: LOG.warn('Unknown error while downloading. Traceback:') import traceback LOG.warn(traceback.format_exc()) @@ -255,7 +257,7 @@ class DownloadUtils(): # xml response r = utils.defused_etree.fromstring(r.content) return r - except: + except Exception: r.encoding = 'utf-8' if r.text == '': # Answer does not contain a body @@ -264,7 +266,7 @@ class DownloadUtils(): # UNICODE - JSON object r = r.json() return r - except: + except Exception: if '200 OK' in r.text: # Received fucked up OK from PMS on playstate # update diff --git a/resources/lib/kodi_db/video.py b/resources/lib/kodi_db/video.py index 6954e19e..5eaf3eea 100644 --- a/resources/lib/kodi_db/video.py +++ b/resources/lib/kodi_db/video.py @@ -694,7 +694,7 @@ class KodiVideoDB(common.KodiDBBase): SET tag_id = ? WHERE media_id = ? AND media_type = ? AND tag_id = ? ''', (newtag, kodiid, mediatype, oldtag,)) - except: + except Exception: # The new tag we are going to apply already exists for this item # delete current tag instead self.cursor.execute(''' diff --git a/resources/lib/library_sync/fanart.py b/resources/lib/library_sync/fanart.py index 121d3786..c9f9ff9a 100644 --- a/resources/lib/library_sync/fanart.py +++ b/resources/lib/library_sync/fanart.py @@ -41,7 +41,7 @@ class FanartThread(backgroundthread.KillableThread): def run(self): try: self._run_internal() - except: + except Exception: utils.ERROR(notify=True) def _run_internal(self): diff --git a/resources/lib/library_sync/full_sync.py b/resources/lib/library_sync/full_sync.py index ccda2b7e..62cd3b89 100644 --- a/resources/lib/library_sync/full_sync.py +++ b/resources/lib/library_sync/full_sync.py @@ -65,6 +65,10 @@ class FullSync(common.fullsync_mixin): self.title = '' self.section = None self.section_name = None + self.section_type_text = None + self.context = None + self.get_children = None + self.successful = None self.install_sync_done = utils.settings('SyncInstallRunDone') == 'true' self.threader = backgroundthread.ThreaderManager( worker=backgroundthread.NonstoppingBackgroundWorker, @@ -181,7 +185,7 @@ class FullSync(common.fullsync_mixin): while True: # Check Plex DB to see what we need to add/update with PlexDB() as self.plexdb: - for i, (last, xml_item) in enumerate(loop): + for last, xml_item in loop: if self.isCanceled(): return False self.process_item(xml_item) @@ -428,7 +432,4 @@ class FullSync(common.fullsync_mixin): def start(show_dialog, repair=False, callback=None): - """ - """ - # FullSync(repair, callback, show_dialog).start() FullSync(repair, callback, show_dialog).run() diff --git a/resources/lib/library_sync/videonodes.py b/resources/lib/library_sync/videonodes.py index 3eae6eb3..54c1e51f 100644 --- a/resources/lib/library_sync/videonodes.py +++ b/resources/lib/library_sync/videonodes.py @@ -15,7 +15,8 @@ LOG = getLogger('PLEX.videonodes') class VideoNodes(object): - def commonRoot(self, order, label, tagname, roottype=1): + @staticmethod + def commonRoot(order, label, tagname, roottype=1): if roottype == 0: # Index @@ -113,10 +114,7 @@ class VideoNodes(object): label=tagname, tagname=tagname, roottype=0) - try: - utils.indent(root) - except: - pass + utils.indent(root) etree.ElementTree(root).write(nodeXML, encoding="UTF-8") nodetypes = { @@ -406,10 +404,7 @@ class VideoNodes(object): rule = etree.SubElement(root, 'rule', {'field': "inprogress", 'operator':"true"}) - try: - utils.indent(root) - except: - pass + utils.indent(root) etree.ElementTree(root).write(path_ops.encode_path(nodeXML), encoding="UTF-8") @@ -464,13 +459,11 @@ class VideoNodes(object): etree.SubElement(root, 'content').text = mediatype - try: - utils.indent(root) - except: - pass + utils.indent(root) etree.ElementTree(root).write(nodeXML, encoding="UTF-8") - def clearProperties(self): + @staticmethod + def clearProperties(): LOG.info("Clearing nodes properties.") plexprops = utils.window('Plex.nodes.total') diff --git a/resources/lib/plex_api.py b/resources/lib/plex_api.py index f6749481..29418253 100644 --- a/resources/lib/plex_api.py +++ b/resources/lib/plex_api.py @@ -495,12 +495,6 @@ class API(object): """ pass - def votecount(self): - """ - Not yet implemented - """ - pass - def tagline(self): """ Returns a shorter tagline or None @@ -1517,7 +1511,7 @@ class API(object): return try: date = sub(r'(\d+)-(\d+)-(\d+)', r'\3.\2.\1', date) - except: + except Exception: date = None return date diff --git a/resources/lib/plex_companion.py b/resources/lib/plex_companion.py index 57f287f8..01b7f259 100644 --- a/resources/lib/plex_companion.py +++ b/resources/lib/plex_companion.py @@ -282,7 +282,7 @@ class PlexCompanion(backgroundthread.KillableThread): listener.MyHandler) httpd.timeout = 0.95 break - except: + except Exception: LOG.error("Unable to start PlexCompanion. Traceback:") import traceback LOG.error(traceback.print_exc()) @@ -330,7 +330,7 @@ class PlexCompanion(backgroundthread.KillableThread): subscription_manager.notify() if not httpd: message_count = 0 - except: + except Exception: LOG.warn("Error in loop, continuing anyway. Traceback:") import traceback LOG.warn(traceback.format_exc()) diff --git a/resources/lib/plexbmchelper/httppersist.py b/resources/lib/plexbmchelper/httppersist.py index 450cba3c..89a77ed8 100644 --- a/resources/lib/plexbmchelper/httppersist.py +++ b/resources/lib/plexbmchelper/httppersist.py @@ -70,7 +70,7 @@ class RequestMgr: # Close connection just in case try: conn.close() - except: + except Exception: pass return False diff --git a/resources/lib/plexbmchelper/listener.py b/resources/lib/plexbmchelper/listener.py index 807f96d4..7efd94bf 100644 --- a/resources/lib/plexbmchelper/listener.py +++ b/resources/lib/plexbmchelper/listener.py @@ -93,7 +93,7 @@ class MyHandler(BaseHTTPRequestHandler): self.end_headers() self.wfile.write(body) self.wfile.close() - except: + except Exception: pass def answer_request(self, send_data): diff --git a/resources/lib/plexbmchelper/plexgdm.py b/resources/lib/plexbmchelper/plexgdm.py index b28df861..077c46c2 100644 --- a/resources/lib/plexbmchelper/plexgdm.py +++ b/resources/lib/plexbmchelper/plexgdm.py @@ -95,7 +95,7 @@ class plexgdm: % (self.client_header, self.client_data), self.client_register_group) log.debug('(Re-)registering PKC Plex Companion successful') - except: + except Exception: log.error("Unable to send registration message") def client_update(self): @@ -109,14 +109,14 @@ class plexgdm: update_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) - except: + except Exception: pass # Attempt to bind to the socket to recieve and send data. If we cant # do this, then we cannot send registration try: update_sock.bind(('0.0.0.0', self.client_update_port)) - except: + except Exception: log.error("Unable to bind to port [%s] - Plex Companion will not " "be registered. Change the Plex Companion update port!" % self.client_update_port) @@ -165,7 +165,7 @@ class plexgdm: update_sock.sendto("HTTP/1.0 200 OK\n%s" % self.client_data, addr) - except: + except Exception: log.error("Unable to send client update message") log.debug("Sending registration data HTTP/1.0 200 OK") @@ -180,7 +180,7 @@ class plexgdm: update_sock.sendto("BYE %s\n%s" % (self.client_header, self.client_data), self.client_register_group) - except: + except Exception: log.error("Unable to send client update message") self.client_registered = False diff --git a/resources/lib/sync.py b/resources/lib/sync.py index 7f2f6afb..dacca413 100644 --- a/resources/lib/sync.py +++ b/resources/lib/sync.py @@ -146,7 +146,7 @@ class Sync(backgroundthread.KillableThread): def run(self): try: self._run_internal() - except: + except Exception: app.SYNC.db_scan = False utils.window('plex_dbScan', clear=True) utils.ERROR(txt='sync.py crashed', notify=True) diff --git a/resources/lib/utils.py b/resources/lib/utils.py index 56ac9f50..df021cde 100644 --- a/resources/lib/utils.py +++ b/resources/lib/utils.py @@ -607,19 +607,22 @@ def indent(elem, level=0): """ Prettifies xml trees. Pass the etree root in """ - i = "\n" + level * " " - if len(elem): - if not elem.text or not elem.text.strip(): - elem.text = i + " " - if not elem.tail or not elem.tail.strip(): - elem.tail = i - for elem in elem: - indent(elem, level + 1) - if not elem.tail or not elem.tail.strip(): - elem.tail = i - else: - if level and (not elem.tail or not elem.tail.strip()): - elem.tail = i + try: + i = "\n" + level * " " + if len(elem): + if not elem.text or not elem.text.strip(): + elem.text = i + " " + if not elem.tail or not elem.tail.strip(): + elem.tail = i + for elem in elem: + indent(elem, level + 1) + if not elem.tail or not elem.tail.strip(): + elem.tail = i + else: + if level and (not elem.tail or not elem.tail.strip()): + elem.tail = i + except Exception as err: + LOG.info('Indentation failed with: %s', err) class XmlKodiSetting(object): diff --git a/resources/lib/websocket.py b/resources/lib/websocket.py index 6b039429..7c4e3452 100644 --- a/resources/lib/websocket.py +++ b/resources/lib/websocket.py @@ -705,7 +705,7 @@ class WebSocket(object): """ try: self.sock.shutdown(socket.SHUT_RDWR) - except: + except Exception: pass self._closeInternal() diff --git a/resources/lib/websocket_client.py b/resources/lib/websocket_client.py index 09f919c1..2068d2f8 100644 --- a/resources/lib/websocket_client.py +++ b/resources/lib/websocket_client.py @@ -237,7 +237,7 @@ class Alexa_Websocket(WebSocket): LOG.error('%s: Unknown Alexa message received', self.__class__.__name__) return - except: + except Exception: LOG.error('%s: Could not parse Alexa message', self.__class__.__name__) return