Skip to content

Commit

Permalink
major pep8 cleanup.
Browse files Browse the repository at this point in the history
funneled scope of imports where possible
fixed #59
Added fstrings where possible
added logging both temporarily and in secondary places
  • Loading branch information
dirtycajunrice committed Dec 18, 2018
1 parent 47f2f61 commit 61fcf3b
Show file tree
Hide file tree
Showing 13 changed files with 131 additions and 121 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@ GeoLite2-City.mmdb
GeoLite2-City.tar.gz
data/varken.ini
.idea/
Legacy/configuration.py
varken-venv/
logs/
63 changes: 33 additions & 30 deletions Varken.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
import sys

# Check for python3.6 or newer to resolve erroneous typing.NamedTuple issues
if sys.version_info < (3, 6):
exit('Varken requires python3.6 or newer')

import schedule
import threading
import platform
import distro
import os
import schedule

from sys import exit
from time import sleep
from os import access, R_OK
from sys import version
from threading import Thread
from os import access, R_OK, getenv
from distro import linux_distribution
from os.path import isdir, abspath, dirname, join
from argparse import ArgumentParser, RawTextHelpFormatter
from logging import getLogger, StreamHandler, Formatter, DEBUG

from varken.iniparser import INIParser
from varken.sonarr import SonarrAPI
from varken.tautulli import TautulliAPI
from varken.radarr import RadarrAPI
from varken.ombi import OmbiAPI
from varken.sickchill import SickChillAPI
from varken.cisco import CiscoAPI
from varken.sonarr import SonarrAPI
from varken.radarr import RadarrAPI
from varken.iniparser import INIParser
from varken.dbmanager import DBManager
from varken.tautulli import TautulliAPI
from varken.sickchill import SickChillAPI
from varken.varkenlogger import VarkenLogger

PLATFORM_LINUX_DISTRO = ' '.join(x for x in distro.linux_distribution() if x)
PLATFORM_LINUX_DISTRO = ' '.join(x for x in linux_distribution() if x)


def threaded(job):
thread = threading.Thread(target=job)
thread = Thread(target=job)
thread.start()


Expand All @@ -46,35 +40,43 @@ def threaded(job):

DATA_FOLDER = abspath(join(dirname(__file__), 'data'))

templogger = getLogger('temp')
templogger.setLevel(DEBUG)
tempch = StreamHandler()
tempformatter = Formatter('%(asctime)s : %(levelname)s : %(module)s : %(message)s', '%Y-%m-%d %H:%M:%S')
tempch.setFormatter(tempformatter)
templogger.addHandler(tempch)

if opts.data_folder:
ARG_FOLDER = opts.data_folder

if isdir(ARG_FOLDER):
DATA_FOLDER = ARG_FOLDER
if not access(ARG_FOLDER, R_OK):
exit("Read permission error for {}".format(ARG_FOLDER))
if not access(DATA_FOLDER, R_OK):
templogger.error("Read permission error for %s", DATA_FOLDER)
exit(1)
else:
exit("{} does not exist".format(ARG_FOLDER))
templogger.error("%s does not exist", ARG_FOLDER)
exit(1)

# Set Debug to True if DEBUG env is set
enable_opts = ['True', 'true', 'yes']
debug_opts = ['debug', 'Debug', 'DEBUG']

if not opts.debug:
opts.debug = True if any([os.getenv(string, False) for true in enable_opts
for string in debug_opts if os.getenv(string, False) == true]) else False
opts.debug = True if any([getenv(string, False) for true in enable_opts
for string in debug_opts if getenv(string, False) == true]) else False

# Initiate the logger
vl = VarkenLogger(data_folder=DATA_FOLDER, debug=opts.debug)
vl.logger.info('Starting Varken...')

vl.logger.info('Data folder is "%s"', DATA_FOLDER)

vl.logger.info(u"{} {} ({}{})".format(
platform.system(), platform.release(), platform.version(),
' - {}'.format(PLATFORM_LINUX_DISTRO) if PLATFORM_LINUX_DISTRO else ''
))
vl.logger.info(u"Python {}".format(sys.version))
vl.logger.info(u"%s %s (%s%s)", platform.system(), platform.release(), platform.version(),
f' - {PLATFORM_LINUX_DISTRO}' if PLATFORM_LINUX_DISTRO else '')

vl.logger.info(u"Python %s", version)

CONFIG = INIParser(DATA_FOLDER)
DBMANAGER = DBManager(CONFIG.influx_server)
Expand Down Expand Up @@ -126,7 +128,8 @@ def threaded(job):
SERVICES_ENABLED = [CONFIG.ombi_enabled, CONFIG.radarr_enabled, CONFIG.tautulli_enabled,
CONFIG.sonarr_enabled, CONFIG.ciscoasa_enabled, CONFIG.sickchill_enabled]
if not [enabled for enabled in SERVICES_ENABLED if enabled]:
exit("All services disabled. Exiting")
vl.logger.error("All services disabled. Exiting")
exit(1)
schedule.run_all()

while True:
Expand Down
6 changes: 3 additions & 3 deletions varken/cisco.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from logging import getLogger
from requests import Session, Request
from datetime import datetime, timezone

Expand All @@ -13,12 +13,12 @@ def __init__(self, firewall, dbmanager):
# Create session to reduce server web thread load, and globally define pageSize for all requests
self.session = Session()
self.session.auth = (self.firewall.username, self.firewall.password)
self.logger = logging.getLogger()
self.logger = getLogger()

self.get_token()

def __repr__(self):
return "<ciscoasa-{}>".format(self.firewall.id)
return f"<ciscoasa-{self.firewall.id}>"

def get_token(self):
endpoint = '/api/tokenservices'
Expand Down
10 changes: 6 additions & 4 deletions varken/dbmanager.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import logging

from logging import getLogger
from influxdb import InfluxDBClient

logger = logging.getLogger('varken')

class DBManager(object):
def __init__(self, server):
self.server = server
self.influx = InfluxDBClient(self.server.url, self.server.port, self.server.username, self.server.password,
'varken')
databases = [db['name'] for db in self.influx.get_list_database()]
self.logger = getLogger()

if 'varken' not in databases:
self.logger.info("Creating varken database")
self.influx.create_database('varken')

self.logger.info("Creating varken retention policy (30d/1h)")
self.influx.create_retention_policy('varken 30d/1h', '30d', '1', 'varken', False, '1h')

def write_points(self, data):
d = data
logger.debug('Writing Data to InfluxDB %s', d)
self.logger.debug('Writing Data to InfluxDB %s', d)
self.influx.write_points(d)
8 changes: 4 additions & 4 deletions varken/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ def connection_handler(session, request, verify):


def mkdir_p(path):
"""http://stackoverflow.com/a/600612/190597 (tzot)"""
templogger = logging.getLogger('temp')
try:
logger.info('Creating folder %s ', path)
templogger.info('Creating folder %s ', path)
os.makedirs(path, exist_ok=True)
except Exception as e:
logger.error('Could not create folder %s : %s ', path, e)
templogger.error('Could not create folder %s : %s ', path, e)


def clean_sid_check(server_id_list, server_type=None):
Expand All @@ -123,7 +123,7 @@ def clean_sid_check(server_id_list, server_type=None):
try:
valid_sids.append(int(sid))
except ValueError:
logger.error("{} is not a valid server id number".format(sid))
logger.error("%s is not a valid server id number", sid)
if valid_sids:
logger.info('%s : %s', t.upper(), valid_sids)
return valid_sids
Expand Down
34 changes: 16 additions & 18 deletions varken/iniparser.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import configparser
import logging
import re

from sys import exit
from logging import getLogger
from os.path import join, exists
from re import match, compile, IGNORECASE
from configparser import ConfigParser, NoOptionError

from varken.helpers import clean_sid_check
from varken.structures import SickChillServer
from varken.varkenlogger import BlacklistFilter
from varken.structures import SonarrServer, RadarrServer, OmbiServer, TautulliServer, InfluxServer, CiscoASAFirewall
from varken.structures import SickChillServer


class INIParser(object):
def __init__(self, data_folder):
self.config = configparser.ConfigParser(interpolation=None)
self.config = ConfigParser(interpolation=None)
self.data_folder = data_folder

self.logger = logging.getLogger()
self.logger = getLogger()

self.influx_server = InfluxServer()

Expand Down Expand Up @@ -59,7 +57,7 @@ def enable_check(self, server_type=None):
else:
sids = clean_sid_check(global_server_ids, t)
return sids
except configparser.NoOptionError as e:
except NoOptionError as e:
self.logger.error(e)

def read_file(self):
Expand All @@ -86,11 +84,11 @@ def url_check(self, url=None, include_port=True):
else:
search = (search + r'(?:/?|[/?]\S+)$')

