<s> """<STR_LIT>""" <EOL> from xdg . Menu import * <EOL> from xdg . BaseDirectory import * <EOL> from xdg . Exceptions import * <EOL> from xdg . DesktopEntry import * <EOL> from xdg . Config import * <EOL> import xml . dom . minidom <EOL> import os <EOL> import re <EOL> class MenuEditor : <EOL> def __init__ ( self , menu = None , filename = None , root = False ) : <EOL> self . menu = None <EOL> self . filename = None <EOL> self . doc = None <EOL> self . parse ( menu , filename , root ) <EOL> self . filenames = [ ] <EOL> def parse ( self , menu = None , filename = None , root = False ) : <EOL> if root == True : <EOL> setRootMode ( True ) <EOL> if isinstance ( menu , Menu ) : <EOL> self . menu = menu <EOL> elif menu : <EOL> self . menu = parse ( menu ) <EOL> else : <EOL> self . menu = parse ( ) <EOL> if root == True : <EOL> self . filename = self . menu . Filename <EOL> elif filename : <EOL> self . filename = filename <EOL> else : <EOL> self . filename = os . path . join ( xdg_config_dirs [ <NUM_LIT:0> ] , "<STR_LIT>" , os . path . split ( self . menu . Filename ) [ <NUM_LIT:1> ] ) <EOL> try : <EOL> self . doc = xml . dom . minidom . parse ( self . filename ) <EOL> except IOError : <EOL> self . doc = xml . dom . minidom . parseString ( '<STR_LIT>' + self . menu . Filename + '<STR_LIT>' ) <EOL> except xml . parsers . expat . ExpatError : <EOL> raise ParsingError ( '<STR_LIT>' , self . filename ) <EOL> self . __remove_whilespace_nodes ( self . doc ) <EOL> def save ( self ) : <EOL> self . __saveEntries ( self . menu ) <EOL> self . __saveMenu ( ) <EOL> def createMenuEntry ( self , parent , name , command = None , genericname = None , comment = None , icon = None , terminal = None , after = None , before = None ) : <EOL> menuentry = MenuEntry ( self . __getFileName ( name , "<STR_LIT>" ) ) <EOL> menuentry = self . editMenuEntry ( menuentry , name , genericname , comment , command , icon , terminal ) <EOL> self . __addEntry ( parent , menuentry , after , before ) <EOL> sort ( self . menu ) <EOL> return menuentry <EOL> def createMenu ( self , parent , name , genericname = None , comment = None , icon = None , after = None , before = None ) : <EOL> menu = Menu ( ) <EOL> menu . Parent = parent <EOL> menu . Depth = parent . Depth + <NUM_LIT:1> <EOL> menu . Layout = parent . DefaultLayout <EOL> menu . DefaultLayout = parent . DefaultLayout <EOL> menu = self . editMenu ( menu , name , genericname , comment , icon ) <EOL> self . __addEntry ( parent , menu , after , before ) <EOL> sort ( self . menu ) <EOL> return menu <EOL> def createSeparator ( self , parent , after = None , before = None ) : <EOL> separator = Separator ( parent ) <EOL> self . __addEntry ( parent , separator , after , before ) <EOL> sort ( self . menu ) <EOL> return separator <EOL> def moveMenuEntry ( self , menuentry , oldparent , newparent , after = None , before = None ) : <EOL> self . __deleteEntry ( oldparent , menuentry , after , before ) <EOL> self . __addEntry ( newparent , menuentry , after , before ) <EOL> sort ( self . menu ) <EOL> return menuentry <EOL> def moveMenu ( self , menu , oldparent , newparent , after = None , before = None ) : <EOL> self . __deleteEntry ( oldparent , menu , after , before ) <EOL> self . __addEntry ( newparent , menu , after , before ) <EOL> root_menu = self . __getXmlMenu ( self . menu . Name ) <EOL> if oldparent . getPath ( True ) != newparent . getPath ( True ) : <EOL> self . __addXmlMove ( root_menu , os . path . join ( oldparent . getPath ( True ) , menu . Name ) , os . path . join ( newparent . getPath ( True ) , menu . Name ) ) <EOL> sort ( self . menu ) <EOL> return menu <EOL> def moveSeparator ( self , separator , parent , after = None , before = None ) : <EOL> self . __deleteEntry ( parent , separator , after , before ) <EOL> self . __addEntry ( parent , separator , after , before ) <EOL> sort ( self . menu ) <EOL> return separator <EOL> def copyMenuEntry ( self , menuentry , oldparent , newparent , after = None , before = None ) : <EOL> self . __addEntry ( newparent , menuentry , after , before ) <EOL> sort ( self . menu ) <EOL> return menuentry <EOL> def editMenuEntry ( self , menuentry , name = None , genericname = None , comment = None , command = None , icon = None , terminal = None , nodisplay = None , hidden = None ) : <EOL> deskentry = menuentry . DesktopEntry <EOL> if name : <EOL> if not deskentry . hasKey ( "<STR_LIT:Name>" ) : <EOL> deskentry . set ( "<STR_LIT:Name>" , name ) <EOL> deskentry . set ( "<STR_LIT:Name>" , name , locale = True ) <EOL> if comment : <EOL> if not deskentry . hasKey ( "<STR_LIT>" ) : <EOL> deskentry . set ( "<STR_LIT>" , comment ) <EOL> deskentry . set ( "<STR_LIT>" , comment , locale = True ) <EOL> if genericname : <EOL> if not deskentry . hasKey ( "<STR_LIT>" ) : <EOL> deskentry . set ( "<STR_LIT>" , genericname ) <EOL> deskentry . set ( "<STR_LIT>" , genericname , locale = True ) <EOL> if command : <EOL> deskentry . set ( "<STR_LIT>" , command ) <EOL> if icon : <EOL> deskentry . set ( "<STR_LIT>" , icon ) <EOL> if terminal == True : <EOL> deskentry . set ( "<STR_LIT>" , "<STR_LIT:true>" ) <EOL> elif terminal == False : <EOL> deskentry . set ( "<STR_LIT>" , "<STR_LIT:false>" ) <EOL> if nodisplay == True : <EOL> deskentry . set ( "<STR_LIT>" , "<STR_LIT:true>" ) <EOL> elif nodisplay == False : <EOL> deskentry . set ( "<STR_LIT>" , "<STR_LIT:false>" ) <EOL> if hidden == True : <EOL> deskentry . set ( "<STR_LIT>" , "<STR_LIT:true>" ) <EOL> elif hidden == False : <EOL> deskentry . set ( "<STR_LIT>" , "<STR_LIT:false>" ) <EOL> menuentry . updateAttributes ( ) <EOL> if len ( menuentry . Parents ) > <NUM_LIT:0> : <EOL> sort ( self . menu ) <EOL> return menuentry <EOL> def editMenu ( self , menu , name = None , genericname = None , comment = None , icon = None , nodisplay = None , hidden = None ) : <EOL> if isinstance ( menu . Directory , MenuEntry ) and menu . Directory . Filename == "<STR_LIT>" : <EOL> xml_menu = self . __getXmlMenu ( menu . getPath ( True , True ) ) <EOL> self . __addXmlTextElement ( xml_menu , '<STR_LIT>' , menu . Name + "<STR_LIT>" ) <EOL> menu . Directory . setAttributes ( menu . Name + "<STR_LIT>" ) <EOL> elif not isinstance ( menu . Directory , MenuEntry ) : <EOL> if not name : <EOL> name = menu . Name <EOL> filename = self . __getFileName ( name , "<STR_LIT>" ) . replace ( "<STR_LIT:/>" , "<STR_LIT>" ) <EOL> if not menu . Name : <EOL> menu . Name = filename . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> xml_menu = self . __getXmlMenu ( menu . getPath ( True , True ) ) <EOL> self . __addXmlTextElement ( xml_menu , '<STR_LIT>' , filename ) <EOL> menu . Directory = MenuEntry ( filename ) <EOL> deskentry = menu . Directory . DesktopEntry <EOL> if name : <EOL> if not deskentry . hasKey ( "<STR_LIT:Name>" ) : <EOL> deskentry . set ( "<STR_LIT:Name>" , name ) <EOL> deskentry . set ( "<STR_LIT:Name>" , name , locale = True ) <EOL> if genericname : <EOL> if not deskentry . hasKey ( "<STR_LIT>" ) : <EOL> deskentry . set ( "<STR_LIT>" , genericname ) <EOL> deskentry . set ( "<STR_LIT>" , genericname , locale = True ) <EOL> if comment : <EOL> if not deskentry . hasKey ( "<STR_LIT>" ) : <EOL> deskentry . set ( "<STR_LIT>" , comment ) <EOL> deskentry . set ( "<STR_LIT>" , comment , locale = True ) <EOL> if icon : <EOL> deskentry . set ( "<STR_LIT>" , icon ) <EOL> if nodisplay == True : <EOL> deskentry . set ( "<STR_LIT>" , "<STR_LIT:true>" ) <EOL> elif nodisplay == False : <EOL> deskentry . set ( "<STR_LIT>" , "<STR_LIT:false>" ) <EOL> if hidden == True : <EOL> deskentry . set ( "<STR_LIT>" , "<STR_LIT:true>" ) <EOL> elif hidden == False : <EOL> deskentry . set ( "<STR_LIT>" , "<STR_LIT:false>" ) <EOL> menu . Directory . updateAttributes ( ) <EOL> if isinstance ( menu . Parent , Menu ) : <EOL> sort ( self . menu ) <EOL> return menu <EOL> def hideMenuEntry ( self , menuentry ) : <EOL> self . editMenuEntry ( menuentry , nodisplay = True ) <EOL> def unhideMenuEntry ( self , menuentry ) : <EOL> self . editMenuEntry ( menuentry , nodisplay = False , hidden = False ) <EOL> def hideMenu ( self , menu ) : <EOL> self . editMenu ( menu , nodisplay = True ) <EOL> def unhideMenu ( self , menu ) : <EOL> self . editMenu ( menu , nodisplay = False , hidden = False ) <EOL> xml_menu = self . __getXmlMenu ( menu . getPath ( True , True ) , False ) <EOL> for node in self . __getXmlNodesByName ( [ "<STR_LIT>" , "<STR_LIT>" ] , xml_menu ) : <EOL> node . parentNode . removeChild ( node ) <EOL> def deleteMenuEntry ( self , menuentry ) : <EOL> if self . getAction ( menuentry ) == "<STR_LIT>" : <EOL> self . __deleteFile ( menuentry . DesktopEntry . filename ) <EOL> for parent in menuentry . Parents : <EOL> self . __deleteEntry ( parent , menuentry ) <EOL> sort ( self . menu ) <EOL> return menuentry <EOL> def revertMenuEntry ( self , menuentry ) : <EOL> if self . getAction ( menuentry ) == "<STR_LIT>" : <EOL> self . __deleteFile ( menuentry . DesktopEntry . filename ) <EOL> menuentry . Original . Parents = [ ] <EOL> for parent in menuentry . Parents : <EOL> index = parent . Entries . index ( menuentry ) <EOL> parent . Entries [ index ] = menuentry . Original <EOL> index = parent . MenuEntries . index ( menuentry ) <EOL> parent . MenuEntries [ index ] = menuentry . Original <EOL> menuentry . Original . Parents . append ( parent ) <EOL> sort ( self . menu ) <EOL> return menuentry <EOL> def deleteMenu ( self , menu ) : <EOL> if self . getAction ( menu ) == "<STR_LIT>" : <EOL> self . __deleteFile ( menu . Directory . DesktopEntry . filename ) <EOL> self . __deleteEntry ( menu . Parent , menu ) <EOL> xml_menu = self . __getXmlMenu ( menu . getPath ( True , True ) ) <EOL> xml_menu . parentNode . removeChild ( xml_menu ) <EOL> sort ( self . menu ) <EOL> return menu <EOL> def revertMenu ( self , menu ) : <EOL> if self . getAction ( menu ) == "<STR_LIT>" : <EOL> self . __deleteFile ( menu . Directory . DesktopEntry . filename ) <EOL> menu . Directory = menu . Directory . Original <EOL> sort ( self . menu ) <EOL> return menu <EOL> def deleteSeparator ( self , separator ) : <EOL> self . __deleteEntry ( separator . Parent , separator , after = True ) <EOL> sort ( self . menu ) <EOL> return separator <EOL> """<STR_LIT>""" <EOL> def getAction ( self , entry ) : <EOL> if isinstance ( entry , Menu ) : <EOL> if not isinstance ( entry . Directory , MenuEntry ) : <EOL> return "<STR_LIT:none>" <EOL> elif entry . Directory . getType ( ) == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> elif entry . Directory . getType ( ) == "<STR_LIT>" and ( len ( entry . Submenus ) + len ( entry . MenuEntries ) ) == <NUM_LIT:0> : <EOL> return "<STR_LIT>" <EOL> elif isinstance ( entry , MenuEntry ) : <EOL> if entry . getType ( ) == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> elif entry . getType ( ) == "<STR_LIT>" : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return "<STR_LIT:none>" <EOL> return "<STR_LIT:none>" <EOL> def __saveEntries ( self , menu ) : <EOL> if not menu : <EOL> menu = self . menu <EOL> if isinstance ( menu . Directory , MenuEntry ) : <EOL> menu . Directory . save ( ) <EOL> for entry in menu . getEntries ( hidden = True ) : <EOL> if isinstance ( entry , MenuEntry ) : <EOL> entry . save ( ) <EOL> elif isinstance ( entry , Menu ) : <EOL> self . __saveEntries ( entry ) <EOL> def __saveMenu ( self ) : <EOL> if not os . path . isdir ( os . path . dirname ( self . filename ) ) : <EOL> os . makedirs ( os . path . dirname ( self . filename ) ) <EOL> fd = open ( self . filename , '<STR_LIT:w>' ) <EOL> fd . write ( re . sub ( "<STR_LIT>" , "<STR_LIT>" , self . doc . toprettyxml ( ) . replace ( '<STR_LIT>' , '<STR_LIT>' ) ) ) <EOL> fd . close ( ) <EOL> def __getFileName ( self , name , extension ) : <EOL> postfix = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> if postfix == <NUM_LIT:0> : <EOL> filename = name + extension <EOL> else : <EOL> filename = name + "<STR_LIT:->" + str ( postfix ) + extension <EOL> if extension == "<STR_LIT>" : <EOL> dir = "<STR_LIT>" <EOL> elif extension == "<STR_LIT>" : <EOL> dir = "<STR_LIT>" <EOL> if not filename in self . filenames and not os . path . isfile ( os . path . join ( xdg_data_dirs [ <NUM_LIT:0> ] , dir , filename ) ) : <EOL> self . filenames . append ( filename ) <EOL> break <EOL> else : <EOL> postfix += <NUM_LIT:1> <EOL> return filename <EOL> def __getXmlMenu ( self , path , create = True , element = None ) : <EOL> if not element : <EOL> element = self . doc <EOL> if "<STR_LIT:/>" in path : <EOL> ( name , path ) = path . split ( "<STR_LIT:/>" , <NUM_LIT:1> ) <EOL> else : <EOL> name = path <EOL> path = "<STR_LIT>" <EOL> found = None <EOL> for node in self . __getXmlNodesByName ( "<STR_LIT>" , element ) : <EOL> for child in self . __getXmlNodesByName ( "<STR_LIT:Name>" , node ) : <EOL> if child . childNodes [ <NUM_LIT:0> ] . nodeValue == name : <EOL> if path : <EOL> found = self . __getXmlMenu ( path , create , node ) <EOL> else : <EOL> found = node <EOL> break <EOL> if found : <EOL> break <EOL> if not found and create == True : <EOL> node = self . __addXmlMenuElement ( element , name ) <EOL> if path : <EOL> found = self . __getXmlMenu ( path , create , node ) <EOL> else : <EOL> found = node <EOL> return found <EOL> def __addXmlMenuElement ( self , element , name ) : <EOL> node = self . doc . createElement ( '<STR_LIT>' ) <EOL> self . __addXmlTextElement ( node , '<STR_LIT:Name>' , name ) <EOL> return element . appendChild ( node ) <EOL> def __addXmlTextElement ( self , element , name , text ) : <EOL> node = self . doc . createElement ( name ) <EOL> text = self . doc . createTextNode ( text ) <EOL> node . appendChild ( text ) <EOL> return element . appendChild ( node ) <EOL> def __addXmlFilename ( self , element , filename , type = "<STR_LIT>" ) : <EOL> for node in self . __getXmlNodesByName ( [ "<STR_LIT>" , "<STR_LIT>" ] , element ) : <EOL> if node . childNodes [ <NUM_LIT:0> ] . nodeName == "<STR_LIT>" and node . childNodes [ <NUM_LIT:0> ] . childNodes [ <NUM_LIT:0> ] . nodeValue == filename : <EOL> element . removeChild ( node ) <EOL> node = self . doc . createElement ( type ) <EOL> node . appendChild ( self . __addXmlTextElement ( node , '<STR_LIT>' , filename ) ) <EOL> return element . appendChild ( node ) <EOL> def __addXmlMove ( self , element , old , new ) : <EOL> node = self . doc . createElement ( "<STR_LIT>" ) <EOL> node . appendChild ( self . __addXmlTextElement ( node , '<STR_LIT>' , old ) ) <EOL> node . appendChild ( self . __addXmlTextElement ( node , '<STR_LIT>' , new ) ) <EOL> return element . appendChild ( node ) <EOL> def __addXmlLayout ( self , element , layout ) : <EOL> for node in self . __getXmlNodesByName ( "<STR_LIT>" , element ) : <EOL> element . removeChild ( node ) <EOL> node = self . doc . createElement ( "<STR_LIT>" ) <EOL> for order in layout . order : <EOL> if order [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> child = self . doc . createElement ( "<STR_LIT>" ) <EOL> node . appendChild ( child ) <EOL> elif order [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> child = self . __addXmlTextElement ( node , "<STR_LIT>" , order [ <NUM_LIT:1> ] ) <EOL> elif order [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> child = self . __addXmlTextElement ( node , "<STR_LIT>" , order [ <NUM_LIT:1> ] ) <EOL> elif order [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> child = self . doc . createElement ( "<STR_LIT>" ) <EOL> child . setAttribute ( "<STR_LIT:type>" , order [ <NUM_LIT:1> ] ) <EOL> node . appendChild ( child ) <EOL> return element . appendChild ( node ) <EOL> def __getXmlNodesByName ( self , name , element ) : <EOL> for child in element . childNodes : <EOL> if child . nodeType == xml . dom . Node . ELEMENT_NODE and child . nodeName in name : <EOL> yield child <EOL> def __addLayout ( self , parent ) : <EOL> layout = Layout ( ) <EOL> layout . order = [ ] <EOL> layout . show_empty = parent . Layout . show_empty <EOL> layout . inline = parent . Layout . inline <EOL> layout . inline_header = parent . Layout . inline_header <EOL> layout . inline_alias = parent . Layout . inline_alias <EOL> layout . inline_limit = parent . Layout . inline_limit <EOL> layout . order . append ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> for entry in parent . Entries : <EOL> if isinstance ( entry , Menu ) : <EOL> layout . parseMenuname ( entry . Name ) <EOL> elif isinstance ( entry , MenuEntry ) : <EOL> layout . parseFilename ( entry . DesktopFileID ) <EOL> elif isinstance ( entry , Separator ) : <EOL> layout . parseSeparator ( ) <EOL> layout . order . append ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> parent . Layout = layout <EOL> return layout <EOL> def __addEntry ( self , parent , entry , after = None , before = None ) : <EOL> if after or before : <EOL> if after : <EOL> index = parent . Entries . index ( after ) + <NUM_LIT:1> <EOL> elif before : <EOL> index = parent . Entries . index ( before ) <EOL> parent . Entries . insert ( index , entry ) <EOL> else : <EOL> parent . Entries . append ( entry ) <EOL> xml_parent = self . __getXmlMenu ( parent . getPath ( True , True ) ) <EOL> if isinstance ( entry , MenuEntry ) : <EOL> parent . MenuEntries . append ( entry ) <EOL> entry . Parents . append ( parent ) <EOL> self . __addXmlFilename ( xml_parent , entry . DesktopFileID , "<STR_LIT>" ) <EOL> elif isinstance ( entry , Menu ) : <EOL> parent . addSubmenu ( entry ) <EOL> if after or before : <EOL> self . __addLayout ( parent ) <EOL> self . __addXmlLayout ( xml_parent , parent . Layout ) <EOL> def __deleteEntry ( self , parent , entry , after = None , before = None ) : <EOL> parent . Entries . remove ( entry ) <EOL> xml_parent = self . __getXmlMenu ( parent . getPath ( True , True ) ) <EOL> if isinstance ( entry , MenuEntry ) : <EOL> entry . Parents . remove ( parent ) <EOL> parent . MenuEntries . remove ( entry ) <EOL> self . __addXmlFilename ( xml_parent , entry . DesktopFileID , "<STR_LIT>" ) <EOL> elif isinstance ( entry , Menu ) : <EOL> parent . Submenus . remove ( entry ) <EOL> if after or before : <EOL> self . __addLayout ( parent ) <EOL> self . __addXmlLayout ( xml_parent , parent . Layout ) <EOL> def __deleteFile ( self , filename ) : <EOL> try : <EOL> os . remove ( filename ) <EOL> except OSError : <EOL> pass <EOL> try : <EOL> self . filenames . remove ( filename ) <EOL> except ValueError : <EOL> pass <EOL> def __remove_whilespace_nodes ( self , node ) : <EOL> remove_list = [ ] <EOL> for child in node . childNodes : <EOL> if child . nodeType == xml . dom . minidom . Node . TEXT_NODE : <EOL> child . data = child . data . strip ( ) <EOL> if not child . data . strip ( ) : <EOL> remove_list . append ( child ) <EOL> elif child . hasChildNodes ( ) : <EOL> self . __remove_whilespace_nodes ( child ) <EOL> for node in remove_list : <EOL> node . parentNode . removeChild ( node ) </s>
