Code: Alles auswählen
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
"""
Dateiverknüpfungen Anzeigen und setzten unter Windows.
Nutzt:
reg.exe, assoc.exe
Hilfe zum Thema:
http://www.google.de/search?hl=de&as_qdr=all&q=%22Befehlszeilenreferenz+AZ%22+site%3Amicrosoft.com&btnG=Suche&meta=lr%3Dlang_de
http://www.microsoft.com/technet/prodtechnol/windowsserver2003/de/library/ServerHelp/552ed70a-208d-48c4-8da8-2e27b530eac7.mspx?mfr=true
"""
import os, sys, subprocess
#_____________________________________________________________________________
SIMULATION = False
#~ SIMULATION = True
DEBUG = False
#~ DEBUG = True
#_____________________________________________________________________________
def subprocess2(command, cwd=None):
"""
Wenn process.stdout.read() blockiert, ist der subprocess noch nicht
beendet. Evtl. wartet er auf eingaben?
Hilfreich kann hierbei "print process.stdout.readline()" sein ;)
"""
if DEBUG:
print "-"*79
print command
print "-"*79
if SIMULATION:
print "(cmd simulation only)"
return
try:
process = subprocess.Popen(
command,
bufsize = 1,
cwd = cwd,
shell = True,
stdout = subprocess.PIPE,
stderr = subprocess.STDOUT
)
except Exception, e:
print "subprocess Error:", e
sys.exit()
#~ print process.stdout.readline()
# Ausgaben
output = process.stdout.read()
return output.strip()
#_____________________________________________________________________________
def reg_add(key, value, reg_type="REG_SZ"):
cmd = 'reg add "%s" /f /ve /t %s /d "%s"' % (key, reg_type, value)
subprocess2(cmd)
def reg_delete(key):
cmd = 'reg delete "%s" /f' % key
subprocess2(cmd)
def reg_query(key, subtree=False):
cmd = 'reg query "%s"' % key
if subtree:
cmd += " /s"
result = subprocess2(cmd)
result = result.split(os.linesep)
result = os.linesep.join(result[2:])
return result
#_____________________________________________________________________________
def set_filetype(ext, filetype):
key = "HKCR\%s" % ext
reg_add(key, filetype)
def set_filetype_command(filetype, shelltype, command):
key = "HKCR\%s\shell\%s\command" % (filetype, shelltype)
reg_add(key, command)
def file_assoc(ext, filetype, shelltype, command):
"""
Verknüpft eine Endung mit einem Dateitypen und setzt dabei
eine shell-type mit command.
Beispiele:
file_assoc(".py", "Python.File", "open", 'c:\python\python.exe "%1" %*')
"""
assert ext.startswith(".")
assert shelltype in ("open", "edit")
filetype = filetype.replace(" ", "_") # Darf keine Leerzeichen haben (?)
set_filetype(ext, filetype)
set_filetype_command(filetype, shelltype, command)
print
#_____________________________________________________________________________
def del_file_assoc(ext):
"""
Löscht eine Dateiendung
"""
assert ext.startswith(".")
key = "HKCR\%s" % ext
reg_delete(key)
def del_filetype(filetype):
"""
Löscht einen Dateitypen Eintrag *komplett*
"""
key = "HKCR\%s" % filetype
reg_delete(key)
#_____________________________________________________________________________
def get_filetype(ext):
"""
Liefert den Dateityp anhand der Endung zurück
"""
cmd = 'assoc %s' % ext
result = subprocess2(cmd)
try:
return result.split("=")[1]
except IndexError, e:
print "Error:", e
print result
def display_shell_commands(filetype):
"""
Listet vorhandene shell-Kommandos zu einem Dateityp auf.
"""
key = "HKCR\%s\shell" % filetype
result = reg_query(key, subtree=True)
result = result.split(os.linesep+os.linesep)
for line in result:
if not "command" in line:
continue
print line
print
def file_info(ext):
"""
Zeigt Informationen zu einer Dateiendung.
"""
print "\n-----[Fileinfo for '%s']-----" % ext
assert ext.startswith(".")
filetype = get_filetype(ext)
print "'%s' files are associated with filetype '%s'" % (ext, filetype)
print
display_shell_commands(filetype)
print "-"*79
#_____________________________________________________________________________
if __name__ == "__main__":
print "Neue Endung einfügen."
file_assoc(".000", "AAA test", "open", "python %1")
file_info(".000")
print "\nDaten wieder löschen."
del_file_assoc(".000")
del_filetype("AAA test")
file_info(".000")
file_info(".py")