I am currently working on adding some features to a plone website and I would like to be able to run a python script using content rules. For example, I've written an article and I want to publish it : using a rule triggered by a condition I want to run a certain script which is not related to the object.
I have already installed Runscript but the problem is that apparently the script has to be related to the object that triggered the action.
I have a simple script that just opens a text file to write 'Hello World' in it :
with open("hello.txt", "w") as fichier:
fichier.write("Hello world")
and plone give me the following error on my script (which works outside of plone) :
Module script, line 1, in hello
<FSPythonScript at /Plone2/hello used for /Plone2/news/azertyazert> Line 1
NameError: global name 'open' is not defined
On other scripts I also get the page I don't have sufficient rights to change the state of the article (thus triggering the condition rule).
Anyway, that was with Runscript, does anyone know either a another way to do execute scripts when rules are triggered or how to make runscript any script ?
Python scripts inside Zope / Plone run in a restricted environment. The error is thrown because you cannot open files from the filesystem there.
Use an External Method instead to point to a file-system stored script if you want to execute Python code that is not restricted.
The runscript action only uses the object as a context. Your script is free to ignore that context.
Related
I've got a LibreOffice python script that uses serial IO. On my systems, opening a serial port is a very slow process (around 1 second), so I'd like to keep the serial port open, and just send stuff as required.
But LibreOffice python apparently reloads the python framework every time a call is made. Unlike most python implementations, where the process is persistent, and un-enclosed code in a module is run once, when the module is imported.
Is there a way in LibreOffice python to persist objects between calls?
SerialObject=None
def return_global():
return str(SerialObject) #always returns "None"
def init_serial_object():
SerialObject=True
It looks like there is a simple bug. Add global and then it works.
However, it may be that the real problem is your setup. Here is a working example. Put the code in a file called inctest.py under $LIBREOFFICE_USER_DIR/Scripts/python/pythonpath/incmod/.
def init_serial_object():
global SerialObject
SerialObject=True
def moduleVersion():
return "2.0" #change to verify that this is the most recently updated code
The code to call it should be located in the user profile directory (that is, location=user).
from incmod import inctest
inctest.init_serial_object()
msgbox("{}: {}".format(
inctest.moduleVersion(), inctest.return_global()))
Run the script by going to Tools > Macros > Run Macro and find it under My Macros.
Be aware that inctest.py will not always get reloaded. There are two ways to reload it: restart LibreOffice, or force python to reload it by doing del sys.modules[mod].
The moduleVersion() function is not necessary but helps you see when the module is getting reloaded — make changes to that line and then see whether the output changes.
When I try to use the script from afl-unicorn I get the error below when I try to run it:
AttributeError("'NoneType' object has no attribute 'GetTriple'")
Related line is arch, arch_vendor, arch_os = lldb.target.GetTriple().split('-') but it works when I just type lldb.target.GetTriple() on interactive console. I don't know why it does not work when importing as comments in the script states.
I tried to add the script as a command later by doing command script add dumper -f unicorn_dumper_lldb but this throws error: unable to execute script function; so that didn't work.
Any idea why?
The error message is because lldb.target is None. To understand why this is so, see the couple of paragraphs in this section of the lldb Python reference after the table:
https://lldb.llvm.org/use/python-reference.html#embedded-python-interpreter
The lldb.target, etc variables are only defined when running in the embedded script interpreter. It doesn't make sense to use a global "selected target" in general purpose scripting, since lldb supports more than one target at a time, and you have no way of knowing that the currently selected target will be the one you want to operate on.
To work properly, the script will have to figure out how to pass the target it intends to work on around within the script.
I am trying to run a python script that is triggered by a rule in outlook. My current process is to write a VBA script that is triggered by the rule - the script simply just calls the shell method as so:
Shell (".../Anaconda3/python.exe" & ".../test_email.py")
However, when I call the script in VBA I receive the error message "Compile Error: Invalid outside procedure" and the reference to my python exe path is highlighted. Does anyone know how I can resolve this issue? I've added what I have in the VBA portion with the actual error
When I put the code in a sub, I am not able to call the actual code from the rules manager.
You must define a sub which has the following signature:
Public Sub Test(mail as MailItem)
Shell (".../Anaconda3/python.exe" & ".../test_email.py")
End Sub
Then Outlook will be able to recognize the method to run for a rule.
See Outlook's Rules and Alerts: Run a Script for more information.
I think you need to put the code inside a Sub, and then call the sub - not just launch it from the editor.
Sub PyExecSub()
Shell (".../Anaconda3/python.exe" & ".../test_email.py")
End Sub
The two answers above are great. However, I ran into the issue of not being able to track potential errors in my script, so I ultimately ended up creating a .bat file that referenced the python executable and script and created an error logger in the bat.
Public Sub PyExecSub(mail As MailItem)
Shell "...\Desktop\test_email.bat"
End Sub
I am trying to create a Shiny app where a user will choose a short string from a drop-down menu, that string will then be passed to a python script which will output some values which will be used in the shiny app.
Using reticulate's py_run_file function with the needed values hardcoded works great. However, using this:
py_run_file('test_script.py arg1')
gives this:
Error in py_run_file_impl(file, local, convert) :
Unable to open file 'test_script.py arg1' (does it exist?)
Several threads suggest using a system() call to run a .py script with command line arguments but I don't think that would be feasible for the goals because the argument needs to be able to change. Other threads have suggested creating a python file that calls the original python file using os.system() with arguments, but that also doesn't work for my situation.
Does anyone have any ideas?
Thanks
If anyone else is struggling with this problem: I found a workaround.
Instead of feeding an argument to the python script, I just create a R global environment variable then call it in the python script.
Had no idea you could reference R environment variables by calls such as r.RVar in the python script, similar to py$PythonVar when calling python variables in R scripts.
I try to create a python program which will deobfuscate powershell malware, which uses IEX. My python program is actually hooking the IEX function and instead of running the desired string, it will print the string.
Now my problem is that I have some .ps1 scripts (for examples 1.ps1, 2.ps1, etc..) and I want to run all of them under the same session so that by this, all the local variables created by 1.ps1 script, the 2.ps1 script will be able to use...
Now I tried so many ways, First I tried with subprocess but it always creates a new session for every time I enter a command (which is the path of the .ps1 file). Then I found this project at GitHub:
https://gist.github.com/MarkBaggett/a7c10195b2626c78009bf73bcdb6db20
Which is really awesome and did work but still, it seems that when I run the command ./1.ps1 it still does not store the local variables at the session (Maybe it opens a new one when running a script).
I tried to do also "Get-Content 1.ps1 | iex" but then it crashes since I have functions there for example:
function Invoke-Expression()
{
param(
[Parameter( `
Mandatory=$True, `
Valuefrompipeline = $True)]
[String]$Command
)
Write-Host $Command
}
taken from PSDecode project:
https://github.com/R3MRUM/PSDecode/blob/master/PSDecode.psm1#L28
Anyway, any ideas about how I can do this? I have those scripts on my desktop but no idea how to run them at the same session so they will use the same local variables...
Two things that I did though but they really suck:
1. Convert all the scripts to 1 script and run it, but in next run that I will use this program I might have 100 scripts or more and I don't really want to do this.
2. I can save the local variables from each script and load it to another yet I want to use it in the worst case scenario and still didn't get there.
Thank you so much for helping me and sorry for my grammar my English is not my mother language as you can see :)
Maybe you're looking for dot sourcing:
Runs a script in the current scope so that any functions, aliases, and variables that the script creates are added to the current scope.
PowerShell
. c:\scripts\sample.ps1
If so dot-source your ps1 files, and call the functions inside them.
Hope that helps.