Skip to content

Commit

Permalink
refactored how the config.py data is accessed
Browse files Browse the repository at this point in the history
  • Loading branch information
shitwolfymakes authored and wolfy committed Mar 28, 2022
1 parent 9817a71 commit e715f85
Show file tree
Hide file tree
Showing 13 changed files with 197 additions and 216 deletions.
10 changes: 6 additions & 4 deletions arm/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import os
import yaml

yamlfile = os.path.join("/etc/arm/config", "arm.yaml")
abcde_config_path = os.path.join("/etc/arm/config", ".abcde.yaml")
apprise_config_path = os.path.join("/etc/arm/config", "apprise.yaml")
arm_config_path = os.path.join("/etc/arm/config", "arm.yaml")

with open(yamlfile, "r") as f:
with open(arm_config_path, "r") as f:
try:
cfg = yaml.load(f, Loader=yaml.FullLoader)
arm_config = yaml.load(f, Loader=yaml.FullLoader)
except Exception:
cfg = yaml.safe_load(f) # For older versions use this
arm_config = yaml.safe_load(f) # For older versions use this
8 changes: 4 additions & 4 deletions arm/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import psutil
import logging
import time
import arm.config.config as cfg

from arm.ripper import music_brainz
from arm.ui import db
from arm.config.config import cfg
from flask_login import LoginManager, current_user, login_user, UserMixin # noqa: F401
from prettytable import PrettyTable

Expand Down Expand Up @@ -64,8 +64,8 @@ def __init__(self, devpath):
self.video_type = "unknown"
self.ejected = False
self.updated = False
if cfg['VIDEOTYPE'] != "auto":
self.video_type = cfg['VIDEOTYPE']
if cfg.arm_config['VIDEOTYPE'] != "auto":
self.video_type = cfg.arm_config['VIDEOTYPE']
self.parse_udev()
self.get_pid()

Expand Down Expand Up @@ -143,7 +143,7 @@ def identify_audio_cd(self):
logfile = f"{mb_title}.log"
new_log_file = f"{mb_title}_{round(time.time() * 100)}.log"

temp_log_full = os.path.join(cfg['LOGPATH'], logfile)
temp_log_full = os.path.join(cfg.arm_config['LOGPATH'], logfile)
logfile = new_log_file if os.path.isfile(temp_log_full) else logfile
return logfile

Expand Down
54 changes: 27 additions & 27 deletions arm/ripper/handbrake.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import time # noqa: F401
import datetime # noqa: F401
import psutil # noqa: F401
import arm.config.config as cfg

from arm.ripper import utils
from arm.ui import app, db # noqa E402
from arm.config.config import cfg

PROCESS_COMPLETE = "Handbrake processing complete"

Expand All @@ -34,10 +34,10 @@ def handbrake_mainfeature(srcpath, basepath, logfile, job):

utils.database_updater({'status': "waiting_transcode"}, job)
# TODO: send a notification that jobs are waiting ?
utils.sleep_check_process("HandBrakeCLI", int(cfg["MAX_CONCURRENT_TRANSCODES"]))
utils.sleep_check_process("HandBrakeCLI", int(cfg.arm_config["MAX_CONCURRENT_TRANSCODES"]))
logging.debug("Setting job status to 'transcoding'")
utils.database_updater({'status': "transcoding"}, job)
filename = os.path.join(basepath, job.title + "." + cfg["DEST_EXT"])
filename = os.path.join(basepath, job.title + "." + cfg.arm_config["DEST_EXT"])
filepathname = os.path.join(basepath, filename)
logging.info(f"Ripping title Mainfeature to {shlex.quote(filepathname)}")

Expand All @@ -53,14 +53,14 @@ def handbrake_mainfeature(srcpath, basepath, logfile, job):
db.session.commit()

if job.disctype == "dvd":
hb_args = cfg["HB_ARGS_DVD"]
hb_preset = cfg["HB_PRESET_DVD"]
hb_args = cfg.arm_config["HB_ARGS_DVD"]
hb_preset = cfg.arm_config["HB_PRESET_DVD"]
elif job.disctype == "bluray":
hb_args = cfg["HB_ARGS_BD"]
hb_preset = cfg["HB_PRESET_BD"]
hb_args = cfg.arm_config["HB_ARGS_BD"]
hb_preset = cfg.arm_config["HB_PRESET_BD"]

