Skip to content

Commit

Permalink
[Quickfix] Allow duplicate entries in the AssociationsDatabase.
Browse files Browse the repository at this point in the history
For example if the mimeinfo.cache file contains two lines with the same key (mime type), the parser would fail (see #5). This fix allows the parser to replace previous value, and adds additional fail-safe checks.
  • Loading branch information
rchaput committed Dec 19, 2020
1 parent 41a0ac3 commit 15a92c3
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions xdgprefs/core/associations_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ADDED = 'Added Applications'
REMOVED = 'Removed Applications'
DEFAULT = 'Default Applications'
CACHE = 'MIME Cache'


class Associations(object):
Expand Down Expand Up @@ -118,7 +119,8 @@ def before_write(self, parser, section, option, value):

def parse_mimeapps(file_path):
config = configparser.ConfigParser(delimiters='=',
interpolation=ArrayInterpolation())
interpolation=ArrayInterpolation(),
strict=False)
try:
config.read(file_path)
for section in [ADDED, REMOVED, DEFAULT]:
Expand Down Expand Up @@ -154,19 +156,22 @@ def _parse_mimeapps_file(self, path):
if config is None:
self.logger.warning(f'Badly formatted file: {path}')
return
section = config['Added Applications']
section = config[ADDED]
for mimetype, apps in section.items():
self.associations[mimetype].extend_added(apps)
section = config['Removed Applications']
section = config[REMOVED]
for mimetype, apps in section.items():
self.associations[mimetype].extend_removed(apps)
section = config['Default Applications']
section = config[DEFAULT]
for mimetype, apps in section.items():
self.associations[mimetype].extend_default(apps)

def _parse_cache_file(self, path):
config = parse_mimeapps(path)
for mimetype, apps in config['MIME Cache'].items():
if config is None:
self.logger.warning(f'Badly formatted file: {path}')
return
for mimetype, apps in config[CACHE].items():
assoc = self.associations[mimetype]
assoc.extend_default(apps)

Expand Down

0 comments on commit 15a92c3

Please sign in to comment.