Et voilà (merci strings :-D)
Bon la mise en page a un peu souffert (strings ne renvoi que les lignes de plus de 4 caractères, donc adieu les sauts de lignes) mais il s'exécute correctement.
#!/usr/bin/python
import os
import xml.dom.minidom
target_file_name = 1
target_item_name = 2
target_icon_path = 3
def acceptable ( char, mask ) :
acceptable_chars = [ 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x22, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x00, 0x3F, 0x3F, 0x22,\
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x00, 0x00, 0x22, 0x00, 0x22,\
0x00, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,\
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x3F,\
0x00, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,\
0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x00, 0x00, 0x3F, 0x00 ]
n = ord ( char )
return n >= 32 and n < 128 and acceptable_chars [ n - 32 ] & mask
def escape_string ( string, target ) :
hex_escape = '%'
hex_chars = "0123456789ABCDEF"
rstring = ""
if target == target_icon_path :
mask = 0x02
else :
mask = 0x01
for char in string :
n = ord ( char )
if acceptable ( char, mask ) :
rstring = rstring + char
else :
if char != '/' and target == target_file_name :
rstring = rstring + hex_escape + "25" + hex_chars [ n >> 4 ] + hex_chars [ n & 15 ]
else :
rstring = rstring + hex_escape + hex_chars [ n >> 4 ] + hex_chars [ n & 15 ]
return rstring
def get_metafile_name_from_path ( path ) :
filename = os.path.split ( path ) [ 0 ]
filename = escape_string ( filename, target_file_name )
filename = "file:%2F%2F" + filename + ".xml"
return filename
def get_item_name_from_path ( path ) :
itemname = os.path.split ( path ) [ 1 ]
itemname = escape_string ( itemname, target_item_name )
return itemname
def write_xml ( data, path ) :
xml_metafile = open ( path, "w" )
xml_metafile.write ( data )
xml_metafile.close ()
def set_icon ( path ) :
xml_file_path = os.path.expanduser ( "~/.nautilus/metafiles/" ) + get_metafile_name_from_path ( path )
xml_item_name = get_item_name_from_path ( path )
print ( "Setting icon for: " + path )
icon_path = "file://" + escape_string ( path + "/folder.png", target_icon_path )
if os.path.exists ( xml_file_path ) :
xml_object = xml.dom.minidom.parse ( xml_file_path )
for item in xml_object.getElementsByTagName( "file" ) :
if item.getAttribute ( "name" ) == xml_item_name :
item.setAttribute ( "custom_icon", icon_path )
write_xml ( xml_object.toxml (), xml_file_path )
break;
else :
xml_directory_node = xml_object.documentElement
xml_file_node = xml_object.createElement ( "file" )
xml_file_node.setAttribute ( "name", xml_item_name )
xml_file_node.setAttribute ( "custom_icon", icon_path )
xml_directory_node.appendChild ( xml_file_node )
write_xml ( xml_object.toxml (), xml_file_path )
else :
xml_object = xml.dom.minidom.getDOMImplementation().createDocument ( None, "directory", None )
xml_directory_node = xml_object.documentElement
xml_file_node = xml_object.createElement ( "file" )
xml_file_node.setAttribute ( "name", xml_item_name )
xml_file_node.setAttribute ( "custom_icon", icon_path )
xml_directory_node.appendChild ( xml_file_node )
write_xml ( xml_object.toxml (), xml_file_path )
def find_files ( args, path, filenames ) :
if os.path.exists ( path + "/folder.png" ) and os.path.isfile ( path + "/folder.png" ) :
set_icon ( path )
print ( "starting script" )
os.path.walk ( os.path.abspath ( "./" ), find_files, None )
print ( "done" )
Le script analyse récursivement tous les sous dossiers à partir du dossier d'exécution, et si il trouve un folder.png (a modifier pour adapter à votre cas) il le défini comme icône du dossier qui le contient.
Résultat: