Page 1 of 1

Python 2.6 tvsort bug (fix inside)

Posted: December 5th, 2009, 10:43 am
by cj_
Found another bug that results in post processing crash with the following traceback:

Code: Select all

  File "/home/cjones/local/usenet/sabnzbd/tvsort.py", line 630, in check_for_multiple
    regex = re.compile(regex, re.I)
  File "/usr/lib/python2.6/re.py", line 190, in compile
    return _compile(pattern, flags)
  File "/usr/lib/python2.6/re.py", line 238, in _compile
    raise ValueError('Cannot process flags argument with a compiled pattern')
ValueError: Cannot process flags argument with a compiled pattern
The culprit is trying to re-compile an already compiled regex, which does not work in python 2.6:

Code: Select all

621:     def check_for_multiple(self, files):
622:         expressions = []
623:         matched_files = []
624: 
625:         expressions.append(re.compile('cd\W?(\d)\W', re.I)) # .cd1.avi
626:         expressions.append(re.compile('\w\W?([\w\d])$', re.I)) # blah1.avi blaha.avi
627:         expressions.append(re.compile('\w\W([\w\d])\W', re.I)) # blah-1-ok.avi blah-a-ok.avi
628: 
629:         for regex in expressions:
630:             regex = re.compile(regex, re.I)
631:             matched_files = check_for_sequence(regex, files)
632:             if matched_files:
633:                 return matched_files
634:         return ''
Even though this works in python 2.5, there is no need to compile twice.  I would either remove line 630, or not compile in 625-627.  The function could probably be better written like so:

Code: Select all

def check_for_multiple(self, files):
    for regex in r'cd\W?(\d)\W', r'\w\W?([\w\d])$', r'\w\W([\w\d])\W':
        matched_files = check_for_sequence(re.compile(regex, re.I), files)
        if matched_files:
            return matched_files
    return ''

Re: Python 2.6 tvsort bug (fix inside)

Posted: December 5th, 2009, 11:04 am
by cj_
Oh, one other bug in the same module.  No fix here, but you might want to look at it.  Using a sort string like this:

{%s.n.%ext}

Doesn't work right.  The reason is that before the code converts {} to lowercase and strips it out, it does this:

if path.endswith('%ext'):
  ...

Since the path does not actually end with %ext (it ends with "%ext}"), it behaves as if this were a directory.  %e gets replace with the episode number, so you end up with a directory structure like this:

show.name.1xt/OrignalFilename.avi

.. which is suboptimal.  There are a few ways to fix this.  Maybe use a regex that allows for both cases, like: %ext}?$

Re: Python 2.6 tvsort bug (fix inside)

Posted: December 5th, 2009, 11:32 am
by shypike
Thanks.
I need to speak to the tvsort designer...