Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 8 additions & 44 deletions config.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import os
import sys

# TODO: move all logging back to pyfa.py main loop
# We moved it here just to avoid rebuilding windows skeleton for now (any change to pyfa.py needs it)
import logging
import logging.handlers
from logbook import Logger

pyfalog = Logger(__name__)

# Load variable overrides specific to distribution type
try:
import configforced
except ImportError:
pyfalog.warning("Failed to import: configforced")
configforced = None


# Turns on debug mode
debug = False
# Defines if our saveddata will be in pyfa root or not
Expand All @@ -30,22 +31,6 @@
gameDB = None


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 = ''

def write(self, buf):
for line in buf.rstrip().splitlines():
self.logger.log(self.log_level, line.rstrip())


def isFrozen():
if hasattr(sys, 'frozen'):
return True
Expand All @@ -64,9 +49,11 @@ def getPyfaRoot():
root = unicode(root, sys.getfilesystemencoding())
return root


def getDefaultSave():
return unicode(os.path.expanduser(os.path.join("~", ".pyfa")), sys.getfilesystemencoding())


def defPaths(customSavePath):
global debug
global pyfaPath
Expand All @@ -75,10 +62,7 @@ def defPaths(customSavePath):
global gameDB
global saveInRoot

if debug:
logLevel = logging.DEBUG
else:
logLevel = logging.WARN
pyfalog.debug("Configuring Pyfa")

# The main pyfa directory which contains run.py
# Python 2.X uses ANSI by default, so we need to convert the character encoding
Expand All @@ -105,26 +89,6 @@ def defPaths(customSavePath):
os.environ["REQUESTS_CA_BUNDLE"] = os.path.join(pyfaPath, "cacert.pem").encode('utf8')
os.environ["SSL_CERT_FILE"] = os.path.join(pyfaPath, "cacert.pem").encode('utf8')

_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)
handler.setFormatter(formatter)
logging.getLogger('').addHandler(handler)

logging.info("Starting pyfa")

if hasattr(sys, 'frozen'):
stdout_logger = logging.getLogger('STDOUT')
sl = StreamToLogger(stdout_logger, logging.INFO)
sys.stdout = sl

# This interferes with cx_Freeze's own handling of exceptions. Find a way to fix this.
# stderr_logger = logging.getLogger('STDERR')
# sl = StreamToLogger(stderr_logger, logging.ERROR)
# sys.stderr = sl

# The database where we store all the fits etc
saveDB = os.path.join(savePath, "saveddata.db")

Expand Down
7 changes: 6 additions & 1 deletion eos/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

import migration
from eos import config
from logbook import Logger

pyfalog = Logger(__name__)


class ReadOnlyException(Exception):
Expand All @@ -46,7 +49,9 @@ class ReadOnlyException(Exception):
config.gamedata_version = gamedata_session.execute(
"SELECT `field_value` FROM `metadata` WHERE `field_name` LIKE 'client_build'"
).fetchone()[0]
except:
except Exception as e:
pyfalog.warning("Missing gamedata version.")
pyfalog.critical(e)
config.gamedata_version = None

saveddata_connectionstring = config.saveddata_connectionstring
Expand Down
6 changes: 3 additions & 3 deletions eos/db/migration.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import logging
from logbook import Logger
import shutil
import time

import config
import migrations

logger = logging.getLogger(__name__)
pyfalog = Logger(__name__)


def getVersion(db):
Expand Down Expand Up @@ -37,7 +37,7 @@ def update(saveddata_engine):
for version in xrange(dbVersion, appVersion):
func = migrations.updates[version + 1]
if func:
logger.info("Applying database update: %d", version + 1)
pyfalog.info("Applying database update: {0}", version + 1)
func(saveddata_engine)

# when all is said and done, set version to current
Expand Down
50 changes: 25 additions & 25 deletions eos/db/saveddata/databaseRepair.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
# ===============================================================================

from sqlalchemy.exc import DatabaseError
import logging
from logbook import Logger

logger = logging.getLogger(__name__)
pyfalog = Logger(__name__)


class DatabaseCleanup(object):
Expand All @@ -33,15 +33,15 @@ def ExecuteSQLQuery(saveddata_engine, query):
results = saveddata_engine.execute(query)
return results
except DatabaseError:
logger.error("Failed to connect to database or error executing query:\n%s", query)
pyfalog.error("Failed to connect to database or error executing query:\n{0}", query)
return None

@staticmethod
def OrphanedCharacterSkills(saveddata_engine):
# Find orphaned character skills.
# This solves an issue where the character doesn't exist, but skills for that character do.
# See issue #917
logger.debug("Running database cleanup for character skills.")
pyfalog.debug("Running database cleanup for character skills.")
query = "SELECT COUNT(*) AS num FROM characterSkills WHERE characterID NOT IN (SELECT ID from characters)"
results = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)

