home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Calibre / calibre-0.8.18.msi / file_262 / win32evtlogutil.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-09-09  |  3.9 KB  |  100 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.7)
  3.  
  4. import win32api
  5. import win32con
  6. import winerror
  7. import win32evtlog
  8. error = win32api.error
  9. langid = win32api.MAKELANGID(win32con.LANG_NEUTRAL, win32con.SUBLANG_NEUTRAL)
  10.  
  11. def AddSourceToRegistry(appName, msgDLL = None, eventLogType = 'Application', eventLogFlags = None):
  12.     if msgDLL is None:
  13.         msgDLL = win32evtlog.__file__
  14.     hkey = win32api.RegCreateKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s' % (eventLogType, appName))
  15.     win32api.RegSetValueEx(hkey, 'EventMessageFile', 0, win32con.REG_EXPAND_SZ, msgDLL)
  16.     if eventLogFlags is None:
  17.         eventLogFlags = win32evtlog.EVENTLOG_ERROR_TYPE | win32evtlog.EVENTLOG_WARNING_TYPE | win32evtlog.EVENTLOG_INFORMATION_TYPE
  18.     win32api.RegSetValueEx(hkey, 'TypesSupported', 0, win32con.REG_DWORD, eventLogFlags)
  19.     win32api.RegCloseKey(hkey)
  20.  
  21.  
  22. def RemoveSourceFromRegistry(appName, eventLogType = 'Application'):
  23.     
  24.     try:
  25.         win32api.RegDeleteKey(win32con.HKEY_LOCAL_MACHINE, 'SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s' % (eventLogType, appName))
  26.     except win32api.error:
  27.         exc = None
  28.         if exc.winerror != winerror.ERROR_FILE_NOT_FOUND:
  29.             raise 
  30.  
  31.  
  32.  
  33. def ReportEvent(appName, eventID, eventCategory = 0, eventType = win32evtlog.EVENTLOG_ERROR_TYPE, strings = None, data = None, sid = None):
  34.     hAppLog = win32evtlog.RegisterEventSource(None, appName)
  35.     win32evtlog.ReportEvent(hAppLog, eventType, eventCategory, eventID, sid, strings, data)
  36.     win32evtlog.DeregisterEventSource(hAppLog)
  37.  
  38.  
  39. def FormatMessage(eventLogRecord, logType = 'Application'):
  40.     keyName = 'SYSTEM\\CurrentControlSet\\Services\\EventLog\\%s\\%s' % (logType, eventLogRecord.SourceName)
  41.     handle = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyName)
  42.     
  43.     try:
  44.         dllNames = win32api.RegQueryValueEx(handle, 'EventMessageFile')[0].split(';')
  45.         data = None
  46.         for dllName in dllNames:
  47.             
  48.             try:
  49.                 dllName = win32api.ExpandEnvironmentStrings(dllName)
  50.                 dllHandle = win32api.LoadLibraryEx(dllName, 0, win32con.LOAD_LIBRARY_AS_DATAFILE)
  51.                 
  52.                 try:
  53.                     data = win32api.FormatMessageW(win32con.FORMAT_MESSAGE_FROM_HMODULE, dllHandle, eventLogRecord.EventID, langid, eventLogRecord.StringInserts)
  54.                 finally:
  55.                     win32api.FreeLibrary(dllHandle)
  56.  
  57.             except win32api.error:
  58.                 pass
  59.  
  60.             if data is not None:
  61.                 break
  62.                 continue
  63.     finally:
  64.         win32api.RegCloseKey(handle)
  65.  
  66.     if not data:
  67.         pass
  68.     return u''
  69.  
  70.  
  71. def SafeFormatMessage(eventLogRecord, logType = None):
  72.     if logType is None:
  73.         logType = 'Application'
  74.     
  75.     try:
  76.         return FormatMessage(eventLogRecord, logType)
  77.     except win32api.error:
  78.         if eventLogRecord.StringInserts is None:
  79.             desc = ''
  80.         else:
  81.             desc = u', '.join(eventLogRecord.StringInserts)
  82.         return u'<The description for Event ID ( %d ) in Source ( %r ) could not be found. It contains the following insertion string(s):%r.>' % (winerror.HRESULT_CODE(eventLogRecord.EventID), eventLogRecord.SourceName, desc)
  83.  
  84.  
  85.  
  86. def FeedEventLogRecords(feeder, machineName = None, logName = 'Application', readFlags = None):
  87.     if readFlags is None:
  88.         readFlags = win32evtlog.EVENTLOG_BACKWARDS_READ | win32evtlog.EVENTLOG_SEQUENTIAL_READ
  89.     h = win32evtlog.OpenEventLog(machineName, logName)
  90.     
  91.     try:
  92.         while None:
  93.             objects = win32evtlog.ReadEventLog(h, readFlags, 0)
  94.             if not objects:
  95.                 break
  96.         win32evtlog.CloseEventLog(h)
  97.         return None
  98.  
  99.  
  100.