def create_config(fp=None): """Create a config. Args: fp(str): Where to create the config. If omitted it will be written to the root. Returns: None """ if fp is None: fp = INI_FILE read_or_make(fp)
def cli(debug, username, password, servername, url, token, config, verify_ssl, default_folder): """ Entry point for the CLI.""" global PMS global CONFIG # Remember to update the subcommands in __init__ if sub commands are added. # Default folder is handled in fake_main as we need to modify # the variables before import plex.py, its just listed here for the help # message etc. if config and os.path.isfile(config): CONFIG = read_or_make(config) url = url or CONFIG['server'].get('url') token = token or CONFIG['server'].get('token') verify_ssl = verify_ssl or CONFIG['server'].get('verify_ssl') if url and token or username and password: PMS = get_pms(url=url, token=token, username=username, password=password, servername=servername, verify_ssl=verify_ssl)
def cli(debug, username, password, servername, url, token, config, verify_ssl): """ Entry point for the CLI.""" global PMS global CONFIG if debug: LOG.setLevel(logging.DEBUG) else: LOG.setLevel(logging.INFO) if config and os.path.isfile(config): CONFIG = read_or_make(config) url = url or CONFIG.get('url') token = token or CONFIG.get('token') verify_ssl = verify_ssl or CONFIG.get('verify_ssl') if url and token or username and password: PMS = get_pms(url=url, token=token, username=username, password=password, servername=servername, verify_ssl=verify_ssl)
def create_config(fp=None): """Create a config file. Args: fp(str): Where to create the config file. If omitted it will be written to the default location Returns: filepath to config.ini """ if fp is None: fp = INI_FILE conf_file = read_or_make(fp).filename click.echo('Wrote configfile to %s' % conf_file) return conf_file
def init(folder=None, debug=False, config=None): global DEFAULT_FOLDER, THEMES, TEMP_THEMES, LOG_FILE, INI_FILE, INI_FILE, DB_PATH, CONFIG, FP_HASHES, POOL DEFAULT_FOLDER = folder or os.environ.get('bw_plex_default_folder') or os.path.expanduser('~/.config/bw_plex') if os.path.isdir(DEFAULT_FOLDER) and not os.access(DEFAULT_FOLDER, os.W_OK): print('You default folder is not writeable') sys.exit() THEMES = os.path.join(DEFAULT_FOLDER, 'themes') TEMP_THEMES = os.path.join(DEFAULT_FOLDER, 'temp_themes') FP_HASHES = os.path.join(DEFAULT_FOLDER, 'hashes.pklz') LOG_FILE = os.path.join(DEFAULT_FOLDER, 'log.txt') INI_FILE = config or os.path.join(DEFAULT_FOLDER, 'config.ini') DB_PATH = os.path.join(DEFAULT_FOLDER, 'media.db') os.makedirs(DEFAULT_FOLDER, exist_ok=True) os.makedirs(THEMES, exist_ok=True) os.makedirs(TEMP_THEMES, exist_ok=True) from bw_plex.config import read_or_make CONFIG = read_or_make(INI_FILE) POOL = Pool(int(CONFIG.get('thread_pool_number', 10))) from bw_plex.db import db_init db_init() handle = logging.NullHandler() frmt = logging.Formatter(CONFIG.get('logformat', '%(asctime)s :: %(name)s :: %(levelname)s :: %(filename)s:%(lineno)d :: %(message)s')) handle.setFormatter(frmt) LOG.addHandler(handle) stream_handle = logging.StreamHandler() stream_handle.setFormatter(frmt) LOG.addHandler(stream_handle) handle.setFormatter(frmt) LOG.addHandler(handle) # FILE rfh = RotatingFileHandler(LOG_FILE, 'a', 512000, 3) rfh.setFormatter(frmt) LOG.addHandler(rfh) log_to_lvl = {'debug': logging.DEBUG, 'info': logging.INFO, 'warning': logging.WARNING} if debug is True: LOG.setLevel(logging.DEBUG) else: try: lvl = log_to_lvl[CONFIG['general']['loglevel']] LOG.setLevel(lvl) except KeyError: LOG.error('Invalid option for loglevel in fonfig file, defualting to level to debug') LOG.setLevel(logging.DEBUG) # This import is slow import pkg_resources LOG.info('Using bw_plex version %s', pkg_resources.get_distribution("bw_plex").version) LOG.info('default folder set to %s', DEFAULT_FOLDER) FILTER.add_secret(CONFIG['server']['token']) FILTER.add_secret(CONFIG['server']['password']) secret_args = arg_extract(keys=['username', 'token', 'password']).values() for arg in secret_args: FILTER.add_secret(arg) if debug is None: if CONFIG['general']['debug'] is False: LOG.addFilter(FILTER) elif debug is False: LOG.addFilter(FILTER) else: LOG.info('Log is not sanitized!') packages = ['plexapi', 'requests', 'urllib3'] for pack in packages: _pack = logging.getLogger(pack) _pack.setLevel(logging.DEBUG) _pack.addHandler(rfh) _pack.addHandler(stream_handle)
def test_config(): conf = read_or_make(os.path.join(TEST_DATA, 'test_config.ini')) assert 'level' not in conf['general'] assert conf['general']['loglevel'] == 'info'
def init(folder=None, debug=False, config=None): global DEFAULT_FOLDER, THEMES, TEMP_THEMES, LOG_FILE, INI_FILE, INI_FILE, DB_PATH, CONFIG, FP_HASHES, POOL DEFAULT_FOLDER = folder or os.environ.get( 'bw_plex_default_folder') or os.path.expanduser('~/.config/bw_plex') if os.path.isdir(DEFAULT_FOLDER) and not os.access(DEFAULT_FOLDER, os.W_OK): print('You default folder is not writeable') sys.exit() THEMES = os.path.join(DEFAULT_FOLDER, 'themes') TEMP_THEMES = os.path.join(DEFAULT_FOLDER, 'temp_themes') FP_HASHES = os.path.join(DEFAULT_FOLDER, 'hashes.pklz') LOG_FILE = os.path.join(DEFAULT_FOLDER, 'log.txt') INI_FILE = config or os.path.join(DEFAULT_FOLDER, 'config.ini') DB_PATH = os.path.join(DEFAULT_FOLDER, 'media.db') makedirs(DEFAULT_FOLDER, exist_ok=True) makedirs(THEMES, exist_ok=True) makedirs(TEMP_THEMES, exist_ok=True) from bw_plex.config import read_or_make CONFIG = read_or_make(INI_FILE) POOL = Pool(int(CONFIG.get('thread_pool_number', 10))) from bw_plex.db import db_init db_init() # Setup some logging. if debug or CONFIG['general']['level'] == 'debug': LOG.setLevel(logging.DEBUG) else: LOG.setLevel(logging.INFO) handle = logging.NullHandler() frmt = logging.Formatter( CONFIG.get( 'logformat', '%(asctime)s :: %(name)s :: %(levelname)s :: %(filename)s:%(lineno)d :: %(message)s' )) handle.setFormatter(frmt) LOG.addHandler(handle) stream_handle = logging.StreamHandler() stream_handle.setFormatter(frmt) LOG.addHandler(stream_handle) handle.setFormatter(frmt) LOG.addHandler(handle) # FILE rfh = RotatingFileHandler(LOG_FILE, 'a', 512000, 3) rfh.setFormatter(frmt) LOG.addHandler(rfh) LOG.info('default folder set to %s', DEFAULT_FOLDER) FILTER.add_secret(CONFIG['server']['token']) FILTER.add_secret(CONFIG['server']['password']) secret_args = arg_extract(keys=['username', 'token', 'password']).values() for arg in secret_args: FILTER.add_secret(arg) if not CONFIG['general']['debug'] and debug is False: LOG.addFilter(FILTER) else: LOG.info('Log is not sanitized!') packages = ['plexapi', 'requests', 'urllib3'] for pack in packages: _pack = logging.getLogger(pack) _pack.setLevel(logging.DEBUG) _pack.addHandler(rfh) _pack.addHandler(stream_handle)