Files

167 lines
4.9 KiB
Python
Raw Permalink Normal View History

2011-04-12 01:32:07 -07:00
'''
This file is part of the PyPhantomJS project.
Copyright (C) 2011 James Roe <roejames12@hotmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
2011-09-23 04:40:56 -07:00
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2011-04-12 01:32:07 -07:00
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
2011-09-23 04:40:56 -07:00
along with this program. If not, see <http://www.gnu.org/licenses/>.
2011-04-12 01:37:47 -07:00
'''
2011-04-12 01:32:07 -07:00
2011-06-01 00:26:06 -07:00
import sys
2011-11-27 10:56:12 -08:00
from PyQt4.QtCore import (QByteArray, QDateTime, QFile, Qt,
2011-09-18 15:37:40 -07:00
QtCriticalMsg, QtDebugMsg, QtFatalMsg,
QtWarningMsg)
2011-05-31 16:23:06 -07:00
2011-11-27 10:56:12 -08:00
def debug(debug_type):
def excepthook(type_, value, tb):
import traceback
2011-11-27 10:56:12 -08:00
# print the exception...
traceback.print_exception(type_, value, tb)
print
# ...then start the debugger in post-mortem mode
pdb.pm()
2011-11-27 10:56:12 -08:00
# we are NOT in interactive mode
if not hasattr(sys, 'ps1') or sys.stderr.target.isatty():
import pdb
2011-11-27 10:56:12 -08:00
from PyQt4.QtCore import pyqtRemoveInputHook
pyqtRemoveInputHook()
2011-11-27 10:56:12 -08:00
if debug_type == 'exception':
sys.excepthook = excepthook
elif debug_type == 'program':
pdb.set_trace()
2011-12-02 16:02:52 -08:00
class CaseInsensitiveDict(dict):
def __delitem__(self, key):
for dictKey in self:
2011-12-03 23:04:08 -08:00
if self.sameKey(key, dictKey):
2011-12-02 16:02:52 -08:00
super(CaseInsensitiveDict, self).__delitem__(dictKey)
return
raise KeyError(key)
def __contains__(self, key):
for dictKey in self:
2011-12-03 23:04:08 -08:00
if self.sameKey(key, dictKey):
2011-12-02 16:02:52 -08:00
return True
return False
def __getitem__(self, key):
for dictKey, dictValue in self.items():
2011-12-03 23:04:08 -08:00
if self.sameKey(key, dictKey):
2011-12-02 16:02:52 -08:00
return dictValue
raise KeyError(key)
def __setitem__(self, key, value):
for dictKey in self:
2011-12-03 23:04:08 -08:00
if self.sameKey(key, dictKey):
2011-12-02 16:02:52 -08:00
super(CaseInsensitiveDict, self).__setitem__(dictKey, value)
return
2011-12-03 23:04:08 -08:00
2011-12-02 16:02:52 -08:00
super(CaseInsensitiveDict, self).__setitem__(key, value)
2011-12-03 23:04:08 -08:00
def sameKey(self, key, dictKey):
2011-12-02 16:02:52 -08:00
if hasattr(key, 'lower'):
2011-12-03 23:04:08 -08:00
key = key.lower()
if hasattr(dictKey, 'lower'):
dictKey = dictKey.lower()
if key == dictKey:
return True
return False
2011-12-02 16:02:52 -08:00
2011-08-15 12:00:13 -07:00
class MessageHandler(object):
def __init__(self, verbose):
self.verbose = verbose
def process(self, msgType, msg):
now = QDateTime.currentDateTime().toString(Qt.ISODate)
if msgType == QtDebugMsg:
if self.verbose:
print '%s [DEBUG] %s' % (now, msg)
elif msgType == QtWarningMsg:
print >> sys.stderr, '%s [WARNING] %s' % (now, msg)
elif msgType == QtCriticalMsg:
print >> sys.stderr, '%s [CRITICAL] %s' % (now, msg)
elif msgType == QtFatalMsg:
print >> sys.stderr, '%s [FATAL] %s' % (now, msg)
2011-05-03 16:45:53 -07:00
2011-05-31 16:23:06 -07:00
2011-05-03 16:45:53 -07:00
class SafeStreamFilter(object):
2011-05-04 16:15:51 -07:00
'''Convert string to something safe'''
2011-05-03 16:45:53 -07:00
def __init__(self, target):
self.target = target
2011-08-24 16:39:04 -07:00
self.encoding = self.encode_to = self.encoding_sys = self.target.encoding or 'utf-8'
2011-05-03 16:45:53 -07:00
self.errors = 'replace'
def write(self, s):
s = self.encode(s)
self.target.write(s)
2011-05-04 12:32:17 -07:00
def flush(self):
self.target.flush()
2011-05-03 16:45:53 -07:00
def encode(self, s):
return s.encode(self.encode_to, self.errors)
2011-09-13 14:45:28 -07:00
class QPyFile(QFile):
2011-09-18 15:37:40 -07:00
'''Simple subclass of QFile which supports the context manager
It also wraps methods that require/return some foreign data type,
such as QByteArray.
'''
2011-09-13 14:45:28 -07:00
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
self.close()
2011-09-18 15:37:40 -07:00
def __init__(self, filename, mode='r'):
super(QPyFile, self).__init__(filename)
modeMap = {
'r': QFile.ReadOnly,
'r+': QFile.ReadOnly | QFile.WriteOnly,
'w': QFile.WriteOnly | QFile.Truncate,
'w+': QFile.WriteOnly | QFile.ReadOnly | QFile.Truncate,
'a': QFile.Append,
'a+': QFile.Append | QFile.ReadOnly
}
flags = QFile.NotOpen
for key, flag in modeMap.items():
if key in mode:
flags = flags | flag
if not self.open(flags):
raise IOError("Could not open file: '%s'" % self.fileName())
def peek(self, maxlen):
return str(super(QPyFile, self).peek(maxlen))
def readAll(self):
return str(super(QPyFile, self).readAll())
def write(self, data):
return super(QPyFile, self).write(QByteArray(data))