<commit_before>import os
import sys
import web
import rtorrent
import time
import random
import simplejson as json
from urlparse import parse_qs

#from actions import build_torrent_info, perform_torrent_action
import actions
import common
import formatters
import sql
import utils
import http

render = http.render
to_json = utils.to_json

class Index:
    def GET(self):
        db = common.conf.get_db()
        if db.settings["show_welcome"]:
            raise(web.seeother("/welcome"))
        else:
            return(render.index(GetTorrents().GET(), GetSettings().GET()))
        
class Welcome:
    def GET(self):        
        return(render.welcome())
        
class SetSettings:
    def POST(self):
        args = web.input()
        #print(args)
        settings = {}
        for key, data_type in sql.Database.DATA_TYPES.items():
            if data_type == bool:
                # for options that use checkboxes (bool types)
                # if they're unchecked, the key won't be in args
                # if checked, the key will be in args with the value of "checked"
                if key in args and args[key] == "checked":
                    settings[key] = True
                else:
                    settings[key] = False
            elif data_type == None:
                if args[key] == "":
                    settings[key] = None
                else:
                    settings[key] = args[key]
            else:
                if key in args:
                    settings[key] = args[key]
                    
                    
        # dont send password if censored
        if args.has_key("password") and len(args["password"]) > 0:
            if args["password"] == "*" * len(args["password"]):
                del settings["password"]
                
        #print(settings)
        
        db = common.conf.get_db()
        
        common.conf.update_settings(settings)
        db.close()
        common.conf.refresh() # refresh conf info        
        
        raise(web.seeother("/"))
        
class GetSettings:
    def GET(self):
        db = common.conf.get_db()
        settings = db.get_settings()
        db.close()
        # remove sensitive info
        if settings["password"] is not None:
            settings["password"] = "*" * len(settings["password"])
        return(to_json(settings))

class GetTorrents:
    def GET(self):
        args = web.input()
        rpc_ids = args.get("rpc_id", None)
        rt = common.conf.get_rt()
        json_data = {}
        json_data["torrents"] = []
        json_data["client_info"] = {}
        json_data["trackers"] = {}
        json_data["error_code"] = 0
        json_data["error_msg"] = ""
        
        if rt is not None:
            try:
                rt.update()
            except: # workaround for httplib.ResponseNotReady
                pass
            torrents = actions.get_torrents_and_update_cache()
            json_data["trackers"] = common.conf.tracker_cache
            
            # if specific rpc ids were specified
            if rpc_ids is not None:
                #rpc_ids = parse_qs(rpc_ids).get("ids", []) # not needed?
                if isinstance(rpc_ids, (str, unicode)): rpc_ids = [rpc_ids]
            
                for rpc_id in rpc_ids:
                    t = actions.get_torrent(rpc_id)
                    json_data["torrents"].append(actions.build_torrent_info(t))
        
            else:
                # return all torrents (plus extra info)
                json_data["torrents"] = actions.build_torrent_info(torrents)        
            
                du = utils.get_disk_usage(common.conf.settings["du_path"])
                json_data["client_info"]["disk_free_str"] = formatters.format_size(du.free)
                json_data["client_info"]["disk_total_str"] = formatters.format_size(du.total)
                json_data["client_info"]["disk_free_percentage"] = formatters.format_percentage(du.free, du.total)
                json_data["client_info"]["down_rate"] = formatters.format_speed(rt.down_rate)
                json_data["client_info"]["up_rate"] = formatters.format_speed(rt.up_rate)
                json_data["client_info"]["client_version"] = rt.client_version<commit_msg>added OSError exception if du_path is invalid<commit_after>import os
import sys
import web
import rtorrent
import time
import random
import simplejson as json
from urlparse import parse_qs

#from actions import build_torrent_info, perform_torrent_action
import actions
import common
import formatters
import sql
import utils
import http

render = http.render
to_json = utils.to_json

class Index:
    def GET(self):
        db = common.conf.get_db()
        if db.settings["show_welcome"]:
            raise(web.seeother("/welcome"))
        else:
            return(render.index(GetTorrents().GET(), GetSettings().GET()))
        
class Welcome:
    def GET(self):        
        return(render.welcome())
        
class SetSettings:
    def POST(self):
        args = web.input()
        #print(args)
        settings = {}
        for key, data_type in sql.Database.DATA_TYPES.items():
            if data_type == bool:
                # for options that use checkboxes (bool types)
                # if they're unchecked, the key won't be in args
                # if checked, the key will be in args with the value of "checked"
                if key in args and args[key] == "checked":
                    settings[key] = True
                else:
                    settings[key] = False
            elif data_type == None:
                if args[key] == "":
                    settings[key] = None
                else:
                    settings[key] = args[key]
            else:
                if key in args:
                    settings[key] = args[key]
                    
                    
        # dont send password if censored
        if args.has_key("password") and len(args["password"]) > 0:
            if args["password"] == "*" * len(args["password"]):
                del settings["password"]
                
        #print(settings)
        
        db = common.conf.get_db()
        
        common.conf.update_settings(settings)
        db.close()
        common.conf.refresh() # refresh conf info        
        
        raise(web.seeother("/"))
        
class GetSettings:
    def GET(self):
        db = common.conf.get_db()
        settings = db.get_settings()
        db.close()
        # remove sensitive info
        if settings["password"] is not None:
            settings["password"] = "*" * len(settings["password"])
        return(to_json(settings))

class GetTorrents:
    def GET(self):
        args = web.input()
        rpc_ids = args.get("rpc_id", None)
        rt = common.conf.get_rt()
        json_data = {}
        json_data["torrents"] = []
        json_data["client_info"] = {}
        json_data["trackers"] = {}
        json_data["error_code"] = 0
        json_data["error_msg"] = ""
        
        if rt is not None:
            try:
                rt.update()
            except: # workaround for httplib.ResponseNotReady
                pass
            torrents = actions.get_torrents_and_update_cache()
            json_data["trackers"] = common.conf.tracker_cache
            
            # if specific rpc ids were specified
            if rpc_ids is not None:
                #rpc_ids = parse_qs(rpc_ids).get("ids", []) # not needed?
                if isinstance(rpc_ids, (str, unicode)): rpc_ids = [rpc_ids]
            
                for rpc_id in rpc_ids:
                    t = actions.get_torrent(rpc_id)
                    json_data["torrents"].append(actions.build_torrent_info(t))
        
            else:
                # return all torrents (plus extra info)
                try:
                    du = utils.get_disk_usage(common.conf.settings["du_path"])
                except OSError:
                    print("{0} not found".format(common.conf.settings["du_path"]))
                    du = utils.get_disk_usage("/")
                    
                json_data["torrents"] = actions.build_torrent_info(torrents)        
            
                json_data["client_info"]["disk_free_str"] = formatters.format_size(du.free)
                json_data["client_info"]["disk_total_str"] = formatters.format_size(du.total)
                json_data["client_info"]["disk_free_percentage"] = formatters.format_percentage(du.free, du.total)
                json_data["client_info"]["down_rate"] = formatters.format_speed(rt.down_rate)
                json_data["client_info"]["up_rate"] = formatters.format_speed(rt.up_rate)
                json_data["client_info"]["client_version"] = rt.client_version
{'fraction_non_alphanumeric': 0.10598446779351302, 'fraction_numerical': 0.0005710370031978072, 'mean_word_length': 1.7485875706214689, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, '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': '5851458', 'n_tokens_mistral': 2477, 'n_tokens_neox': 2420, 'n_words': 539}