Merge branch 'master' of ssh://repo.or.cz/srv/git/recordtv
[recordtv.git] / src / rtv_favourite.py
blob1d9091201f1d4f470dd23bfb26250bf31b60bfc8
1 #!/usr/bin/python
3 import re
4 import rtv_propertiesfile, rtv_programmeinfo
6 class Favourite(rtv_propertiesfile.PropertiesFile):
8 def __init__( self ):
9 self.title_re = None
10 self.sub_title_re = None
11 self.channel = None
12 self.not_channel = None
13 self.deleteAfterDays = None
14 self.priority = None
15 self.record = "yes"
16 self.destination = None
17 self.real_title = None
18 self.unique_subtitles = True
19 self.filetype = None
21 self._title_re = None
22 self._sub_title_re = None
25 def matches( self, pi ):
27 if self.title_re is None:
28 return False # This favourite is empty
30 if self._title_re is None:
31 self._title_re = re.compile( self.title_re + "$" )
33 if not self._title_re.match( pi.title ):
34 return False
36 if self.not_channel is not None and self.not_channel == pi.channel:
37 return False
39 if self.channel is not None and self.channel != pi.channel:
40 return False
42 if self.sub_title_re is not None and pi.sub_title is not None:
43 if self._sub_title_re is None:
44 self._sub_title_re = re.compile( self.sub_title_re + "$" )
45 if not self._sub_title_re.match( pi.sub_title ):
46 return False
48 return True
49 # TODO: other things to match on e.g. time, categories
51 class FakeProg( object ):
52 def __init__( self, title, channel, sub_title = None ):
53 self.title = title
54 self.channel = channel
55 self.sub_title = sub_title
57 def test_Empty_favourite_never_matches():
58 fav = Favourite()
59 assert( not fav.matches( FakeProg( "t", "c" ) ) )
61 def test_Wrong_title_doesnt_match():
62 fav = Favourite()
63 fav.title_re = "other"
64 assert( not fav.matches( FakeProg( "this", "c" ) ) )
66 def test_Right_title_matches():
67 fav = Favourite()
68 fav.title_re = "this"
69 assert( fav.matches( FakeProg( "this", "c" ) ) )
71 def test_Wrong_subtitle_doesnt_match():
72 fav = Favourite()
73 fav.title_re = "this"
74 fav.sub_title_re = "other"
75 assert( not fav.matches( FakeProg( "this", "c", "this_sub" ) ) )
77 def test_Right_subtitle_matches():
78 fav = Favourite()
79 fav.title_re = "this"
80 fav.sub_title_re = "this_sub"
81 assert( fav.matches( FakeProg( "this", "c", "this_sub" ) ) )
83 def test_Wrong_channel_doesnt_match():
84 fav = Favourite()
85 fav.title_re = "this"
86 fav.channel = "oc"
87 assert( not fav.matches( FakeProg( "this", "c" ) ) )
89 def test_Right_channel_does_match():
90 fav = Favourite()
91 fav.title_re = "this"
92 fav.channel = "c"
93 assert( fav.matches( FakeProg( "this", "c" ) ) )
95 def test_Not_channel_excludes():
96 fav = Favourite()
97 fav.title_re = "this"
98 fav.not_channel = "Dave"
99 assert( not fav.matches( FakeProg( "this", "Dave" ) ) )
101 def test_Unmatching_not_channel_doesnt_exclude():
102 fav = Favourite()
103 fav.title_re = "this"
104 fav.not_channel = "Dave"
105 assert( fav.matches( FakeProg( "this", "BBC One" ) ) )
107 def test( config ):
108 test_Empty_favourite_never_matches()
109 test_Wrong_title_doesnt_match()
110 test_Right_title_matches()
111 test_Wrong_subtitle_doesnt_match()
112 test_Right_subtitle_matches()
113 test_Wrong_channel_doesnt_match()
114 test_Right_channel_does_match()
115 test_Not_channel_excludes()
116 test_Unmatching_not_channel_doesnt_exclude()