Source code for bin.debug
from __future__ import print_function
[docs]__author__ = 'Martin Paul Eve'
[docs]__email__ = "martin@martineve.com"
import sys
import os
[docs]class Debug(object):
def __init__(self):
"""
Initialise this debug instance
@param gv: a reference to an instance of the meTypeset global configuration class
"""
self.debug = False
self.has_run = False
self.git = True
self.prompt = None
self.git_objects = []
[docs] def enable_debug(self, nogit):
self.debug = True
self.git = not nogit
[docs] def enable_prompt(self, prompt):
self.prompt = prompt
[docs] def print_(self, module, message):
if self.prompt is None:
print(u'[{0}] {1}'.format(module.get_module_name(), message))
else:
self.prompt.print_(u'[{0}] {1}'.format(self.prompt.colorize('red', module.get_module_name()),
message))
[docs] def get_module_name(self):
return 'Debugger'
[docs] def mkdir(self, path):
try:
os.makedirs(path)
if self.debug and self.git:
self.print_(self, u'Initializing git repo at {0}'.format(path))
from git import repo
from git import Git
repo = Git(path)
repo.init()
self.git_objects.append(repo)
except:
self.fatal_error(self, 'Output directory {0} already exists or could not init git. If you are running with --debug, try installing GitPython.'.format(path))
[docs] def print_debug(self, module, message):
"""
This method prints debug information to stdout when the global debug flag is set
@param module: the calling module
@param message: the debug message to print
"""
if self.debug:
self.print_(module, message)
# optionally, if the calling module has a "gv" object within it, we will try to take a git snapshot
if self.git:
for repo in self.git_objects:
repo.add('.', with_exceptions=False)
# remove all characters above 128 as the git module does not handle unicode commits well
repo.commit(u'-m', u'{0}'.format("".join(i for i in message if ord(i)<128)), with_exceptions=False)
[docs] def write_error(self, module, message, error_number):
"""
This method writes parsing information to the error log
@param module: the calling module
@param message: the error message to print
"""
if not self.has_run:
module.gv.mk_dir(module.gv.error_folder_path)
self.has_run = True
error_file = open(module.gv.error_file_path, 'w')
print(u'[{0}] {1}\n'.format(error_number, message), file=error_file)
error_file.close()
@staticmethod
[docs] def fatal_error(module, message):
print(u'[FATAL ERROR] [{0}] {1}'.format(module.get_module_name(), message))
sys.exit(1)
[docs]class Debuggable(object):
def __init__(self, module_name):
self.module_name = module_name
[docs] def get_module_name(self):
return self.module_name