Skip to content

Commit

Permalink
Merge 3f2dacc into 771b829
Browse files Browse the repository at this point in the history
  • Loading branch information
jean committed Nov 19, 2013
2 parents 771b829 + 3f2dacc commit 86c149f
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 12 deletions.
43 changes: 31 additions & 12 deletions Products/CMFPlomino/PlominoDesignManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,18 +702,37 @@ def compileFormulaScript(self, script_id, formula, with_args=False):
)
import_list = ";".join(import_list)

r = re.compile('^#Plomino import (.+)$', re.MULTILINE)
for i in r.findall(formula):
scriptname = i.strip()
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 = formula.replace(
'#Plomino import '+scriptname,
script_code)
# 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)

if (formula.strip().count('\n') == 0 and
not formula.startswith('return ')):
Expand Down
53 changes: 53 additions & 0 deletions Products/CMFPlomino/tests/plomino.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,59 @@ Let's set a layout which contains our fields::
... <span class="plominoFieldClass">field4</span></p>""")


Formulas can include scripts from resources
--------------------------------------------

A formula can include scripts from ``resources``::

>>> from Products.PythonScripts.PythonScript import PythonScript
>>> ps = PythonScript('script')
>>> script_id = db.resources._setObject('script', ps)
>>> ps.write('return "hello"')
>>> id = db.frm1.invokeFactory('PlominoField',
... id='field5',
... title='Field with import',
... FieldType="TEXT",
... FieldMode="DISPLAY",
... Formula="#Plomino import script")
>>> db.frm1.field5.at_post_create_script()
>>> db.frm1.computeFieldValue('field5', db.frm1)
'hello'

Scripts can also include other scripts (including themselves; i.e.
recursive includes), and script names can be prefixes of other scripts::

>>> ps = PythonScript('script_too')
>>> script_id = db.resources._setObject('script_too', ps)
>>> ps.write('#Plomino import script\n#Plomino import script_too')
>>> id = db.frm1.invokeFactory('PlominoField',
... id='field6',
... title='Field with import',
... FieldType="TEXT",
... FieldMode="DISPLAY",
... Formula="#Plomino import script_too")
>>> db.frm1.field6.at_post_create_script()
>>> db.frm1.computeFieldValue('field6', db.frm1)
'hello'

You can also spell it ``include`` (because what we're doing is really
not much like Python's ``import``, it's straight string interpolation).
And scripts can include scripts that include scripts ...::

>>> ps = PythonScript('script_three')
>>> script_id = db.resources._setObject('script_three', ps)
>>> ps.write('#Plomino include script_too')
>>> id = db.frm1.invokeFactory('PlominoField',
... id='field7',
... title='Field with import',
... FieldType="TEXT",
... FieldMode="DISPLAY",
... Formula="#Plomino include script_three")
>>> db.frm1.field7.at_post_create_script()
>>> db.frm1.computeFieldValue('field7', db.frm1)
'hello'


Documents
---------------

Expand Down

0 comments on commit 86c149f

Please sign in to comment.