📄 configfile.py
字号:
# The contents of this file are subject to the BitTorrent Open Source License# Version 1.1 (the License). You may not copy or use this file, in either# source code or executable form, except in compliance with the License. You# may obtain a copy of the License at http://www.bittorrent.com/license/.## Software distributed under the License is distributed on an AS IS basis,# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License# for the specific language governing rights and limitations under the# License.# Written by Uoti Urpala and Matt Chisholm############ Needs redesign. Adding an app requires modifying a lot of if's. Blech.# --Dave############import osimport sysimport gettextimport localeimport tracebackfrom BitTorrent.translation import _from ConfigParser import RawConfigParserfrom ConfigParser import MissingSectionHeaderError, ParsingErrorfrom BitTorrent import parseargsfrom BitTorrent import app_name, version, BTFailurefrom BitTorrent.platform import get_dot_dir, get_save_dir, locale_root, is_frozen_exe, get_incomplete_data_dir, enforce_shortcut, enforce_association, smart_gettext_and_install, desktop, set_config_dir, get_old_incomplete_data_dir, decode_from_filesystem, encode_for_filesystemfrom BitTorrent.zurllib import bind_tracker_connection, set_zurllib_rawserverfrom BitTorrent.platform import get_temp_dir, get_temp_subdir, old_broken_config_subencodingfrom BitTorrent.shortargs import convert_from_shortformsdownloader_save_options = [ # General 'confirm_quit' , # Appearance 'progressbar_style' , 'toolbar_text' , 'toolbar_size' , # Bandwidth 'max_upload_rate' , 'max_download_rate' , # Saving 'save_in' , 'save_incomplete_in' , 'ask_for_save' , # Network 'minport' , 'maxport' , 'upnp' , 'ip' , # Misc 'open_from' , 'geometry' , 'start_maximized' , 'column_order' , 'enabled_columns' , 'column_widths' , 'sort_column' , 'sort_ascending' , 'show_details' , 'details_tab' , 'splitter_height' , 'theme' , 'donated' , 'notified' , ]if os.name == 'nt': downloader_save_options.extend([ # General 'enforce_association' , 'launch_on_startup' , 'minimize_to_tray' , 'start_minimized' , 'close_to_tray' , # Bandwidth 'bandwidth_management', ])MAIN_CONFIG_FILE = 'ui_config'TORRENT_CONFIG_FILE = 'torrent_config'alt_uiname = {'bittorrent':'btdownloadgui', 'maketorrent':'btmaketorrentgui',}def _read_config(filename): """Returns a RawConfigParser that has parsed the config file specified by the passed filename.""" # check for bad config files p = RawConfigParser() fp = None try: fp = open(filename) except IOError: pass if fp is not None: try: p.readfp(fp, filename=filename) except MissingSectionHeaderError: fp.close() del fp bad_config(filename) except ParsingError: fp.close() del fp bad_config(filename) else: fp.close() return pdef _write_config(error_callback, filename, p): if not p.has_section('format'): p.add_section('format') p.set('format', 'encoding', 'utf-8') try: f = file(filename, 'wb') p.write(f) f.close() except Exception, e: try: f.close() except: pass error_callback(_("Could not permanently save options: ")+unicode(e.args[0]))def bad_config(filename): base_bad_filename = filename + '.broken' bad_filename = base_bad_filename i = 0 while os.access(bad_filename, os.F_OK): bad_filename = base_bad_filename + str(i) i+=1 os.rename(filename, bad_filename) sys.stderr.write(_("Error reading config file. " "Old config file stored in \"%s\"") % bad_filename)def get_config(defaults, section): """This reads the key-value pairs from the specified section in the config file and from the common section. It then places those appearing in the defaults into a dict, which is then returned. @type defaults: dict @param defaults: dict of name-value pairs derived from the defaults list for this application (see defaultargs.py). @type section: str @param section: in the configuration from which to read options. So far, the sections have been named after applications, e.g., bittorrent, bittorrent-console, etc. @return: a dict containing option-value pairs. """ assert type(defaults)==dict assert type(section)==str configdir = get_dot_dir() if configdir is None: return {} if not os.path.isdir(configdir): try: os.mkdir(configdir, 0700) except: pass p = _read_config(os.path.join(configdir, 'config')) # returns parser. if p.has_section('format'): encoding = p.get('format', 'encoding') else: encoding = old_broken_config_subencoding values = {} if p.has_section(section): for name, value in p.items(section): if name in defaults: values[name] = value if p.has_section('common'): for name, value in p.items('common'): if name in defaults and name not in values: values[name] = value if defaults.get('data_dir') == '' and \ 'data_dir' not in values and os.path.isdir(configdir): datadir = os.path.join(configdir, 'data') values['data_dir'] = decode_from_filesystem(datadir) parseargs.parse_options(defaults, values, encoding) return valuesdef save_global_config(defaults, section, error_callback, save_options=downloader_save_options): filename = os.path.join(defaults['data_dir'], MAIN_CONFIG_FILE) p = _read_config(filename) p.remove_section(section) if p.has_section(alt_uiname[section]): p.remove_section(alt_uiname[section]) p.add_section(section) for name in save_options: name.decode('ascii').encode('utf-8') # just to make sure we can if defaults.has_key(name): value = defaults[name] if isinstance(value, str): value = value.decode('ascii').encode('utf-8') elif isinstance(value, unicode): value = value.encode('utf-8') p.set(section, name, value) else: err_str = _("Configuration option mismatch: '%s'") % name if is_frozen_exe: err_str = _("You must quit %s and reinstall it. (%s)") % (app_name, err_str) error_callback(err_str) _write_config(error_callback, filename, p)def save_torrent_config(path, infohash, config, error_callback): section = infohash.encode('hex') filename = os.path.join(path, TORRENT_CONFIG_FILE) p = _read_config(filename) p.remove_section(section) p.add_section(section) for key, value in config.items(): p.set(section, key, value)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -