Commit 3c166480 authored by Gottfried Gaisbauer's avatar Gottfried Gaisbauer
Browse files

--delete-playlist-entry implemented

parent 25b38987
This diff is collapsed.
......@@ -52,7 +52,7 @@ class Guru:
# help="Add new source to LiquidSoap mixer [Experimental]")
# playlist manipulation
parser.add_argument("-mpe", "--swap-playlist-entries", action="store", dest="swap_playlist_entries", default=0, metavar=("FROM", "TO"), nargs=2,
parser.add_argument("-spe", "--swap-playlist-entries", action="store", dest="swap_playlist_entries", default=0, metavar=("FROM", "TO"), nargs=2,
help="Swaps two Playlistentries")
parser.add_argument("-dpe", "--delete-playlist-entry", action="store", dest="delete_playlist_entry", default=0, metavar="INDEX",
help="Delete Playlistentry at INDEX")
......
......@@ -24,6 +24,7 @@ import sys
import threading
import simplejson
import queue
import traceback
from datetime import datetime, timedelta
......@@ -136,7 +137,6 @@ class AuraCalendarService(threading.Thread):
# store playlists to play
entrynum = 0
print(schedule)
for entry in schedule["playlist"]["entries"]:
self.store_schedule_entry(schedule, entry, entrynum)
entrynum = entrynum + 1
......@@ -144,13 +144,12 @@ class AuraCalendarService(threading.Thread):
# release the mutex
self.queue.put("fetching_finished")
print("WARNING: fallback pools/playlists not processed!")
# terminate the thread
return
def store_schedule(self, schedule):
schedule_db = Schedule.query.filter(Schedule.schedule_id == schedule['schedule_id']).first()
if not schedule_db:
print("no schedule with given schedule id in database => create new")
schedule_db = Schedule()
......@@ -193,7 +192,8 @@ class AuraCalendarService(threading.Thread):
schedule_entry = ScheduleEntry.select_one(schedule["playlist_id"], entrynum)
if not schedule_entry:
print("no scheduleentry with id " + str(schedule["playlist_id"]) + " and pos " + str(entrynum) + " in database => creating a new one")
if self.debug:
print("no scheduleentry with id " + str(schedule["playlist_id"]) + " and pos " + str(entrynum) + " in database => creating a new one")
schedule_entry = ScheduleEntry()
schedule_entry.playlist_id = schedule["playlist_id"]
......@@ -216,9 +216,6 @@ class AuraCalendarService(threading.Thread):
print("Storing entries... playlist_id: " + str(schedule["playlist_id"]) + " schedule_id: " + str(
schedule["schedule_id"]) + " num: " + str(entrynum))
from pprint import pprint
pprint(vars(schedule_entry))
schedule_entry.store(True)
return schedule_entry
......@@ -264,8 +261,7 @@ class AuraCalendarService(threading.Thread):
url = self.get_url()
data = self.get_data()
except IOError as e:
self.messenger.send("Could not connect to service " + self.dataURL, '1101', 'error', 'fetchCalenderData',
self.__get_error_data__(), 'getcalendar')
self.messenger.send("Could not connect to service " + self.dataURL, '1101', 'error', 'fetchCalenderData', self.__get_error_data__(), 'getcalendar')
try:
for schedule in self.fetched_schedule_data:
......@@ -284,9 +280,7 @@ class AuraCalendarService(threading.Thread):
print("station_fallback: "+str(schedule["station_fallback"]))
except Exception as e:
import traceback
traceback.print_exc()
print(e)
print("houston we have a problem")
# ------------------------------------------------------------------------------------------ #
......@@ -325,7 +319,8 @@ class AuraCalendarService(threading.Thread):
for entry in schedule_entries["entries"]:
if entry["source"].startswith("file"):
if not os.path.isfile(entry["source"]):
print("WARNING: File ", entry["source"], " does not exist!")
print("WARNING: File", entry["source"], "does not exist!")
# elif entry["source"].startswith("pool"):
fetched_schedule_entries.append(schedule_entries)
......
......@@ -12,10 +12,25 @@ from libraries.database.database import db
class Model:
def store(self, commit=False):
curr_db_sessions = db.session.object_session(self)
curr_db_sessions.add(self)
#db.session.add(self)
# update or insert
if curr_db_sessions is None:
# insert
db.session.add(self)
if commit:
db.session.commit()
else:
# update
curr_db_sessions.add(self)
if commit:
curr_db_sessions.commit()
def delete(self, commit=False):
curr_db_sessions = db.session.object_session(self)
curr_db_sessions.delete(self)
if commit:
self.commit()
curr_db_sessions.commit()
@staticmethod
def commit():
......@@ -56,6 +71,11 @@ class ScheduleEntryModel(Model):
# ------------------------------------------------------------------------------------------ #
@staticmethod
def select_all():
# when deleting all entries, and fetching new programmes, the entries are stored and commited in the code.
# but sqlalchemy thinks somehow it is not commit and returns an empty set
db.session.commit()
# fetching all
all_entries = ScheduleEntry.query.filter().all()
cnt = 0
......
......@@ -9,3 +9,4 @@ class Channels(Enum):
GAP_REPLY = "get_act_programme_reply"
PMQ_REPLY = "print_message_queue_reply"
MPE_REPLY = "move_playlist_entry_reply"
DPE_REPLY = "delete_playlist_entry_reply"
\ No newline at end of file
......@@ -140,6 +140,11 @@ class ServerRedisAdapter(threading.Thread, RedisMessenger):
param = {"from_index": extracted[0], "to_index": extracted[1]}
self.execute(Channels.MPE_REPLY.value, self.scheduler.swap_playlist_entries, param)
elif item["data"].find("delete_playlist_entry") >= 0:
entrynum = item["data"].split()[1]
print("entry to del: ", entrynum)
self.execute(Channels.DPE_REPLY.value, self.scheduler.delete_playlist_entry, entrynum)
else:
raise RedisConnectionException("ServerRedisAdapter Cannot understand command: " + item["data"])
......@@ -153,7 +158,7 @@ class ServerRedisAdapter(threading.Thread, RedisMessenger):
if reply is None:
reply = ""
# sometimes the sender is faster than the receiver and with redis messages would be lost
# sometimes the sender is faster than the receiver. redis messages would be lost
time.sleep(0.1)
print(TerminalColors.OK_ORANGE.value + "replying " + reply + " on channel " + channel + TerminalColors.ENDC.value)
......
......@@ -81,26 +81,23 @@ class AuraController:
response = queue.get()
if response == "fetching_finished":
acs.get_calendar_data()
# acs.get_calendar_data()
self.scheduler.load_programme_from_db()
return self.scheduler.get_act_programme()
return self.scheduler.get_act_programme_as_string()
else:
print("Got an unknown response from AuraCalendarService: "+response)
# ------------------------------------------------------------------------------------------ #
def get_act_programme(self):
try:
programme = self.scheduler.get_act_programme()
programme = self.scheduler.get_act_programme_as_string()
except NoProgrammeLoadedException as e:
if self.debug:
print("WARNING: no programme in memory. i have to reload it!")
# refetch the programme from pv and importer
self.fetch_new_programme()
# is the recursion here really needed, or an additional error source?
return self.get_act_programme()
return self.fetch_new_programme()
except Exception as e:
traceback.print_exc()
......
......@@ -137,7 +137,6 @@ class AuraScheduler():
# ------------------------------------------------------------------------------------------ #
def init_database(self):
import sys
# check if tables do exist. if not create them
try:
ScheduleEntry.select_all()
......@@ -239,7 +238,7 @@ class AuraScheduler():
# when do we have to start?
diff = entry.entry_start_unix - now_unix
diff = diff/100 # testing purpose
# diff = diff/100 # testing purpose
# create the activation threads and run them after <diff> seconds
if entry.source.startswith("linein"):
......@@ -262,6 +261,11 @@ class AuraScheduler():
if not silent:
print(entry.__dict__)
# print("SCHEDULER Len of PROGRAMME")
# print(len(self.programme))
# if len(self.programme) == 0:
# self.programme = None
# ------------------------------------------------------------------------------------------ #
def add_or_update_timer(self, entry, diff, func, type):
# check if something is planned at given time
......@@ -314,8 +318,9 @@ class AuraScheduler():
return False
# ------------------------------------------------------------------------------------------ #
def get_act_programme(self):
def get_act_programme_as_string(self):
programme_as_string = ""
if self.programme is None:
raise NoProgrammeLoadedException("")
......@@ -366,7 +371,24 @@ class AuraScheduler():
to_entry.store(True)
# and return the programme with swapped entries
return self.get_act_programme()
return self.get_act_programme_as_string()
# ------------------------------------------------------------------------------------------ #
def delete_playlist_entry(self, index):
found = False
for p in self.programme:
if p.programme_index == int(index):
p.delete(True)
self.load_programme_from_db()
found = True
break
if not found:
print("WARNING: Nothing to delete")
return self.get_act_programme_as_string()
# ------------------------------------------------------------------------------------------ #
def __load_config__(self):
......
......@@ -116,7 +116,7 @@ class Padavan:
self.print_programme(actprogramme)
def print_programme(self, programme):
idx = 0
# idx = 0
for entry in programme:
self.stringreply += "idx: " + str(entry["programme_index"]) + \
" --- schedule id #" + str(entry["schedule_id"]) + \
......@@ -124,7 +124,7 @@ class Padavan:
" starting @ " + entry["entry_start"] + \
" ending @ " + entry["entry_end"] + \
" is playing " + entry["source"] + "\n"
idx = idx + 1
# idx = idx + 1
def init_player(self):
self.stringreply = self.send_and_wait_redis("aura", "init_player", Channels.IP_REPLY)
......@@ -139,7 +139,9 @@ class Padavan:
self.print_programme(actprogramme)
def delete_playlist_entry(self, index):
return ""
json_reply = self.send_and_wait_redis("aura", "delete_playlist_entry " + str(index), Channels.DPE_REPLY)
actprogramme = simplejson.loads(json_reply)
self.print_programme(actprogramme)
def add_playlist_entry(self, from_index, to_index):
return ""
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment