def __init__(self, gtk_window):
self._window = gtk_window
self._hwnd = gtk_window.window.handle
self._message_map = {}
# Windows transparency is only supported windows2000 and above.
if sys.getwindowsversion()[0] <= 4:
self.alpha = None
else:
self.alpha = 100
self._transparency = False
self.notify_icon = None
# Sublass the window and inject a WNDPROC to process messages.
self._oldwndproc = win32gui.SetWindowLong(self._hwnd, GWL_WNDPROC,
self._wndproc)
gtk_window.connect('unrealize', self.remove)
python类getwindowsversion()的实例源码
def encodeFilename(s, for_subprocess=False):
"""
@param s The name of the file
"""
assert type(s) == compat_str
# Python 3 has a Unicode API
if sys.version_info >= (3, 0):
return s
# Pass '' directly to use Unicode APIs on Windows 2000 and up
# (Detecting Windows NT 4 is tricky because 'major >= 4' would
# match Windows 9x series as well. Besides, NT 4 is obsolete.)
if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
return s
return s.encode(get_subprocess_encoding(), 'ignore')
def UseCommandLine(*classes, **flags):
unregisterInfo = '--unregister_info' in sys.argv
unregister = '--unregister' in sys.argv
flags['quiet'] = flags.get('quiet',0) or '--quiet' in sys.argv
flags['debug'] = flags.get('debug',0) or '--debug' in sys.argv
flags['unattended'] = flags.get('unattended',0) or '--unattended' in sys.argv
if unregisterInfo:
return UnregisterInfoClasses(*classes, **flags)
try:
if unregister:
UnregisterClasses(*classes, **flags)
else:
RegisterClasses(*classes, **flags)
except win32api.error, exc:
# If we are on xp+ and have "access denied", retry using
# ShellExecuteEx with 'runas' verb to force elevation (vista) and/or
# admin login dialog (vista/xp)
if flags['unattended'] or exc.winerror != winerror.ERROR_ACCESS_DENIED \
or sys.getwindowsversion()[0] < 5:
raise
ReExecuteElevated(flags)
def encodeFilename(s, for_subprocess=False):
"""
@param s The name of the file
"""
assert type(s) == compat_str
# Python 3 has a Unicode API
if sys.version_info >= (3, 0):
return s
# Pass '' directly to use Unicode APIs on Windows 2000 and up
# (Detecting Windows NT 4 is tricky because 'major >= 4' would
# match Windows 9x series as well. Besides, NT 4 is obsolete.)
if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
return s
# Jython assumes filenames are Unicode strings though reported as Python 2.x compatible
if sys.platform.startswith('java'):
return s
return s.encode(get_subprocess_encoding(), 'ignore')
def UseCommandLine(*classes, **flags):
unregisterInfo = '--unregister_info' in sys.argv
unregister = '--unregister' in sys.argv
flags['quiet'] = flags.get('quiet',0) or '--quiet' in sys.argv
flags['debug'] = flags.get('debug',0) or '--debug' in sys.argv
flags['unattended'] = flags.get('unattended',0) or '--unattended' in sys.argv
if unregisterInfo:
return UnregisterInfoClasses(*classes, **flags)
try:
if unregister:
UnregisterClasses(*classes, **flags)
else:
RegisterClasses(*classes, **flags)
except win32api.error, exc:
# If we are on xp+ and have "access denied", retry using
# ShellExecuteEx with 'runas' verb to force elevation (vista) and/or
# admin login dialog (vista/xp)
if flags['unattended'] or exc.winerror != winerror.ERROR_ACCESS_DENIED \
or sys.getwindowsversion()[0] < 5:
raise
ReExecuteElevated(flags)
def encodeFilename(s, for_subprocess=False):
"""
@param s The name of the file
"""
assert type(s) == compat_str
# Python 3 has a Unicode API
if sys.version_info >= (3, 0):
return s
# Pass '' directly to use Unicode APIs on Windows 2000 and up
# (Detecting Windows NT 4 is tricky because 'major >= 4' would
# match Windows 9x series as well. Besides, NT 4 is obsolete.)
if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
return s
# Jython assumes filenames are Unicode strings though reported as Python 2.x compatible
if sys.platform.startswith('java'):
return s
return s.encode(get_subprocess_encoding(), 'ignore')
def encodeFilename(s, for_subprocess=False):
"""
@param s The name of the file
"""
assert type(s) == compat_str
# Python 3 has a Unicode API
if sys.version_info >= (3, 0):
return s
# Pass '' directly to use Unicode APIs on Windows 2000 and up
# (Detecting Windows NT 4 is tricky because 'major >= 4' would
# match Windows 9x series as well. Besides, NT 4 is obsolete.)
if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
return s
# Jython assumes filenames are Unicode strings though reported as Python 2.x compatible
if sys.platform.startswith('java'):
return s
return s.encode(get_subprocess_encoding(), 'ignore')
def get_winver():
wv = sys.getwindowsversion()
if hasattr(wv, 'service_pack_major'): # python >= 2.7
sp = wv.service_pack_major or 0
else:
r = re.search("\s\d$", wv[4])
if r:
sp = int(r.group(0))
else:
sp = 0
return (wv[0], wv[1], sp)
# ===================================================================
# --- sync primitives
# ===================================================================
def getlanguage(self, language=None, windowsversion=None):
"""
Get and return the manifest's language as string.
Can be either language-culture e.g. 'en-us' or a string indicating
language neutrality, e.g. 'x-ww' on Windows XP or 'none' on Vista
and later.
"""
if not language:
language = self.language
if language in (None, "", "*", "neutral"):
return (LANGUAGE_NEUTRAL_NT5,
LANGUAGE_NEUTRAL_NT6)[(windowsversion or
sys.getwindowsversion()) >= (6, )]
return language
def encodeFilename(s, for_subprocess=False):
"""
@param s The name of the file
"""
assert type(s) == compat_str
# Python 3 has a Unicode API
if sys.version_info >= (3, 0):
return s
# Pass '' directly to use Unicode APIs on Windows 2000 and up
# (Detecting Windows NT 4 is tricky because 'major >= 4' would
# match Windows 9x series as well. Besides, NT 4 is obsolete.)
if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
return s
# Jython assumes filenames are Unicode strings though reported as Python 2.x compatible
if sys.platform.startswith('java'):
return s
return s.encode(get_subprocess_encoding(), 'ignore')
def encodeFilename(s, for_subprocess=False):
"""
@param s The name of the file
"""
assert type(s) == compat_str
# Python 3 has a Unicode API
if sys.version_info >= (3, 0):
return s
# Pass '' directly to use Unicode APIs on Windows 2000 and up
# (Detecting Windows NT 4 is tricky because 'major >= 4' would
# match Windows 9x series as well. Besides, NT 4 is obsolete.)
if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
return s
# Jython assumes filenames are Unicode strings though reported as Python 2.x compatible
if sys.platform.startswith('java'):
return s
return s.encode(get_subprocess_encoding(), 'ignore')
def UseCommandLine(*classes, **flags):
unregisterInfo = '--unregister_info' in sys.argv
unregister = '--unregister' in sys.argv
flags['quiet'] = flags.get('quiet',0) or '--quiet' in sys.argv
flags['debug'] = flags.get('debug',0) or '--debug' in sys.argv
flags['unattended'] = flags.get('unattended',0) or '--unattended' in sys.argv
if unregisterInfo:
return UnregisterInfoClasses(*classes, **flags)
try:
if unregister:
UnregisterClasses(*classes, **flags)
else:
RegisterClasses(*classes, **flags)
except win32api.error, exc:
# If we are on xp+ and have "access denied", retry using
# ShellExecuteEx with 'runas' verb to force elevation (vista) and/or
# admin login dialog (vista/xp)
if flags['unattended'] or exc.winerror != winerror.ERROR_ACCESS_DENIED \
or sys.getwindowsversion()[0] < 5:
raise
ReExecuteElevated(flags)
def UseCommandLine(*classes, **flags):
unregisterInfo = '--unregister_info' in sys.argv
unregister = '--unregister' in sys.argv
flags['quiet'] = flags.get('quiet',0) or '--quiet' in sys.argv
flags['debug'] = flags.get('debug',0) or '--debug' in sys.argv
flags['unattended'] = flags.get('unattended',0) or '--unattended' in sys.argv
if unregisterInfo:
return UnregisterInfoClasses(*classes, **flags)
try:
if unregister:
UnregisterClasses(*classes, **flags)
else:
RegisterClasses(*classes, **flags)
except win32api.error as exc:
# If we are on xp+ and have "access denied", retry using
# ShellExecuteEx with 'runas' verb to force elevation (vista) and/or
# admin login dialog (vista/xp)
if flags['unattended'] or exc.winerror != winerror.ERROR_ACCESS_DENIED \
or sys.getwindowsversion()[0] < 5:
raise
ReExecuteElevated(flags)
def encodeFilename(s, for_subprocess=False):
"""
@param s The name of the file
"""
assert type(s) == compat_str
# Python 3 has a Unicode API
if sys.version_info >= (3, 0):
return s
# Pass '' directly to use Unicode APIs on Windows 2000 and up
# (Detecting Windows NT 4 is tricky because 'major >= 4' would
# match Windows 9x series as well. Besides, NT 4 is obsolete.)
if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
return s
# Jython assumes filenames are Unicode strings though reported as Python 2.x compatible
if sys.platform.startswith('java'):
return s
return s.encode(get_subprocess_encoding(), 'ignore')
def encodeFilename(s, for_subprocess=False):
"""
@param s The name of the file
"""
assert type(s) == compat_str
# Python 3 has a Unicode API
if sys.version_info >= (3, 0):
return s
# Pass '' directly to use Unicode APIs on Windows 2000 and up
# (Detecting Windows NT 4 is tricky because 'major >= 4' would
# match Windows 9x series as well. Besides, NT 4 is obsolete.)
if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
return s
# Jython assumes filenames are Unicode strings though reported as Python 2.x compatible
if sys.platform.startswith('java'):
return s
return s.encode(get_subprocess_encoding(), 'ignore')
def get_winver():
wv = sys.getwindowsversion()
if hasattr(wv, 'service_pack_major'): # python >= 2.7
sp = wv.service_pack_major or 0
else:
r = re.search("\s\d$", wv[4])
if r:
sp = int(r.group(0))
else:
sp = 0
return (wv[0], wv[1], sp)
# ===================================================================
# --- sync primitives
# ===================================================================
def encodeFilename(s, for_subprocess=False):
"""
@param s The name of the file
"""
assert type(s) == compat_str
# Python 3 has a Unicode API
if sys.version_info >= (3, 0):
return s
# Pass '' directly to use Unicode APIs on Windows 2000 and up
# (Detecting Windows NT 4 is tricky because 'major >= 4' would
# match Windows 9x series as well. Besides, NT 4 is obsolete.)
if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
return s
# Jython assumes filenames are Unicode strings though reported as Python 2.x compatible
if sys.platform.startswith('java'):
return s
return s.encode(get_subprocess_encoding(), 'ignore')
def get_winver():
wv = sys.getwindowsversion()
if hasattr(wv, 'service_pack_major'): # python >= 2.7
sp = wv.service_pack_major or 0
else:
r = re.search("\s\d$", wv[4])
if r:
sp = int(r.group(0))
else:
sp = 0
return (wv[0], wv[1], sp)
# ===================================================================
# --- sync primitives
# ===================================================================
def encodeFilename(s, for_subprocess=False):
"""
@param s The name of the file
"""
assert type(s) == compat_str
# Python 3 has a Unicode API
if sys.version_info >= (3, 0):
return s
# Pass '' directly to use Unicode APIs on Windows 2000 and up
# (Detecting Windows NT 4 is tricky because 'major >= 4' would
# match Windows 9x series as well. Besides, NT 4 is obsolete.)
if not for_subprocess and sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
return s
# Jython assumes filenames are Unicode strings though reported as Python 2.x compatible
if sys.platform.startswith('java'):
return s
return s.encode(get_subprocess_encoding(), 'ignore')
def getlanguage(self, language=None, windowsversion=None):
"""
Get and return the manifest's language as string.
Can be either language-culture e.g. 'en-us' or a string indicating
language neutrality, e.g. 'x-ww' on Windows XP or 'none' on Vista
and later.
"""
if not language:
language = self.language
if language in (None, "", "*", "neutral"):
return (LANGUAGE_NEUTRAL_NT5,
LANGUAGE_NEUTRAL_NT6)[(windowsversion or
sys.getwindowsversion()) >= (6, )]
return language
def os_id_index():
"""Get the index of the machine OS in the `OS_ID` tuple.
The `OS_ID` tuple contains the major and minor version of all
affected Windows versions. These are matched against
the major and minor version of `sys.getwindowsversion()`.
For Windows 8.1 and above `sys.getwindowsversion()` doesn't
report the correct value, these systems are handled specially.
Windows 8 and Server 2012 are special cased because the have the
same version numbers but require different KBs.
:return: The index of the operating system in `OS_ID`.
:rtype: int
"""
winver = sys.getwindowsversion()
# sys.getwindowsversion is not enough by itself as the underlying
# API has been deprecated. Only applications which have been
# developed specifically for Windows 8.1 and above, and write that
# into their manifest file get the correct Windows version on those
# systems. Other applications (Python doesn't have the manifest)
# get a version that pretends to be Windows 8 (major=6, minor=2).
# See:
# https://msdn.microsoft.com/en-us/library/windows/desktop/ms724834.aspx
major, minor = winver.major, winver.minor
if (major, minor) == (6, 2):
# Determine if this system is a newer version than Windows 8 by
# parsing the version string in `platform.win32_ver()[1]`:
major, minor = tuple(map(int, platform.win32_ver()[1].split('.')[:2]))
for i, os_id in enumerate(OS_ID):
if os_id[:2] == (major, minor):
if len(os_id) == 2:
return i
# else evaluate the third item if present which is a lambda:
if os_id[2]():
return i
# otherwise continue with the next item
def __load_functions(modules):
import platform
import struct
# Ensure we're on Windows
assert 'windows' in platform.system().lower()
void_ptr_bits = struct.calcsize('P') * 8
winver = sys.getwindowsversion()
environ = {
'32-bit': void_ptr_bits == 32,
'64-bit': void_ptr_bits == 64,
'release': platform.release(),
'version': platform.version(),
'system': platform.system(),
'version-major': winver.major,
'version-minor': winver.minor,
'version-build': winver.build,
'version-platform': winver.platform,
'version-service_pack': winver.service_pack,
}
ret = {}
for name in modules:
if isinstance(name, str):
try:
mod = importlib.import_module(name, __name__)
except:
print("Problem loading module " + name)
raise
else:
mod = name
mod.load_functions(environ, ret)
return ret
def __init__(self):
self._user = None # User-specified default encoding
self._learned = [] # Learned default encodings
self._widefiles = False # File system can be wide
# Can the file system be Unicode?
try:
self._widefiles = os.path.supports_unicode_filenames
except AttributeError:
try:
self._widefiles = sys.getwindowsversion() == os.VER_PLATFORM_WIN32_NT
except AttributeError:
pass
# Try to guess a working default
try:
encoding = sys.getfilesystemencoding()
if encoding and not (encoding.upper() in ENC_ASCII_LIST):
self._learned = [ encoding ]
except AttributeError:
pass
if not self._learned:
encoding = sys.getdefaultencoding()
if encoding and not (encoding.upper() in ENC_ASCII_LIST):
self._learned = [ encoding ]
# If we had no guesses, start with some European defaults
if not self._learned:
self._learned = ENC_DEFAULT_LIST
#end def __init__
def get_subprocess_encoding():
if sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
# For subprocess calls, encode with locale encoding
# Refer to http://stackoverflow.com/a/9951851/35070
encoding = preferredencoding()
else:
encoding = sys.getfilesystemencoding()
if encoding is None:
encoding = 'utf-8'
return encoding
def DllRegisterServer():
import _winreg
if sys.getwindowsversion()[0] < 6:
print "This sample only works on Vista"
sys.exit(1)
key = _winreg.CreateKey(_winreg.HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\" \
"Explorer\\Desktop\\Namespace\\" + \
ShellFolder._reg_clsid_)
_winreg.SetValueEx(key, None, 0, _winreg.REG_SZ, ShellFolder._reg_desc_)
# And special shell keys under our CLSID
key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT,
"CLSID\\" + ShellFolder._reg_clsid_ + "\\ShellFolder")
# 'Attributes' is an int stored as a binary! use struct
attr = shellcon.SFGAO_FOLDER | shellcon.SFGAO_HASSUBFOLDER | \
shellcon.SFGAO_BROWSABLE
import struct
s = struct.pack("i", attr)
_winreg.SetValueEx(key, "Attributes", 0, _winreg.REG_BINARY, s)
# register the context menu handler under the FolderViewSampleType type.
keypath = "%s\\shellex\\ContextMenuHandlers\\%s" % (ContextMenu._context_menu_type_, ContextMenu._reg_desc_)
key = _winreg.CreateKey(_winreg.HKEY_CLASSES_ROOT, keypath)
_winreg.SetValueEx(key, None, 0, _winreg.REG_SZ, ContextMenu._reg_clsid_)
propsys.PSRegisterPropertySchema(get_schema_fname())
print ShellFolder._reg_desc_, "registration complete."
def set_context(self):
self.tags_context(
dict(
frozen=constant.FROZEN,
platform=sys.platform,
release_name=constant.APP_RELEASE_NAME,
)
)
try:
self.tags_context(dict(windows_version=sys.getwindowsversion()))
except AttributeError:
pass
def get_subprocess_encoding():
if sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
# For subprocess calls, encode with locale encoding
# Refer to http://stackoverflow.com/a/9951851/35070
encoding = preferredencoding()
else:
encoding = sys.getfilesystemencoding()
if encoding is None:
encoding = 'utf-8'
return encoding
def get_subprocess_encoding():
if sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
# For subprocess calls, encode with locale encoding
# Refer to http://stackoverflow.com/a/9951851/35070
encoding = preferredencoding()
else:
encoding = sys.getfilesystemencoding()
if encoding is None:
encoding = 'utf-8'
return encoding
def isWindows():
if getattr(sys, "getwindowsversion", None) is not None:
return sys.getwindowsversion()[0] == 6
else:
return False
def get_subprocess_encoding():
if sys.platform == 'win32' and sys.getwindowsversion()[0] >= 5:
# For subprocess calls, encode with locale encoding
# Refer to http://stackoverflow.com/a/9951851/35070
encoding = preferredencoding()
else:
encoding = sys.getfilesystemencoding()
if encoding is None:
encoding = 'utf-8'
return encoding