def _connect(self): self.connected = False self.create_socket(socket.AF_INET, socket.SOCK_STREAM) try: self.connect((self.host, self.port)) except socket.gaierror as excp: log.error("%s failed to connect: %s", self.name, excp) raise # Send our name to the server to register the connection with. # try: while True: if self.qthread: sent = self.send(CommunicationQThread.packer.pack(self.name)) else: sent = self.send(CommunicationThread.packer.pack(self.name)) if sent != 0: break self.connected = True log.warn("%s sent name" % self.name) except socket.error as excp: log.error("%s failed to send name: %s", self.name, excp)
def create_database(self): log.warn("Creating database") self._open_connection() self.cursor.execute("""CREATE TABLE media (id INTEGER PRIMARY KEY AUTOINCREMENT, category TEXT, paths TEXT, name_one TEXT, name_two TEXT, name_three TEXT, name_four TEXT, year INTEGER, extension TEXT, modified INTEGER) """) self.cursor.execute("""CREATE TABLE viewed (id TEXT, viewed INTEGER, elapsed INTEGER) """) self._close_connection() log.warn("Created database")
def play(self, item=None): """ Play the next media item in the queue. If a new item is passed, add it to the queue and then play it. """ if self.media_id: self.stop() if item: self._add_to_queue(item) if self._queue: item = self._queue.pop(0) else: return self.media_id = item[0] self.media_name = item[1] elapsed = item[2] path = item[3] log.warn("About to play: %s", path) self._insert_viewed(self.media_id) self.player.set_media(self.instance.media_new(path)) self.start() self._play.emit() if elapsed: self.jump_to(elapsed) self.overlay(self.media_name)
def handle_accept(self): client, address = self.accept() log.warn("Received connection from: %s", address[0]) # Pass this client off to a handler. CommunicationClientHandler(client)
def handle_close(self): self.close() self.connected = False log.info("CommunicationClient closed") time.sleep(5) log.warn("Attempting to reconnect to server") self._connect()
def delete_media(self, media_ids): with self.database: for media_id in media_ids: log.warn("Deleting media: %s", media_id) self.database.delete_media_by_id(media_id) if media_ids: self.clear_cache()
def _set_volume(self, volume): log.warn("Setting volume to %s", volume) if volume > self.volume_max: volume = self.volume_max if volume < self.volume_min: volume = self.volume_min self.player.audio_set_volume(volume)
def handle_close(self): self.close() try: Communicate.connections.pop(self.name, None) except TypeError as excp: log.error("Failed to remove connection: %s", excp) log.warn("%s disconnected", self.name)
def action(self, message): """ Try to perform the received playback action on the Control object. """ try: function, arguments = message getattr(self.control, function)(*arguments) log.warn("Performed action: %s", function) except Exception as excp: log.error("Action %s failed: %s", function, excp) log.error(traceback.format_exc())
def insert_media(self, media): if isinstance(media, dict): media = [media] inserted = False with self.database: for data in media: check = self.database.select_media_by_paths(data) if not check: log.warn("Inserting new media: %s", data) self.database.insert_media(data) inserted = True if inserted: self.clear_cache()
def send(self, *args): """ Send a message in the appropriate direction. """ if self.client: result = self.thread._send_client(args[0]) # If the message fails to send, restart the client connection. # if not result: log.warn("Relaunching communication thread") self._start() return result else: return self.thread._send_server(args[0], args[1])
def __init__(self, proxy, host, port): asyncore.dispatcher.__init__(self) log.info("CommunicationServer initialised") CommunicationServer.proxy = proxy() self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.set_reuse_addr() try: self.bind((host, port)) except socket.error as excp: log.error("Failed to bind socket: %s", excp) raise log.warn("Listening on port: %s", port) self.listen(5)
def index(self): log.warn("About to index media") self.stop = True media = [] for category, value in self.naming.items(): path = value.get("path") names = value.get("names") expressions = [] for name in names: expressions.append(self._format_expression(name)) media.extend(self.find_media(category, path, expressions)) self.insert_new_media(media) self.delete_missing_media() self.now = False self.stop = False log.warn("Finished indexing media")
def clear_cache(cls, category=None, media_id=None): if media_id: if not category: return log.warn("Clearing cache for media ID: %s", media_id) for category in cls._cache.keys(): if cls._cache[category].get(media_id): del cls._cache[category][media_id] elif category: log.warn("Clearing cache for category: %s", category) del cls._cache[category] else: log.warn("Clearing cache for all categories") cls._cache = {}
def update_viewed(self, media_id, elapsed): log.warn("Updating elapsed for %s: %s", media_id, elapsed) with self.database: self.database.update_viewed(media_id, elapsed)
def delete_viewed(self, media_id): log.warn("Deleting viewed: %s", media_id) with self.database: self.database.delete_viewed(media_id)
interface.initialise() control._send_update() application.exec_() #------------------------------------------------------------------------------ def parse_arguments(): parser = argparse.ArgumentParser() parser.add_argument("-n", "--name", action="store", required=True) parser.add_argument("-m", "--media", action="store", type=unicode, required=True) parser.add_argument("-d", "--downloads", action="store", type=unicode) return parser.parse_args() #------------------------------------------------------------------------------ if __name__ == "__main__": try: log.warn("Snake initialised") main(parse_arguments()) finally: log.warn("Snake exited")
def insert_viewed(self, media_id): log.warn("Inserting viewed media: %s", media_id) with self.database: self.database.insert_viewed(media_id)
application.exec_() #------------------------------------------------------------------------------ def parse_arguments(): parser = argparse.ArgumentParser() parser.add_argument("-n", "--name", action="store", required=True) parser.add_argument("-m", "--media", action="store", type=unicode, required=True) parser.add_argument("-d", "--downloads", action="store", type=unicode) return parser.parse_args() #------------------------------------------------------------------------------ if __name__ == "__main__": try: log.warn("Snake initialised") main(parse_arguments()) finally: log.warn("Snake exited")
Index() Communicate(proxy=Proxy) #------------------------------------------------------------------------------ app = flask.Flask(__name__, template_folder="%s/templates" % PATH, static_folder="%s/static" % PATH, static_url_path="/%s/static" % URL_BASE) app.register_blueprint(api, url_prefix="/%s/%s" % (URL_BASE, API_BASE)) app.register_blueprint(pages, url_prefix="/%s" % URL_BASE) #------------------------------------------------------------------------------ @app.route("/") def root(): return flask.redirect("/%s" % URL_BASE) #------------------------------------------------------------------------------ if __name__ == "__main__": try: log.warn("Head initialised") app.run(host="0.0.0.0", port=config.getint("ports", "head")) finally: log.warn("Head exited")