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 ):
'''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
......
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment