<commit_before>#!/usr/local/sickbeard/env/bin/python
import os
import subprocess
import cgi
import configobj
from pyextdirect.configuration import create_configuration, expose, LOAD, SUBMIT
from pyextdirect.router import Router
from pyextdirect.api import create_api


sickbeard_sss = '/var/packages/sickbeard/scripts/start-stop-status'
sickbeard_path = '/usr/local/sickbeard/var/config.ini'
autoprocesstv_path = '/usr/local/sickbeard/share/SickBeard/autoProcessTV/autoProcessTV.cfg'
sabnzbd_path = '/usr/local/sabnzbd/var/config.ini'
nzbget_path = ''  #TODO


Base = create_configuration()


class SickBeardCfg(Base):
    def __init__(self):
        '''Load available configuration files'''
        self.configs = {'sickbeard': configobj.ConfigObj(sickbeard_path),
                        'autoprocesstv': configobj.ConfigObj(autoprocesstv_path)}
        if os.path.exists(sabnzbd_path):
            self.configs['sabnzbd'] = configobj.ConfigObj(sabnzbd_path)
        if os.path.exists(nzbget_path):
            self.configs['nzbget'] = configobj.ConfigObj(nzbget_path)

    @expose(kind=LOAD)
    def load(self):
        '''Read configuration files to determine current state'''
        autoprocesstv = False
        configure_for = self.configs['sickbeard']['General']['nzb_method']
        if 'sabnzbd' in self.configs and self.configs['sabnzbd']['misc']['script_dir'] == os.path.dirname(autoprocesstv_path):
            autoprocesstv = True
        if configure_for not in ['sabnzbd', 'nzbget']:
            configure_for = 'nothing'
        return {'configure_for': configure_for, 'autoprocesstv': autoprocesstv}

    @expose(kind=SUBMIT)
    def save(self, configure_for=None, autoprocesstv=None):
        '''Change configuration files as requested'''
        if configure_for == 'sabnzbd' and 'sabnzbd' in self.configs:
            devnull = open(os.devnull, 'w')
            subprocess.call([sickbeard_sss, 'stop'], stdout=devnull, stderr=devnull)
            self.configs['sickbeard']['General']['nzb_method'] = 'sabnzbd'
            self.configs['sickbeard']['SABnzbd']['sab_username'] = self.configs['sabnzbd']['misc']['username']
            self.configs['sickbeard']['SABnzbd']['sab_password'] = self.configs['sabnzbd']['misc']['password']
            self.configs['sickbeard']['SABnzbd']['sab_apikey'] = self.configs['sabnzbd']['misc']['api_key']
            self.configs['sickbeard']['SABnzbd']['sab_host'] = 'http://localhost:' + self.configs['sabnzbd']['misc']['port'] + '/'
            self.configs['sickbeard'].write()
            subprocess.call([sickbeard_sss, 'start'], stdout=devnull, stderr=devnull)
        elif configure_for == 'nzbget' and 'nzbget' in self.configs:
            #TODO<commit_msg>Configure autoProcessTV in sickbeard SPK<commit_after>#!/usr/local/sickbeard/env/bin/python
import os
import subprocess
import cgi
import configobj
from pyextdirect.configuration import create_configuration, expose, LOAD, SUBMIT
from pyextdirect.router import Router
from pyextdirect.api import create_api


sickbeard_sss = '/var/packages/sickbeard/scripts/start-stop-status'
sickbeard_path = '/usr/local/sickbeard/var/config.ini'
autoprocesstv_path = '/usr/local/sickbeard/share/SickBeard/autoProcessTV/autoProcessTV.cfg'
sabnzbd_path = '/usr/local/sabnzbd/var/config.ini'
nzbget_path = ''  #TODO


Base = create_configuration()


class SickBeardCfg(Base):
    def __init__(self):
        '''Load available configuration files'''
        self.configs = {'sickbeard': configobj.ConfigObj(sickbeard_path),
                        'autoprocesstv': configobj.ConfigObj(autoprocesstv_path)}
        if os.path.exists(sabnzbd_path):
            self.configs['sabnzbd'] = configobj.ConfigObj(sabnzbd_path)
        if os.path.exists(nzbget_path):
            self.configs['nzbget'] = configobj.ConfigObj(nzbget_path)

    @expose(kind=LOAD)
    def load(self):
        '''Read configuration files to determine current state'''
        autoprocesstv = False
        configure_for = self.configs['sickbeard']['General']['nzb_method']
        if 'sabnzbd' in self.configs and self.configs['sabnzbd']['misc']['script_dir'] == os.path.dirname(autoprocesstv_path):
            autoprocesstv = True
        if configure_for not in ['sabnzbd', 'nzbget']:
            configure_for = 'nothing'
        return {'configure_for': configure_for, 'autoprocesstv': autoprocesstv}

    @expose(kind=SUBMIT)
    def save(self, configure_for=None, autoprocesstv=None):
        '''Change configuration files as requested'''
        if configure_for == 'sabnzbd' and 'sabnzbd' in self.configs:
            devnull = open(os.devnull, 'w')
            subprocess.call([sickbeard_sss, 'stop'], stdout=devnull, stderr=devnull)
            self.configs['sickbeard']['General']['nzb_method'] = 'sabnzbd'
            self.configs['sickbeard']['SABnzbd']['sab_username'] = self.configs['sabnzbd']['misc']['username']
            self.configs['sickbeard']['SABnzbd']['sab_password'] = self.configs['sabnzbd']['misc']['password']
            self.configs['sickbeard']['SABnzbd']['sab_apikey'] = self.configs['sabnzbd']['misc']['api_key']
            self.configs['sickbeard']['SABnzbd']['sab_host'] = 'http://localhost:' + self.configs['sabnzbd']['misc']['port'] + '/'
            self.configs['sickbeard'].write()
            self.configs['autoprocesstv']['SickBeard']['username'] = self.configs['sickbeard']['General']['web_username']
            self.configs['autoprocesstv']['SickBeard']['password'] = self.configs['sickbeard']['General']['web_password']
            self.configs['autoprocesstv'].write()
            subprocess.call([sickbeard_sss, 'start'], stdout=devnull, stderr=devnull)
        elif configure_for == 'nzbget' and 'nzbget' in self.configs:
            #TODO
{'fraction_non_alphanumeric': 0.14947112883648345, 'fraction_numerical': 0.0, 'mean_word_length': 3.896434634974533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4728388', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1919, 'n_words': 290}