cmd = 'nice {0} -i {1} -o {2} --main-feature --preset "{3}" {4} >> {5} 2>&1'.format(
cfg["HANDBRAKE_CLI"],
cfg.arm_config["HANDBRAKE_CLI"],
shlex.quote(srcpath),
shlex.quote(filepathname),
hb_preset,
Expand Down Expand Up @@ -102,38 +102,38 @@ def handbrake_all(srcpath, basepath, logfile, job):
# Wait until there is a spot to transcode
job.status = "waiting_transcode"
db.session.commit()
utils.sleep_check_process("HandBrakeCLI", int(cfg["MAX_CONCURRENT_TRANSCODES"]))
utils.sleep_check_process("HandBrakeCLI", int(cfg.arm_config["MAX_CONCURRENT_TRANSCODES"]))
job.status = "transcoding"
db.session.commit()
logging.info("Starting BluRay/DVD transcoding - All titles")

if job.disctype == "dvd":
hb_args = cfg["HB_ARGS_DVD"]
hb_preset = cfg["HB_PRESET_DVD"]
hb_args = cfg.arm_config["HB_ARGS_DVD"]
hb_preset = cfg.arm_config["HB_PRESET_DVD"]
elif job.disctype == "bluray":
hb_args = cfg["HB_ARGS_BD"]
hb_preset = cfg["HB_PRESET_BD"]
hb_args = cfg.arm_config["HB_ARGS_BD"]
hb_preset = cfg.arm_config["HB_PRESET_BD"]

get_track_info(srcpath, job)

logging.debug(f"Total number of tracks is {job.no_of_titles}")

for track in job.tracks:

if track.length < int(cfg["MINLENGTH"]):
if track.length < int(cfg.arm_config["MINLENGTH"]):
# too short
logging.info(f"Track #{track.track_number} of {job.no_of_titles}. "
f"Length ({track.length}) is less than minimum length ({cfg['MINLENGTH']}). Skipping")
elif track.length > int(cfg["MAXLENGTH"]):
f"Length ({track.length}) is less than minimum length ({cfg.arm_config['MINLENGTH']}). Skipping")
elif track.length > int(cfg.arm_config["MAXLENGTH"]):
# too long
logging.info(f"Track #{track.track_number} of {job.no_of_titles}. "
f"Length ({track.length}) is greater than maximum length ({cfg['MAXLENGTH']}). Skipping")
f"Length ({track.length}) is greater than maximum length ({cfg.arm_config['MAXLENGTH']}). Skipping")
else:
# just right
logging.info(f"Processing track #{track.track_number} of {job.no_of_titles}. "
f"Length is {track.length} seconds.")

filename = "title_" + str.zfill(str(track.track_number), 2) + "." + cfg["DEST_EXT"]
filename = "title_" + str.zfill(str(track.track_number), 2) + "." + cfg.arm_config["DEST_EXT"]
filepathname = os.path.join(basepath, filename)

logging.info(f"Transcoding title {track.track_number} to {shlex.quote(filepathname)}")
Expand All @@ -142,7 +142,7 @@ def handbrake_all(srcpath, basepath, logfile, job):
db.session.commit()

