⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configfile.py

📁 bittorrent source by python. please enjoy
💻 PY
📖 第 1 页 / 共 2 页
字号:
    _write_config(error_callback, filename, p)def read_torrent_config(global_config, path, infohash, error_callback):    section = infohash.encode('hex')    filename = os.path.join(path, TORRENT_CONFIG_FILE)    p = _read_config(filename)    if not p.has_section(section):        return {}    else:        c = {}        for name, value in p.items(section):            if global_config.has_key(name):                t = type(global_config[name])                if t == bool:                    c[name] = value in ('1', 'True', True)                else:                    try:                        c[name] = type(global_config[name])(value)                    except ValueError, e:                        error_callback('ValueError %s (name:%s value:%s type:%s global:%s)' %                                       (unicode(e.args[0]), name, repr(value),                                        type(global_config[name]), global_config[name]))                        # is this reasonable?                        c[name] = global_config[name]            elif name == 'save_as':                # Backwards compatibility for BitTorrent 4.4 torrent_config file                c[name] = value            try:                c[name] = c[name].decode('utf-8')            except:                pass        return cdef remove_torrent_config(path, infohash, error_callback):    section = infohash.encode('hex')    filename = os.path.join(path, TORRENT_CONFIG_FILE)    p = _read_config(filename)    if p.has_section(section):        p.remove_section(section)    _write_config(error_callback, filename, p)def parse_configuration_and_args(defaults, uiname, arglist=[], minargs=None,                                 maxargs=None):    """Given the default option settings and overrides these defaults       from values read from the config file, and again overrides the       config file with the arguments that appear in the arglist.       'defaults' is a list of tuples of the form (optname, value,       desc) where 'optname' is a string containing the option's name,       value is the option's default value, and desc is the option's       description.       'uiname' is a string specifying the user interface that has been       created by the caller.  Ex: bittorrent, maketorrent.       arglist is usually argv[1:], i.e., excluding the name used to       execute the program.       minargs specifies the minimum number of arguments that must appear in       arglist.  If the number of arguments is less than the minimum then       a BTFailure exception is raised.       maxargs specifies the maximum number of arguments that can appear       in arglist.  If the number of arguments exceeds the maximum then       a BTFailure exception is raised.       This returns the tuple (config,args) where config is       a dictionary of (option, value) pairs, and args is the list       of arguments in arglist after the command-line arguments have       been removed.       For example:          bittorrent-curses.py --save_as lx-2.6.rpm lx-2.6.rpm.torrent --max_upload_rate 0          returns a (config,args) pair where the          config dictionary contains many defaults plus          the mappings            'save_as': 'linux-2.6.15.tar.gz'          and            'max_upload_rate': 0          The args in the returned pair is            args= ['linux-2.6.15.tar.gz.torrent']    """    assert type(defaults)==list    assert type(uiname)==str    assert type(arglist)==list    assert minargs is None or type(minargs) in (int,long) and minargs>=0    assert maxargs is None or type(maxargs) in (int,long) and maxargs>=minargs    # remap shortform arguments to their long-forms.    arglist = convert_from_shortforms(arglist)    defconfig = dict([(name, value) for (name, value, doc) in defaults])    if arglist[0:] == ['--version']:        print version        sys.exit(0)    if arglist[0:] in (['--help'], ['-h'], ['--usage'], ['-?']):        parseargs.printHelp(uiname, defaults)        sys.exit(0)    if "--use_factory_defaults" not in arglist:        presets = get_config(defconfig, uiname)    # run as if fresh install using temporary directories.    else:        presets = {}        temp_dir = get_temp_subdir()        #set_config_dir(temp_dir)  # is already set in platform.py.        save_in = encode_for_filesystem( u"save_in" )[0]        presets["save_in"] = \            decode_from_filesystem(os.path.join(temp_dir,save_in))        data = encode_for_filesystem( u"data" )[0]        presets["data_dir"] = \            decode_from_filesystem(os.path.join(temp_dir,data))        incomplete = encode_for_filesystem( u"incomplete" )[0]        presets["save_incomplete_in"] = \            decode_from_filesystem(os.path.join(temp_dir,incomplete))        presets["one_connection_per_ip"] = False    config = args = None    try:        config, args = parseargs.parseargs(arglist, defaults, minargs, maxargs,                                           presets)    except parseargs.UsageException, e:        print e        parseargs.printHelp(uiname, defaults)        sys.exit(0)    datadir = config.get('data_dir')    found_4x_config = False    if datadir:        datadir,bad = encode_for_filesystem(datadir)        if bad:            raise BTFailure(_("Invalid path encoding."))        if not os.path.exists(datadir):            os.mkdir(datadir)        if uiname in ('bittorrent', 'maketorrent'):            values = {}            p = _read_config(os.path.join(datadir, MAIN_CONFIG_FILE))            if p.has_section('format'):                encoding = p.get('format', 'encoding')            else:                encoding = old_broken_config_subencoding            if not p.has_section(uiname) and p.has_section(alt_uiname[uiname]):                uiname = alt_uiname[uiname]            if p.has_section(uiname):                for name, value in p.items(uiname):                    if name in defconfig:                        values[name] = value                    elif not found_4x_config:                        # identify 4.x version config file                        if name in ('start_torrent_behavior',                                    'seed_forever',                                    'progressbar_hack',                                    'seed_last_forever',                                    'next_torrent_ratio',                                    'next_torrent_time',                                    'last_torrent_ratio',                                    ):                            found_4x_config = True            parseargs.parse_options(defconfig, values, encoding)            presets.update(values)            config, args = parseargs.parseargs(arglist, defaults, minargs,                                               maxargs, presets)        for d in ('', 'resume', 'metainfo', 'torrents'):            ddir = os.path.join(datadir, d)            if not os.path.exists(ddir):                os.mkdir(ddir, 0700)            else:                assert(os.path.isdir(ddir))    if found_4x_config:        # version 4.x stored KB/s, < version 4.x stores B/s        config['max_upload_rate'] *= 1024    if config.get('language'):        # this is non-blocking if the language does not exist        smart_gettext_and_install('bittorrent', locale_root,                                  languages=[config['language']])    if config.has_key('bind') and config['bind'] != '':        bind_tracker_connection(config['bind'])    if config.has_key('launch_on_startup'):        enforce_shortcut(config, log_func=sys.stderr.write)    if os.name == 'nt' and config.has_key('enforce_association'):        enforce_association()    if config.has_key('save_in') and config['save_in'] == '' and \       (not config.has_key("save_as") or config['save_as'] == '' ) \       and uiname != 'bittorrent':        config['save_in'] = decode_from_filesystem(get_save_dir())    incomplete = decode_from_filesystem(get_incomplete_data_dir())    if config.get('save_incomplete_in') == '':        config['save_incomplete_in'] = incomplete    if config.get('save_incomplete_in') == get_old_incomplete_data_dir():        config['save_incomplete_in'] = incomplete    if uiname == "test-client" or (uiname.startswith("bittorrent")                                   and uiname != 'bittorrent-tracker'):        if not config.get('ask_for_save'):            # we check for existance, so things like "D:\" don't trip us up.            if (config['save_in'] and                not os.path.exists(config['save_in'])):                try:                    os.makedirs(config['save_in'])                except OSError, e:                    if (e.errno == 2 or # no such file or directory                        e.errno == 13): # permission denied                        traceback.print_exc()                        print >> sys.stderr, "save_in could not be created. Falling back to prompting."                        config['ask_for_save'] = True                    elif e.errno != 17: # path already exists                        raise            if (config['save_incomplete_in'] and                not os.path.exists(config['save_incomplete_in'])):                try:                    os.makedirs(config['save_incomplete_in'])                except OSError, e:                    if e.errno != 17: # path already exists                        traceback.print_exc()                        print >> sys.stderr, "save_incomplete_in could not be created. Falling back to default incomplete path."                        config['save_incomplete_in'] = incomplete                        raise                    return config, args

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -