def mimeopen(filename): """mimeopen a filter_func open a file and supply enough programs params: filename - include the file extention """ import re match = re.search(r'.+(\.[\w]+)', filename) if match: ext = match.group(1) proginfo = get_all_program(ext) if proginfo: choice = query_user_choice(proginfo) command = ExpandEnvironmentStrings(proginfo[choice][2]) match = re.search(r'\"(.+\.exe)\"', command[0]) error = execute_program(match.group(1), filename) if error: print 'Error happens when execute the program.' else: print 'No proper program found.'
def set_user_editor(ext, progid, command): """set the default editor for the user this method rely on the program path params: ext - file ext progid - progid progpath - program execute path """ # must use the expand winreg string, if not, you may not have access # to the app and you need to use REG_EXPAND_SZ value type command = ExpandEnvironmentStrings(command) sub_key = '\\'.join(['Software', 'Classes', ext]) key = CreateKeyEx(HKEY_CURRENT_USER, sub_key, 0, KEY_SET_VALUE) SetValueEx(key, None, 0, REG_SZ, progid) sub_key = '\\'.join( ['Software', 'Classes', progid, 'shell', 'edit', 'command']) key = CreateKeyEx(HKEY_CURRENT_USER, sub_key, 0, KEY_SET_VALUE) SetValueEx(key, None, 0, REG_SZ, command)
def test_expand_environment_string(self): from _winreg import ExpandEnvironmentStrings import nt r = ExpandEnvironmentStrings(u"%windir%\\test") assert isinstance(r, unicode) if 'WINDIR' in nt.environ.keys(): assert r == nt.environ["WINDIR"] + "\\test" else: assert r == nt.environ["windir"] + "\\test"