Skip to content

Commit

Permalink
Fix name parser unicode bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
midgetspy committed Dec 22, 2010
1 parent 963e425 commit 7ecc49e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
11 changes: 10 additions & 1 deletion sickbeard/name_parser/parser.py
Expand Up @@ -142,6 +142,12 @@ def _combine_results(self, first, second, attr):
else:
return b

def _unicodify(self, obj, encoding = "utf-8"):
if isinstance(obj, basestring):
if not isinstance(obj, unicode):
obj = unicode(obj, encoding)
return obj

def _convert_number(self, number):
if type(number) == int:
return number
Expand All @@ -167,6 +173,8 @@ def _convert_number(self, number):

def parse(self, name):

name = self._unicodify(name)

# break it into parts if there are any (dirname, file name, extension)
dir_name, file_name = os.path.split(name)
ext_match = re.match('(.*)\.\w{3,4}$', file_name)
Expand Down Expand Up @@ -225,6 +233,7 @@ def __init__(self,
release_group=None,
air_date=None
):

self.original_name = original_name

self.series_name = series_name
Expand Down Expand Up @@ -276,7 +285,7 @@ def __str__(self):
if self.release_group:
to_return += ' (' + self.release_group + ')'

return to_return
return to_return.encode('utf-8')

def _is_air_by_date(self):
if self.season_number == None and len(self.episode_numbers) == 0 and self.air_date:
Expand Down
2 changes: 1 addition & 1 deletion sickbeard/postProcessor.py
Expand Up @@ -313,7 +313,7 @@ def _analyze_name(self, name, file=True):
# parse the name to break it into show name, season, and episode
np = NameParser(file)
parse_result = np.parse(name)
self._log("Parsed "+name+" into "+str(parse_result), logger.DEBUG)
self._log("Parsed "+name+" into "+str(parse_result).decode('utf-8'), logger.DEBUG)

if parse_result.air_by_date:
season = -1
Expand Down
26 changes: 25 additions & 1 deletion tests/name_parser_tests.py
Expand Up @@ -99,6 +99,27 @@

]

unicode_test_cases = [
(u'The.Big.Bang.Theory.2x07.The.Panty.Pi\xf1ata.Polarization.720p.HDTV.x264.AC3-SHELDON.mkv',
parser.ParseResult(None, 'The.Big.Bang.Theory', 2, [7], '720p.HDTV.x264.AC3', 'SHELDON')
),
('The.Big.Bang.Theory.2x07.The.Panty.Pi\xc3\xb1ata.Polarization.720p.HDTV.x264.AC3-SHELDON.mkv',
parser.ParseResult(None, 'The.Big.Bang.Theory', 2, [7], '720p.HDTV.x264.AC3', 'SHELDON')
),
]

class UnicodeTests(unittest.TestCase):

def _test_unicode(self, name, result):
print repr(name)
np = parser.NameParser(True)
parse_result = np.parse(name)
print repr(str(parse_result))

def test_unicode(self):
for (name, result) in unicode_test_cases:
self._test_unicode(name, result)

class ComboTests(unittest.TestCase):

def _test_combo(self, name, result, which_regexes):
Expand Down Expand Up @@ -232,7 +253,10 @@ def test_combination_names(self):
suite = unittest.TestLoader().loadTestsFromName('name_parser_tests.BasicTests.test_'+sys.argv[1])
else:
suite = unittest.TestLoader().loadTestsFromTestCase(BasicTests)
unittest.TextTestRunner(verbosity=2).run(suite)
#unittest.TextTestRunner(verbosity=2).run(suite)

suite = unittest.TestLoader().loadTestsFromTestCase(ComboTests)
#unittest.TextTestRunner(verbosity=2).run(suite)

suite = unittest.TestLoader().loadTestsFromTestCase(UnicodeTests)
unittest.TextTestRunner(verbosity=2).run(suite)

0 comments on commit 7ecc49e

Please sign in to comment.