-
Notifications
You must be signed in to change notification settings - Fork 3
/
pp_showlist.py
173 lines (140 loc) · 4.91 KB
/
pp_showlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import copy
import json
import string
from pp_utils import Monitor
class ShowList(object):
"""
manages a list of show and the show selected from the showlist
"""
def __init__(self):
self.mon=Monitor()
self.clear()
def clear(self):
self._shows = [] #MediaList, stored as a list of dicts
self._num_shows=0
self._selected_show_index=-1 # index of currently selected show
def deselect_all(self):
self._selected_show_index=-1 # index of currently selected show
def print_list(self):
print(self._shows)
def length(self):
return self._num_shows
def at_end(self):
if self._selected_show_index==self._num_shows-1:
return True
else:
return False
def at_start(self):
if self._selected_show_index==0:
return True
else:
return False
def show_is_selected(self):
if self._selected_show_index>=0:
return True
else:
return False
def selected_show_index(self):
return self._selected_show_index
def shows(self):
return self._shows
def show(self,index):
return self._shows[index]
def selected_show(self):
"""returns a dictionary containing all fields in the selected show """
return self._selected_show
def append(self, show_dict):
"""appends a show dictionary to the end of the showlist store"""
self._shows.append(copy.deepcopy(show_dict))
self._num_shows+=1
def copy(self,show_dict,new_name):
show_to_copy=copy.deepcopy(show_dict)
show_to_copy['show-ref']=new_name
show_to_copy['title']=new_name
return show_to_copy
def remove(self,index):
self._shows.pop(index)
self._num_shows-=1
# deselect any show, saves worrying about whether index needs changing
self._selected_show_index=-1
def replace(self,index,replacement):
self._shows[index]= replacement
def first(self):
self.select(0)
def do_next(self):
if self.length()>0:
if self._selected_show_index== self.length()-1:
index=0
else:
index= self._selected_show_index+1
self.select(index)
def previous(self):
if self.length()>0:
if self._selected_show_index == 0:
index=self.length()-1
else:
index= self._selected_show_index-1
self.select(index)
def index_of_start_show(self):
index = 0
for show in self._shows:
if show['type'] == 'start':
return index
index +=1
return -1
def index_of_show(self,wanted_show):
index = 0
for show in self._shows:
if show['type']!='start' and show['show-ref'] == wanted_show:
return index
index +=1
return -1
def select(self, index):
"""
user clicks on a show in the Shows list so try and select it.
"""
# needs forgiving int for possible tkinter upgrade
self._selected_show_index=index
self._selected_show=self._shows[index]
def open_json(self,filename):
"""
opens a saved showlistlist
showlists are stored in files as json arrays within a object 'shows'.
shows are stored internally as a list of dictionaries in self._shows
"""
if filename != '' and os.path.exists(filename):
ifile = open(filename, 'r')
sdict= json.load(ifile)
ifile.close()
self._rawshows=sdict['shows']
self._shows= sorted(self._rawshows, key=lambda k: k['show-ref'])
if 'issue' in sdict:
self.profile_version_string= sdict['issue']
else:
self.profile_version_string="1.0"
self._num_shows=len(self._shows)
self._selected_show_index=-1
return True
else:
return False
def profile_version(self):
vitems=self.profile_version_string.split('.')
if len(vitems)==2:
# cope with 2 digit version numbers before 1.3.2
return 1000*int(vitems[0])+100*int(vitems[1])
else:
return 1000*int(vitems[0])+100*int(vitems[1])+int(vitems[2])
def save_list(self,filename):
""" save a showlist """
if filename=="":
return False
if os.name=='nt':
filename = str.replace(filename,'/','\\')
else:
filename = str.replace(filename,'\\','/')
dic={'issue':self.profile_version_string,'shows':self._shows}
ofile = open(filename, "w")
json.dump(dic,ofile,sort_keys=True,indent=1)
ofile.close()
return