Handle unicode, utf8, and windows-1252

(cherry picked from commit 0d4f24a)
This commit is contained in:
Ebag333
2016-11-26 22:57:53 -08:00
parent 38bf143704
commit 08b5abc7ad
6 changed files with 104 additions and 27 deletions

View File

@@ -35,6 +35,15 @@ class StreamToLogger(object):
Fake file-like stream object that redirects writes to a logger instance.
From: http://www.electricmonk.nl/log/2011/08/14/redirect-stdout-and-stderr-to-a-logger-in-python/
"""
def __init__(self, logger, log_level=logging.INFO):
self.logger = logger
self.log_level = log_level
self.linebuf = ''
"""
Fake file-like stream object that redirects writes to a logger instance.
From: http://www.electricmonk.nl/log/2011/08/14/redirect-stdout-and-stderr-to-a-logger-in-python/
"""
def __init__(self, logger, log_level=logging.INFO):
self.logger = logger
self.log_level = log_level
@@ -52,12 +61,6 @@ def isFrozen():
return False
def getPyfaRoot():
base = getattr(sys.modules['__main__'], "__file__", sys.executable) if isFrozen() else sys.argv[0]
root = os.path.dirname(os.path.realpath(os.path.abspath(base)))
root = unicode(root, sys.getfilesystemencoding())
return root
def __createDirs(path):
if not os.path.exists(path):
@@ -81,32 +84,32 @@ def defPaths(customSavePath):
# Python 2.X uses ANSI by default, so we need to convert the character encoding
pyfaPath = getattr(configforced, "pyfaPath", pyfaPath)
if pyfaPath is None:
pyfaPath = getPyfaRoot()
pyfaPath = getPyfaPath()
# Where we store the saved fits etc, default is the current users home directory
if saveInRoot is True:
savePath = getattr(configforced, "savePath", None)
if savePath is None:
savePath = os.path.join(pyfaPath, "saveddata")
savePath = getPyfaPath("saveddata")
else:
savePath = getattr(configforced, "savePath", None)
if savePath is None:
if customSavePath is None: # customSavePath is not overriden
savePath = unicode(os.path.expanduser(os.path.join("~", ".pyfa")),
sys.getfilesystemencoding())
savePath = getSavePath()
else:
savePath = customSavePath
__createDirs(savePath)
if isFrozen():
os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(pyfaPath, "cacert.pem")
os.environ["SSL_CERT_FILE"] = os.path.join(pyfaPath, "cacert.pem")
certName = "cacert.pem"
os.environ["REQUESTS_CA_BUNDLE"] = getPyfaPath(certName)
os.environ["SSL_CERT_FILE"] = getPyfaPath(certName)
format_ = '%(asctime)s %(name)-24s %(levelname)-8s %(message)s'
logging.basicConfig(format=format_, level=logLevel)
handler = logging.handlers.RotatingFileHandler(os.path.join(savePath, "log.txt"), maxBytes=1000000, backupCount=3)
formatter = logging.Formatter(format_)
loggingFormat = '%(asctime)s %(name)-24s %(levelname)-8s %(message)s'
logging.basicConfig(format=loggingFormat, level=logLevel)
handler = logging.handlers.RotatingFileHandler(getSavePath("log.txt"), maxBytes=1000000, backupCount=3)
formatter = logging.Formatter(loggingFormat)
handler.setFormatter(formatter)
logging.getLogger('').addHandler(handler)
@@ -123,12 +126,12 @@ def defPaths(customSavePath):
# sys.stderr = sl
# The database where we store all the fits etc
saveDB = os.path.join(savePath, "saveddata.db")
saveDB = getSavePath("saveddata.db")
# The database where the static EVE data from the datadump is kept.
# This is not the standard sqlite datadump but a modified version created by eos
# maintenance script
gameDB = os.path.join(pyfaPath, "eve.db")
gameDB = getPyfaPath("eve.db")
# DON'T MODIFY ANYTHING BELOW!
import eos.config
@@ -138,3 +141,48 @@ def defPaths(customSavePath):
# saveddata db location modifier, shouldn't ever need to touch this
eos.config.saveddata_connectionstring = "sqlite:///" + saveDB + "?check_same_thread=False"
eos.config.gamedata_connectionstring = "sqlite:///" + gameDB + "?check_same_thread=False"
def getPyfaPath(Append=None):
base = getattr(sys.modules['__main__'], "__file__", sys.executable) if isFrozen() else sys.argv[0]
root = os.path.dirname(os.path.realpath(os.path.abspath(base)))
if type(root) == str: # leave unicode ones alone
try:
root = root.decode('utf8')
except UnicodeDecodeError:
root = root.decode('windows-1252')
if not Append:
return root
if type(root) == str: # leave unicode ones alone
try:
path = os.path.abspath(os.path.join(root, Append)).decode('utf8')
except UnicodeDecodeError:
path = os.path.abspath(os.path.join(root, Append)).decode('windows-1252')
else:
path = os.path.abspath(os.path.join(root, Append))
return path
def getSavePath(Append=None):
root = os.path.expanduser(os.path.join("~", ".pyfa"))
if type(root) == str: # leave unicode ones alone
try:
root = root.decode('utf8')
except UnicodeDecodeError:
root = root.decode('windows-1252')
if not Append:
return root
if type(root) == str: # leave unicode ones alone
try:
path = os.path.abspath(os.path.join(root, Append)).decode('utf8')
except UnicodeDecodeError:
path = os.path.abspath(os.path.join(root, Append)).decode('windows-1252')
else:
path = os.path.abspath(os.path.join(root, Append))
return path

View File

@@ -31,7 +31,7 @@ except ImportError:
class BitmapLoader(object):
try:
archive = zipfile.ZipFile(os.path.join(config.pyfaPath, 'imgs.zip'), 'r')
archive = zipfile.ZipFile(config.getPyfaPath('imgs.zip'), 'r')
except IOError:
archive = None
@@ -85,7 +85,7 @@ class BitmapLoader(object):
except KeyError:
print("Missing icon file from zip: {0}".format(path))
else:
path = os.path.join(config.pyfaPath, 'imgs', location, filename)
path = config.getPyfaPath('imgs\\' + location + "\\" + filename)
if os.path.exists(path):
return wx.Image(path)

View File

@@ -48,9 +48,20 @@ class GraphFrame(wx.Frame):
try:
cache_dir = mpl._get_cachedir()
except:
cache_dir = unicode(os.path.expanduser(os.path.join("~", ".matplotlib")))
cache_dir = os.path.expanduser(os.path.join("~", ".matplotlib"))
if type(cache_dir) == str: # leave unicode ones alone
try:
cache_dir = cache_dir.decode('utf8')
except UnicodeDecodeError:
cache_dir = cache_dir.decode('windows-1252')
cache_file = os.path.join(cache_dir, 'fontList.cache')
if type(cache_file) == str: # leave unicode ones alone
try:
cache_file = cache_file.decode('utf8')
except UnicodeDecodeError:
cache_file = cache_file.decode('windows-1252')
if os.access(cache_dir, os.W_OK | os.X_OK) and os.path.isfile(cache_file):
# remove matplotlib font cache, see #234
os.remove(cache_file)
@@ -174,7 +185,7 @@ class GraphFrame(wx.Frame):
if not isinstance(defaultVal, basestring):
defaultVal = ("%f" % defaultVal).rstrip("0")
if defaultVal[-1:] == ".":
defaultVal = defaultVal + "0"
defaultVal += "0"
textBox.ChangeValue(defaultVal)

View File

@@ -40,7 +40,14 @@ from gui.contextMenu import ContextMenu
class ItemStatsDialog(wx.Dialog):
counter = 0
def __init__(self, victim, fullContext=None, pos=wx.DefaultPosition, size=wx.DefaultSize, maximized=False):
def __init__(
self,
victim,
fullContext=None,
pos=wx.DefaultPosition,
size=wx.DefaultSize,
maximized=False
):
wx.Dialog.__init__(
self,
@@ -827,7 +834,7 @@ class ItemEffects(wx.Panel):
If effect file does not exist, create it
"""
file_ = os.path.join(config.pyfaPath, "eos", "effects", "%s.py" % event.GetText().lower())
file_ = config.getPyfaPath("eos\\effects\\%s.py" % event.GetText().lower())
if not os.path.isfile(file_):
open(file_, 'a').close()

View File

@@ -45,7 +45,13 @@ class FileCache(APICache):
os.mkdir(self.path, 0o700)
def _getpath(self, key):
return os.path.join(self.path, str(hash(key)) + '.cache')
path = os.path.join(self.path, str(hash(key)) + '.cache')
if type(path) == str: # leave unicode ones alone
try:
path = path.decode('utf8')
except UnicodeDecodeError:
path = path.decode('windows-1252')
return path
def put(self, key, value):
with open(self._getpath(key), 'wb') as f:

View File

@@ -45,6 +45,11 @@ class SettingsProvider(object):
s = self.settings.get(area)
if s is None:
p = os.path.join(self.BASE_PATH, area)
if type(p) == str: # leave unicode ones alone
try:
p = p.decode('utf8')
except UnicodeDecodeError:
p = p.decode('windows-1252')
if not os.path.exists(p):
info = {}
@@ -188,10 +193,10 @@ class NetworkSettings(object):
def setAccess(self, access):
self.serviceNetworkSettings["access"] = access
def autodetect(self):
@staticmethod
def autodetect():
proxy = None
proxAddr = proxPort = ""
proxydict = urllib2.ProxyHandler().proxies
validPrefixes = ("http", "https")