Mentions légales du service

Skip to content
Snippets Groups Projects
Commit 0aef36be authored by Mathieu Faverge's avatar Mathieu Faverge
Browse files

RP: Add the possibility to create exception rules on specific keywords

parent 92d76dc6
No related branches found
No related tags found
1 merge request!68Precision rules: Make sure we can use a list of dictionnaries as expected and enables protected names
...@@ -184,9 +184,11 @@ class SourceFile( object ): ...@@ -184,9 +184,11 @@ class SourceFile( object ):
'''Apply substitutions to text for given precision.''' '''Apply substitutions to text for given precision.'''
try: try:
# Get substitution table based on self._table # Get substitution table based on self._table
subs_o = self._subs.subs[ self._table ] # original subs_ef = self._subs.exceptfr
subs_s = self._subs.subs_search[ self._table ] # compiled as search regexp subs_et = self._subs.exceptto
subs_r = self._subs.subs_replace[ self._table ] # with regexp removed for replacement 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. # Get which column is from and to.
header = subs_o[0] header = subs_o[0]
...@@ -198,6 +200,18 @@ class SourceFile( object ): ...@@ -198,6 +200,18 @@ class SourceFile( object ):
traceback.print_exc() traceback.print_exc()
exit(1) 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 # Apply substitutions
try: try:
line = 0 line = 0
...@@ -213,6 +227,18 @@ class SourceFile( object ): ...@@ -213,6 +227,18 @@ class SourceFile( object ):
traceback.print_exc() traceback.print_exc()
exit(1) 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 return text
# end # end
......
...@@ -240,6 +240,9 @@ _subs = { ...@@ -240,6 +240,9 @@ _subs = {
], # end normal ], # end normal
} #end _subs } #end _subs
_fixedstrings = [
]
class Substitution( object ): class Substitution( object ):
def __init__( self, subsfiles=[] ): def __init__( self, subsfiles=[] ):
# Fill in subs_search with same structure as subs, but containing None values. # Fill in subs_search with same structure as subs, but containing None values.
...@@ -258,14 +261,27 @@ class Substitution( object ): ...@@ -258,14 +261,27 @@ class Substitution( object ):
if filepath not in sys.path: if filepath not in sys.path:
sys.path.append( filepath ) sys.path.append( filepath )
remove = True 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: try:
from local_subs import subs from local_subs import subs
for key in subs.keys(): for key in subs.keys():
_subs[key] = _subs[key] + subs[key] _subs[key] = _subs[key] + subs[key]
imported = True
except Exception as err: except Exception as err:
print( "Error: in importing:", file, file=sys.stderr ) print( "Error: in importing:", file, file=sys.stderr )
traceback.print_exc() if not imported:
exit(1) traceback.print_exc()
exit(1)
if remove: if remove:
sys.path.remove( filepath ) sys.path.remove( filepath )
...@@ -302,3 +318,26 @@ class Substitution( object ): ...@@ -302,3 +318,26 @@ class Substitution( object ):
self.subs = subs self.subs = subs
self.subs_search = subs_search self.subs_search = subs_search
self.subs_replace = subs_replace 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment