Today is the first time I've used Python, so I'm sure this'll be an easy question.
I need to convert this Python script from a command line application: webkit2png. The end result will be a URL that returns an image of the webpage passed into it as a querystring param. I've achieved this on Windows with .NET and IE, Gecko and WebKit, but now need to do the same for Safari on OS X.
I think I've got it converted, but unfortunately I'm running into a problem running the script from Apache on OS X:
app = AppKit.NSApplication.sharedApplication()
# create an app delegate
delegate = AppDelegate.alloc().init()
AppKit.NSApp().setDelegate_(delegate)
# create a window
rect = Foundation.NSMakeRect(0,0,100,100)
win = AppKit.NSWindow.alloc()
win.initWithContentRect_styleMask_backing_defer_ (rect,
AppKit.NSBorderlessWindowMask, 2, 0)
The error is thrown on the final line "initWithContentRect...". The error I see is:
<class 'objc.error'>: NSInternalInconsistencyException - Error (1002) creating CGSWindow
args = ('NSInternalInconsistencyException - Error (1002) creating CGSWindow',)
message = 'NSInternalInconsistencyException - Error (1002) creating CGSWindow'
name = u'NSInternalInconsistencyException'
If I run the script on the command line (after removing the CGI stuff), it runs perfectly.
Here's the libraries I'm importing:
import cgi
import cgitb; cgitb.enable() # for troubleshooting
import sys
try:
import Foundation
import WebKit
import AppKit
import objc
except ImportError:
print "Cannot find pyobjc library files. Are you sure it is installed?"
sys.exit()
You cannot (usually) connect to the window server from a process not associated to a GUI user. See this Apple tech note.
Basically, it's a big no-no to use NSWindow etc. from the process spawned by Apache. The window server is not even guaranteed to exist if there's no GUI user logged in. So, you can't reliably do what you're trying to do.
The problem is that the WebKit which comes with OS X depends on the window server. One way out might be to install Qt, which hopefully has a backend of WebKit independent of the Core Graphics window server.
Related
This question is focused on Windows + LibreOffice + Python 3.
I've installed LibreOffice (6.3.4.2), also
pip install unoconv and pip install unotools (pip install uno is another unrelated library), but still I get this error after import uno:
ModuleNotFoundError: No module named 'uno'
More generally, and as an example of use of UNO, how to open a .docx document with LibreOffice UNO and export it to PDF?
I've searched extensively on this since a few days, but I haven't found a reproducible sample code working on Windows:
headless use of soffice.exe, see my question+answer Headless LibreOffice very slow to export to PDF on Windows (6 times slow than on Linux) and the notes on the answer: it "works" with soffice.exe --headless ... but something closer to a COM interaction (Component Object Model) would be useful for many applications, thus this question here
Related forum post, and LibreOffice: Programming with Python Scripts, but the way uno should be installed on Windows, with Python, is not detailed; also Detailed tutorial regarding LibreOffice to Python macro writing, especially for Calc
I've also tried this (unsuccessfully): Getting python to import uno / pyuno:
import os
os.environ["URE_BOOTSTRAP"] = r"vnd.sun.star.pathname:C:\Program Files\LibreOffice\program\fundamental.ini"
os.environ["PATH"] += r";C:\Program Files\LibreOffice\program"
import uno
In order to interact with LibreOffice, start an instance listening on a socket. I don't use COM much, but I think this is the equivalent of the COM interaction you asked about. This can be done most easily on the command line or using a shell script, but it can also work with a system call using a time delay and subprocess.
chdir "%ProgramFiles%\LibreOffice\program\"
start soffice -accept=socket,host=localhost,port=2002;urp;
Next, run the installation of python that comes with LibreOffice, which has uno installed by default.
"C:\Program Files\LibreOffice\program\python.exe"
>> import uno
If instead you are using an installation of Python on Windows that was not shipped with LibreOffice, then getting it to work with UNO is much more difficult, and I would not recommend it unless you enjoy hacking.
Now, here is all the code. In a real project, it's probably best to organize into classes, but this is a simplified version.
import os
import uno
from com.sun.star.beans import PropertyValue
def createProp(name, value):
prop = PropertyValue()
prop.Name = name
prop.Value = value
return prop
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", localContext)
ctx = resolver.resolve(
"uno:socket,host=localhost,port=2002;urp;"
"StarOffice.ComponentContext")
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext(
"com.sun.star.frame.Desktop", ctx)
dispatcher = smgr.createInstanceWithContext(
"com.sun.star.frame.DispatchHelper", ctx)
filepath = r"C:\Users\JimStandard\Desktop\Untitled 1.docx"
fileUrl = uno.systemPathToFileUrl(os.path.realpath(filepath))
uno_args = (
createProp("Minimized", True),
)
document = desktop.loadComponentFromURL(
fileUrl, "_default", 0, uno_args)
uno_args = (
createProp("FilterName", "writer_pdf_Export"),
createProp("Overwrite", False),
)
newpath = filepath[:-len("docx")] + "pdf"
fileUrl = uno.systemPathToFileUrl(os.path.realpath(newpath))
try:
document.storeToURL(fileUrl, uno_args) # Export
except ErrorCodeIOException:
raise
try:
document.close(True)
except CloseVetoException:
raise
Finally, since speed is a concern, using a listening instance of LibreOffice can be slow. To do this faster, move the code into a macro. APSO provides a menu to organize Python macros. Then call the macro like this:
soffice "vnd.sun.star.script:myscript.py$name_of_maindef?language=Python&location=user"
In macros, obtain the document objects from XSCRIPTCONTEXT rather than the resolver.
I have a simple script which parses a file and loads it's contents to a database. I don't need a UI, but right now I'm prompting the user for the file to parse using raw_input which is most unfriendly, especially because the user can't copy/paste the path. I would like a quick and easy way to present a file selection dialog to the user, they can select the file, and then it's loaded to the database. (In my use case, if they happened to chose the wrong file, it would fail parsing, and wouldn't be a problem even if it was loaded to the database.)
import tkFileDialog
file_path_string = tkFileDialog.askopenfilename()
This code is close to what I want, but it leaves an annoying empty frame open (which isn't able to be closed, probably because I haven't registered a close event handler).
I don't have to use tkInter, but since it's in the Python standard library it's a good candidate for quickest and easiest solution.
Whats a quick and easy way to prompt for a file or filename in a script without any other UI?
Tkinter is the easiest way if you don't want to have any other dependencies.
To show only the dialog without any other GUI elements, you have to hide the root window using the withdraw method:
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
Python 2 variant:
import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
You can use easygui:
import easygui
path = easygui.fileopenbox()
To install easygui, you can use pip:
pip3 install easygui
It is a single pure Python module (easygui.py) that uses tkinter.
Try with wxPython:
import wx
def get_path(wildcard):
app = wx.App(None)
style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
dialog = wx.FileDialog(None, 'Open', wildcard=wildcard, style=style)
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
else:
path = None
dialog.Destroy()
return path
print get_path('*.txt')
pywin32 provides access to the GetOpenFileName win32 function. From the example
import win32gui, win32con, os
filter='Python Scripts\0*.py;*.pyw;*.pys\0Text files\0*.txt\0'
customfilter='Other file types\0*.*\0'
fname, customfilter, flags=win32gui.GetOpenFileNameW(
InitialDir=os.environ['temp'],
Flags=win32con.OFN_ALLOWMULTISELECT|win32con.OFN_EXPLORER,
File='somefilename', DefExt='py',
Title='GetOpenFileNameW',
Filter=filter,
CustomFilter=customfilter,
FilterIndex=0)
print 'open file names:', repr(fname)
print 'filter used:', repr(customfilter)
print 'Flags:', flags
for k,v in win32con.__dict__.items():
if k.startswith('OFN_') and flags & v:
print '\t'+k
Using tkinter (python 2) or Tkinter (python 3) it's indeed possible to display file open dialog (See other answers here). Please notice however that user interface of that dialog is outdated and does not corresponds to newer file open dialogs available in Windows 10.
Moreover - if you're looking on way to embedd python support into your own application - you will find out soon that tkinter library is not open source code and even more - it is commercial library.
(For example search for "activetcl pricing" will lead you to this web page: https://reviews.financesonline.com/p/activetcl/)
So tkinter library will cost money for any application wanting to embedd python.
I by myself managed to find pythonnet library:
Overview here: http://pythonnet.github.io/
Source code here: https://github.com/pythonnet/pythonnet
(MIT License)
Using following command it's possible to install pythonnet:
pip3 install pythonnet
And here you can find out working example for using open file dialog:
https://stackoverflow.com/a/50446803/2338477
Let me copy an example also here:
import sys
import ctypes
co_initialize = ctypes.windll.ole32.CoInitialize
# Force STA mode
co_initialize(None)
import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import OpenFileDialog
file_dialog = OpenFileDialog()
ret = file_dialog.ShowDialog()
if ret != 1:
print("Cancelled")
sys.exit()
print(file_dialog.FileName)
If you also miss more complex user interface - see Demo folder
in pythonnet git.
I'm not sure about portability to other OS's, haven't tried, but .net 5 is planned to be ported to multiple OS's (Search ".net 5 platforms", https://devblogs.microsoft.com/dotnet/introducing-net-5/ ) - so this technology is also future proof.
If you don't need the UI or expect the program to run in a CLI, you could parse the filepath as an argument. This would allow you to use the autocomplete feature of your CLI to quickly find the file you need.
This would probably only be handy if the script is non-interactive besides the filepath input.
Another os-agnostic option, use pywebview:
import webview
def webview_file_dialog():
file = None
def open_file_dialog(w):
nonlocal file
try:
file = w.create_file_dialog(webview.OPEN_DIALOG)[0]
except TypeError:
pass # user exited file dialog without picking
finally:
w.destroy()
window = webview.create_window("", hidden=True)
webview.start(open_file_dialog, window)
# file will either be a string or None
return file
print(webview_file_dialog())
Environment: python3.8.6 on Mac - though I've used pywebview on windows 10 before.
I just stumbled on this little trick for Windows only: run powershell.exe from subprocess.
import subprocess
sys_const = ssfDESKTOP # Starts at the top level
# sys_const = 0x2a # Correct value for "Program Files (0x86)" folder
powershell_browse = "(new-object -COM 'Shell.Application')."
powershell_browse += "BrowseForFolder(0,'window title here',0,sys_const).self.path"
ret = subprocess.run(["powershell.exe",powershell_browse], stdout=subprocess.PIPE)
print(ret.stdout.decode())
Note the optional use of system folder constants. (There's an obscure typo in shldisp.h that the "Program Files (0x86)" constant was assigned wrong. I added a comment with the correct value. Took me a bit to figure that one out.)
More info below:
System folder constants
I'm using a Nao robot, and the python SDK, and I'm trying to create my own module for it. Right now it is just a dummy module with one function: getJoke().
I have my file tellAJoke.py located under the path /home/nao/tellAJoke.py and I have updated the autoload.ini file to include the following:
[python]
/home/nao/tellAJoke.py
When I boot up the robot, it says what it normally does on startup, but also says what my getJoke() function returns, "This is a bad joke".
I'm not sure what I'm doing wrong here. When I ssh onto the robot, and run the code it runs just fine, but never when I want to import the module with ALProxy in Choreographe.
EDIT: I added the actual dummy code I have.
from naoqi import ALBroker
from naoqi import ALModule
from naoqi import ALProxy
import sys
class JokerModule(ALModule):
"""Tells you a random joke"""
def __init__(self, name):
print "WE HAVE INITED"
self.tts = ALProxy("ALTextToSpeech")
ALModule.__init__(self, name)
global memory
memory = ALProxy("ALMemory")
memory.subscribeToEvent("SayingJoke", "Joker", "getJoke")
def getJoke(self, *_args):
"""Returns a joke"""
self.tts.say("Joke time!")
def main():
"""main entry point"""
pip = '192.168.1.104'
pport = 9559
myBroker = ALBroker("myBroker", '0.0.0.0', 0, pip, pport)
global Joker
Joker = JokerModule("Joker")
speechProxy = ALProxy("ALTextToSpeech")
Joker.getJoke()
if __name__ == '__main__':
main()
Here is a guide on making services (a.k.a. "modules", but that term is confusing because it has another meaning in Python): http://doc.aldebaran.com/2-4/dev/libqi/guide/py-service.html (this doc is for NAOqi 2.4 but things should work mostly the same for 2.1, which is more often used on NAO)
But, you might want to try Robot Jumpstarter, that contains templates for various typical projects, including a python service (that works as explained in the document above).
clone it and run python jumpstart.py python-service tell-a-joke TellAJoke
... and it will generate a project that you can:
install on the robot witch Choregraphe
run in standalone with python tell-a-joke/app/scripts/tellajoke.py --qi-url your-naos-ip
... and in both cases you will be able to call it from from Choregraphe boxes etc.
(edit)
Now that you posted your code - in this specific case your problem is just that after Joker.getJoke(), your program reaches the end and terminates. The usual "modern" way of doing that would be with a qi.Application() that would .run() (all that is done in the jumpstarter template). You could do a while True: sleep(1) or something, which is not very pretty but would work (I recommend migrating to NAOqi 2, and instead of using ALProxy and ALBroker, use session.service and qi.Application ... the two are interoperable)
I have a simple script which parses a file and loads it's contents to a database. I don't need a UI, but right now I'm prompting the user for the file to parse using raw_input which is most unfriendly, especially because the user can't copy/paste the path. I would like a quick and easy way to present a file selection dialog to the user, they can select the file, and then it's loaded to the database. (In my use case, if they happened to chose the wrong file, it would fail parsing, and wouldn't be a problem even if it was loaded to the database.)
import tkFileDialog
file_path_string = tkFileDialog.askopenfilename()
This code is close to what I want, but it leaves an annoying empty frame open (which isn't able to be closed, probably because I haven't registered a close event handler).
I don't have to use tkInter, but since it's in the Python standard library it's a good candidate for quickest and easiest solution.
Whats a quick and easy way to prompt for a file or filename in a script without any other UI?
Tkinter is the easiest way if you don't want to have any other dependencies.
To show only the dialog without any other GUI elements, you have to hide the root window using the withdraw method:
import tkinter as tk
from tkinter import filedialog
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
Python 2 variant:
import Tkinter, tkFileDialog
root = Tkinter.Tk()
root.withdraw()
file_path = tkFileDialog.askopenfilename()
You can use easygui:
import easygui
path = easygui.fileopenbox()
To install easygui, you can use pip:
pip3 install easygui
It is a single pure Python module (easygui.py) that uses tkinter.
Try with wxPython:
import wx
def get_path(wildcard):
app = wx.App(None)
style = wx.FD_OPEN | wx.FD_FILE_MUST_EXIST
dialog = wx.FileDialog(None, 'Open', wildcard=wildcard, style=style)
if dialog.ShowModal() == wx.ID_OK:
path = dialog.GetPath()
else:
path = None
dialog.Destroy()
return path
print get_path('*.txt')
pywin32 provides access to the GetOpenFileName win32 function. From the example
import win32gui, win32con, os
filter='Python Scripts\0*.py;*.pyw;*.pys\0Text files\0*.txt\0'
customfilter='Other file types\0*.*\0'
fname, customfilter, flags=win32gui.GetOpenFileNameW(
InitialDir=os.environ['temp'],
Flags=win32con.OFN_ALLOWMULTISELECT|win32con.OFN_EXPLORER,
File='somefilename', DefExt='py',
Title='GetOpenFileNameW',
Filter=filter,
CustomFilter=customfilter,
FilterIndex=0)
print 'open file names:', repr(fname)
print 'filter used:', repr(customfilter)
print 'Flags:', flags
for k,v in win32con.__dict__.items():
if k.startswith('OFN_') and flags & v:
print '\t'+k
Using tkinter (python 2) or Tkinter (python 3) it's indeed possible to display file open dialog (See other answers here). Please notice however that user interface of that dialog is outdated and does not corresponds to newer file open dialogs available in Windows 10.
Moreover - if you're looking on way to embedd python support into your own application - you will find out soon that tkinter library is not open source code and even more - it is commercial library.
(For example search for "activetcl pricing" will lead you to this web page: https://reviews.financesonline.com/p/activetcl/)
So tkinter library will cost money for any application wanting to embedd python.
I by myself managed to find pythonnet library:
Overview here: http://pythonnet.github.io/
Source code here: https://github.com/pythonnet/pythonnet
(MIT License)
Using following command it's possible to install pythonnet:
pip3 install pythonnet
And here you can find out working example for using open file dialog:
https://stackoverflow.com/a/50446803/2338477
Let me copy an example also here:
import sys
import ctypes
co_initialize = ctypes.windll.ole32.CoInitialize
# Force STA mode
co_initialize(None)
import clr
clr.AddReference('System.Windows.Forms')
from System.Windows.Forms import OpenFileDialog
file_dialog = OpenFileDialog()
ret = file_dialog.ShowDialog()
if ret != 1:
print("Cancelled")
sys.exit()
print(file_dialog.FileName)
If you also miss more complex user interface - see Demo folder
in pythonnet git.
I'm not sure about portability to other OS's, haven't tried, but .net 5 is planned to be ported to multiple OS's (Search ".net 5 platforms", https://devblogs.microsoft.com/dotnet/introducing-net-5/ ) - so this technology is also future proof.
If you don't need the UI or expect the program to run in a CLI, you could parse the filepath as an argument. This would allow you to use the autocomplete feature of your CLI to quickly find the file you need.
This would probably only be handy if the script is non-interactive besides the filepath input.
Another os-agnostic option, use pywebview:
import webview
def webview_file_dialog():
file = None
def open_file_dialog(w):
nonlocal file
try:
file = w.create_file_dialog(webview.OPEN_DIALOG)[0]
except TypeError:
pass # user exited file dialog without picking
finally:
w.destroy()
window = webview.create_window("", hidden=True)
webview.start(open_file_dialog, window)
# file will either be a string or None
return file
print(webview_file_dialog())
Environment: python3.8.6 on Mac - though I've used pywebview on windows 10 before.
I just stumbled on this little trick for Windows only: run powershell.exe from subprocess.
import subprocess
sys_const = ssfDESKTOP # Starts at the top level
# sys_const = 0x2a # Correct value for "Program Files (0x86)" folder
powershell_browse = "(new-object -COM 'Shell.Application')."
powershell_browse += "BrowseForFolder(0,'window title here',0,sys_const).self.path"
ret = subprocess.run(["powershell.exe",powershell_browse], stdout=subprocess.PIPE)
print(ret.stdout.decode())
Note the optional use of system folder constants. (There's an obscure typo in shldisp.h that the "Program Files (0x86)" constant was assigned wrong. I added a comment with the correct value. Took me a bit to figure that one out.)
More info below:
System folder constants
I've got a IIS 7.5 web server runnig a python website using the isapi-wsgi module. As the website is bound to port 80 I would rather like the python website to be running under an added application to this website.
The code generating the corresponding isapi module looks like this
import isapi_wsgi
def install_virtual_dir():
import isapi.install
params = isapi.install.ISAPIParameters()
# Setup the virtual directories - this is a list of directories our
# extension uses - in this case only 1.
# Each extension has a "script map" - this is the mapping of ISAPI
# extensions.
sm = [isapi.install.ScriptMapParams(Extension="*", Flags=0)]
vd = isapi.install.VirtualDirParameters(
Server="dsh",
Name=site_root,
Description="CherryPy Application Stable",
ScriptMaps=sm,
ScriptMapUpdate="end",
)
params.VirtualDirs = [vd]
isapi.install.HandleCommandLine(params)
if __name__ == '__main__':
# If run from the command-line, install ourselves.
install_virtual_dir()
Unfortunately the module is unable to register the corresponding dll resulting in the error
ItemNotFound: No web sites match the description 'dsh'
although I've added the application 'dsh' to the Website. Something like 'Website/dsh' doesn't work either.
Does anybody have encountered the same problem and has a solution for that?
If somebody encounters the same problem, I never solved the problem, I did only find a work around, which is to simply commenting and thus not to using the line
params.VirtualDirs = [vd]
Instead manually add the resulting dll to the Handler Mapping within IIS.