def parse_user_menu(config): logINFO('Parse user menu ..') cfg_file = USERMENU cat_index = None if os.access(cfg_file, os.F_OK | os.R_OK): logINFO('Found user menu ..') f = open(cfg_file, 'r') for line in f: try: if line == '\n' or line.startswith('#'): continue elif line.startswith('@'): if line.count("##") > 0: cat = line[1:].strip('\n ').split("##") config[cat[0]] = [] cat_icon[cat[0]] = expanduser( expandvars ( cat[1] ) ) cat_index = cat[0] else: printERROR(u"# Error reading line: %s, not enough parameters" u" (must be at least 2)" % line) else: if line.count("##") > 2: if cat_index is not None: #Parameters:#[0]CMD#[1]Icon#[2]Name#[3]--/--#[4]Desc cmd_line = line.strip('\n\t ').split('##') cmd_line[0] = expanduser( expandvars ( cmd_line[0] ) ) cmd_line[1] = expanduser( expandvars ( cmd_line[1] ) ) cmd_line.insert(3, "") cmd_line[0] = parse_cmd(cmd_line[0], "", cmd_line[1], cmd_line[2]) config[cat_index].append(cmd_line) else: printERROR( u"# Error reading line: %s, not enough parameters" u" (must be at least 3)" % line) else: printERROR( u"# Error reading line: %s, not enough parameters" u" (must be at least 3)" % line) except: logINFO("# Error in user config menu at line: %s" % line) logTRACEBACK() f.close() return config
def info_desktop(filename, hideList, term_cmd=None): cmd = icon = name = comment = generic_name = None comment_locales, name_locales, generic_name_locales = {}, {}, {} category = 'Other' terminal = False #has_desktop_entry: если уже была секция с описанием .desktop файла - то дальше парсить .desktop файл нету #смысла. Если её еще не было - парсим дальше. has_desktop_entry = False try: cfile = open(filename, "r") for line in cfile: if line.startswith("[Desktop Entry]"): has_desktop_entry = True if "[Property::" in line: if has_desktop_entry: break if line.startswith("[") and not line.startswith("[Desktop Entry]"): if has_desktop_entry: break if '=' in line: key, value = line.strip().split('=', 1) if key == 'Type' and value != 'Application': icon, category = None, None break elif key == 'NoDisplay' and value == 'true': icon, category = None, None break elif key == 'OnlyShowIn': showinlist = line.split(';') hideList = set(hideList) if not [item for item in showinlist if item not in hideList]: # Original behaivour was strange; if we forbid GNOME # and app is for GNOME and XFCE, we used to ban it. # Todo: add support for NotShowIn. icon, category = None, None break elif key == 'Terminal' and value == 'true': terminal = True elif key.startswith('Name'): name_locales[key] = value elif key.startswith('Comment'): comment_locales[key] = value elif key.startswith('GenericName'): generic_name_locales[key] = value elif key == 'Icon' and value: icon = value if not icon[0] == '/': icon = icon.split('.', 1)[0] if key == 'Exec': cmd = value elif key == 'TryExec': tryexec = config.PATH_search(value) if tryexec is None: icon, category = None, None continue if key == 'Categories': tab = value.split(';') for cat in tab: found = False for c_name in cat_name: if cat in cat_name[c_name]: category = c_name found = True break if found: break name = get_i18n_name(name_locales) comment = get_i18n_comment(comment_locales) generic_name = get_i18n_generic_name(generic_name_locales) if cmd is not None: cmd = parse_cmd(cmd, filename, icon, name) # if command is 'console only', launch it with terminal .. if terminal: cmd = [term_cmd if term_cmd else 'xterm', '-e'] + cmd cfile.close() if comment is None: comment = generic_name if not category: category = 'Other' if category: logINFO("%s: %s->%s" % ( os.path.basename(filename), category, name )) return cmd, icon, name, category, comment except: logINFO(u"# Error : parsing %s" % filename) logTRACEBACK() return None