2018-09-06 08:36:32 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2019-02-02 17:21:22 +00:00
|
|
|
from client import JellyfinClient
|
2019-11-01 00:43:20 +00:00
|
|
|
from helper import has_attribute
|
2018-09-06 08:36:32 +00:00
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
2019-10-03 02:14:54 +00:00
|
|
|
|
2018-09-06 08:36:32 +00:00
|
|
|
class NullHandler(logging.Handler):
|
|
|
|
def emit(self, record):
|
|
|
|
print(self.format(record))
|
|
|
|
|
2019-10-03 02:14:54 +00:00
|
|
|
|
2018-09-06 08:36:32 +00:00
|
|
|
loghandler = NullHandler
|
2019-02-02 13:10:33 +00:00
|
|
|
LOG = logging.getLogger('Jellyfin')
|
2018-09-06 08:36:32 +00:00
|
|
|
|
|
|
|
#################################################################################################
|
|
|
|
|
2019-10-03 02:14:54 +00:00
|
|
|
|
2018-09-06 08:36:32 +00:00
|
|
|
def config(level=logging.INFO):
|
|
|
|
|
2019-02-02 13:10:33 +00:00
|
|
|
logger = logging.getLogger('Jellyfin')
|
2019-02-02 17:21:22 +00:00
|
|
|
logger.addHandler(Jellyfin.loghandler())
|
2018-09-06 08:36:32 +00:00
|
|
|
logger.setLevel(level)
|
|
|
|
|
2019-10-03 02:14:54 +00:00
|
|
|
|
2018-09-06 08:36:32 +00:00
|
|
|
def ensure_client():
|
|
|
|
|
|
|
|
def decorator(func):
|
|
|
|
def wrapper(self, *args, **kwargs):
|
|
|
|
|
|
|
|
if self.client.get(self.server_id) is None:
|
|
|
|
self.construct()
|
|
|
|
|
|
|
|
return func(self, *args, **kwargs)
|
|
|
|
|
|
|
|
return wrapper
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2019-02-02 17:21:22 +00:00
|
|
|
class Jellyfin(object):
|
2018-09-06 08:36:32 +00:00
|
|
|
|
2019-02-02 13:10:33 +00:00
|
|
|
''' This is your Jellyfinclient, you can create more than one. The server_id is only a temporary thing
|
|
|
|
to communicate with the JellyfinClient().
|
2019-01-25 13:02:27 +00:00
|
|
|
|
2019-02-02 13:10:33 +00:00
|
|
|
from jellyfin import Jellyfin
|
2018-09-06 08:36:32 +00:00
|
|
|
|
2019-09-09 00:20:58 +00:00
|
|
|
Jellyfin('123456').config.data['app']
|
2019-01-25 13:02:27 +00:00
|
|
|
|
|
|
|
# Permanent client reference
|
2019-02-02 13:10:33 +00:00
|
|
|
client = Jellyfin('123456').get_client()
|
2019-09-09 00:20:58 +00:00
|
|
|
client.config.data['app']
|
2018-09-06 08:36:32 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
# Borg - multiple instances, shared state
|
|
|
|
_shared_state = {}
|
|
|
|
client = {}
|
|
|
|
server_id = "default"
|
|
|
|
loghandler = loghandler
|
|
|
|
|
|
|
|
def __init__(self, server_id=None):
|
|
|
|
self.__dict__ = self._shared_state
|
|
|
|
self.server_id = server_id or "default"
|
|
|
|
|
2019-01-25 13:02:27 +00:00
|
|
|
def get_client(self):
|
|
|
|
return self.client[self.server_id]
|
|
|
|
|
2018-09-06 08:36:32 +00:00
|
|
|
@classmethod
|
|
|
|
def set_loghandler(cls, func=loghandler, level=logging.INFO):
|
|
|
|
|
2019-02-02 13:10:33 +00:00
|
|
|
for handler in logging.getLogger('Jellyfin').handlers:
|
2018-09-06 08:36:32 +00:00
|
|
|
if isinstance(handler, cls.loghandler):
|
2019-02-02 13:10:33 +00:00
|
|
|
logging.getLogger('Jellyfin').removeHandler(handler)
|
2018-09-06 08:36:32 +00:00
|
|
|
|
|
|
|
cls.loghandler = func
|
|
|
|
config(level)
|
|
|
|
|
|
|
|
def close(self):
|
|
|
|
|
|
|
|
if self.server_id not in self.client:
|
|
|
|
return
|
|
|
|
|
|
|
|
self.client[self.server_id].stop()
|
|
|
|
self.client.pop(self.server_id, None)
|
|
|
|
|
2019-02-02 13:10:33 +00:00
|
|
|
LOG.info("---[ STOPPED JELLYFINCLIENT: %s ]---", self.server_id)
|
2018-09-06 08:36:32 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def close_all(cls):
|
|
|
|
|
|
|
|
for client in cls.client:
|
|
|
|
cls.client[client].stop()
|
|
|
|
|
|
|
|
cls.client = {}
|
2019-02-02 17:21:22 +00:00
|
|
|
LOG.info("---[ STOPPED ALL JELLYFINCLIENTS ]---")
|
2018-09-06 08:36:32 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_active_clients(cls):
|
|
|
|
return cls.client
|
|
|
|
|
|
|
|
@ensure_client()
|
|
|
|
def __setattr__(self, name, value):
|
|
|
|
|
|
|
|
if has_attribute(self, name):
|
2019-02-02 17:21:22 +00:00
|
|
|
return super(Jellyfin, self).__setattr__(name, value)
|
2018-09-06 08:36:32 +00:00
|
|
|
|
|
|
|
setattr(self.client[self.server_id], name, value)
|
|
|
|
|
|
|
|
@ensure_client()
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return getattr(self.client[self.server_id], name)
|
2019-10-03 02:14:54 +00:00
|
|
|
|
2018-09-06 08:36:32 +00:00
|
|
|
def construct(self):
|
|
|
|
|
2019-02-02 17:21:22 +00:00
|
|
|
self.client[self.server_id] = JellyfinClient()
|
2018-09-06 08:36:32 +00:00
|
|
|
|
|
|
|
if self.server_id == 'default':
|
2019-02-02 13:10:33 +00:00
|
|
|
LOG.info("---[ START JELLYFINCLIENT ]---")
|
2018-09-06 08:36:32 +00:00
|
|
|
else:
|
2019-02-02 13:10:33 +00:00
|
|
|
LOG.info("---[ START JELLYFINCLIENT: %s ]---", self.server_id)
|
2018-09-06 08:36:32 +00:00
|
|
|
|
2019-07-09 20:05:28 +00:00
|
|
|
|
|
|
|
config()
|