def get_dir_root(shellvars, default_to_home=True): def check_sysvars(x): y = os.path.expandvars(x) if y != x and os.path.isdir(y): return y return None dir_root = None for d in shellvars: dir_root = check_sysvars(d) if dir_root is not None: break else: if default_to_home: dir_root = os.path.expanduser('~') if dir_root == '~' or not os.path.isdir(dir_root): dir_root = None if get_filesystem_encoding() == None: try: dir_root = dir_root.decode(sys.getfilesystemencoding()) except: try: dir_root = dir_root.decode('utf8') except: pass return dir_root
def decode_from_filesystem(path): encoding = get_filesystem_encoding() if encoding == None: assert isinstance(path, unicode), "Path should be unicode not %s" % type(path) decoded_path = path else: assert isinstance(path, str), "Path should be str not %s" % type(path) decoded_path = path.decode(encoding) return decoded_path
def to_utf8(name): if isinstance(name, unicode): u = name else: try: u = decode_from_filesystem(name) except Exception, e: s = str_exc(e) raise BTFailure(_('Could not convert file/directory name %r to ' 'Unicode (%s). Either the assumed filesystem ' 'encoding "%s" is wrong or the filename contains ' 'illegal bytes.') % (name, s, get_filesystem_encoding()))
def to_utf8(name): if isinstance(name, unicode): u = name else: try: u = decode_from_filesystem(name) except Exception, e: s = str_exc(e) raise BTFailure( _('Could not convert file/directory name %r to ' 'Unicode (%s). Either the assumed filesystem ' 'encoding "%s" is wrong or the filename contains ' 'illegal bytes.') % (name, s, get_filesystem_encoding()))
def encode_for_filesystem(path): assert isinstance(path, unicode), "Path should be unicode not %s" % type(path) bad = False encoding = get_filesystem_encoding() if encoding == None: encoded_path = path else: try: encoded_path = path.encode(encoding) except: bad = True path.replace(u"%", urllib.quote(u"%")) encoded_path = path.encode(encoding, 'urlquote') return (encoded_path, bad)
def _read_torrent_config(self, infohash): path = os.path.join(self.data_dir, 'torrents', infohash.encode('hex')) if not os.path.exists(path): raise BTFailure, _("Coult not open the torrent config: " + infohash.encode('hex')) f = file(path, 'rb') data = f.read() f.close() try: torrent_config = cPickle.loads(data) except: # backward compatibility with <= 4.9.3 torrent_config = bdecode(data) for k, v in torrent_config.iteritems(): try: torrent_config[k] = v.decode('utf8') if k in ('destination_path', 'working_path'): torrent_config[k] = encode_for_filesystem( torrent_config[k])[0] except: pass if not torrent_config.get('destination_path'): raise BTFailure(_("Invalid torrent config file")) if not torrent_config.get('working_path'): raise BTFailure(_("Invalid torrent config file")) if get_filesystem_encoding() == None: # These paths should both be unicode. If they aren't, they are the # broken product of some old version, and probably are in the # encoding we used to use in config files. Attempt to recover. dp = torrent_config['destination_path'] if isinstance(dp, str): try: dp = dp.decode(old_broken_config_subencoding) torrent_config['destination_path'] = dp except: raise BTFailure(_("Invalid torrent config file")) wp = torrent_config['working_path'] if isinstance(wp, str): try: wp = wp.decode(old_broken_config_subencoding) torrent_config['working_path'] = wp except: raise BTFailure(_("Invalid torrent config file")) return torrent_config
def _read_torrent_config(self, infohash): path = os.path.join(self.data_dir, 'torrents', infohash.encode('hex')) if not os.path.exists(path): raise BTFailure,_("Coult not open the torrent config: " + infohash.encode('hex')) f = file(path, 'rb') data = f.read() f.close() try: torrent_config = cPickle.loads(data) except: # backward compatibility with <= 4.9.3 torrent_config = bdecode(data) for k, v in torrent_config.iteritems(): try: torrent_config[k] = v.decode('utf8') if k in ('destination_path', 'working_path'): torrent_config[k] = encode_for_filesystem(torrent_config[k])[0] except: pass if not torrent_config.get('destination_path'): raise BTFailure( _("Invalid torrent config file")) if not torrent_config.get('working_path'): raise BTFailure( _("Invalid torrent config file")) if get_filesystem_encoding() == None: # These paths should both be unicode. If they aren't, they are the # broken product of some old version, and probably are in the # encoding we used to use in config files. Attempt to recover. dp = torrent_config['destination_path'] if isinstance(dp, str): try: dp = dp.decode(old_broken_config_subencoding) torrent_config['destination_path'] = dp except: raise BTFailure( _("Invalid torrent config file")) wp = torrent_config['working_path'] if isinstance(wp, str): try: wp = wp.decode(old_broken_config_subencoding) torrent_config['working_path'] = wp except: raise BTFailure( _("Invalid torrent config file")) return torrent_config
def show_encoding_errors(self, errorfunc): self.reported_errors = True if self.bad_torrent_unsolvable: errorfunc( logging.ERROR, _("This .torrent file has been created with a broken " "tool and has incorrectly encoded filenames. Some or " "all of the filenames may appear different from what " "the creator of the .torrent file intended.")) elif self.bad_torrent_noncharacter: errorfunc( logging.ERROR, _("This .torrent file has been created with a broken " "tool and has bad character values that do not " "correspond to any real character. Some or all of the " "filenames may appear different from what the creator " "of the .torrent file intended.")) elif self.bad_torrent_wrongfield: errorfunc( logging.ERROR, _("This .torrent file has been created with a broken " "tool and has incorrectly encoded filenames. The " "names used may still be correct.")) elif self.bad_conversion: errorfunc( logging.WARNING, _('The character set used on the local filesystem ("%s") ' 'cannot represent all characters used in the ' 'filename(s) of this torrent. Filenames have been ' 'changed from the original.') % get_filesystem_encoding()) elif self.bad_windows: errorfunc( logging.WARNING, _("The Windows filesystem cannot handle some " "characters used in the filename(s) of this torrent. " "Filenames have been changed from the original.")) elif self.bad_path: errorfunc( logging.WARNING, _("This .torrent file has been created with a broken " "tool and has at least 1 file with an invalid file " "or directory name. However since all such files " "were marked as having length 0 those files are " "just ignored."))
def show_encoding_errors(self, errorfunc): self.reported_errors = True if self.bad_torrent_unsolvable: errorfunc(logging.ERROR, _("This .torrent file has been created with a broken " "tool and has incorrectly encoded filenames. Some or " "all of the filenames may appear different from what " "the creator of the .torrent file intended.")) elif self.bad_torrent_noncharacter: errorfunc(logging.ERROR, _("This .torrent file has been created with a broken " "tool and has bad character values that do not " "correspond to any real character. Some or all of the " "filenames may appear different from what the creator " "of the .torrent file intended.")) elif self.bad_torrent_wrongfield: errorfunc(logging.ERROR, _("This .torrent file has been created with a broken " "tool and has incorrectly encoded filenames. The " "names used may still be correct.")) elif self.bad_conversion: errorfunc(logging.WARNING, _('The character set used on the local filesystem ("%s") ' 'cannot represent all characters used in the ' 'filename(s) of this torrent. Filenames have been ' 'changed from the original.') % get_filesystem_encoding()) elif self.bad_windows: errorfunc(logging.WARNING, _("The Windows filesystem cannot handle some " "characters used in the filename(s) of this torrent. " "Filenames have been changed from the original.")) elif self.bad_path: errorfunc(logging.WARNING, _("This .torrent file has been created with a broken " "tool and has at least 1 file with an invalid file " "or directory name. However since all such files " "were marked as having length 0 those files are " "just ignored."))