Commit bf30b60e authored by Evan Simpson's avatar Evan Simpson

Allow upload with Add, and include binding meta-data in read/write

parent 336a4b28
...@@ -83,7 +83,7 @@ ...@@ -83,7 +83,7 @@
# #
############################################################################## ##############################################################################
__version__='$Revision: 1.2 $'[11:-2] __version__='$Revision: 1.3 $'[11:-2]
import Globals import Globals
from Globals import Persistent, HTMLFile, package_home from Globals import Persistent, HTMLFile, package_home
...@@ -148,6 +148,10 @@ class NameAssignments: ...@@ -148,6 +148,10 @@ class NameAssignments:
raise KeyError, name raise KeyError, name
return val return val
def getAssignedNames(self):
# Returns a copy of the assigned names mapping
return self._asgns.copy()
def getAssignedNamesInOrder(self): def getAssignedNamesInOrder(self):
# Returns the assigned names in the same order as that of # Returns the assigned names in the same order as that of
# self._exprs. # self._exprs.
......
...@@ -89,7 +89,7 @@ This product provides support for Script objects containing restricted ...@@ -89,7 +89,7 @@ This product provides support for Script objects containing restricted
Python code. Python code.
""" """
__version__='$Revision: 1.2 $'[11:-2] __version__='$Revision: 1.3 $'[11:-2]
import sys, os, traceback, re import sys, os, traceback, re
from Globals import MessageDialog, HTMLFile, package_home from Globals import MessageDialog, HTMLFile, package_home
...@@ -113,6 +113,10 @@ def manage_addPythonScript(self, id, REQUEST=None): ...@@ -113,6 +113,10 @@ def manage_addPythonScript(self, id, REQUEST=None):
id = str(id) id = str(id)
id = self._setObject(id, PythonScript(id)) id = self._setObject(id, PythonScript(id))
if REQUEST is not None: if REQUEST is not None:
file = REQUEST.form.get('file', None)
if file:
if type(file) is not type(''): file = file.read()
getattr(self, id).write(file)
try: u = self.DestinationURL() try: u = self.DestinationURL()
except: u = REQUEST['URL1'] except: u = REQUEST['URL1']
REQUEST.RESPONSE.redirect('%s/%s/manage_main' % (u, quote(id))) REQUEST.RESPONSE.redirect('%s/%s/manage_main' % (u, quote(id)))
...@@ -337,6 +341,8 @@ class PythonScript(Script, Historical): ...@@ -337,6 +341,8 @@ class PythonScript(Script, Historical):
def write(self, text): def write(self, text):
self._validateProxy() self._validateProxy()
mdata = self._metadata_map() mdata = self._metadata_map()
bindmap = self.getBindingAssignments().getAssignedNames()
bup = 0
st = 0 st = 0
try: try:
while 1: while 1:
...@@ -371,8 +377,13 @@ class PythonScript(Script, Historical): ...@@ -371,8 +377,13 @@ class PythonScript(Script, Historical):
self.title = v self.title = v
elif k == 'parameters': elif k == 'parameters':
self._params = v self._params = v
elif k[:5] == 'bind ':
bindmap[_nice_bind_names[k[5:]]] = v
bup = 1
self._body = rstrip(body) self._body = rstrip(body)
if bup:
self._setupBindings(bindmap)
self._makeFunction(1) self._makeFunction(1)
except: except:
LOG(self.meta_type, ERROR, 'write failed', error=sys.exc_info()) LOG(self.meta_type, ERROR, 'write failed', error=sys.exc_info())
...@@ -384,10 +395,14 @@ class PythonScript(Script, Historical): ...@@ -384,10 +395,14 @@ class PythonScript(Script, Historical):
return self.read() return self.read()
def _metadata_map(self): def _metadata_map(self):
return { m = {
'title': self.title, 'title': self.title,
'parameters': self._params, 'parameters': self._params,
} }
bindmap = self.getBindingAssignments().getAssignedNames()
for k, v in _nice_bind_names.items():
m['bind '+k] = bindmap.get(v, '')
return m
def read(self): def read(self):
# Construct metadata header lines, indented the same as the body. # Construct metadata header lines, indented the same as the body.
...@@ -396,7 +411,9 @@ class PythonScript(Script, Historical): ...@@ -396,7 +411,9 @@ class PythonScript(Script, Historical):
else: prefix = '##' else: prefix = '##'
hlines = ['%s %s "%s"' % (prefix, self.meta_type, self.id)] hlines = ['%s %s "%s"' % (prefix, self.meta_type, self.id)]
for kv in self._metadata_map().items(): mm = self._metadata_map().items()
mm.sort()
for kv in mm:
hlines.append('%s=%s' % kv) hlines.append('%s=%s' % kv)
hlines.append('') hlines.append('')
return join(hlines, '\n' + prefix) + '\n' + self._body return join(hlines, '\n' + prefix) + '\n' + self._body
...@@ -419,3 +436,7 @@ class PythonScript(Script, Historical): ...@@ -419,3 +436,7 @@ class PythonScript(Script, Historical):
_first_indent = re.compile('(?m)^ *(?! |$)') _first_indent = re.compile('(?m)^ *(?! |$)')
_nonempty_line = re.compile('(?m)^(.*\S.*)$') _nonempty_line = re.compile('(?m)^(.*\S.*)$')
_nice_bind_names = {'context': 'name_context', 'container': 'name_container',
'script': 'name_m_self', 'namespace': 'name_ns',
'subpath': 'name_subpath'}
...@@ -10,15 +10,20 @@ ...@@ -10,15 +10,20 @@
<P> <P>
Python Scripts allow you to add functionality to Zope by writing Python Scripts allow you to add functionality to Zope by writing
scripts in the Python programming language scripts in the Python programming language
that are exposed as callable Zope objects. that are exposed as callable Zope objects. You may choose to upload
the script from a local file by typing the file name or using the
<i>Browse</i> button.
</P> </P>
<form action="&dtml-URL1;" method="POST"> <form action="&dtml-URL1;" method="POST" enctype="multipart/form-data">
<input type="hidden" name="manage_addPythonScript:default_method" value=""> <input type="hidden" name="manage_addPythonScript:default_method" value="">
<p> <p>
<strong>ID:</strong> <input type="text" name="id" size="20"> <strong>ID:</strong> <input type="text" name="id" size="20">
</p> </p>
<p> <p>
<strong>File:</strong> <input type="file" name="file" size="25">
</p>
<p>
<input type="submit" value="Add and Edit" name="addedit"> <input type="submit" value="Add and Edit" name="addedit">
<input type="submit" value="Cancel" name="manage_workspace:method"> <input type="submit" value="Cancel" name="manage_workspace:method">
</p> </p>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment