-
Notifications
You must be signed in to change notification settings - Fork 3
/
pp_pathmanager.py
136 lines (107 loc) · 4.38 KB
/
pp_pathmanager.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
import copy
class PathManager(object):
def __init__(self):
self.debug=False #shows the path commands and the stack
# self.debug=True
self.path_stack=[]
# pops back to 'stop_at'
# then pops back one further and returns the track-ref of this track for replaying
# if stop-at is not found returns ''
def back_to(self,stop_at,):
if self.debug: print('pathmanager command - back_to: ',stop_at)
for page in self.path_stack:
if page[0] == stop_at:
break
else:
return ''
# found, so pop until we reach it
while self.path_stack[len(self.path_stack)-1][0] != stop_at:
self.path_stack.pop()
track_to_play = self.path_stack[len(self.path_stack)-1][0]
self.path_stack.pop()
return track_to_play
# pops back 'number' tracks or to 'stop_at' whichever is first
# then pops back one further and returns the track-ref of this track for replaying
# if stop-at is not found and everything is popped the stack is left empty and the first track is returned
def back_by(self,stop_at,back_by_text='1000'):
if self.debug: print('pathmanager command - back by: ',back_by_text,' or stop at: ',stop_at)
back_by=int(back_by_text)
count=0
while self.path_stack != []:
top = self.path_stack.pop()
if top[0] == stop_at or count == back_by-1:
break
count=count+1
# go back 1 if not empty
if self.path_stack != []:
top=self.path_stack.pop()
track_to_play = top[0]
if self.debug:
print(' removed for playing: ',track_to_play)
return track_to_play
def append(self,page):
if self.debug: print('pathmanager command - append: ',page)
self.path_stack.append([page])
def empty(self):
self.path_stack=[]
# sibling - just pop the media track so sibling is appended and can go back to page track
def pop_for_sibling(self):
if self.debug: print('pathmanger command - pop for sibling: ')
self.path_stack.pop()
def pretty_path(self):
path= '\nPath now is:'
for page in self.path_stack:
path += "\n " + page[0]
print()
return path
# *******************
# Extract links
# ***********************
def parse_links(self,links_text,allowed_list):
links=[]
lines = links_text.split('\n')
num_lines=0
for line in lines:
if line.strip() == "":
continue
num_lines+=1
error_text,link=self.parse_link(line,allowed_list)
if error_text != "":
return 'error',error_text,links
links.append(copy.deepcopy(link))
# print "\nreading"
# print links
return 'normal','',links
def parse_link(self,line,allowed_list):
fields = line.split()
if len(fields)<2 or len(fields)>3:
return "incorrect number of fields in command",['','','']
symbol=fields[0]
operation=fields[1]
if operation in allowed_list or operation[0:4] == 'omx-' or operation[0:6] == 'mplay-'or operation[0:5] == 'uzbl-':
if len(fields) == 3:
arg=fields[2]
else:
arg=''
return '',[symbol,operation,arg]
else:
return "unknown operation: "+operation,['','','']
def merge_links(self,current_links,track_links):
for track_link in track_links:
for link in current_links:
if track_link[0] == link[0]:
# link exists so overwrite
link[1]=track_link[1]
link[2]=track_link[2]
break
else:
# new link so append it
current_links.append(track_link)
# print "\n merging"
# print current_links
def find_link(self,symbol,links):
for link in links:
# print link
if symbol == link[0]:
return True,link[1],link[2]
return False,'',''