Expand All @@ -53,14 +53,14 @@ def OrphanedCharacterSkills(saveddata_engine):
if row and row['num']:
query = "DELETE FROM characterSkills WHERE characterID NOT IN (SELECT ID from characters)"
delete = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)
logger.error("Database corruption found. Cleaning up %d records.", delete.rowcount)
pyfalog.error("Database corruption found. Cleaning up {0} records.", delete.rowcount)

@staticmethod
def OrphanedFitDamagePatterns(saveddata_engine):
# Find orphaned damage patterns.
# This solves an issue where the damage pattern doesn't exist, but fits reference the pattern.
# See issue #777
logger.debug("Running database cleanup for orphaned damage patterns attached to fits.")
pyfalog.debug("Running database cleanup for orphaned damage patterns attached to fits.")
query = "SELECT COUNT(*) AS num FROM fits WHERE damagePatternID NOT IN (SELECT ID FROM damagePatterns) OR damagePatternID IS NULL"
results = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)

Expand All @@ -80,20 +80,20 @@ def OrphanedFitDamagePatterns(saveddata_engine):
rows = uniform_results.fetchall()

if len(rows) == 0:
logger.error("Missing uniform damage pattern.")
pyfalog.error("Missing uniform damage pattern.")
elif len(rows) > 1:
logger.error("More than one uniform damage pattern found.")
pyfalog.error("More than one uniform damage pattern found.")
else:
uniform_damage_pattern_id = rows[0]['ID']
update_query = "UPDATE 'fits' SET 'damagePatternID' = {} " \
"WHERE damagePatternID NOT IN (SELECT ID FROM damagePatterns) OR damagePatternID IS NULL".format(uniform_damage_pattern_id)
update_results = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, update_query)
logger.error("Database corruption found. Cleaning up %d records.", update_results.rowcount)
pyfalog.error("Database corruption found. Cleaning up {0} records.", update_results.rowcount)

@staticmethod
def OrphanedFitCharacterIDs(saveddata_engine):
# Find orphaned character IDs. This solves an issue where the character doesn't exist, but fits reference the pattern.
logger.debug("Running database cleanup for orphaned characters attached to fits.")
pyfalog.debug("Running database cleanup for orphaned characters attached to fits.")
query = "SELECT COUNT(*) AS num FROM fits WHERE characterID NOT IN (SELECT ID FROM characters) OR characterID IS NULL"
results = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)

Expand All @@ -113,22 +113,22 @@ def OrphanedFitCharacterIDs(saveddata_engine):
rows = all5_results.fetchall()

if len(rows) == 0:
logger.error("Missing 'All 5' character.")
pyfalog.error("Missing 'All 5' character.")
elif len(rows) > 1:
logger.error("More than one 'All 5' character found.")
pyfalog.error("More than one 'All 5' character found.")
else:
all5_id = rows[0]['ID']
update_query = "UPDATE 'fits' SET 'characterID' = " + str(all5_id) + \
" WHERE characterID not in (select ID from characters) OR characterID IS NULL"
update_results = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, update_query)
logger.error("Database corruption found. Cleaning up %d records.", update_results.rowcount)
pyfalog.error("Database corruption found. Cleaning up {0} records.", update_results.rowcount)

@staticmethod
def NullDamagePatternNames(saveddata_engine):
# Find damage patterns that are missing the name.
# This solves an issue where the damage pattern ends up with a name that is null.
# See issue #949
logger.debug("Running database cleanup for missing damage pattern names.")
pyfalog.debug("Running database cleanup for missing damage pattern names.")
query = "SELECT COUNT(*) AS num FROM damagePatterns WHERE name IS NULL OR name = ''"
results = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)

Expand All @@ -140,14 +140,14 @@ def NullDamagePatternNames(saveddata_engine):
if row and row['num']:
query = "DELETE FROM damagePatterns WHERE name IS NULL OR name = ''"
delete = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)
logger.error("Database corruption found. Cleaning up %d records.", delete.rowcount)
pyfalog.error("Database corruption found. Cleaning up {0} records.", delete.rowcount)

@staticmethod
def NullTargetResistNames(saveddata_engine):
# Find target resists that are missing the name.
# This solves an issue where the target resist ends up with a name that is null.
# See issue #949
logger.debug("Running database cleanup for missing target resist names.")
pyfalog.debug("Running database cleanup for missing target resist names.")
query = "SELECT COUNT(*) AS num FROM targetResists WHERE name IS NULL OR name = ''"
results = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)

Expand All @@ -159,14 +159,14 @@ def NullTargetResistNames(saveddata_engine):
if row and row['num']:
query = "DELETE FROM targetResists WHERE name IS NULL OR name = ''"
delete = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)
logger.error("Database corruption found. Cleaning up %d records.", delete.rowcount)
pyfalog.error("Database corruption found. Cleaning up {0} records.", delete.rowcount)

