<s> import argparse <EOL> import json <EOL> import logging <EOL> import os <EOL> import shutil <EOL> from six . moves import configparser , input <EOL> from autotorrent . at import AutoTorrent <EOL> from autotorrent . clients import TORRENT_CLIENTS <EOL> from autotorrent . db import Database <EOL> from autotorrent . humanize import humanize_bytes <EOL> def query_yes_no ( question , default = "<STR_LIT:yes>" ) : <EOL> """<STR_LIT>""" <EOL> valid = { "<STR_LIT:yes>" : True , "<STR_LIT:y>" : True , "<STR_LIT>" : True , <EOL> "<STR_LIT>" : False , "<STR_LIT:n>" : False } <EOL> if default is None : <EOL> prompt = "<STR_LIT>" <EOL> elif default == "<STR_LIT:yes>" : <EOL> prompt = "<STR_LIT>" <EOL> elif default == "<STR_LIT>" : <EOL> prompt = "<STR_LIT>" <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % default ) <EOL> while True : <EOL> print ( question + prompt ) <EOL> choice = input ( ) . lower ( ) <EOL> if default is not None and choice == '<STR_LIT>' : <EOL> return valid [ default ] <EOL> elif choice in valid : <EOL> return valid [ choice ] <EOL> else : <EOL> print ( "<STR_LIT>" ) <EOL> def commandline_handler ( ) : <EOL> parser = argparse . ArgumentParser ( ) <EOL> parser . add_argument ( "<STR_LIT:-c>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = "<STR_LIT:default>" , help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , dest = "<STR_LIT>" , nargs = '<STR_LIT:?>' , const = '<STR_LIT>' , default = None , help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , help = '<STR_LIT>' ) <EOL> parser . add_argument ( "<STR_LIT>" , nargs = '<STR_LIT:?>' , const = '<STR_LIT>' , default = None , dest = "<STR_LIT>" , choices = [ '<STR_LIT>' , '<STR_LIT>' ] , help = "<STR_LIT>" ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = False , help = '<STR_LIT>' , nargs = '<STR_LIT:*>' ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = False , help = '<STR_LIT>' , nargs = '<STR_LIT:+>' ) <EOL> parser . add_argument ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" , default = False , help = '<STR_LIT>' ) <EOL> parser . add_argument ( "<STR_LIT>" , help = "<STR_LIT>" , action = "<STR_LIT:store_true>" , dest = "<STR_LIT>" ) <EOL> args = parser . parse_args ( ) <EOL> logging . basicConfig ( level = logging . DEBUG if args . verbose else logging . ERROR ) <EOL> if args . create_config_file : <EOL> if os . path . exists ( args . create_config_file ) : <EOL> parser . error ( "<STR_LIT>" % args . create_config_file ) <EOL> else : <EOL> src = os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> shutil . copy ( src , args . create_config_file ) <EOL> print ( '<STR_LIT>' % args . create_config_file ) <EOL> client_config = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:url>' : '<STR_LIT>' , <EOL> '<STR_LIT:label>' : '<STR_LIT>' , <EOL> } <EOL> if query_yes_no ( '<STR_LIT>' ) : <EOL> working_clients = [ ] <EOL> for client_name , cls in TORRENT_CLIENTS . items ( ) : <EOL> obj = cls . auto_config ( ) <EOL> try : <EOL> if obj . test_connection ( ) : <EOL> working_clients . append ( obj ) <EOL> except : <EOL> continue <EOL> if working_clients : <EOL> print ( '<STR_LIT>' % len ( working_clients ) ) <EOL> for i , client in enumerate ( working_clients , <NUM_LIT:1> ) : <EOL> print ( '<STR_LIT>' % ( i , client . identifier ) ) <EOL> print ( '<STR_LIT>' ) <EOL> while True : <EOL> error = False <EOL> try : <EOL> choice = int ( input ( '<STR_LIT>' ) ) <EOL> except ValueError : <EOL> error = True <EOL> else : <EOL> if len ( working_clients ) < choice or choice < <NUM_LIT:0> : <EOL> error = True <EOL> if error : <EOL> print ( '<STR_LIT>' ) <EOL> else : <EOL> if choice > <NUM_LIT:0> : <EOL> client = working_clients [ choice - <NUM_LIT:1> ] <EOL> print ( '<STR_LIT>' % client . identifier ) <EOL> client_config = client . get_config ( ) <EOL> client_config [ '<STR_LIT>' ] = client . identifier <EOL> break <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> config = configparser . ConfigParser ( ) <EOL> config . read ( args . create_config_file ) <EOL> for k , v in client_config . items ( ) : <EOL> config . set ( '<STR_LIT>' , k , v ) <EOL> with open ( args . create_config_file , '<STR_LIT:w>' ) as configfile : <EOL> config . write ( configfile ) <EOL> quit ( ) <EOL> if not os . path . isfile ( args . config_file ) : <EOL> parser . error ( "<STR_LIT>" % args . config_file ) <EOL> config = configparser . ConfigParser ( ) <EOL> config . read ( args . config_file ) <EOL> current_path = os . getcwd ( ) <EOL> os . chdir ( os . path . dirname ( os . path . abspath ( args . config_file ) ) ) <EOL> if not config . has_section ( '<STR_LIT>' ) : <EOL> parser . error ( '<STR_LIT>' % args . config_file ) <EOL> quit ( <NUM_LIT:1> ) <EOL> i = <NUM_LIT:1> <EOL> disks = [ ] <EOL> while config . has_option ( '<STR_LIT>' , '<STR_LIT>' % i ) : <EOL> disks . append ( config . get ( '<STR_LIT>' , '<STR_LIT>' % i ) ) <EOL> i += <NUM_LIT:1> <EOL> scan_mode = set ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) ) <EOL> exact_mode = '<STR_LIT>' in scan_mode <EOL> unsplitable_mode = '<STR_LIT>' in scan_mode <EOL> normal_mode = '<STR_LIT>' in scan_mode <EOL> hash_name_mode = '<STR_LIT>' in scan_mode <EOL> hash_size_mode = '<STR_LIT>' in scan_mode <EOL> hash_slow_mode = '<STR_LIT>' in scan_mode <EOL> db = Database ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) , disks , <EOL> config . get ( '<STR_LIT>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) , <EOL> normal_mode , unsplitable_mode , exact_mode , <EOL> hash_name_mode , hash_size_mode , hash_slow_mode ) <EOL> client_option = '<STR_LIT>' <EOL> if args . client != '<STR_LIT:default>' : <EOL> client_option += '<STR_LIT>' % args . client <EOL> try : <EOL> client_name = config . get ( client_option , '<STR_LIT>' ) <EOL> except configparser . NoSectionError : <EOL> print ( '<STR_LIT>' % args . client ) <EOL> quit ( <NUM_LIT:1> ) <EOL> if client_name not in TORRENT_CLIENTS : <EOL> print ( '<STR_LIT>' % ( client_name , '<STR_LIT:U+002CU+0020>' . join ( TORRENT_CLIENTS . keys ( ) ) ) ) <EOL> quit ( <NUM_LIT:1> ) <EOL> client_options = dict ( config . items ( client_option ) ) <EOL> client_options . pop ( '<STR_LIT>' ) <EOL> client = TORRENT_CLIENTS [ client_name ] ( ** client_options ) <EOL> at = AutoTorrent ( <EOL> db , <EOL> client , <EOL> config . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> config . getint ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> config . getfloat ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> args . delete_torrents , <EOL> ( config . get ( '<STR_LIT>' , '<STR_LIT>' ) if config . has_option ( '<STR_LIT>' , '<STR_LIT>' ) else '<STR_LIT>' ) , <EOL> ) <EOL> if args . test_connection : <EOL> proxy_test_result = client . test_connection ( ) <EOL> if proxy_test_result : <EOL> print ( '<STR_LIT>' ) <EOL> print ( '<STR_LIT>' % proxy_test_result ) <EOL> if isinstance ( args . rebuild , list ) : <EOL> if args . rebuild : <EOL> print ( '<STR_LIT>' ) <EOL> db . rebuild ( args . rebuild ) <EOL> print ( '<STR_LIT>' ) <EOL> else : <EOL> print ( '<STR_LIT>' ) <EOL> db . rebuild ( ) <EOL> print ( '<STR_LIT>' ) <EOL> if args . addfile : <EOL> dry_run = bool ( args . dry_run ) <EOL> dry_run_data = [ ] <EOL> print ( '<STR_LIT>' % len ( args . addfile ) ) <EOL> if not dry_run : <EOL> at . populate_torrents_seeded ( ) <EOL> for torrent in args . addfile : <EOL> result = at . handle_torrentfile ( os . path . join ( current_path , torrent ) , dry_run ) <EOL> if dry_run : <EOL> dry_run_data . append ( { <EOL> '<STR_LIT>' : torrent , <EOL> '<STR_LIT>' : result [ <NUM_LIT:0> ] , <EOL> '<STR_LIT>' : result [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : not result [ <NUM_LIT:2> ] , <EOL> '<STR_LIT>' : result [ <NUM_LIT:3> ] , <EOL> } ) <EOL> if dry_run : <EOL> if args . dry_run == '<STR_LIT>' : <EOL> print ( json . dumps ( dry_run_data ) ) <EOL> elif args . dry_run == '<STR_LIT>' : <EOL> for torrent in dry_run_data : <EOL> print ( '<STR_LIT>' % torrent [ '<STR_LIT>' ] ) <EOL> print ( '<STR_LIT>' % ( humanize_bytes ( torrent [ '<STR_LIT>' ] ) , <EOL> humanize_bytes ( torrent [ '<STR_LIT>' ] ) , <EOL> torrent [ '<STR_LIT>' ] and '<STR_LIT>' or '<STR_LIT>' ) ) <EOL> print ( '<STR_LIT>' ) <EOL> for f in torrent [ '<STR_LIT>' ] : <EOL> print ( '<STR_LIT>' % f ) <EOL> print ( '<STR_LIT>' ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> commandline_handler ( ) </s>
