Merge pull request #1569 from clinton-hall/refactor/configuration

Refactor/configuration
This commit is contained in:
Clinton Hall 2019-03-11 08:20:12 +13:00 committed by GitHub
commit 832ef32340
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 272 additions and 220 deletions

View file

@ -11,9 +11,9 @@ import subprocess
import sys
import time
import eol
import libs.autoload
import libs.util
import eol
if not libs.autoload.completed:
sys.exit('Could not load vendored libraries.')
@ -46,12 +46,22 @@ from six.moves import reload_module
from core import logger, main_db, version_check, databases, transcoder
from core.configuration import config
from core.plugins.downloaders.configuration import (
configure_nzbs,
configure_torrents,
configure_torrent_class,
)
from core.plugins.downloaders.utils import (
pause_torrent,
remove_torrent,
resume_torrent,
)
from core.plugins.plex import configure_plex
from core.utils import (
RunningProcess,
category_search,
clean_dir,
copy_link,
create_torrent_class,
extract_files,
flatten,
get_dirs,
@ -59,13 +69,10 @@ from core.utils import (
list_media_files,
make_dir,
parse_args,
pause_torrent,
rchmod,
remove_dir,
remove_read_only,
remove_torrent,
restart,
resume_torrent,
sanitize_name,
update_download_info_status,
wake_up,
@ -403,26 +410,6 @@ def configure_wake_on_lan():
wake_up()
def configure_sabnzbd():
global SABNZBD_HOST
global SABNZBD_PORT
global SABNZBD_APIKEY
SABNZBD_HOST = CFG['Nzb']['sabnzbd_host']
SABNZBD_PORT = int(CFG['Nzb']['sabnzbd_port'] or 8080) # defaults to accommodate NzbGet
SABNZBD_APIKEY = CFG['Nzb']['sabnzbd_apikey']
def configure_nzbs():
global NZB_CLIENT_AGENT
global NZB_DEFAULT_DIRECTORY
NZB_CLIENT_AGENT = CFG['Nzb']['clientAgent'] # sabnzbd
NZB_DEFAULT_DIRECTORY = CFG['Nzb']['default_downloadDirectory']
configure_sabnzbd()
def configure_groups():
global GROUPS
@ -435,114 +422,6 @@ def configure_groups():
GROUPS = None
def configure_utorrent():
global UTORRENT_WEB_UI
global UTORRENT_USER
global UTORRENT_PASSWORD
UTORRENT_WEB_UI = CFG['Torrent']['uTorrentWEBui'] # http://localhost:8090/gui/
UTORRENT_USER = CFG['Torrent']['uTorrentUSR'] # mysecretusr
UTORRENT_PASSWORD = CFG['Torrent']['uTorrentPWD'] # mysecretpwr
def configure_transmission():
global TRANSMISSION_HOST
global TRANSMISSION_PORT
global TRANSMISSION_USER
global TRANSMISSION_PASSWORD
TRANSMISSION_HOST = CFG['Torrent']['TransmissionHost'] # localhost
TRANSMISSION_PORT = int(CFG['Torrent']['TransmissionPort'])
TRANSMISSION_USER = CFG['Torrent']['TransmissionUSR'] # mysecretusr
TRANSMISSION_PASSWORD = CFG['Torrent']['TransmissionPWD'] # mysecretpwr
def configure_deluge():
global DELUGE_HOST
global DELUGE_PORT
global DELUGE_USER
global DELUGE_PASSWORD
DELUGE_HOST = CFG['Torrent']['DelugeHost'] # localhost
DELUGE_PORT = int(CFG['Torrent']['DelugePort']) # 8084
DELUGE_USER = CFG['Torrent']['DelugeUSR'] # mysecretusr
DELUGE_PASSWORD = CFG['Torrent']['DelugePWD'] # mysecretpwr
def configure_qbittorrent():
global QBITTORRENT_HOST
global QBITTORRENT_PORT
global QBITTORRENT_USER
global QBITTORRENT_PASSWORD
QBITTORRENT_HOST = CFG['Torrent']['qBittorrenHost'] # localhost
QBITTORRENT_PORT = int(CFG['Torrent']['qBittorrentPort']) # 8080
QBITTORRENT_USER = CFG['Torrent']['qBittorrentUSR'] # mysecretusr
QBITTORRENT_PASSWORD = CFG['Torrent']['qBittorrentPWD'] # mysecretpwr
def configure_flattening():
global NOFLATTEN
NOFLATTEN = (CFG['Torrent']['noFlatten'])
if isinstance(NOFLATTEN, str):
NOFLATTEN = NOFLATTEN.split(',')
def configure_torrent_categories():
global CATEGORIES
CATEGORIES = (CFG['Torrent']['categories']) # music,music_videos,pictures,software
if isinstance(CATEGORIES, str):
CATEGORIES = CATEGORIES.split(',')
def configure_torrent_resuming():
global TORRENT_RESUME
global TORRENT_RESUME_ON_FAILURE
TORRENT_RESUME_ON_FAILURE = int(CFG['Torrent']['resumeOnFailure'])
TORRENT_RESUME = int(CFG['Torrent']['resume'])
def configure_torrent_permissions():
global TORRENT_CHMOD_DIRECTORY
TORRENT_CHMOD_DIRECTORY = int(str(CFG['Torrent']['chmodDirectory']), 8)
def configure_torrent_deltetion():
global DELETE_ORIGINAL
DELETE_ORIGINAL = int(CFG['Torrent']['deleteOriginal'])
def configure_torrent_linking():
global USE_LINK
USE_LINK = CFG['Torrent']['useLink'] # no | hard | sym
def configure_torrents():
global TORRENT_CLIENT_AGENT
global OUTPUT_DIRECTORY
global TORRENT_DEFAULT_DIRECTORY
TORRENT_CLIENT_AGENT = CFG['Torrent']['clientAgent'] # utorrent | deluge | transmission | rtorrent | vuze | qbittorrent |other
OUTPUT_DIRECTORY = CFG['Torrent']['outputDirectory'] # /abs/path/to/complete/
TORRENT_DEFAULT_DIRECTORY = CFG['Torrent']['default_downloadDirectory']
configure_torrent_linking()
configure_flattening()
configure_torrent_deltetion()
configure_torrent_categories()
configure_torrent_permissions()
configure_torrent_resuming()
configure_utorrent()
configure_transmission()
configure_deluge()
configure_qbittorrent()
def configure_remote_paths():
global REMOTE_PATHS
@ -565,28 +444,6 @@ def configure_remote_paths():
]
def configure_plex():
global PLEX_SSL
global PLEX_HOST
global PLEX_PORT
global PLEX_TOKEN
global PLEX_SECTION
PLEX_SSL = int(CFG['Plex']['plex_ssl'])
PLEX_HOST = CFG['Plex']['plex_host']
PLEX_PORT = CFG['Plex']['plex_port']
PLEX_TOKEN = CFG['Plex']['plex_token']
PLEX_SECTION = CFG['Plex']['plex_sections'] or []
if PLEX_SECTION:
if isinstance(PLEX_SECTION, list):
PLEX_SECTION = ','.join(PLEX_SECTION) # fix in case this imported as list.
PLEX_SECTION = [
tuple(item.split(','))
for item in PLEX_SECTION.split('|')
]
def configure_niceness():
global NICENESS
@ -973,13 +830,6 @@ def configure_passwords_file():
PASSWORDS_FILE = CFG['passwords']['PassWordFile']
def configure_torrent_class():
global TORRENT_CLASS
# create torrent class
TORRENT_CLASS = create_torrent_class(TORRENT_CLIENT_AGENT)
def configure_sections(section):
global SECTIONS
global CATEGORIES
@ -1138,10 +988,10 @@ def initialize(section=None):
configure_general()
configure_updates()
configure_wake_on_lan()
configure_nzbs()
configure_torrents()
configure_nzbs(CFG)
configure_torrents(CFG)
configure_remote_paths()
configure_plex()
configure_plex(CFG)
configure_niceness()
configure_containers()
configure_transcoder()

View file

@ -8,9 +8,23 @@ import requests
import core
from core import logger, transcoder
from core.auto_process.common import command_complete, completed_download_handling, ProcessResult
from core.auto_process.common import (
ProcessResult,
command_complete,
completed_download_handling,
)
from core.plugins.downloaders.nzb.utils import report_nzb
from core.plugins.subtitles import import_subs
from core.scene_exceptions import process_all_exceptions
from core.utils import convert_to_ascii, find_download, find_imdbid, import_subs, list_media_files, remote_dir, remove_dir, report_nzb, server_responding
from core.utils import (
convert_to_ascii,
find_download,
find_imdbid,
list_media_files,
remote_dir,
remove_dir,
server_responding,
)
requests.packages.urllib3.disable_warnings()

View file

@ -10,10 +10,23 @@ import requests
import core
from core import logger, transcoder
from core.auto_process.common import command_complete, completed_download_handling, ProcessResult
from core.auto_process.common import (
ProcessResult,
command_complete,
completed_download_handling,
)
from core.forks import auto_fork
from core.plugins.downloaders.nzb.utils import report_nzb
from core.plugins.subtitles import import_subs
from core.scene_exceptions import process_all_exceptions
from core.utils import convert_to_ascii, flatten, import_subs, list_media_files, remote_dir, remove_dir, report_nzb, server_responding
from core.utils import (
convert_to_ascii,
flatten,
list_media_files,
remote_dir,
remove_dir,
server_responding,
)
requests.packages.urllib3.disable_warnings()

0
core/plugins/__init__.py Normal file
View file

View file

View file

@ -0,0 +1,5 @@
from core.plugins.downloaders.nzb.configuration import configure_nzbs
from core.plugins.downloaders.torrent.configuration import (
configure_torrents,
configure_torrent_class,
)

View file

View file

@ -0,0 +1,15 @@
import core
def configure_nzbs(config):
nzb_config = config['Nzb']
core.NZB_CLIENT_AGENT = nzb_config['clientAgent'] # sabnzbd
core.NZB_DEFAULT_DIRECTORY = nzb_config['default_downloadDirectory']
configure_sabnzbd(nzb_config)
def configure_sabnzbd(config):
core.SABNZBD_HOST = config['sabnzbd_host']
core.SABNZBD_PORT = int(config['sabnzbd_port'] or 8080) # defaults to accommodate NzbGet
core.SABNZBD_APIKEY = config['sabnzbd_apikey']

View file

@ -0,0 +1,81 @@
import core
from core.plugins.downloaders.torrent.utils import create_torrent_class
def configure_torrents(config):
torrent_config = config['Torrent']
core.TORRENT_CLIENT_AGENT = torrent_config['clientAgent'] # utorrent | deluge | transmission | rtorrent | vuze | qbittorrent |other
core.OUTPUT_DIRECTORY = torrent_config['outputDirectory'] # /abs/path/to/complete/
core.TORRENT_DEFAULT_DIRECTORY = torrent_config['default_downloadDirectory']
configure_torrent_linking(torrent_config)
configure_flattening(torrent_config)
configure_torrent_deletion(torrent_config)
configure_torrent_categories(torrent_config)
configure_torrent_permissions(torrent_config)
configure_torrent_resuming(torrent_config)
configure_utorrent(torrent_config)
configure_transmission(torrent_config)
configure_deluge(torrent_config)
configure_qbittorrent(torrent_config)
def configure_torrent_linking(config):
core.USE_LINK = config['useLink'] # no | hard | sym
def configure_flattening(config):
core.NOFLATTEN = (config['noFlatten'])
if isinstance(core.NOFLATTEN, str):
core.NOFLATTEN = core.NOFLATTEN.split(',')
def configure_torrent_categories(config):
core.CATEGORIES = (config['categories']) # music,music_videos,pictures,software
if isinstance(core.CATEGORIES, str):
core.CATEGORIES = core.CATEGORIES.split(',')
def configure_torrent_resuming(config):
core.TORRENT_RESUME_ON_FAILURE = int(config['resumeOnFailure'])
core.TORRENT_RESUME = int(config['resume'])
def configure_torrent_permissions(config):
core.TORRENT_CHMOD_DIRECTORY = int(str(config['chmodDirectory']), 8)
def configure_torrent_deletion(config):
core.DELETE_ORIGINAL = int(config['deleteOriginal'])
def configure_utorrent(config):
core.UTORRENT_WEB_UI = config['uTorrentWEBui'] # http://localhost:8090/gui/
core.UTORRENT_USER = config['uTorrentUSR'] # mysecretusr
core.UTORRENT_PASSWORD = config['uTorrentPWD'] # mysecretpwr
def configure_transmission(config):
core.TRANSMISSION_HOST = config['TransmissionHost'] # localhost
core.TRANSMISSION_PORT = int(config['TransmissionPort'])
core.TRANSMISSION_USER = config['TransmissionUSR'] # mysecretusr
core.TRANSMISSION_PASSWORD = config['TransmissionPWD'] # mysecretpwr
def configure_deluge(config):
core.DELUGE_HOST = config['DelugeHost'] # localhost
core.DELUGE_PORT = int(config['DelugePort']) # 8084
core.DELUGE_USER = config['DelugeUSR'] # mysecretusr
core.DELUGE_PASSWORD = config['DelugePWD'] # mysecretpwr
def configure_qbittorrent(config):
core.QBITTORRENT_HOST = config['qBittorrenHost'] # localhost
core.QBITTORRENT_PORT = int(config['qBittorrentPort']) # 8080
core.QBITTORRENT_USER = config['qBittorrentUSR'] # mysecretusr
core.QBITTORRENT_PASSWORD = config['qBittorrentPWD'] # mysecretpwr
def configure_torrent_class():
# create torrent class
core.TORRENT_CLASS = create_torrent_class(core.TORRENT_CLIENT_AGENT)

View file

@ -0,0 +1,21 @@
from synchronousdeluge.client import DelugeClient
import core
from core import logger
def configure_client():
agent = 'deluge'
host = core.DELUGE_HOST
port = core.DELUGE_PORT
user = core.DELUGE_USER
password = core.DELUGE_PASSWORD
logger.debug('Connecting to {0}: http://{1}:{2}'.format(agent, host, port))
client = DelugeClient()
try:
client.connect(host, port, user, password)
except Exception:
logger.error('Failed to connect to Deluge')
else:
return client

View file

@ -0,0 +1,23 @@
from qbittorrent import Client as qBittorrentClient
import core
from core import logger
def configure_client():
agent = 'qbittorrent'
host = core.QBITTORRENT_HOST
port = core.QBITTORRENT_PORT
user = core.QBITTORRENT_USER
password = core.QBITTORRENT_PASSWORD
logger.debug(
'Connecting to {0}: http://{1}:{2}'.format(agent, host, port)
)
client = qBittorrentClient('http://{0}:{1}/'.format(host, port))
try:
client.login(user, password)
except Exception:
logger.error('Failed to connect to qBittorrent')
else:
return client

View file

@ -0,0 +1,20 @@
from transmissionrpc.client import Client as TransmissionClient
import core
from core import logger
def configure_client():
agent = 'transmission'
host = core.TRANSMISSION_HOST
port = core.TRANSMISSION_PORT
user = core.TRANSMISSION_USER
password = core.TRANSMISSION_PASSWORD
logger.debug('Connecting to {0}: http://{1}:{2}'.format(agent, host, port))
try:
client = TransmissionClient(host, port, user, password)
except Exception:
logger.error('Failed to connect to Transmission')
else:
return client

View file

@ -1,55 +1,28 @@
import time
from qbittorrent import Client as qBittorrentClient
from synchronousdeluge.client import DelugeClient
from transmissionrpc.client import Client as TransmissionClient
from utorrent.client import UTorrentClient
import core
from core import logger
from .deluge import configure_client as deluge_client
from .qbittorrent import configure_client as qbittorrent_client
from .transmission import configure_client as transmission_client
from .utorrent import configure_client as utorrent_client
torrent_clients = {
'deluge': deluge_client,
'qbittorrent': qbittorrent_client,
'transmission': transmission_client,
'utorrent': utorrent_client,
}
def create_torrent_class(client_agent):
# Hardlink solution for Torrents
tc = None
if not core.APP_NAME == 'TorrentToMedia.py': #Skip loading Torrent for NZBs.
return tc
if not core.APP_NAME == 'TorrentToMedia.py':
return # Skip loading Torrent for NZBs.
if client_agent == 'utorrent':
try:
logger.debug('Connecting to {0}: {1}'.format(client_agent, core.UTORRENT_WEB_UI))
tc = UTorrentClient(core.UTORRENT_WEB_UI, core.UTORRENT_USER, core.UTORRENT_PASSWORD)
except Exception:
logger.error('Failed to connect to uTorrent')
if client_agent == 'transmission':
try:
logger.debug('Connecting to {0}: http://{1}:{2}'.format(
client_agent, core.TRANSMISSION_HOST, core.TRANSMISSION_PORT))
tc = TransmissionClient(core.TRANSMISSION_HOST, core.TRANSMISSION_PORT,
core.TRANSMISSION_USER,
core.TRANSMISSION_PASSWORD)
except Exception:
logger.error('Failed to connect to Transmission')
if client_agent == 'deluge':
try:
logger.debug('Connecting to {0}: http://{1}:{2}'.format(client_agent, core.DELUGE_HOST, core.DELUGE_PORT))
tc = DelugeClient()
tc.connect(host=core.DELUGE_HOST, port=core.DELUGE_PORT, username=core.DELUGE_USER,
password=core.DELUGE_PASSWORD)
except Exception:
logger.error('Failed to connect to Deluge')
if client_agent == 'qbittorrent':
try:
logger.debug('Connecting to {0}: http://{1}:{2}'.format(client_agent, core.QBITTORRENT_HOST, core.QBITTORRENT_PORT))
tc = qBittorrentClient('http://{0}:{1}/'.format(core.QBITTORRENT_HOST, core.QBITTORRENT_PORT))
tc.login(core.QBITTORRENT_USER, core.QBITTORRENT_PASSWORD)
except Exception:
logger.error('Failed to connect to qBittorrent')
return tc
client = torrent_clients.get(client_agent)
if client:
return client()
def pause_torrent(client_agent, input_hash, input_id, input_name):

View file

@ -0,0 +1,19 @@
from utorrent.client import UTorrentClient
import core
from core import logger
def configure_client():
agent = 'utorrent'
web_ui = core.UTORRENT_WEB_UI
user = core.UTORRENT_USER
password = core.UTORRENT_PASSWORD
logger.debug('Connecting to {0}: {1}'.format(agent, web_ui))
try:
client = UTorrentClient(web_ui, user, password)
except Exception:
logger.error('Failed to connect to uTorrent')
else:
return client

View file

@ -0,0 +1,5 @@
from core.plugins.downloaders.torrent.utils import (
pause_torrent,
remove_torrent,
resume_torrent,
)

View file

@ -4,6 +4,24 @@ import core
from core import logger
def configure_plex(config):
core.PLEX_SSL = int(config['Plex']['plex_ssl'])
core.PLEX_HOST = config['Plex']['plex_host']
core.PLEX_PORT = config['Plex']['plex_port']
core.PLEX_TOKEN = config['Plex']['plex_token']
plex_section = config['Plex']['plex_sections'] or []
if plex_section:
if isinstance(plex_section, list):
plex_section = ','.join(plex_section) # fix in case this imported as list.
plex_section = [
tuple(item.split(','))
for item in plex_section.split('|')
]
core.PLEX_SECTION = plex_section
def plex_update(category):
if core.FAILED:
return
@ -26,5 +44,3 @@ def plex_update(category):
logger.debug('Plex Library has been refreshed.', 'PLEX')
else:
logger.debug('Could not identify section for plex update', 'PLEX')

View file

@ -5,7 +5,8 @@ from subprocess import Popen
import core
from core import logger, transcoder
from core.utils import import_subs, list_media_files, remove_dir
from core.plugins.subtitles import import_subs
from core.utils import list_media_files, remove_dir
def external_script(output_destination, torrent_name, torrent_label, settings):

View file

@ -19,8 +19,6 @@ from core.utils.identification import category_search, find_imdbid
from core.utils.links import copy_link, replace_links
from core.utils.naming import clean_file_name, is_sample, sanitize_name
from core.utils.network import find_download, server_responding, test_connection, wake_on_lan, wake_up
from core.utils.notifications import plex_update
from core.utils.nzbs import get_nzoid, report_nzb
from core.utils.parsers import (
parse_args,
parse_deluge,
@ -44,8 +42,6 @@ from core.utils.paths import (
remove_read_only,
)
from core.utils.processes import RunningProcess, restart
from core.utils.subtitles import import_subs
from core.utils.torrents import create_torrent_class, pause_torrent, remove_torrent, resume_torrent
requests.packages.urllib3.disable_warnings()
shutil_custom.monkey_patch()