@staticmethod
def OrphanedFitIDItemID(saveddata_engine):
# Orphaned items that are missing the fit ID or item ID.
# See issue #954
for table in ['drones', 'cargo', 'fighters']:
logger.debug("Running database cleanup for orphaned %s items.", table)
pyfalog.debug("Running database cleanup for orphaned {0} items.", table)
query = "SELECT COUNT(*) AS num FROM {} WHERE itemID IS NULL OR itemID = '' or itemID = '0' or fitID IS NULL OR fitID = '' or fitID = '0'".format(
table)
results = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)
Expand All @@ -180,10 +180,10 @@ def OrphanedFitIDItemID(saveddata_engine):
query = "DELETE FROM {} WHERE itemID IS NULL OR itemID = '' or itemID = '0' or fitID IS NULL OR fitID = '' or fitID = '0'".format(
table)
delete = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)
logger.error("Database corruption found. Cleaning up %d records.", delete.rowcount)
pyfalog.error("Database corruption found. Cleaning up {0} records.", delete.rowcount)

for table in ['modules']:
logger.debug("Running database cleanup for orphaned %s items.", table)
pyfalog.debug("Running database cleanup for orphaned {0} items.", table)
query = "SELECT COUNT(*) AS num FROM {} WHERE itemID = '0' or fitID IS NULL OR fitID = '' or fitID = '0'".format(
table)
results = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)
Expand All @@ -196,15 +196,15 @@ def OrphanedFitIDItemID(saveddata_engine):
if row and row['num']:
query = "DELETE FROM {} WHERE itemID = '0' or fitID IS NULL OR fitID = '' or fitID = '0'".format(table)
delete = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)
logger.error("Database corruption found. Cleaning up %d records.", delete.rowcount)
pyfalog.error("Database corruption found. Cleaning up {0} records.", delete.rowcount)

@staticmethod
def NullDamageTargetPatternValues(saveddata_engine):
# Find patterns that have null values
# See issue #954
for profileType in ['damagePatterns', 'targetResists']:
for damageType in ['em', 'thermal', 'kinetic', 'explosive']:
logger.debug("Running database cleanup for null %s values. (%s)", profileType, damageType)
pyfalog.debug("Running database cleanup for null {0} values. ({1})", profileType, damageType)
query = "SELECT COUNT(*) AS num FROM {0} WHERE {1}Amount IS NULL OR {1}Amount = ''".format(profileType,
damageType)
results = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)
Expand All @@ -218,13 +218,13 @@ def NullDamageTargetPatternValues(saveddata_engine):
query = "UPDATE '{0}' SET '{1}Amount' = '0' WHERE {1}Amount IS NULL OR Amount = ''".format(profileType,
damageType)
delete = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)
logger.error("Database corruption found. Cleaning up %d records.", delete.rowcount)
pyfalog.error("Database corruption found. Cleaning up {0} records.", delete.rowcount)

@staticmethod
def DuplicateSelectedAmmoName(saveddata_engine):
# Orphaned items that are missing the fit ID or item ID.
# See issue #954
logger.debug("Running database cleanup for duplicated selected ammo profiles.")
pyfalog.debug("Running database cleanup for duplicated selected ammo profiles.")
query = "SELECT COUNT(*) AS num FROM damagePatterns WHERE name = 'Selected Ammo'"
results = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)

Expand All @@ -236,4 +236,4 @@ def DuplicateSelectedAmmoName(saveddata_engine):
if row and row['num'] > 1:
query = "DELETE FROM damagePatterns WHERE name = 'Selected Ammo'"
delete = DatabaseCleanup.ExecuteSQLQuery(saveddata_engine, query)
logger.error("Database corruption found. Cleaning up %d records.", delete.rowcount)
pyfalog.error("Database corruption found. Cleaning up {0} records.", delete.rowcount)
8 changes: 4 additions & 4 deletions eos/effectHandlerHelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
# along with eos. If not, see <http://www.gnu.org/licenses/>.
# ===============================================================================

import logging
from logbook import Logger

logger = logging.getLogger(__name__)
pyfalog = Logger(__name__)


class HandledList(list):
Expand Down Expand Up @@ -198,7 +198,7 @@ def append(self, thing):
# if needed, remove booster that was occupying slot
oldObj = next((m for m in self if m.slot == thing.slot), None)
if oldObj:
logging.info("Slot %d occupied with %s, replacing with %s", thing.slot, oldObj.item.name, thing.item.name)
pyfalog.info("Slot {0} occupied with {1}, replacing with {2}", thing.slot, oldObj.item.name, thing.item.name)
oldObj.itemID = 0 # hack to remove from DB. See GH issue #324
self.remove(oldObj)

Expand All @@ -222,7 +222,7 @@ def append(self, proj):
oldEffect = next((m for m in self if m.item.group.name == "Effect Beacon"), None)

if oldEffect:
logging.info("System effect occupied with %s, replacing with %s", oldEffect.item.name, proj.item.name)
pyfalog.info("System effect occupied with {0}, replacing with {1}", oldEffect.item.name, proj.item.name)
self.remove(oldEffect)

HandledList.append(self, proj)
Expand Down
Loading