Permalink
Browse files

Changed how character encoding is dealt with and wrote some test to h…

…opefully chase down any remaining character encoding problems for good. Yeah right.
  • Loading branch information...
1 parent c66ac22 commit 96f4d29055a9b324a041e483305c594f4fb0e7d6 @midgetspy committed Jul 16, 2012
Showing with 109 additions and 23 deletions.
  1. +11 −11 SickBeard.py
  2. +11 −12 sickbeard/encodingKludge.py
  3. +47 −0 tests/encodingTests.py
  4. +40 −0 tests/test_data/encoding_tests/test.îâéè.txt
View
@@ -120,16 +120,6 @@ def main():
sickbeard.CREATEPID = False
sickbeard.DAEMON = False
- if not hasattr(sys,"setdefaultencoding"):
- reload(sys)
-
- try:
- sys.setdefaultencoding('utf-8')
- except:
- print 'Sorry, you MUST add the Sick Beard folder to the PYTHONPATH environment variable'
- print 'or find another way to force Python to use UTF-8 for string encoding.'
- sys.exit(1)
-
sickbeard.SYS_ENCODING = None
try:
@@ -138,10 +128,20 @@ def main():
except (locale.Error, IOError):
pass
- # for OSes that are poorly configured I'll just force UTF-8
+ # for OSes that are poorly configured I'll just randomly force UTF-8
if not sickbeard.SYS_ENCODING or sickbeard.SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'):
sickbeard.SYS_ENCODING = 'UTF-8'
+ if not hasattr(sys,"setdefaultencoding"):
+ reload(sys)
+
+ try:
+ sys.setdefaultencoding(sickbeard.SYS_ENCODING)
+ except:
+ print 'Sorry, you MUST add the Sick Beard folder to the PYTHONPATH environment variable'
+ print 'or find another way to force Python to use '+sickbeard.SYS_ENCODING+' for string encoding.'
+ sys.exit(1)
+
# need console logging for SickBeard.py and SickBeard-console.exe
consoleLogging = (not hasattr(sys, "frozen")) or (sickbeard.MY_NAME.lower().find('-console') > 0)
@@ -16,7 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with Sick Beard. If not, see <http://www.gnu.org/licenses/>.
-import os
+import os, traceback
from sickbeard import logger
import sickbeard
@@ -48,16 +48,15 @@ def fixListEncodings(x):
def ek(func, *args):
- result = None
- if os.name == 'nt':
- result = func(*args)
- else:
- result = func(*[x.encode(sickbeard.SYS_ENCODING) if type(x) in (str, unicode) else x for x in args])
+ def changeByteStringToUnicode(obj):
+ if type(obj) == str:
+ logger.log(u"A bytestring was used as an argument for function "+func.__name__+": "+repr(obj), logger.WARNING)
+ logger.log(''.join(traceback.format_stack()), logger.DEBUG)
+ return obj.decode(sickbeard.SYS_ENCODING)
+ else:
+ return obj
- if type(result) == list:
- return fixListEncodings(result)
- elif type(result) == str:
- return fixStupidEncodings(result)
- else:
- return result
+ args = map(changeByteStringToUnicode, args)
+
+ return func(*args)
@@ -0,0 +1,47 @@
+import unittest
+import sys, os, locale
+
+sys.path.append(os.path.abspath('..'))
+sys.path.append(os.path.abspath('../lib'))
+
+import sickbeard, sickbeard.encodingKludge as ek
+
+try:
+ locale.setlocale(locale.LC_ALL, "")
+ sickbeard.SYS_ENCODING = locale.getpreferredencoding()
+except (locale.Error, IOError):
+ pass
+
+# for OSes that are poorly configured I'll just force UTF-8
+if not sickbeard.SYS_ENCODING or sickbeard.SYS_ENCODING in ('ANSI_X3.4-1968', 'US-ASCII', 'ASCII'):
+ sickbeard.SYS_ENCODING = 'UTF-8'
+
+class EncodingTests(unittest.TestCase):
+
+ def test_unicode(self):
+ ufiles = os.listdir(u'test_data\\encoding_tests')
+ files = os.listdir('test_data\\encoding_tests')
+
+ print
+ print 'Testing that the encoding', sickbeard.SYS_ENCODING, 'behaves as we expect it would'
+
+ self.assertEqual(ufiles[0], files[0].decode(sickbeard.SYS_ENCODING))
+
+ def test_ek(self):
+ str_file = ek.ek(os.listdir, 'test_data\\encoding_tests')[0]
+ unicode_file = ek.ek(os.listdir, u'test_data\\encoding_tests')[0]
+
+ # no matter if we give unicode or not we should get unicode back
+ self.assertEqual(u'test.\xee\xe2\xe9\xe8.txt', unicode_file)
+ self.assertEqual(u'test.\xee\xe2\xe9\xe8.txt', str_file)
+
+ # we should be able to append it onto a string without an exception
+ self.assertEqual(unicode, type("str" + unicode_file))
+ self.assertEqual(unicode, type(u"unicode" + unicode_file))
+
+ # we should be able to encode it arbitrarily without an exception
+ self.assertEqual('test.\xc3\xae\xc3\xa2\xc3\xa9\xc3\xa8.txt', unicode_file.encode('utf-8'))
+
+if __name__ == '__main__':
+ suite = unittest.TestLoader().loadTestsFromTestCase(EncodingTests)
+ unittest.TextTestRunner(verbosity=2).run(suite)
@@ -0,0 +1,40 @@
+Episode Naming
+
+Show Name %SN Show Name
+ %S.N Show.Name
+ %S_N Show_Name
+Episode Name %EN Episode Name
+ %E.N Episode.Name
+ %E_N Episode_Name
+Season Number %S 1
+ %0S 01
+Episode Number %E 5
+ %0E 05
+Quality %Q 720p BluRay
+
+Use lower case if you want lower case names (eg. %sn, %e.n, %q etc)
+
+Date format:
+- 2010-11-23
+- 2010.11.23
+- 2010_11_23
+- 2010 11 23
+
+Multi-ep:
+- repeat (1x2x3x4)
+- extend (1x2-3-4)
+- duplicate (1x2 1x3 1x4)
+
+PP-only stuff:
+
+%OF Original_File_Name
+%RG Release_Group
+
+
+Validation:
+- season/episode can only occur once
+- episode MUST occur once
+
+Also:
+- remove season folder option, require users to put it in the ep naming
+- on rename, show proposed changes and ask about folder sorting too

0 comments on commit 96f4d29

Please sign in to comment.