Skip to content

Commit

Permalink
Merge pull request #510 from plomino/jean-import-until-imported
Browse files Browse the repository at this point in the history
Import until imported
  • Loading branch information
ebrehault committed Dec 6, 2013
2 parents becce4e + 8eb4546 commit 474714b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 49 deletions.
35 changes: 4 additions & 31 deletions Products/CMFPlomino/PlominoDesignManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
from Products.CMFPlomino.config import *
from Products.CMFPlomino import get_utils
from Products.CMFPlomino import plomino_profiler
from Products.CMFPlomino.PlominoUtils import _expandIncludes
# get AT specific schemas for each Plomino class
from Products.CMFPlomino.interfaces import IXMLImportExportSubscriber
from Products.CMFPlomino.PlominoAction import schema as action_schema
Expand Down Expand Up @@ -669,6 +670,7 @@ def cleanFormulaScripts(self, script_id_pattern=None):
if not script_id_pattern or script_id_pattern in script_id:
self.scripts._delObject(script_id)


security.declarePublic('compileFormulaScript')
def compileFormulaScript(self, script_id, formula, with_args=False):
# Remember the current user
Expand Down Expand Up @@ -703,41 +705,12 @@ def compileFormulaScript(self, script_id, formula, with_args=False):
)
import_list = ";".join(import_list)

# Match any include
r = re.compile('^#Plomino (import|include) (.+)$', re.MULTILINE)

matches = r.findall(formula)
seen = []
while matches:
for include, scriptname in matches:

scriptname = scriptname.strip()
# Match only this include; don't match script names that are
# prefixes of other script names
exact_r = re.compile(
'^#Plomino %s %s\\b' % (include, scriptname),
re.MULTILINE)

if scriptname in seen:
# Included already, blank the include statement
formula = exact_r.sub('', formula)
continue

seen.append(scriptname)
try:
script_code = self.resources._getOb(scriptname).read()
except:
logger.warning("compileFormulaScript> %s not found in resources" % scriptname)
script_code = (
"#ALERT: %s not found in resources" % scriptname)

formula = exact_r.sub(script_code, formula)

matches = r.findall(formula)
formula = _expandIncludes(self, formula)

if (formula.strip().count('\n') == 0 and
not formula.startswith('return ')):
formula = "return " + formula

str_formula = STR_FORMULA % {
'script_id': script_id,
'import_list': import_list,
Expand Down
40 changes: 40 additions & 0 deletions Products/CMFPlomino/PlominoUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,3 +511,43 @@ def getDatagridRowdata(context, REQUEST):
def save_point():
txn = transaction.get()
txn.savepoint(optimistic=True)


def _expandIncludes(context, formula):
""" Recursively expand include statements
"""
# First, we match any includes
r = re.compile('^#Plomino (import|include) (.+)$', re.MULTILINE)

matches = r.findall(formula)
seen = []
while matches:
for include, scriptname in matches:

scriptname = scriptname.strip()
# Now, we match only *this* include; don't match script names
# that are prefixes of other script names
exact_r = re.compile(
'^#Plomino %s %s\\b' % (include, scriptname),
re.MULTILINE)

if scriptname in seen:
# Included already, blank the include statement
formula = exact_r.sub('', formula)
continue

seen.append(scriptname)
try:
db = context.getParentDatabase()
script_code = db.resources._getOb(scriptname).read()
except:
logger.warning("expandIncludes> %s not found in resources" % scriptname)
script_code = (
"#ALERT: %s not found in resources" % scriptname)

formula = exact_r.sub(script_code, formula)

matches = r.findall(formula)

return formula

37 changes: 19 additions & 18 deletions Products/CMFPlomino/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
from Products.CMFCore.utils import getToolByName

# Plomino
from PlominoUtils import asUnicode

from PlominoUtils import asUnicode, _expandIncludes

class PlominoScriptException(Exception):
def __init__(self, context, exception_obj, formula, script_id, compilation_errors):
Expand Down Expand Up @@ -47,22 +46,24 @@ def traceErr(self):
code = ["Code : "]
line_number = 6
formula = self.formula()
r = re.compile('#Plomino import (.+)[\r\n]')
for i in r.findall(formula):
scriptname = i.strip()
db = self.context.getParentDatabase()
script_code = db.resources._getOb(scriptname, None)
if script_code:
try:
script_code = script_code.read()
except:
msg = "#ALERT: " + scriptname + " invalid"
logger.error(msg, exc_info=True)
script_code = msg
else:
script_code = "#ALERT: %s not found in resources" % scriptname
formula = formula.replace(
'#Plomino import ' + scriptname, script_code)
formula = _expandIncludes(self.context, formula)
# r = re.compile('#Plomino import (.+)[\r\n]')
# for i in r.findall(formula):
# scriptname = i.strip()
# db = self.context.getParentDatabase()
# script_code = db.resources._getOb(scriptname, None)
# if script_code:
# try:
# script_code = script_code.read()
# except:
# msg = "#ALERT: " + scriptname + " invalid"
# logger.error(msg, exc_info=True)
# script_code = msg
# else:
# script_code = "#ALERT: %s not found in resources" % scriptname
# formula = formula.replace(
# '#Plomino import ' + scriptname, script_code)

for l in formula.replace('\r', '').split('\n'):
code.append("%d: %s" % (line_number, l))
line_number += 1
Expand Down
4 changes: 4 additions & 0 deletions Products/CMFPlomino/tests/plomino.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,10 @@ And scripts can include scripts that include scripts ...::
>>> db.frm1.computeFieldValue('field7', db.frm1)
'hello'

When formulas fail, and the database is in debug mode, the error is logged
--------------------------------------------------------------------------

TODO

Documents
---------------
Expand Down

0 comments on commit 474714b

Please sign in to comment.