@ -14,6 +14,7 @@
# along with Korman. If not, see <http://www.gnu.org/licenses/>.
# along with Korman. If not, see <http://www.gnu.org/licenses/>.
from . . korlib import ConsoleToggler
from . . korlib import ConsoleToggler
from . explosions import NonfatalExportError
from pathlib import Path
from pathlib import Path
import threading
import threading
import time
import time
@ -24,6 +25,7 @@ _MAX_TIME_UNTIL_ELIPSES = 2.0
class _ExportLogger :
class _ExportLogger :
def __init__ ( self , print_logs , age_path = None ) :
def __init__ ( self , print_logs , age_path = None ) :
self . _errors = [ ]
self . _porting = [ ]
self . _porting = [ ]
self . _warnings = [ ]
self . _warnings = [ ]
self . _age_path = Path ( age_path ) if age_path is not None else None
self . _age_path = Path ( age_path ) if age_path is not None else None
@ -42,10 +44,23 @@ class _ExportLogger:
def __exit__ ( self , type , value , traceback ) :
def __exit__ ( self , type , value , traceback ) :
if value is not None :
if value is not None :
ConsoleToggler ( ) . keep_console = True
ConsoleToggler ( ) . keep_console = not isinstance ( value , NonfatalExportError )
self . _file . close ( )
self . _file . close ( )
return False
return False
def error ( self , * args , * * kwargs ) :
assert args
indent = kwargs . get ( " indent " , 0 )
msg = " {} ERROR: {} " . format ( " " * indent , args [ 0 ] )
if len ( args ) > 1 :
msg = msg . format ( * args [ 1 : ] , * * kwargs )
if self . _file is not None :
self . _file . writelines ( ( msg , " \n " ) )
if self . _print_logs :
print ( msg )
cache = args [ 0 ] if len ( args ) == 1 else args [ 0 ] . format ( * args [ 1 : ] )
self . _errors . append ( cache )
def msg ( self , * args , * * kwargs ) :
def msg ( self , * args , * * kwargs ) :
assert args
assert args
indent = kwargs . get ( " indent " , 0 )
indent = kwargs . get ( " indent " , 0 )
@ -67,7 +82,8 @@ class _ExportLogger:
self . _file . writelines ( ( msg , " \n " ) )
self . _file . writelines ( ( msg , " \n " ) )
if self . _print_logs :
if self . _print_logs :
print ( msg )
print ( msg )
self . _porting . append ( args [ 0 ] )
cache = args [ 0 ] if len ( args ) == 1 else args [ 0 ] . format ( * args [ 1 : ] )
self . _porting . append ( cache )
def progress_add_step ( self , name ) :
def progress_add_step ( self , name ) :
@ -92,6 +108,14 @@ class _ExportLogger:
self . msg ( " Exporting ' {} ' " , self . _age_path . name )
self . msg ( " Exporting ' {} ' " , self . _age_path . name )
self . _time_start_overall = time . perf_counter ( )
self . _time_start_overall = time . perf_counter ( )
def raise_errors ( self ) :
num_errors = len ( self . _errors )
if num_errors == 1 :
raise NonfatalExportError ( self . _errors [ 0 ] )
elif num_errors :
raise NonfatalExportError ( """ {} errors were encountered during export. Check the export log for more details:
{ } """ , num_errors, self._file.name)
def save ( self ) :
def save ( self ) :
# TODO
# TODO
pass
pass
@ -106,7 +130,8 @@ class _ExportLogger:
self . _file . writelines ( ( msg , " \n " ) )
self . _file . writelines ( ( msg , " \n " ) )
if self . _print_logs :
if self . _print_logs :
print ( msg )
print ( msg )
self . _warnings . append ( args [ 0 ] )
cache = args [ 0 ] if len ( args ) == 1 else args [ 0 ] . format ( * args [ 1 : ] )
self . _warnings . append ( cache )
class ExportProgressLogger ( _ExportLogger ) :
class ExportProgressLogger ( _ExportLogger ) :