| 1 | __doc__ = """Rplain product module""" |
|---|
| 2 | __version__ = '0.1' |
|---|
| 3 | |
|---|
| 4 | import os |
|---|
| 5 | from App.special_dtml import HTMLFile |
|---|
| 6 | from App.Dialogs import MessageDialog |
|---|
| 7 | from Persistence import Persistent |
|---|
| 8 | |
|---|
| 9 | import OFS.SimpleItem |
|---|
| 10 | import Acquisition |
|---|
| 11 | import AccessControl.Role |
|---|
| 12 | |
|---|
| 13 | import rpy2.robjects |
|---|
| 14 | from rpy2.robjects import r |
|---|
| 15 | rpy2.robjects.__allow_access_to_unprotected_subobjects__ = 1 |
|---|
| 16 | import rpy2.rlike.container as rlc |
|---|
| 17 | rlc.__allow_access_to_unprotected_subobjects__ = 1 |
|---|
| 18 | |
|---|
| 19 | from zope.proxy import removeAllProxies |
|---|
| 20 | from zope.traversing.browser import absoluteURL |
|---|
| 21 | from plot import cachedImages, registerImage |
|---|
| 22 | from rstat import gdd |
|---|
| 23 | from http_import import http_import |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | def manage_addRplain(self, id='R', REQUEST=None): |
|---|
| 27 | self._setObject(id, Rplain(id)) |
|---|
| 28 | if REQUEST is not None: |
|---|
| 29 | return self.manage_main(self, REQUEST) |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | class Rplain( OFS.SimpleItem.Item, |
|---|
| 34 | Persistent, |
|---|
| 35 | Acquisition.Implicit, |
|---|
| 36 | AccessControl.Role.RoleManager ): |
|---|
| 37 | |
|---|
| 38 | meta_type = 'Rplain' |
|---|
| 39 | manage_main = HTMLFile('RplainEdit', globals()) |
|---|
| 40 | manage_options = ( |
|---|
| 41 | {'label': 'Edit', 'action': 'manage_main'}, |
|---|
| 42 | ) |
|---|
| 43 | __ac_permissions__=( |
|---|
| 44 | ('View management screens', ('manage_tabs','manage_main')), |
|---|
| 45 | ('Change Rplains' , ('manage_edit',) ), |
|---|
| 46 | ) |
|---|
| 47 | def manage_edit(self, REQUEST=None): |
|---|
| 48 | if REQUEST is not None: |
|---|
| 49 | return MessageDialog( |
|---|
| 50 | title = 'Edited', |
|---|
| 51 | message = "Properties for %s changed." % self.id, |
|---|
| 52 | action = './manage_main', |
|---|
| 53 | ) |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | ############################################################################ |
|---|
| 57 | # Constructor |
|---|
| 58 | ############################################################################ |
|---|
| 59 | def __init__(self, id): |
|---|
| 60 | self.id = id |
|---|
| 61 | |
|---|
| 62 | ############################################################################ |
|---|
| 63 | # r |
|---|
| 64 | ############################################################################ |
|---|
| 65 | def r(self): |
|---|
| 66 | return rpy2.robjects.r |
|---|
| 67 | |
|---|
| 68 | ############################################################################ |
|---|
| 69 | # robjects |
|---|
| 70 | ############################################################################ |
|---|
| 71 | def robjects(self): |
|---|
| 72 | return rpy2.robjects |
|---|
| 73 | |
|---|
| 74 | ############################################################################ |
|---|
| 75 | # rlc |
|---|
| 76 | ############################################################################ |
|---|
| 77 | def rlc(self): |
|---|
| 78 | return rpy2.rlike.container |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | ############################################################################ |
|---|
| 83 | # cybertools: __call__ |
|---|
| 84 | ############################################################################ |
|---|
| 85 | |
|---|
| 86 | index_html=None |
|---|
| 87 | |
|---|
| 88 | def __call__(self, REQUEST=None, **kw): |
|---|
| 89 | key = self.REQUEST.form.get('image', 'not_found') |
|---|
| 90 | if '.' in key: # remove extension possibly added to make Flash happy |
|---|
| 91 | key = key.split('.', 1)[0] |
|---|
| 92 | path = cachedImages[key].path |
|---|
| 93 | #--return {'key':key,'keys':cachedImages.keys(),'path':path} |
|---|
| 94 | self.setHeaders(path) |
|---|
| 95 | f = open(path, 'rb') |
|---|
| 96 | data = f.read() |
|---|
| 97 | f.close() |
|---|
| 98 | return data |
|---|
| 99 | |
|---|
| 100 | def setHeaders(self, name=None): |
|---|
| 101 | response = self.REQUEST.response |
|---|
| 102 | # TODO: get content type from name (extension) |
|---|
| 103 | response.setHeader('Content-Type', 'image/jpeg') |
|---|
| 104 | response.setHeader('Expires', 'Sat, 1 Jan 2000 00:00:00 GMT'); |
|---|
| 105 | response.setHeader('Pragma', 'no-cache'); |
|---|
| 106 | |
|---|
| 107 | ############################################################################ |
|---|
| 108 | # cybertools: rplot |
|---|
| 109 | # @deprecated |
|---|
| 110 | ############################################################################ |
|---|
| 111 | def rplot(self, r , c, **kw): |
|---|
| 112 | request = r |
|---|
| 113 | context = c |
|---|
| 114 | fn, robj = gdd(**kw) |
|---|
| 115 | key = registerImage(fn) |
|---|
| 116 | return '%s/R?image=%s.jpg' % (absoluteURL(context, request), key) |
|---|
| 117 | |
|---|
| 118 | ############################################################################ |
|---|
| 119 | # cybertools: rfile |
|---|
| 120 | ############################################################################ |
|---|
| 121 | def rfile(self, r , c, **kw): |
|---|
| 122 | request = r |
|---|
| 123 | context = c |
|---|
| 124 | fn = os.tempnam(None, 'rfile')+'.prn' |
|---|
| 125 | data = http_import(kw['datasource']) |
|---|
| 126 | f = open(fn,'wb') |
|---|
| 127 | f.write(data) |
|---|
| 128 | f.close() |
|---|
| 129 | return fn |
|---|
| 130 | |
|---|
| 131 | ############################################################################ |
|---|
| 132 | # registerImage |
|---|
| 133 | ############################################################################ |
|---|
| 134 | def registerImage(self, r , c, **kw): |
|---|
| 135 | request = r |
|---|
| 136 | context = c |
|---|
| 137 | fn = os.tempnam(None, 'rplot') |
|---|
| 138 | key = registerImage(fn) |
|---|
| 139 | return fn,'%s/R?image=%s.jpg' % (absoluteURL(context, request), key) |
|---|