Repository: pymedusa/Medusa

Issue:
[APP SUBMITTED]: KeyError: u'D:\\- Series\\NCIS [2003]\\Season 07\\NCIS [2003] - S07E12 - Flesh and Blood.avi'
### INFO
**Python Version**: `2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)]`
**Operating System**: `Windows-10-10.0.10586`
**Locale**: `cp1252`
**Branch**: [fix-isinstance](../tree/fix-isinstance)
**Database**: `44.4`
**Commit**: pymedusa/Medusa@ee778e94195098ec9d602703ba0dcbb0b3db8192
**Link to Log**: https://gist.github.com/2646047b6705f9dec943af60d8a30039
### ERROR
<pre>
2017-01-26 05:25:40 ERROR    SEARCHQUEUE-DAILY-SEARCH :: [BinSearch] :: [ee778e9] Error while searching BinSearch, skipping: D:\- Series\NCIS [2003]\Season 07\NCIS [2003] - S07E12 - Flesh and Blood.avi
Traceback (most recent call last):
  File "H:\usr\local\medusa\medusa\search\core.py", line 418, in search_for_needed_episodes
    cur_found_results = cur_provider.search_rss(episodes)
  File "H:\usr\local\medusa\medusa\providers\generic_provider.py", line 425, in search_rss
    return self.cache.find_needed_episodes(episodes)
  File "H:\usr\local\medusa\medusa\tv_cache.py", line 463, in find_needed_episodes
    if not show_name_helpers.filterBadReleases(cur_result[b'name']):
  File "H:\usr\local\medusa\medusa\show_name_helpers.py", line 82, in filterBadReleases
    NameParser().parse(name)
  File "H:\usr\local\medusa\medusa\name_parser\parser.py", line 239, in parse
    name_parser_cache.add(name, result)
  File "H:\usr\local\medusa\medusa\name_parser\parser.py", line 430, in add
    del self._previous_parsed[self._previous_parsed.keys()[0]]
  File "C:\Python\latest\lib\collections.py", line 85, in __delitem__
    dict_delitem(self, key)
KeyError: u'D:\\- Series\\NCIS [2003]\\Season 07\\NCIS [2003] - S07E12 - Flesh and Blood.avi'
</pre>
---
_STAFF NOTIFIED_: @pymedusa/support @pymedusa/moderators


Discussion:

Resolving patch:
diff --git a/medusa/name_parser/parser.py b/medusa/name_parser/parser.py
--- a/medusa/name_parser/parser.py
+++ b/medusa/name_parser/parser.py
@@ -414,8 +414,10 @@ def video_codec(self):
 class NameParserCache(object):
     """Name parser cache."""
 
-    _previous_parsed = OrderedDict()
-    _cache_size = 1000
+    def __init__(self, max_size=1000):
+        """Initiate the cache with a maximum size."""
+        self.cache = OrderedDict()
+        self.max_size = max_size
 
     def add(self, name, parse_result):
         """Add the result to the parser cache.
@@ -425,9 +427,9 @@ def add(self, name, parse_result):
         :param parse_result:
         :type parse_result: ParseResult
         """
-        self._previous_parsed[name] = parse_result
-        while len(self._previous_parsed) > self._cache_size:
-            del self._previous_parsed[self._previous_parsed.keys()[0]]
+        while len(self.cache) >= self.max_size:
+            self.cache.popitem(last=False)
+        self.cache[name] = parse_result
 
     def get(self, name):
         """Return the cached parsed result.
@@ -437,9 +439,9 @@ def get(self, name):
         :return:
         :rtype: ParseResult
         """
-        if name in self._previous_parsed:
+        if name in self.cache:
             logger.debug("Using cached parse result for '{name}'", name=name)
-            return self._previous_parsed[name]
+            return self.cache[name]
 
 
 name_parser_cache = NameParserCache()


Test patch: