Skip to content

Commit

Permalink
Merge branch 'release/0.2.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
fuzeman committed Dec 8, 2013
2 parents 2afa372 + cd1bb3c commit 08edc5d
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 36 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ Extensible filename parsing library for Python

## Development

**Dependencies**
### Requirements

**Python:** versions 2.6 - 3.3 supported

**Packages:**

Logr>=0.2.2

Expand Down
30 changes: 24 additions & 6 deletions src/caper/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from caper.parsers.scene import SceneParser


__version_info__ = ('0', '2', '6')
__version_info__ = ('0', '2', '7')
__version_branch__ = 'master'

__version__ = "%s%s" % (
Expand Down Expand Up @@ -63,7 +63,7 @@ def end_closure(closures, buf):
if len(buf) < 1:
return

cur = CaperClosure(buf)
cur = CaperClosure(len(closures), buf)
cur.left = closures[len(closures) - 1] if len(closures) > 0 else None

if cur.left:
Expand Down Expand Up @@ -109,7 +109,7 @@ def _fragment_split(self, closures):
"""

cur_position = 0
cur = CaperFragment()
cur = None

def end_fragment(fragments, cur, cur_position):
cur.position = cur_position
Expand All @@ -126,12 +126,30 @@ def end_fragment(fragments, cur, cur_position):
for closure in closures:
closure.fragments = []

separator_buffer = ""

for x, ch in enumerate(self._clean_closure(closure.value)):
if not cur:
cur = CaperFragment(closure)

if ch in FRAGMENT_SEPARATORS:
end_fragment(closure.fragments, cur, cur_position)
if cur.value:
separator_buffer = ""

separator_buffer += ch

if cur.value or not closure.fragments:
end_fragment(closure.fragments, cur, cur_position)
elif len(separator_buffer) > 1:
cur.value = separator_buffer.strip()

if cur.value:
end_fragment(closure.fragments, cur, cur_position)

separator_buffer = ""

# Reset
cur = CaperFragment()
cur = None
cur_position += 1
else:
cur.value += ch
Expand All @@ -142,7 +160,7 @@ def end_fragment(fragments, cur, cur_position):

# Reset
cur_position = 0
cur = CaperFragment()
cur = None

return closures

Expand Down
10 changes: 8 additions & 2 deletions src/caper/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@


class CaperClosure(object):
def __init__(self, value):
def __init__(self, index, value):
#: :type: int
self.index = index

#: :type: str
self.value = value

Expand All @@ -30,7 +33,10 @@ def __init__(self, value):


class CaperFragment(object):
def __init__(self):
def __init__(self, closure=None):
#: :type: CaperClosure
self.closure = closure

#: :type: str
self.value = ""

Expand Down
34 changes: 21 additions & 13 deletions src/caper/parsers/scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
(1.0, [
# S01E01-E02
('^S(?P<season>\d+)E(?P<episode_from>\d+)$', '^E(?P<episode_to>\d+)$'),
# S03 E01 to E08
('^S(?P<season>\d+)$', '^E(?P<episode_from>\d+)$', '^to$', '^E(?P<episode_to>\d+)$'),
# 'S03 E01 to E08' or 'S03 E01 - E09'
('^S(?P<season>\d+)$', '^E(?P<episode_from>\d+)$', '^(to|-)$', '^E(?P<episode_to>\d+)$'),
# 'E01 to E08' or 'E01 - E09'
('^E(?P<episode_from>\d+)$', '^(to|-)$', '^E(?P<episode_to>\d+)$'),

# S01-S03
('^S(?P<season_from>\d+)$', '^S(?P<season_to>\d+)$'),
Expand Down Expand Up @@ -58,6 +60,8 @@
# Part.3
# Part.1.and.Part.3
('^Part$', '(?P<part>\d+)'),

r'(?P<extra>Special)'
]),
(0.8, [
# 100 - 1899, 2100 - 9999 (skips 1900 to 2099 - so we don't get years my mistake)
Expand All @@ -69,6 +73,7 @@
r'^(?P<season>([1-9])|([1-9][0-9]))(?P<episode>\d{2})$'
])
]),

('video', [
r'(?P<aspect>FS|WS)',

Expand Down Expand Up @@ -156,10 +161,13 @@ def __init__(self, debug=False):
super(SceneParser, self).__init__(PATTERN_GROUPS, debug)

def capture_group(self, fragment):
if fragment.left_sep == '-' and not fragment.right:
return fragment.value
if fragment.closure.index + 1 != len(self.closures):
return None

if fragment.left_sep != '-' or fragment.right:
return None

return None
return fragment.value

def run(self, closures):
"""
Expand All @@ -170,17 +178,17 @@ def run(self, closures):

self.capture_fragment('show_name', single=False)\
.until(fragment__re='identifier')\
.until(fragment__re='video') \
.until(fragment__re='dvd') \
.until(fragment__re='audio') \
.until(fragment__re='scene') \
.until(fragment__re='video')\
.until(fragment__re='dvd')\
.until(fragment__re='audio')\
.until(fragment__re='scene')\
.execute()

self.capture_fragment('identifier', regex='identifier', single=False)\
.capture_fragment('video', regex='video', single=False) \
.capture_fragment('dvd', regex='dvd', single=False) \
.capture_fragment('audio', regex='audio', single=False) \
.capture_fragment('scene', regex='scene', single=False) \
.capture_fragment('video', regex='video', single=False)\
.capture_fragment('dvd', regex='dvd', single=False)\
.capture_fragment('audio', regex='audio', single=False)\
.capture_fragment('scene', regex='scene', single=False)\
.until(left_sep__eq='-', right__eq=None)\
.execute()

Expand Down
60 changes: 46 additions & 14 deletions tests/test_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
caper = Caper()


def create_testable_closures(closures):
result = []
for closure in closures:
if closure.fragments and len(closure.fragments) > 0:
result.append((closure.value, [fragment.value for fragment in closure.fragments]))
else:
result.append(closure.value)

return result


def test_closure_generation():
assert [s.value for s in caper._closure_split('[Group]_Show_Name_7.2_An_Episode_Title_(Blu-Ray_1280x720_FLAC)_[645G7V54].mkv')] == [
'[Group]',
Expand Down Expand Up @@ -55,30 +66,20 @@ def test_closure_generation():


def test_fragment_generation():
def testable_closures(closures):
result = []
for closure in closures:
if closure.fragments and len(closure.fragments) > 0:
result.append((closure.value, [fragment.value for fragment in closure.fragments]))
else:
result.append(closure.value)

return result

#
# [Another Group] Some Show Name [720p DVD]_[G84VA2BX]
#

closures = caper._closure_split('[Another Group] Some Show Name [720p DVD]_[G84VA2BX]')

assert testable_closures(closures) == [
assert create_testable_closures(closures) == [
'[Another Group]',
'Some Show Name',
'[720p DVD]',
'[G84VA2BX]'
]

assert testable_closures(caper._fragment_split(closures)) == [
assert create_testable_closures(caper._fragment_split(closures)) == [
('[Another Group]', [
'Another',
'Group'
Expand All @@ -103,11 +104,11 @@ def testable_closures(closures):

closures = caper._closure_split('Show.Name.2010.S01.REPACK.1080p.BluRay.x264-GROUP')

assert testable_closures(closures) == [
assert create_testable_closures(closures) == [
'Show.Name.2010.S01.REPACK.1080p.BluRay.x264-GROUP'
]

assert testable_closures(caper._fragment_split(closures)) == [
assert create_testable_closures(caper._fragment_split(closures)) == [
('Show.Name.2010.S01.REPACK.1080p.BluRay.x264-GROUP', [
'Show',
'Name',
Expand All @@ -120,3 +121,34 @@ def testable_closures(closures):
'GROUP'
])
]


def test_fragment_extend():
#
# Show Name S01 (E01 - E08) WEB-DL
#

closures = caper._closure_split('Show Name S01 (E01 - E08) WEB-DL')

assert create_testable_closures(closures) == [
'Show Name S01',
'(E01 - E08)',
'WEB-DL'
]

assert create_testable_closures(caper._fragment_split(closures)) == [
('Show Name S01', [
'Show',
'Name',
'S01'
]),
('(E01 - E08)', [
'E01',
'-',
'E08'
]),
('WEB-DL', [
'WEB',
'DL'
])
]
6 changes: 6 additions & 0 deletions tests/test_scene_parser/test_season.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,9 @@ def test_season_range():
caper.parse('Show.Name.S01-S03.DVDrip.x264'),
has_info('identifier', {'season_from': '01', 'season_to': '03'})
)

assert_that(
caper.parse('Show Name S01 (E01 - E08) WEB-DL'),
has_info('identifier', {'season': '01'}),
has_info('identifier', {'episode_from': '01', 'episode_to': '08'})
)

0 comments on commit 08edc5d

Please sign in to comment.