diff --git a/modules/precision_generator/codegen.py b/modules/precision_generator/codegen.py index eb644d1228de32d136ef23d26b2d027d2da68f15..a19ce5da0872a786378db845fe11723620cd4d14 100755 --- a/modules/precision_generator/codegen.py +++ b/modules/precision_generator/codegen.py @@ -184,9 +184,11 @@ class SourceFile( object ): '''Apply substitutions to text for given precision.''' try: # Get substitution table based on self._table - subs_o = self._subs.subs[ self._table ] # original - subs_s = self._subs.subs_search[ self._table ] # compiled as search regexp - subs_r = self._subs.subs_replace[ self._table ] # with regexp removed for replacement + subs_ef = self._subs.exceptfr + subs_et = self._subs.exceptto + subs_o = self._subs.subs[ self._table ] # original + subs_s = self._subs.subs_search[ self._table ] # compiled as search regexp + subs_r = self._subs.subs_replace[ self._table ] # with regexp removed for replacement # Get which column is from and to. header = subs_o[0] @@ -198,6 +200,18 @@ class SourceFile( object ): traceback.print_exc() exit(1) + # Apply protection + try: + line = 0 + for ( search, replace ) in zip( subs_ef, subs_et ): + line += 1 + text = re.sub( search, "RP"+replace+"RP", text ) + except Exception as err: + print( "Error: in row %d of substitution exception '%s'" % + (line, keyword), file=sys.stderr ) + traceback.print_exc() + exit(1) + # Apply substitutions try: line = 0 @@ -213,6 +227,18 @@ class SourceFile( object ): traceback.print_exc() exit(1) + # Restore protected + try: + line = 0 + for ( search, replace ) in zip( subs_ef, subs_et ): + line += 1 + text = re.sub( "RP"+replace+"RP", replace, text ) + except Exception as err: + print( "Error: in row %d of substitution exception '%s'" % + (line, keyword), file=sys.stderr ) + traceback.print_exc() + exit(1) + return text # end diff --git a/modules/precision_generator/subs.py b/modules/precision_generator/subs.py index af87b55938ea74e897bae6909bbe543c4f50f7cd..ec6ef535218190ab0c422fa3b8c34af78f2ae8e3 100644 --- a/modules/precision_generator/subs.py +++ b/modules/precision_generator/subs.py @@ -240,6 +240,9 @@ _subs = { ], # end normal } #end _subs +_fixedstrings = [ +] + class Substitution( object ): def __init__( self, subsfiles=[] ): # Fill in subs_search with same structure as subs, but containing None values. @@ -258,14 +261,27 @@ class Substitution( object ): if filepath not in sys.path: sys.path.append( filepath ) remove = True + + imported = False + try: + from local_subs import exceptfrom + for value in exceptfrom: + _fixedstrings.append( value ) + imported = True + except: + print( "Error: dictionnary does not include exception rules:", file, file=sys.stderr ) + try: from local_subs import subs for key in subs.keys(): _subs[key] = _subs[key] + subs[key] + imported = True except Exception as err: print( "Error: in importing:", file, file=sys.stderr ) - traceback.print_exc() - exit(1) + if not imported: + traceback.print_exc() + exit(1) + if remove: sys.path.remove( filepath ) @@ -302,3 +318,26 @@ class Substitution( object ): self.subs = subs self.subs_search = subs_search self.subs_replace = subs_replace + + # I don't get why I need the try, but seems to be the only way + try: + fixedstrings = _fixedstrings + except: + exit(1) + + # Register a clean version of the rules for the desination" + exceptfr = fixedstrings + exceptto = [] + for key in exceptfr: + keyto = key + keyto = keyto.replace( r'\b', r'' ) + keyto = keyto.replace( r'\*', r'*' ) + keyto = keyto.replace( r'\(', r'(' ) + keyto = keyto.replace( r'\)', r')' ) + keyto = keyto.replace( r'\.', r'.' ) + keyto = keyto.replace( r'\^', r'^' ) + exceptto.append( keyto ); + + self.exceptfr = exceptfr + self.exceptto = exceptto +