def clean_ini(path, section, parameter): """Delete sections and parameters (aka option) in the file""" # read file to parser config = bleachbit.RawConfigParser() fp = codecs.open(path, 'r', encoding='utf_8_sig') config.readfp(fp) # change file changed = False if config.has_section(section): if parameter is None: changed = True config.remove_section(section) elif config.has_option(section, parameter): changed = True config.remove_option(section, parameter) # write file if changed: from bleachbit.Options import options fp.close() if options.get('shred'): delete(path, True) fp = codecs.open(path, 'wb', encoding='utf_8') config.write(fp)
def __init__(self): self.purged = False self.config = bleachbit.RawConfigParser() self.config.optionxform = str # make keys case sensitive for hashpath purging self.config._boolean_states['t'] = True self.config._boolean_states['f'] = False self.restore()
def is_broken_xdg_desktop(pathname): """Returns boolean whether the given XDG desktop entry file is broken. Reference: http://standards.freedesktop.org/desktop-entry-spec/latest/""" config = bleachbit.RawConfigParser() config.read(pathname) if not config.has_section('Desktop Entry'): logger.info("is_broken_xdg_menu: missing required section 'Desktop Entry': '%s'", pathname) return True if not config.has_option('Desktop Entry', 'Type'): logger.info("is_broken_xdg_menu: missing required option 'Type': '%s'", pathname) return True file_type = config.get('Desktop Entry', 'Type').strip().lower() if 'link' == file_type: if not config.has_option('Desktop Entry', 'URL') and \ not config.has_option('Desktop Entry', 'URL[$e]'): logger.info("is_broken_xdg_menu: missing required option 'URL': '%s'", pathname) return True return False if 'mimetype' == file_type: if not config.has_option('Desktop Entry', 'MimeType'): logger.info("is_broken_xdg_menu: missing required option 'MimeType': '%s'", pathname) return True mimetype = config.get('Desktop Entry', 'MimeType').strip().lower() if is_unregistered_mime(mimetype): logger.info("is_broken_xdg_menu: MimeType '%s' not registered '%s'", mimetype, pathname) return True return False if 'application' != file_type: logger.warning("unhandled type '%s': file '%s'", file_type, pathname) return False if __is_broken_xdg_desktop_application(config, pathname): return True return False
def clean_ini(path, section, parameter): """Delete sections and parameters (aka option) in the file""" def write(parser, ini_file): """ Reimplementation of the original RowConfigParser write function. This function is 99% same as its origin. The only change is removing a cast to str. This is needed to handle unicode chars. """ if parser._defaults: ini_file.write("[%s]\n" % "DEFAULT") for (key, value) in parser._defaults.items(): ini_file.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) ini_file.write("\n") for section in parser._sections: ini_file.write("[%s]\n" % section) for (key, value) in parser._sections[section].items(): if key == "__name__": continue if (value is not None) or (parser._optcre == parser.OPTCRE): # The line bellow is the only changed line of the original function. # This is the orignal line for reference: # key = " = ".join((key, str(value).replace('\n', '\n\t'))) key = " = ".join((key, value.replace('\n', '\n\t'))) ini_file.write("%s\n" % (key)) ini_file.write("\n") encoding = detect_encoding(path) or 'utf_8_sig' # read file to parser config = bleachbit.RawConfigParser() config.optionxform = lambda option: option config.write = write with open(path, 'r', encoding=encoding) as fp: config.read_file(fp) # change file changed = False if config.has_section(section): if parameter is None: changed = True config.remove_section(section) elif config.has_option(section, parameter): changed = True config.remove_option(section, parameter) # write file if changed: from bleachbit.Options import options fp.close() if options.get('shred'): delete(path, True) with open(path, 'w', encoding=encoding, newline='') as fp: config.write(config, fp)
def __init__(self, pathname): """Create cleaners from a Winapp2.ini-style file""" self.cleaners = {} self.cleaner_ids = [] for langsecref in set(langsecref_map.values()): self.add_section(langsecref[0], langsecref[1]) self.errors = 0 self.parser = bleachbit.RawConfigParser() self.parser.read(pathname) for section in self.parser.sections(): try: self.handle_section(section) except Exception: self.errors += 1 logger.exception('parsing error in section %s', section)
def __init__(self, pathname, cb_progress=lambda x: None): """Create cleaners from a Winapp2.ini-style file""" self.cleaners = {} self.cleaner_ids = [] for langsecref in set(langsecref_map.values()): self.add_section(langsecref[0], langsecref[1]) self.errors = 0 self.parser = bleachbit.RawConfigParser() self.parser.read(pathname) self.re_detect = re.compile(r'^detect(\d+)?$') self.re_detectfile = re.compile(r'^detectfile(\d+)?$') self.re_excludekey = re.compile(r'^excludekey\d+$') section_total_count = len(self.parser.sections()) section_done_count = 0 for section in self.parser.sections(): try: self.handle_section(section) except Exception: self.errors += 1 logger.exception('parsing error in section %s', section) else: section_done_count += 1 cb_progress(1.0*section_done_count/section_total_count)
def clean_ini(path, section, parameter): """Delete sections and parameters (aka option) in the file""" def write(parser, ini_file): """ Reimplementation of the original RowConfigParser write function. This function is 99% same as its origin. The only change is removing a cast to str. This is needed to handle unicode chars. """ if parser._defaults: ini_file.write("[%s]\n" % "DEFAULT") for (key, value) in parser._defaults.items(): ini_file.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t'))) ini_file.write("\n") for section in parser._sections: ini_file.write("[%s]\n" % section) for (key, value) in parser._sections[section].items(): if key == "__name__": continue if (value is not None) or (parser._optcre == parser.OPTCRE): # The line bellow is the only changed line of the original function. # This is the orignal line for reference: # key = " = ".join((key, str(value).replace('\n', '\n\t'))) key = " = ".join((key, value.replace('\n', '\n\t'))) ini_file.write("%s\n" % (key)) ini_file.write("\n") try: import chardet except ImportError: has_chardet = False logger.warning( 'chardet is not available, so falling to utf-8 encoding') else: has_chardet = True # detect .ini file encoding with open(path) as file_: detector = chardet.universaldetector.UniversalDetector() for line in file_.readlines(): detector.feed(line) if detector.done: break detector.close() # read file to parser config = bleachbit.RawConfigParser() config.write = write fp = codecs.open( path, 'r', encoding=detector.result['encoding'] if has_chardet else 'utf_8_sig') config.readfp(fp) # change file changed = False if config.has_section(section): if parameter is None: changed = True config.remove_section(section) elif config.has_option(section, parameter): changed = True config.remove_option(section, parameter) # write file if changed: from bleachbit.Options import options fp.close() if options.get('shred'): delete(path, True) fp = codecs.open( path, 'wb', encoding=detector.result['encoding'] if has_chardet else 'utf_8') config.write(config, fp)