Ticket #272: load_desktop_menus - use ConfigParser.patch

File load_desktop_menus - use ConfigParser.patch, 3.8 KB (added by Nathan Rennie-Waldock, 9 years ago)
  • winswitch/util/load_desktop_menus.py

     
    77import os.path
    88import hashlib
    99import re
     10import ConfigParser
     11
    1012from winswitch.util.which import which
    1113
    1214from winswitch.util.simple_logger import Logger, msig
     
    1719debug_import("common")
    1820from winswitch.util.common import csv_list, visible_command, load_binary_file, is_valid_file, delete_if_exists
    1921debug_import("file_io")
    20 from winswitch.util.file_io import get_menu_dir, get_xsessions_dir, get_actions_dir, load_properties, save_properties
     22from winswitch.util.file_io import get_menu_dir, get_xsessions_dir, get_actions_dir, save_properties
    2123debug_import("paths")
    2224from winswitch.util.paths import XSESSION_DIRS, WINSWITCH_COMMAND_WRAPPER, PREFIX_SEARCH_ORDER
    2325debug_import("consts")
     
    361363        sig = msig(menu_dir, filename, req_cmd, ignored_commands, whitelist_commands, ignored_in, skipped, disabled_categories, file_type)
    362364        short_sig = msig(menu_dir, filename, req_cmd, "[..]", "[..]")
    363365        full_path = os.path.join(menu_dir, filename)
    364         desktop_info = load_properties(full_path, False)
    365         if not desktop_info:
     366        desktop_info = ConfigParser.ConfigParser()
     367        if not desktop_info.read(full_path):
    366368                logger.debug(short_sig+" failed to load '%s'" % full_path)
    367369                return  None
    368370
    369371        enabled = True
    370         no_display = desktop_info.get('NoDisplay')
    371         if no_display and (no_display == "true" or no_display == "1"):
    372                 enabled = False
    373 
    374         only_show_in = desktop_info.get('OnlyShowIn')
    375         if only_show_in and len(only_show_in)>0:
    376                 shown_in = only_show_in.split(";")
    377                 ignored = True
    378                 for shown in shown_in:
    379                         if len(shown)>0 and shown not in ignored_in:
    380                                 ignored = False
    381                                 break
    382                 if ignored:
     372        try:
     373                no_display = desktop_info.get('Desktop Entry', 'NoDisplay')
     374                if no_display and (no_display == "true" or no_display == "1"):
    383375                        enabled = False
     376        except ConfigParser.NoOptionError:
     377                pass
    384378
    385         name = desktop_info.get('Name')
     379        try:
     380                only_show_in = desktop_info.get('Desktop Entry', 'OnlyShowIn')
     381                if only_show_in and len(only_show_in)>0:
     382                        shown_in = only_show_in.split(";")
     383                        ignored = True
     384                        for shown in shown_in:
     385                                if len(shown)>0 and shown not in ignored_in:
     386                                        ignored = False
     387                                        break
     388                        if ignored:
     389                                enabled = False
     390        except ConfigParser.NoOptionError:
     391                pass
     392
     393        try:
     394                name = desktop_info.get('Desktop Entry', 'Name')
     395        except ConfigParser.NoOptionError:
     396                name = None
     397
    386398        if not name or len(name)==0:
    387399                logger.debug(sig+" name is missing!")
    388400                return  None
    389         cmd = desktop_info.get('Exec')
     401
     402        try:
     403                cmd = desktop_info.get('Desktop Entry', 'Exec')
     404        except ConfigParser.NoOptionError:
     405                cmd = None
     406
    390407        if req_cmd and (not cmd or len(cmd)==0):
    391408                return  None
    392409
     
    426443                        skipped.append(name)
    427444                        enabled = False
    428445
    429         comment = desktop_info.get('Comment')
    430         icon_filename = desktop_info.get('Icon')
    431         categories_list = desktop_info.get('Categories')
     446        try:
     447                comment = desktop_info.get('Desktop Entry', 'Comment')
     448        except ConfigParser.NoOptionError:
     449                comment = None
     450
     451        try:
     452                icon_filename = desktop_info.get('Desktop Entry', 'Icon')
     453        except ConfigParser.NoOptionError:
     454                icon_filename = None
     455
     456        try:
     457                categories_list = desktop_info.get('Desktop Entry', 'Categories')
     458        except ConfigParser.NoOptionError:
     459                categories_list = None
     460
    432461        categories = []
    433462        uses_sound_out = matches_any(noparams, DEFAULT_SOUNDOUT_ENABLED_COMMANDS) is not None
    434463        uses_sound_in = matches_any(noparams, DEFAULT_SOUNDIN_ENABLED_COMMANDS) is not None
     
    456485
    457486        names = {}
    458487        comments = {}
    459         for key,value in desktop_info.items():
     488        for key,value in desktop_info.items('Desktop Entry'):
    460489                _NAMEKEY='Name['
    461490                _COMMENTKEY="Comment["
    462491                _ENDKEY="]"