cmd = 'nice {0} -i {1} -o {2} --preset "{3}" -t {4} {5}>> {6} 2>&1'.format(
cfg["HANDBRAKE_CLI"],
cfg.arm_config["HANDBRAKE_CLI"],
shlex.quote(srcpath),
shlex.quote(filepathname),
hb_preset,
Expand Down Expand Up @@ -186,27 +186,27 @@ def handbrake_mkv(srcpath, basepath, logfile, job):
# Added to limit number of transcodes
job.status = "waiting_transcode"
db.session.commit()
utils.sleep_check_process("HandBrakeCLI", int(cfg["MAX_CONCURRENT_TRANSCODES"]))
utils.sleep_check_process("HandBrakeCLI", int(cfg.arm_config["MAX_CONCURRENT_TRANSCODES"]))
job.status = "transcoding"
db.session.commit()
if job.disctype == "dvd":
hb_args = cfg["HB_ARGS_DVD"]
hb_preset = cfg["HB_PRESET_DVD"]
hb_args = cfg.arm_config["HB_ARGS_DVD"]
hb_preset = cfg.arm_config["HB_PRESET_DVD"]
elif job.disctype == "bluray":
hb_args = cfg["HB_ARGS_BD"]
hb_preset = cfg["HB_PRESET_BD"]
hb_args = cfg.arm_config["HB_ARGS_BD"]
hb_preset = cfg.arm_config["HB_PRESET_BD"]

# This will fail if the directory raw gets deleted
for f in os.listdir(srcpath):
srcpathname = os.path.join(srcpath, f)
destfile = os.path.splitext(f)[0]
filename = os.path.join(basepath, destfile + "." + cfg["DEST_EXT"])
filename = os.path.join(basepath, destfile + "." + cfg.arm_config["DEST_EXT"])
filepathname = os.path.join(basepath, filename)

logging.info(f"Transcoding file {shlex.quote(f)} to {shlex.quote(filepathname)}")

cmd = 'nice {0} -i {1} -o {2} --preset "{3}" {4}>> {5} 2>&1'.format(
cfg["HANDBRAKE_CLI"],
cfg.arm_config["HANDBRAKE_CLI"],
shlex.quote(srcpathname),
shlex.quote(filepathname),
hb_preset,
Expand Down Expand Up @@ -242,7 +242,7 @@ def get_track_info(srcpath, job):
logging.info("Using HandBrake to get information on all the tracks on the disc. This will take a few minutes...")

cmd = '{0} -i {1} -t 0 --scan'.format(
cfg["HANDBRAKE_CLI"],
cfg.arm_config["HANDBRAKE_CLI"],
shlex.quote(srcpath)
)

Expand Down
10 changes: 5 additions & 5 deletions arm/ripper/identify.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import unicodedata
import xmltodict
import json
import arm.config.config as cfg

from arm.ripper import utils
from arm.ui import db
from arm.config.config import cfg

# flake8: noqa: W605
# from arm.ui.utils import call_omdb_api, tmdb_search
Expand All @@ -39,7 +39,7 @@ def identify(job, logfile):

logging.info("Disc identified as video")

if cfg["GET_VIDEO_TITLE"]:
if cfg.arm_config["GET_VIDEO_TITLE"]:
res = False
if job.disctype == "dvd":
res = identify_dvd(job)
Expand Down Expand Up @@ -239,19 +239,19 @@ def metadata_selector(job, title=None, year=None):
Args:
job:
"""
if cfg['METADATA_PROVIDER'].lower() == "tmdb":
if cfg.arm_config['METADATA_PROVIDER'].lower() == "tmdb":
logging.debug("provider tmdb")
x = u.tmdb_search(title, year)
if x is not None:
update_job(job, x)
return x
elif cfg['METADATA_PROVIDER'].lower() == "omdb":
elif cfg.arm_config['METADATA_PROVIDER'].lower() == "omdb":
logging.debug("provider omdb")
x = u.call_omdb_api(str(title), str(year))
if x is not None:
update_job(job, x)
return x
logging.debug(cfg['METADATA_PROVIDER'])
logging.debug(cfg.arm_config['METADATA_PROVIDER'])
logging.debug("unknown provider - doing nothing, saying nothing. Getting Kryten")


Expand Down
16 changes: 8 additions & 8 deletions arm/ripper/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging
import time

from arm.config.config import cfg
import arm.config.config as cfg


def setup_logging(job):
Expand All @@ -21,25 +21,25 @@ def setup_logging(job):
else:
logfile = "empty.log"
# set a logfull for empty.log and music_cd.log
logfull = os.path.join(cfg['LOGPATH'], logfile)
logfull = os.path.join(cfg.arm_config['LOGPATH'], logfile)
else:
logfile = job.label + ".log"
new_log_file = f"{job.label}_{round(time.time() * 100)}.log"
temp_log_full = os.path.join(cfg['LOGPATH'], logfile)
temp_log_full = os.path.join(cfg.arm_config['LOGPATH'], logfile)
logfile = new_log_file if os.path.isfile(temp_log_full) else logfile
# If log already exist use the new_log_file
logfull = os.path.join(cfg['LOGPATH'], new_log_file) if os.path.isfile(temp_log_full) \
else os.path.join(cfg['LOGPATH'], str(job.label) + ".log")
logfull = os.path.join(cfg.arm_config['LOGPATH'], new_log_file) if os.path.isfile(temp_log_full) \
else os.path.join(cfg.arm_config['LOGPATH'], str(job.label) + ".log")
job.logfile = logfile

# Debug formatting
if cfg['LOGLEVEL'] == "DEBUG":
if cfg.arm_config['LOGLEVEL'] == "DEBUG":
logging.basicConfig(filename=logfull, format='[%(asctime)s] %(levelname)s '
'ARM: %(module)s.%(funcName)s %(message)s',
datefmt=cfg['DATE_FORMAT'], level=cfg['LOGLEVEL'])
datefmt=cfg.arm_config['DATE_FORMAT'], level=cfg.arm_config['LOGLEVEL'])
else:
logging.basicConfig(filename=logfull, format='[%(asctime)s] %(levelname)s ARM: %(message)s',
datefmt=cfg['DATE_FORMAT'], level=cfg['LOGLEVEL'])
datefmt=cfg.arm_config['DATE_FORMAT'], level=cfg.arm_config['LOGLEVEL'])

# This stops apprise spitting our secret keys when users posts online
logging.getLogger("apprise").setLevel(logging.WARN)
Expand Down
Loading

0 comments on commit e715f85

Please sign in to comment.