<s> """<STR_LIT>""" <EOL> from __future__ import generators <EOL> import locale , os , xml . dom . minidom <EOL> from xdg . BaseDirectory import * <EOL> from xdg . DesktopEntry import * <EOL> from xdg . Exceptions import * <EOL> import xdg . Xocale <EOL> import xdg . Config <EOL> ELEMENT_NODE = xml . dom . Node . ELEMENT_NODE <EOL> try : <EOL> reversed = reversed <EOL> except NameError : <EOL> def reversed ( x ) : <EOL> return x [ : : - <NUM_LIT:1> ] <EOL> class Menu : <EOL> def __init__ ( self ) : <EOL> self . Name = "<STR_LIT>" <EOL> self . Directory = None <EOL> self . Entries = [ ] <EOL> self . Doc = "<STR_LIT>" <EOL> self . Filename = "<STR_LIT>" <EOL> self . Depth = <NUM_LIT:0> <EOL> self . Parent = None <EOL> self . NotInXml = False <EOL> self . Show = True <EOL> self . Visible = <NUM_LIT:0> <EOL> self . AppDirs = [ ] <EOL> self . DefaultLayout = None <EOL> self . Deleted = "<STR_LIT>" <EOL> self . Directories = [ ] <EOL> self . DirectoryDirs = [ ] <EOL> self . Layout = None <EOL> self . MenuEntries = [ ] <EOL> self . Moves = [ ] <EOL> self . OnlyUnallocated = "<STR_LIT>" <EOL> self . Rules = [ ] <EOL> self . Submenus = [ ] <EOL> def __str__ ( self ) : <EOL> return self . Name <EOL> def __add__ ( self , other ) : <EOL> for dir in other . AppDirs : <EOL> self . AppDirs . append ( dir ) <EOL> for dir in other . DirectoryDirs : <EOL> self . DirectoryDirs . append ( dir ) <EOL> for directory in other . Directories : <EOL> self . Directories . append ( directory ) <EOL> if other . Deleted != "<STR_LIT>" : <EOL> self . Deleted = other . Deleted <EOL> if other . OnlyUnallocated != "<STR_LIT>" : <EOL> self . OnlyUnallocated = other . OnlyUnallocated <EOL> if other . Layout : <EOL> self . Layout = other . Layout <EOL> if other . DefaultLayout : <EOL> self . DefaultLayout = other . DefaultLayout <EOL> for rule in other . Rules : <EOL> self . Rules . append ( rule ) <EOL> for move in other . Moves : <EOL> self . Moves . append ( move ) <EOL> for submenu in other . Submenus : <EOL> self . addSubmenu ( submenu ) <EOL> return self <EOL> def __cmp__ ( self , other ) : <EOL> return locale . strcoll ( self . getName ( ) , other . getName ( ) ) <EOL> def __eq__ ( self , other ) : <EOL> if self . Name == str ( other ) : <EOL> return True <EOL> else : <EOL> return False <EOL> """<STR_LIT>""" <EOL> def getEntries ( self , hidden = False ) : <EOL> for entry in self . Entries : <EOL> if hidden == True : <EOL> yield entry <EOL> elif entry . Show == True : <EOL> yield entry <EOL> def getMenuEntry ( self , desktopfileid , deep = False ) : <EOL> for menuentry in self . MenuEntries : <EOL> if menuentry . DesktopFileID == desktopfileid : <EOL> return menuentry <EOL> if deep == True : <EOL> for submenu in self . Submenus : <EOL> submenu . getMenuEntry ( desktopfileid , deep ) <EOL> def getMenu ( self , path ) : <EOL> array = path . split ( "<STR_LIT:/>" , <NUM_LIT:1> ) <EOL> for submenu in self . Submenus : <EOL> if submenu . Name == array [ <NUM_LIT:0> ] : <EOL> if len ( array ) > <NUM_LIT:1> : <EOL> return submenu . getMenu ( array [ <NUM_LIT:1> ] ) <EOL> else : <EOL> return submenu <EOL> def getPath ( self , org = False , toplevel = False ) : <EOL> parent = self <EOL> names = [ ] <EOL> while <NUM_LIT:1> : <EOL> if org : <EOL> names . append ( parent . Name ) <EOL> else : <EOL> names . append ( parent . getName ( ) ) <EOL> if parent . Depth > <NUM_LIT:0> : <EOL> parent = parent . Parent <EOL> else : <EOL> break <EOL> names . reverse ( ) <EOL> path = "<STR_LIT>" <EOL> if toplevel == False : <EOL> names . pop ( <NUM_LIT:0> ) <EOL> for name in names : <EOL> path = os . path . join ( path , name ) <EOL> return path <EOL> def getName ( self ) : <EOL> try : <EOL> return self . Directory . DesktopEntry . getName ( ) <EOL> except AttributeError : <EOL> return self . Name <EOL> def getGenericName ( self ) : <EOL> try : <EOL> return self . Directory . DesktopEntry . getGenericName ( ) <EOL> except AttributeError : <EOL> return "<STR_LIT>" <EOL> def getComment ( self ) : <EOL> try : <EOL> return self . Directory . DesktopEntry . getComment ( ) <EOL> except AttributeError : <EOL> return "<STR_LIT>" <EOL> def getIcon ( self ) : <EOL> try : <EOL> return self . Directory . DesktopEntry . getIcon ( ) <EOL> except AttributeError : <EOL> return "<STR_LIT>" <EOL> """<STR_LIT>""" <EOL> def addSubmenu ( self , newmenu ) : <EOL> for submenu in self . Submenus : <EOL> if submenu == newmenu : <EOL> submenu += newmenu <EOL> break <EOL> else : <EOL> self . Submenus . append ( newmenu ) <EOL> newmenu . Parent = self <EOL> newmenu . Depth = self . Depth + <NUM_LIT:1> <EOL> class Move : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , node = None ) : <EOL> if node : <EOL> self . parseNode ( node ) <EOL> else : <EOL> self . Old = "<STR_LIT>" <EOL> self . New = "<STR_LIT>" <EOL> def __cmp__ ( self , other ) : <EOL> return cmp ( self . Old , other . Old ) <EOL> def parseNode ( self , node ) : <EOL> for child in node . childNodes : <EOL> if child . nodeType == ELEMENT_NODE : <EOL> if child . tagName == "<STR_LIT>" : <EOL> try : <EOL> self . parseOld ( child . childNodes [ <NUM_LIT:0> ] . nodeValue ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif child . tagName == "<STR_LIT>" : <EOL> try : <EOL> self . parseNew ( child . childNodes [ <NUM_LIT:0> ] . nodeValue ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def parseOld ( self , value ) : <EOL> self . Old = value <EOL> def parseNew ( self , value ) : <EOL> self . New = value <EOL> class Layout : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , node = None ) : <EOL> self . order = [ ] <EOL> if node : <EOL> self . show_empty = node . getAttribute ( "<STR_LIT>" ) or "<STR_LIT:false>" <EOL> self . inline = node . getAttribute ( "<STR_LIT>" ) or "<STR_LIT:false>" <EOL> self . inline_limit = node . getAttribute ( "<STR_LIT>" ) or <NUM_LIT:4> <EOL> self . inline_header = node . getAttribute ( "<STR_LIT>" ) or "<STR_LIT:true>" <EOL> self . inline_alias = node . getAttribute ( "<STR_LIT>" ) or "<STR_LIT:false>" <EOL> self . inline_limit = int ( self . inline_limit ) <EOL> self . parseNode ( node ) <EOL> else : <EOL> self . show_empty = "<STR_LIT:false>" <EOL> self . inline = "<STR_LIT:false>" <EOL> self . inline_limit = <NUM_LIT:4> <EOL> self . inline_header = "<STR_LIT:true>" <EOL> self . inline_alias = "<STR_LIT:false>" <EOL> self . order . append ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> self . order . append ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> def parseNode ( self , node ) : <EOL> for child in node . childNodes : <EOL> if child . nodeType == ELEMENT_NODE : <EOL> if child . tagName == "<STR_LIT>" : <EOL> try : <EOL> self . parseMenuname ( <EOL> child . childNodes [ <NUM_LIT:0> ] . nodeValue , <EOL> child . getAttribute ( "<STR_LIT>" ) or "<STR_LIT:false>" , <EOL> child . getAttribute ( "<STR_LIT>" ) or "<STR_LIT:false>" , <EOL> child . getAttribute ( "<STR_LIT>" ) or <NUM_LIT:4> , <EOL> child . getAttribute ( "<STR_LIT>" ) or "<STR_LIT:true>" , <EOL> child . getAttribute ( "<STR_LIT>" ) or "<STR_LIT:false>" ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> elif child . tagName == "<STR_LIT>" : <EOL> self . parseSeparator ( ) <EOL> elif child . tagName == "<STR_LIT>" : <EOL> try : <EOL> self . parseFilename ( child . childNodes [ <NUM_LIT:0> ] . nodeValue ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> elif child . tagName == "<STR_LIT>" : <EOL> self . parseMerge ( child . getAttribute ( "<STR_LIT:type>" ) or "<STR_LIT:all>" ) <EOL> def parseMenuname ( self , value , empty = "<STR_LIT:false>" , inline = "<STR_LIT:false>" , inline_limit = <NUM_LIT:4> , inline_header = "<STR_LIT:true>" , inline_alias = "<STR_LIT:false>" ) : <EOL> self . order . append ( [ "<STR_LIT>" , value , empty , inline , inline_limit , inline_header , inline_alias ] ) <EOL> self . order [ - <NUM_LIT:1> ] [ <NUM_LIT:4> ] = int ( self . order [ - <NUM_LIT:1> ] [ <NUM_LIT:4> ] ) <EOL> def parseSeparator ( self ) : <EOL> self . order . append ( [ "<STR_LIT>" ] ) <EOL> def parseFilename ( self , value ) : <EOL> self . order . append ( [ "<STR_LIT>" , value ] ) <EOL> def parseMerge ( self , type = "<STR_LIT:all>" ) : <EOL> self . order . append ( [ "<STR_LIT>" , type ] ) <EOL> class Rule : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , type , node = None ) : <EOL> self . Type = type <EOL> self . Rule = "<STR_LIT>" <EOL> self . Depth = <NUM_LIT:0> <EOL> self . Expr = [ "<STR_LIT>" ] <EOL> self . New = True <EOL> if node : <EOL> self . parseNode ( node ) <EOL> self . compile ( ) <EOL> def __str__ ( self ) : <EOL> return self . Rule <EOL> def compile ( self ) : <EOL> exec ( """<STR_LIT>""" % self . Rule ) in self . __dict__ <EOL> def parseNode ( self , node ) : <EOL> for child in node . childNodes : <EOL> if child . nodeType == ELEMENT_NODE : <EOL> if child . tagName == '<STR_LIT>' : <EOL> try : <EOL> self . parseFilename ( child . childNodes [ <NUM_LIT:0> ] . nodeValue ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> try : <EOL> self . parseCategory ( child . childNodes [ <NUM_LIT:0> ] . nodeValue ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , "<STR_LIT>" ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> self . parseAll ( ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> self . parseAnd ( child ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> self . parseOr ( child ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> self . parseNot ( child ) <EOL> def parseNew ( self , set = True ) : <EOL> if not self . New : <EOL> self . Rule += "<STR_LIT:U+0020>" + self . Expr [ self . Depth ] + "<STR_LIT:U+0020>" <EOL> if not set : <EOL> self . New = True <EOL> elif set : <EOL> self . New = False <EOL> def parseFilename ( self , value ) : <EOL> self . parseNew ( ) <EOL> self . Rule += "<STR_LIT>" % value . strip ( ) . replace ( "<STR_LIT:\\>" , r"<STR_LIT:\\>" ) . replace ( "<STR_LIT:'>" , r"<STR_LIT>" ) <EOL> def parseCategory ( self , value ) : <EOL> self . parseNew ( ) <EOL> self . Rule += "<STR_LIT>" % value . strip ( ) <EOL> def parseAll ( self ) : <EOL> self . parseNew ( ) <EOL> self . Rule += "<STR_LIT:True>" <EOL> def parseAnd ( self , node ) : <EOL> self . parseNew ( False ) <EOL> self . Rule += "<STR_LIT:(>" <EOL> self . Depth += <NUM_LIT:1> <EOL> self . Expr . append ( "<STR_LIT>" ) <EOL> self . parseNode ( node ) <EOL> self . Depth -= <NUM_LIT:1> <EOL> self . Expr . pop ( ) <EOL> self . Rule += "<STR_LIT:)>" <EOL> def parseOr ( self , node ) : <EOL> self . parseNew ( False ) <EOL> self . Rule += "<STR_LIT:(>" <EOL> self . Depth += <NUM_LIT:1> <EOL> self . Expr . append ( "<STR_LIT>" ) <EOL> self . parseNode ( node ) <EOL> self . Depth -= <NUM_LIT:1> <EOL> self . Expr . pop ( ) <EOL> self . Rule += "<STR_LIT:)>" <EOL> def parseNot ( self , node ) : <EOL> self . parseNew ( False ) <EOL> self . Rule += "<STR_LIT>" <EOL> self . Depth += <NUM_LIT:1> <EOL> self . Expr . append ( "<STR_LIT>" ) <EOL> self . parseNode ( node ) <EOL> self . Depth -= <NUM_LIT:1> <EOL> self . Expr . pop ( ) <EOL> self . Rule += "<STR_LIT:)>" <EOL> class MenuEntry : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , filename , dir = "<STR_LIT>" , prefix = "<STR_LIT>" ) : <EOL> self . DesktopEntry = DesktopEntry ( os . path . join ( dir , filename ) ) <EOL> self . setAttributes ( filename , dir , prefix ) <EOL> self . Show = True <EOL> self . Original = None <EOL> self . Parents = [ ] <EOL> self . Allocated = False <EOL> self . Add = False <EOL> self . MatchedInclude = False <EOL> self . Categories = self . DesktopEntry . getCategories ( ) <EOL> def save ( self ) : <EOL> if self . DesktopEntry . tainted == True : <EOL> self . DesktopEntry . write ( ) <EOL> def getDir ( self ) : <EOL> return self . DesktopEntry . filename . replace ( self . Filename , '<STR_LIT>' ) <EOL> def getType ( self ) : <EOL> if xdg . Config . root_mode == False : <EOL> if self . Original : <EOL> return "<STR_LIT>" <EOL> elif xdg_data_dirs [ <NUM_LIT:0> ] in self . DesktopEntry . filename : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT>" <EOL> def setAttributes ( self , filename , dir = "<STR_LIT>" , prefix = "<STR_LIT>" ) : <EOL> self . Filename = filename <EOL> self . Prefix = prefix <EOL> self . DesktopFileID = os . path . join ( prefix , filename ) . replace ( "<STR_LIT:/>" , "<STR_LIT:->" ) <EOL> if not os . path . isabs ( self . DesktopEntry . filename ) : <EOL> self . __setFilename ( ) <EOL> def updateAttributes ( self ) : <EOL> if self . getType ( ) == "<STR_LIT>" : <EOL> self . Original = MenuEntry ( self . Filename , self . getDir ( ) , self . Prefix ) <EOL> self . __setFilename ( ) <EOL> def __setFilename ( self ) : <EOL> if xdg . Config . root_mode == False : <EOL> path = xdg_data_dirs [ <NUM_LIT:0> ] <EOL> else : <EOL> path = xdg_data_dirs [ <NUM_LIT:1> ] <EOL> if self . DesktopEntry . getType ( ) == "<STR_LIT>" : <EOL> dir = os . path . join ( path , "<STR_LIT>" ) <EOL> else : <EOL> dir = os . path . join ( path , "<STR_LIT>" ) <EOL> self . DesktopEntry . filename = os . path . join ( dir , self . Filename ) <EOL> def __cmp__ ( self , other ) : <EOL> return locale . strcoll ( self . DesktopEntry . getName ( ) , other . DesktopEntry . getName ( ) ) <EOL> def __eq__ ( self , other ) : <EOL> if self . DesktopFileID == str ( other ) : <EOL> return True <EOL> else : <EOL> return False <EOL> def __repr__ ( self ) : <EOL> return self . DesktopFileID <EOL> class Separator : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , parent ) : <EOL> self . Parent = parent <EOL> self . Show = True <EOL> class Header : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , name , generic_name , comment ) : <EOL> self . Name = name <EOL> self . GenericName = generic_name <EOL> self . Comment = comment <EOL> def __str__ ( self ) : <EOL> return self . Name <EOL> tmp = { } <EOL> def __getFileName ( filename ) : <EOL> dirs = xdg_config_dirs [ : ] <EOL> if xdg . Config . root_mode == True : <EOL> dirs . pop ( <NUM_LIT:0> ) <EOL> for dir in dirs : <EOL> menuname = os . path . join ( dir , "<STR_LIT>" , filename ) <EOL> if os . path . isdir ( dir ) and os . path . isfile ( menuname ) : <EOL> return menuname <EOL> def parse ( filename = None ) : <EOL> if filename and not os . path . isabs ( filename ) : <EOL> filename = __getFileName ( filename ) <EOL> if not filename : <EOL> candidate = os . environ . get ( '<STR_LIT>' , '<STR_LIT>' ) + "<STR_LIT>" <EOL> filename = __getFileName ( candidate ) <EOL> if not filename : <EOL> raise ParsingError ( '<STR_LIT>' , "<STR_LIT>" % candidate ) <EOL> if not os . path . splitext ( filename ) [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> raise ParsingError ( '<STR_LIT>' , filename ) <EOL> try : <EOL> doc = xml . dom . minidom . parse ( filename ) <EOL> except xml . parsers . expat . ExpatError : <EOL> raise ParsingError ( '<STR_LIT>' , filename ) <EOL> tmp [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> tmp [ "<STR_LIT>" ] = [ ] <EOL> tmp [ "<STR_LIT>" ] = [ ] <EOL> tmp [ "<STR_LIT>" ] = MenuEntryCache ( ) <EOL> __parse ( doc , filename , tmp [ "<STR_LIT>" ] ) <EOL> __parsemove ( tmp [ "<STR_LIT>" ] ) <EOL> __postparse ( tmp [ "<STR_LIT>" ] ) <EOL> tmp [ "<STR_LIT>" ] . Doc = doc <EOL> tmp [ "<STR_LIT>" ] . Filename = filename <EOL> __genmenuNotOnlyAllocated ( tmp [ "<STR_LIT>" ] ) <EOL> __genmenuOnlyAllocated ( tmp [ "<STR_LIT>" ] ) <EOL> sort ( tmp [ "<STR_LIT>" ] ) <EOL> return tmp [ "<STR_LIT>" ] <EOL> def __parse ( node , filename , parent = None ) : <EOL> for child in node . childNodes : <EOL> if child . nodeType == ELEMENT_NODE : <EOL> if child . tagName == '<STR_LIT>' : <EOL> __parseMenu ( child , filename , parent ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> try : <EOL> __parseAppDir ( child . childNodes [ <NUM_LIT:0> ] . nodeValue , filename , parent ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , filename ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> __parseDefaultAppDir ( filename , parent ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> try : <EOL> __parseDirectoryDir ( child . childNodes [ <NUM_LIT:0> ] . nodeValue , filename , parent ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , filename ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> __parseDefaultDirectoryDir ( filename , parent ) <EOL> elif child . tagName == '<STR_LIT:Name>' : <EOL> try : <EOL> parent . Name = child . childNodes [ <NUM_LIT:0> ] . nodeValue <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , filename ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> try : <EOL> parent . Directories . append ( child . childNodes [ <NUM_LIT:0> ] . nodeValue ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , filename ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> parent . OnlyUnallocated = True <EOL> elif child . tagName == '<STR_LIT>' : <EOL> parent . OnlyUnallocated = False <EOL> elif child . tagName == '<STR_LIT>' : <EOL> parent . Deleted = True <EOL> elif child . tagName == '<STR_LIT>' : <EOL> parent . Deleted = False <EOL> elif child . tagName == '<STR_LIT>' or child . tagName == '<STR_LIT>' : <EOL> parent . Rules . append ( Rule ( child . tagName , child ) ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> try : <EOL> if child . getAttribute ( "<STR_LIT:type>" ) == "<STR_LIT>" : <EOL> __parseMergeFile ( "<STR_LIT>" , child , filename , parent ) <EOL> else : <EOL> __parseMergeFile ( child . childNodes [ <NUM_LIT:0> ] . nodeValue , child , filename , parent ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , filename ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> try : <EOL> __parseMergeDir ( child . childNodes [ <NUM_LIT:0> ] . nodeValue , child , filename , parent ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , filename ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> __parseDefaultMergeDirs ( child , filename , parent ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> parent . Moves . append ( Move ( child ) ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> if len ( child . childNodes ) > <NUM_LIT:1> : <EOL> parent . Layout = Layout ( child ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> if len ( child . childNodes ) > <NUM_LIT:1> : <EOL> parent . DefaultLayout = Layout ( child ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> try : <EOL> __parseLegacyDir ( child . childNodes [ <NUM_LIT:0> ] . nodeValue , child . getAttribute ( "<STR_LIT>" ) , filename , parent ) <EOL> except IndexError : <EOL> raise ValidationError ( '<STR_LIT>' , filename ) <EOL> elif child . tagName == '<STR_LIT>' : <EOL> __parseKDELegacyDirs ( filename , parent ) <EOL> def __parsemove ( menu ) : <EOL> for submenu in menu . Submenus : <EOL> __parsemove ( submenu ) <EOL> for move in menu . Moves : <EOL> move_from_menu = menu . getMenu ( move . Old ) <EOL> if move_from_menu : <EOL> move_to_menu = menu . getMenu ( move . New ) <EOL> menus = move . New . split ( "<STR_LIT:/>" ) <EOL> oldparent = None <EOL> while len ( menus ) > <NUM_LIT:0> : <EOL> if not oldparent : <EOL> oldparent = menu <EOL> newmenu = oldparent . getMenu ( menus [ <NUM_LIT:0> ] ) <EOL> if not newmenu : <EOL> newmenu = Menu ( ) <EOL> newmenu . Name = menus [ <NUM_LIT:0> ] <EOL> if len ( menus ) > <NUM_LIT:1> : <EOL> newmenu . NotInXml = True <EOL> oldparent . addSubmenu ( newmenu ) <EOL> oldparent = newmenu <EOL> menus . pop ( <NUM_LIT:0> ) <EOL> newmenu += move_from_menu <EOL> move_from_menu . Parent . Submenus . remove ( move_from_menu ) <EOL> def __postparse ( menu ) : <EOL> if menu . Deleted == "<STR_LIT>" : <EOL> menu . Deleted = False <EOL> if menu . OnlyUnallocated == "<STR_LIT>" : <EOL> menu . OnlyUnallocated = False <EOL> if not menu . Layout or not menu . DefaultLayout : <EOL> if menu . DefaultLayout : <EOL> menu . Layout = menu . DefaultLayout <EOL> elif menu . Layout : <EOL> if menu . Depth > <NUM_LIT:0> : <EOL> menu . DefaultLayout = menu . Parent . DefaultLayout <EOL> else : <EOL> menu . DefaultLayout = Layout ( ) <EOL> else : <EOL> if menu . Depth > <NUM_LIT:0> : <EOL> menu . Layout = menu . Parent . DefaultLayout <EOL> menu . DefaultLayout = menu . Parent . DefaultLayout <EOL> else : <EOL> menu . Layout = Layout ( ) <EOL> menu . DefaultLayout = Layout ( ) <EOL> if menu . Depth > <NUM_LIT:0> : <EOL> menu . AppDirs = menu . Parent . AppDirs + menu . AppDirs <EOL> menu . DirectoryDirs = menu . Parent . DirectoryDirs + menu . DirectoryDirs <EOL> menu . Directories = __removeDuplicates ( menu . Directories ) <EOL> menu . DirectoryDirs = __removeDuplicates ( menu . DirectoryDirs ) <EOL> menu . AppDirs = __removeDuplicates ( menu . AppDirs ) <EOL> for submenu in menu . Submenus : <EOL> __postparse ( submenu ) <EOL> menu . Directories . reverse ( ) <EOL> menu . DirectoryDirs . reverse ( ) <EOL> menu . AppDirs . reverse ( ) <EOL> for directory in menu . Directories : <EOL> for dir in menu . DirectoryDirs : <EOL> if os . path . isfile ( os . path . join ( dir , directory ) ) : <EOL> menuentry = MenuEntry ( directory , dir ) <EOL> if not menu . Directory : <EOL> menu . Directory = menuentry <EOL> elif menuentry . getType ( ) == "<STR_LIT>" : <EOL> if menu . Directory . getType ( ) == "<STR_LIT>" : <EOL> menu . Directory . Original = menuentry <EOL> if menu . Directory : <EOL> break <EOL> def __parseMenu ( child , filename , parent ) : <EOL> m = Menu ( ) <EOL> __parse ( child , filename , m ) <EOL> if parent : <EOL> parent . addSubmenu ( m ) <EOL> else : <EOL> tmp [ "<STR_LIT>" ] = m <EOL> def __check ( value , filename , type ) : <EOL> path = os . path . dirname ( filename ) <EOL> if not os . path . isabs ( value ) : <EOL> value = os . path . join ( path , value ) <EOL> value = os . path . abspath ( value ) <EOL> if type == "<STR_LIT>" and os . path . exists ( value ) and os . path . isdir ( value ) : <EOL> return value <EOL> elif type == "<STR_LIT:file>" and os . path . exists ( value ) and os . path . isfile ( value ) : <EOL> return value <EOL> else : <EOL> return False <EOL> def __parseAppDir ( value , filename , parent ) : <EOL> value = __check ( value , filename , "<STR_LIT>" ) <EOL> if value : <EOL> parent . AppDirs . append ( value ) <EOL> def __parseDefaultAppDir ( filename , parent ) : <EOL> for dir in reversed ( xdg_data_dirs ) : <EOL> __parseAppDir ( os . path . join ( dir , "<STR_LIT>" ) , filename , parent ) <EOL> def __parseDirectoryDir ( value , filename , parent ) : <EOL> value = __check ( value , filename , "<STR_LIT>" ) <EOL> if value : <EOL> parent . DirectoryDirs . append ( value ) <EOL> def __parseDefaultDirectoryDir ( filename , parent ) : <EOL> for dir in reversed ( xdg_data_dirs ) : <EOL> __parseDirectoryDir ( os . path . join ( dir , "<STR_LIT>" ) , filename , parent ) <EOL> def __parseMergeFile ( value , child , filename , parent ) : <EOL> if child . getAttribute ( "<STR_LIT:type>" ) == "<STR_LIT>" : <EOL> for dir in xdg_config_dirs : <EOL> rel_file = filename . replace ( dir , "<STR_LIT>" ) . strip ( "<STR_LIT:/>" ) <EOL> if rel_file != filename : <EOL> for p in xdg_config_dirs : <EOL> if dir == p : <EOL> continue <EOL> if os . path . isfile ( os . path . join ( p , rel_file ) ) : <EOL> __mergeFile ( os . path . join ( p , rel_file ) , child , parent ) <EOL> break <EOL> else : <EOL> value = __check ( value , filename , "<STR_LIT:file>" ) <EOL> if value : <EOL> __mergeFile ( value , child , parent ) <EOL> def __parseMergeDir ( value , child , filename , parent ) : <EOL> value = __check ( value , filename , "<STR_LIT>" ) <EOL> if value : <EOL> for item in os . listdir ( value ) : <EOL> try : <EOL> if os . path . splitext ( item ) [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> __mergeFile ( os . path . join ( value , item ) , child , parent ) <EOL> except UnicodeDecodeError : <EOL> continue <EOL> def __parseDefaultMergeDirs ( child , filename , parent ) : <EOL> basename = os . path . splitext ( os . path . basename ( filename ) ) [ <NUM_LIT:0> ] <EOL> for dir in reversed ( xdg_config_dirs ) : <EOL> __parseMergeDir ( os . path . join ( dir , "<STR_LIT>" , basename + "<STR_LIT>" ) , child , filename , parent ) <EOL> def __mergeFile ( filename , child , parent ) : <EOL> if filename in tmp [ "<STR_LIT>" ] : <EOL> if debug : <EOL> raise ParsingError ( '<STR_LIT>' , filename ) <EOL> else : <EOL> return <EOL> tmp [ "<STR_LIT>" ] . append ( filename ) <EOL> try : <EOL> doc = xml . dom . minidom . parse ( filename ) <EOL> except IOError : <EOL> if debug : <EOL> raise ParsingError ( '<STR_LIT>' , filename ) <EOL> else : <EOL> return <EOL> except xml . parsers . expat . ExpatError : <EOL> if debug : <EOL> raise ParsingError ( '<STR_LIT>' , filename ) <EOL> else : <EOL> return <EOL> for child in doc . childNodes : <EOL> if child . nodeType == ELEMENT_NODE : <EOL> __parse ( child , filename , parent ) <EOL> break <EOL> def __parseLegacyDir ( dir , prefix , filename , parent ) : <EOL> m = __mergeLegacyDir ( dir , prefix , filename , parent ) <EOL> if m : <EOL> parent += m <EOL> def __mergeLegacyDir ( dir , prefix , filename , parent ) : <EOL> dir = __check ( dir , filename , "<STR_LIT>" ) <EOL> if dir and dir not in tmp [ "<STR_LIT>" ] : <EOL> tmp [ "<STR_LIT>" ] . append ( dir ) <EOL> m = Menu ( ) <EOL> m . AppDirs . append ( dir ) <EOL> m . DirectoryDirs . append ( dir ) <EOL> m . Name = os . path . basename ( dir ) <EOL> m . NotInXml = True <EOL> for item in os . listdir ( dir ) : <EOL> try : <EOL> if item == "<STR_LIT>" : <EOL> m . Directories . append ( item ) <EOL> elif os . path . isdir ( os . path . join ( dir , item ) ) : <EOL> m . addSubmenu ( __mergeLegacyDir ( os . path . join ( dir , item ) , prefix , filename , parent ) ) <EOL> except UnicodeDecodeError : <EOL> continue <EOL> tmp [ "<STR_LIT>" ] . addMenuEntries ( [ dir ] , prefix , True ) <EOL> menuentries = tmp [ "<STR_LIT>" ] . getMenuEntries ( [ dir ] , False ) <EOL> for menuentry in menuentries : <EOL> categories = menuentry . Categories <EOL> if len ( categories ) == <NUM_LIT:0> : <EOL> r = Rule ( "<STR_LIT>" ) <EOL> r . parseFilename ( menuentry . DesktopFileID ) <EOL> r . compile ( ) <EOL> m . Rules . append ( r ) <EOL> if not dir in parent . AppDirs : <EOL> categories . append ( "<STR_LIT>" ) <EOL> menuentry . Categories = categories <EOL> return m <EOL> def __parseKDELegacyDirs ( filename , parent ) : <EOL> f = os . popen3 ( "<STR_LIT>" ) <EOL> output = f [ <NUM_LIT:1> ] . readlines ( ) <EOL> try : <EOL> for dir in output [ <NUM_LIT:0> ] . split ( "<STR_LIT::>" ) : <EOL> __parseLegacyDir ( dir , "<STR_LIT>" , filename , parent ) <EOL> except IndexError : <EOL> pass <EOL> def __removeDuplicates ( list ) : <EOL> set = { } <EOL> list . reverse ( ) <EOL> list = [ set . setdefault ( e , e ) for e in list if e not in set ] <EOL> list . reverse ( ) <EOL> return list <EOL> def __genmenuNotOnlyAllocated ( menu ) : <EOL> for submenu in menu . Submenus : <EOL> __genmenuNotOnlyAllocated ( submenu ) <EOL> if menu . OnlyUnallocated == False : <EOL> tmp [ "<STR_LIT>" ] . addMenuEntries ( menu . AppDirs ) <EOL> menuentries = [ ] <EOL> for rule in menu . Rules : <EOL> menuentries = rule . do ( tmp [ "<STR_LIT>" ] . getMenuEntries ( menu . AppDirs ) , rule . Type , <NUM_LIT:1> ) <EOL> for menuentry in menuentries : <EOL> if menuentry . Add == True : <EOL> menuentry . Parents . append ( menu ) <EOL> menuentry . Add = False <EOL> menuentry . Allocated = True <EOL> menu . MenuEntries . append ( menuentry ) <EOL> def __genmenuOnlyAllocated ( menu ) : <EOL> for submenu in menu . Submenus : <EOL> __genmenuOnlyAllocated ( submenu ) <EOL> if menu . OnlyUnallocated == True : <EOL> tmp [ "<STR_LIT>" ] . addMenuEntries ( menu . AppDirs ) <EOL> menuentries = [ ] <EOL> for rule in menu . Rules : <EOL> menuentries = rule . do ( tmp [ "<STR_LIT>" ] . getMenuEntries ( menu . AppDirs ) , rule . Type , <NUM_LIT:2> ) <EOL> for menuentry in menuentries : <EOL> if menuentry . Add == True : <EOL> menuentry . Parents . append ( menu ) <EOL> menu . MenuEntries . append ( menuentry ) <EOL> def sort ( menu ) : <EOL> menu . Entries = [ ] <EOL> menu . Visible = <NUM_LIT:0> <EOL> for submenu in menu . Submenus : <EOL> sort ( submenu ) <EOL> tmp_s = [ ] <EOL> tmp_e = [ ] <EOL> for order in menu . Layout . order : <EOL> if order [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> tmp_e . append ( order [ <NUM_LIT:1> ] ) <EOL> elif order [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> tmp_s . append ( order [ <NUM_LIT:1> ] ) <EOL> for order in menu . Layout . order : <EOL> if order [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> separator = Separator ( menu ) <EOL> if len ( menu . Entries ) > <NUM_LIT:0> and isinstance ( menu . Entries [ - <NUM_LIT:1> ] , Separator ) : <EOL> separator . Show = False <EOL> menu . Entries . append ( separator ) <EOL> elif order [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> menuentry = menu . getMenuEntry ( order [ <NUM_LIT:1> ] ) <EOL> if menuentry : <EOL> menu . Entries . append ( menuentry ) <EOL> elif order [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> submenu = menu . getMenu ( order [ <NUM_LIT:1> ] ) <EOL> if submenu : <EOL> __parse_inline ( submenu , menu ) <EOL> elif order [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> if order [ <NUM_LIT:1> ] == "<STR_LIT>" or order [ <NUM_LIT:1> ] == "<STR_LIT:all>" : <EOL> menu . MenuEntries . sort ( ) <EOL> for menuentry in menu . MenuEntries : <EOL> if menuentry not in tmp_e : <EOL> menu . Entries . append ( menuentry ) <EOL> elif order [ <NUM_LIT:1> ] == "<STR_LIT>" or order [ <NUM_LIT:1> ] == "<STR_LIT:all>" : <EOL> menu . Submenus . sort ( ) <EOL> for submenu in menu . Submenus : <EOL> if submenu . Name not in tmp_s : <EOL> __parse_inline ( submenu , menu ) <EOL> for entry in menu . Entries : <EOL> entry . Show = True <EOL> menu . Visible += <NUM_LIT:1> <EOL> if isinstance ( entry , Menu ) : <EOL> if entry . Deleted == True : <EOL> entry . Show = "<STR_LIT>" <EOL> menu . Visible -= <NUM_LIT:1> <EOL> elif isinstance ( entry . Directory , MenuEntry ) : <EOL> if entry . Directory . DesktopEntry . getNoDisplay ( ) == True : <EOL> entry . Show = "<STR_LIT>" <EOL> menu . Visible -= <NUM_LIT:1> <EOL> elif entry . Directory . DesktopEntry . getHidden ( ) == True : <EOL> entry . Show = "<STR_LIT>" <EOL> menu . Visible -= <NUM_LIT:1> <EOL> elif isinstance ( entry , MenuEntry ) : <EOL> if entry . DesktopEntry . getNoDisplay ( ) == True : <EOL> entry . Show = "<STR_LIT>" <EOL> menu . Visible -= <NUM_LIT:1> <EOL> elif entry . DesktopEntry . getHidden ( ) == True : <EOL> entry . Show = "<STR_LIT>" <EOL> menu . Visible -= <NUM_LIT:1> <EOL> elif entry . DesktopEntry . getTryExec ( ) and not __try_exec ( entry . DesktopEntry . getTryExec ( ) ) : <EOL> entry . Show = "<STR_LIT>" <EOL> menu . Visible -= <NUM_LIT:1> <EOL> elif xdg . Config . windowmanager : <EOL> if ( entry . DesktopEntry . getOnlyShowIn ( ) != [ ] and xdg . Config . windowmanager not in entry . DesktopEntry . getOnlyShowIn ( ) ) or xdg . Config . windowmanager in entry . DesktopEntry . getNotShowIn ( ) : <EOL> entry . Show = "<STR_LIT>" <EOL> menu . Visible -= <NUM_LIT:1> <EOL> elif isinstance ( entry , Separator ) : <EOL> menu . Visible -= <NUM_LIT:1> <EOL> if len ( menu . Entries ) > <NUM_LIT:0> : <EOL> if isinstance ( menu . Entries [ <NUM_LIT:0> ] , Separator ) : <EOL> menu . Entries [ <NUM_LIT:0> ] . Show = False <EOL> if len ( menu . Entries ) > <NUM_LIT:1> : <EOL> if isinstance ( menu . Entries [ - <NUM_LIT:1> ] , Separator ) : <EOL> menu . Entries [ - <NUM_LIT:1> ] . Show = False <EOL> for entry in menu . Entries : <EOL> if isinstance ( entry , Menu ) and entry . Layout . show_empty == "<STR_LIT:false>" and entry . Visible == <NUM_LIT:0> : <EOL> entry . Show = "<STR_LIT>" <EOL> menu . Visible -= <NUM_LIT:1> <EOL> if entry . NotInXml == True : <EOL> menu . Entries . remove ( entry ) <EOL> def __try_exec ( executable ) : <EOL> paths = os . environ [ '<STR_LIT>' ] . split ( os . pathsep ) <EOL> if not os . path . isfile ( executable ) : <EOL> for p in paths : <EOL> f = os . path . join ( p , executable ) <EOL> if os . path . isfile ( f ) : <EOL> if os . access ( f , os . X_OK ) : <EOL> return True <EOL> else : <EOL> if os . access ( executable , os . X_OK ) : <EOL> return True <EOL> return False <EOL> def __parse_inline ( submenu , menu ) : <EOL> if submenu . Layout . inline == "<STR_LIT:true>" : <EOL> if len ( submenu . Entries ) == <NUM_LIT:1> and submenu . Layout . inline_alias == "<STR_LIT:true>" : <EOL> menuentry = submenu . Entries [ <NUM_LIT:0> ] <EOL> menuentry . DesktopEntry . set ( "<STR_LIT:Name>" , submenu . getName ( ) , locale = True ) <EOL> menuentry . DesktopEntry . set ( "<STR_LIT>" , submenu . getGenericName ( ) , locale = True ) <EOL> menuentry . DesktopEntry . set ( "<STR_LIT>" , submenu . getComment ( ) , locale = True ) <EOL> menu . Entries . append ( menuentry ) <EOL> elif len ( submenu . Entries ) <= submenu . Layout . inline_limit or submenu . Layout . inline_limit == <NUM_LIT:0> : <EOL> if submenu . Layout . inline_header == "<STR_LIT:true>" : <EOL> header = Header ( submenu . getName ( ) , submenu . getGenericName ( ) , submenu . getComment ( ) ) <EOL> menu . Entries . append ( header ) <EOL> for entry in submenu . Entries : <EOL> menu . Entries . append ( entry ) <EOL> else : <EOL> menu . Entries . append ( submenu ) <EOL> else : <EOL> menu . Entries . append ( submenu ) <EOL> class MenuEntryCache : <EOL> "<STR_LIT>" <EOL> def __init__ ( self ) : <EOL> self . cacheEntries = { } <EOL> self . cacheEntries [ '<STR_LIT>' ] = [ ] <EOL> self . cache = { } <EOL> def addMenuEntries ( self , dirs , prefix = "<STR_LIT>" , legacy = False ) : <EOL> for dir in dirs : <EOL> if not self . cacheEntries . has_key ( dir ) : <EOL> self . cacheEntries [ dir ] = [ ] <EOL> self . __addFiles ( dir , "<STR_LIT>" , prefix , legacy ) <EOL> def __addFiles ( self , dir , subdir , prefix , legacy ) : <EOL> for item in os . listdir ( os . path . join ( dir , subdir ) ) : <EOL> if os . path . splitext ( item ) [ <NUM_LIT:1> ] == "<STR_LIT>" : <EOL> try : <EOL> menuentry = MenuEntry ( os . path . join ( subdir , item ) , dir , prefix ) <EOL> except ParsingError : <EOL> continue <EOL> self . cacheEntries [ dir ] . append ( menuentry ) <EOL> if legacy == True : <EOL> self . cacheEntries [ '<STR_LIT>' ] . append ( menuentry ) <EOL> elif os . path . isdir ( os . path . join ( dir , subdir , item ) ) and legacy == False : <EOL> self . __addFiles ( dir , os . path . join ( subdir , item ) , prefix , legacy ) <EOL> def getMenuEntries ( self , dirs , legacy = True ) : <EOL> list = [ ] <EOL> ids = [ ] <EOL> appdirs = dirs [ : ] <EOL> if legacy == True : <EOL> appdirs . append ( "<STR_LIT>" ) <EOL> key = "<STR_LIT>" . join ( appdirs ) <EOL> try : <EOL> return self . cache [ key ] <EOL> except KeyError : <EOL> pass <EOL> for dir in appdirs : <EOL> for menuentry in self . cacheEntries [ dir ] : <EOL> try : <EOL> if menuentry . DesktopFileID not in ids : <EOL> ids . append ( menuentry . DesktopFileID ) <EOL> list . append ( menuentry ) <EOL> elif menuentry . getType ( ) == "<STR_LIT>" : <EOL> i = list . index ( menuentry ) <EOL> e = list [ i ] <EOL> if e . getType ( ) == "<STR_LIT>" : <EOL> e . Original = menuentry <EOL> except UnicodeDecodeError : <EOL> continue <EOL> self . cache [ key ] = list <EOL> return list </s>