regex = re.compile('{}'.format(search), re.IGNORECASE)
regex = compile('{}'.format(search), IGNORECASE)

print(re.match(regex, url_check))
print(match(regex, url_check))

valid = re.match(regex, url_check) is not None
valid = match(regex, url_check) is not None
if not valid:
if inc_port:
self.logger.error('%s is invalid! URL must host/IP and port if not 80 or 443. ie. localhost:8080',
Expand Down Expand Up @@ -151,7 +149,7 @@ def parse_opts(self):
queue, queue_run_seconds)

self.sonarr_servers.append(server)
except configparser.NoOptionError as e:
except NoOptionError as e:
self.sonarr_enabled = False
self.logger.error(
'%s disabled. Error: %s', section, e)
Expand Down Expand Up @@ -185,7 +183,7 @@ def parse_opts(self):
server = RadarrServer(server_id, scheme + url, apikey, verify_ssl, queue, queue_run_seconds,
get_missing, get_missing_run_seconds)
self.radarr_servers.append(server)
except configparser.NoOptionError as e:
except NoOptionError as e:
self.radarr_enabled = False
self.logger.error(
'%s disabled. Error: %s', section, e)
Expand Down Expand Up @@ -217,7 +215,7 @@ def parse_opts(self):
server = TautulliServer(server_id, scheme + url, fallback_ip, apikey, verify_ssl, get_activity,
get_activity_run_seconds)
self.tautulli_servers.append(server)
except configparser.NoOptionError as e:
except NoOptionError as e:
self.tautulli_enabled = False
self.logger.error(
'%s disabled. Error: %s', section, e)
Expand Down Expand Up @@ -251,7 +249,7 @@ def parse_opts(self):
server = OmbiServer(server_id, scheme + url, apikey, verify_ssl, request_type_counts,
request_type_run_seconds, request_total_counts, request_total_run_seconds)
self.ombi_servers.append(server)
except configparser.NoOptionError as e:
except NoOptionError as e:
self.ombi_enabled = False
self.logger.error(
'%s disabled. Error: %s', section, e)
Expand Down Expand Up @@ -281,7 +279,7 @@ def parse_opts(self):
server = SickChillServer(server_id, scheme + url, apikey, verify_ssl,
get_missing, get_missing_run_seconds)
self.sickchill_servers.append(server)
except configparser.NoOptionError as e:
except NoOptionError as e:
self.sickchill_enabled = False
self.logger.error(
'%s disabled. Error: %s', section, e)
Expand Down Expand Up @@ -313,7 +311,7 @@ def parse_opts(self):
firewall = CiscoASAFirewall(firewall_id, scheme + url, username, password, outside_interface,
verify_ssl, get_bandwidth_run_seconds)
self.ciscoasa_firewalls.append(firewall)
except configparser.NoOptionError as e:
except NoOptionError as e:
self.ciscoasa_enabled = False
self.logger.error(
'%s disabled. Error: %s', section, e)
13 changes: 7 additions & 6 deletions varken/ombi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import logging
from logging import getLogger
from requests import Session, Request
from datetime import datetime, timezone

Expand All @@ -13,10 +13,10 @@ def __init__(self, server, dbmanager):
# Create session to reduce server web thread load, and globally define pageSize for all requests
self.session = Session()
self.session.headers = {'Apikey': self.server.api_key}
self.logger = logging.getLogger()
self.logger = getLogger()

def __repr__(self):
return "<ombi-{}>".format(self.server.id)
return f"<ombi-{self.server.id}>"

def get_all_requests(self):
now = datetime.now(timezone.utc).astimezone().isoformat()
Expand Down Expand Up @@ -94,14 +94,16 @@ def get_all_requests(self):

for show in tv_show_requests:
hash_id = hashit(f'{show.id}{show.tvDbId}{show.title}')
status = None
# Denied = 0, Approved = 1, Completed = 2

# Denied = 0, Approved = 1, Completed = 2, Pending = 3
if show.childRequests[0]['denied']:
status = 0
elif show.childRequests[0]['approved'] and show.childRequests[0]['available']:
status = 2
elif show.childRequests[0]['approved']:
status = 1
else:
status = 3

influx_payload.append(
{
Expand All @@ -122,7 +124,6 @@ def get_all_requests(self):
}
)


self.dbmanager.write_points(influx_payload)

def get_request_counts(self):
Expand Down
Loading

0 comments on commit 61fcf3b

Please sign in to comment.