I am trying to have a Gtk.Application which stays unique and handles opening files. I am using python 2.7.3 on Ubuntu 12.04 with Gtk3 (fairly new to both python and Gtk)
The application runs fine without parameters, but it fails to get the file list when I run it trying to open a file. Here is the code, as minimalistic as I could make it:
#!/usr/bin/env python
import sys
from gi.repository import Gtk, Gio
def do_open(app, files, *hint):
print(app)
print(files)
print(hint)
def do_activate(app):
print "activate"
test = Gtk.Application(application_id="a.b", flags=Gio.ApplicationFlags.HANDLES_OPEN)
test.set_inactivity_timeout(10000)
test.connect("open", do_open)
test.connect("activate", do_activate)
test.run(sys.argv)
When I run the program without arguments it just prints "activate", which is fine. When I run it with a parameter (like ./test.py test.py) I get the following:
/usr/lib/python2.7/dist-packages/gi/types.py:43: Warning: g_value_get_boxed: assertion `G_VALUE_HOLDS_BOXED (value)' failed
return info.invoke(*args, **kwargs)
<Application object at 0x1c75230 (GtkApplication at 0x1cba0b0)>
[]
(1, '')
Does anyone understand why that assertion is failing and why I am getting an empty list of files?
As common as this task appears to be, I couldn't find any working example online either.
There is a bug in PyGObject. It is already reported in GNOME Bugzilla, check the bug report titled "Does not handle GFile in signal arguments".
Update: The bug was fixed in 2013. No more assertion and it returns the list of files (GFiles). In other words, the code works as expected (at least using 3.14):
$ python test.py test.py
<Application object at 0x7fcaf18f5d20 (GtkApplication at 0x11892b0)>
[<__main__.GLocalFile object at 0x7fcaf18a6050 (GLocalFile at 0x11aea00)>]
(1, '')
Related
I feel like I'm missing something real basic here, but looking up documentations, references and posts did not get me to any solution, so here goes:
I write Python code, install the functions-framework module and run the command on the terminal to start serving the script
The first curl call returns the expected value of my script ("Hello, World!", for example)
If I make any changes to the code ("Goodbye, World!"), the first values keeps been returned when curl'd
If I re-start the service, it returns the second value as expected
This happens in both Windows command line and Git Bash
I did re-install the module before trying again
Assuming that the module would've been developed to keep track of the source code update and return the last saved version of the code, can anyone help me understand where I'm messing up?
Thanks everybody for the attention!
-- Resources ---
1st code
import functions_framework
#functions_framework.http
def hello(requests):
return 'Hello, World'
2nd code
import functions_framework
#functions_framework.http
def hello(requests):
return 'Goodbye, World'
Framework call command
functions-framework --target=hello
Curl command
curl http://localhost:8080
I am trying to create a function in my automation python code that will open an editor or other text viewer with the test log file while it's been written.
My automation is based on pytest so I am executing my tests with pytest test_name.py command.
I searched in the Internet and found that with subprocess python package I can do it but when I wrote the following code:
def __open_visual_logging(self):
try:
return subprocess.Popen(["vim",self._filename])
except Exception:
return None
Nothing is happening when I'm running my tests.
(The return value is 1)
Is there other way to do this (packages etc.) ? or what am I doing wrong?
I want to have an object, generated by an existing MATLAB script, modelled in FreeCAD. See bolded titles for important content of Intro, Matlab code, python code, Testing via terminal, Matlab error, Notes (OS, versions)
Intro
I have each side of the process worked out, but I am having issues with the calling of the python script through MATLAB.
I have both object constructor, and a script that works in building things in FreeCAD; so essentially, I have either end. What I need now is to interface them.
My test is basic, but should have been enough, or so I assumed.
Matlab:
pyfile = '~/Desktop/FreeCADworkspace/testvec2.py';
filename = 'atestname';
call = "python " + pyfile + " " + filename;
[status,result] = system("sh ~/Desktop/FreeCADworkspace/freecad.sh")
Python:
# Import python roots
import sys
import os
sys.path.append('/usr/lib/freecad-python2/lib')
sys.path.append('/usr/lib/freecad/lib')
# Other imports
import numpy as np
import math
# Import FreeCAD and parts
import FreeCAD
from FreeCAD import Base
import Part, Sketcher, Draft
try:
filename = sys.argv[1]
except: # this should never be thrown, exists for testing and other integration
print('No arguments called into script.')
print('Please use format: python thisScript.py arg_filename')
quit() # exit script
print(filename) # checkmeplz
So, essentially what should happen is that I should get a print-out of the definition of filename in MATLAB. In this case, I should see 'atestname'
Testing
If I call this through terminal:
python ~/Desktop/FreeCADworkspace/testvec2.py atestname
I get:
FreeCAD 0.18.1, Libs: 0.18.1R
atestname
Which is exactly as expected. The same cannot be said of my MATLAB
MATLAB ERROR
Traceback (most recent call last):
File "/home/ashaiden/Desktop/FreeCADworkspace/testvec2.py", line 11, in <module>
import FreeCAD # no FreeCADGui??
ImportError: /usr/lib/freecad-python2/lib/libFreeCADBase.so: undefined symbol: _ZN11xercesc_3_111InputSource11setEncodingEPKt
I have also tried to execute the python script from MATLAB via a bash script. Same error.
What is confusing me: why would some command that executes perfectly through the terminal, be failing when calling via MATLAB?
I feel like I am misunderstanding how MATLAB system calls are performed. I assumed that the call was sent to, and processed by, the OS itself. But where that the case, it would not make sense to be getting an error. Does MATLAB do some interpreting of files that it is handling?
Notes
Operating system: Ubuntu 16.04
MATLAB 2018b
Python 2.7 -> is this potentially the issue? Python 2 seemed to be the default language for my particular FreeCAD install.
FreeCAD 0.18.1
Edit
After much searching by myself and my supervisor, it appears that this might be able to be put down to a conflict between binary files.
MATLAB has libxerces-c.so files defined inside /bin/glnxa64/ and another toolbox folder.
These may be conflicting with the linux binary /usr/lib/x86_64-linux-gnu/libxerces-c.so such that when the terminal is called via matlab, it uses the matlab binary instead of the system binary.
I will continue to investigate further.
Answering my own questions!
Edit 30/7 : This does not actually adequately solve the problem for linux, given that paths may have some slight difference between machines. It also does not solve the problem for mac or windows (the issue does occur on mac, but I have no data if it occurs on windows.
After digging and searching, I edited the question:
MATLAB has libxerces-c.so files defined inside /bin/glnxa64/ and
another toolbox folder.
These may be conflicting with the linux binary
/usr/lib/x86_64-linux-gnu/libxerces-c.so such that when terminal is
called via matlab, it uses the matlab binary instead of the system
binary.
I was correct that this was due to a conflict between MATLAB's bin libxerces-c.so and the linux native libxerces-c.so. I have found at least a partial fix for this. I do not know of its robustness. At worst, the path will be edited and then returned to original format each time FreeCAD will be called within my code (gross).
SOLUTION:
Important first step: oldpath = getenv("LD_LIBRARY_PATH") to get and then SAVE some record of the current path. This, for me, is in case some
issues or conflicts arise later.
I then removed the section of the path '/usr/local/MATLAB/R2018b/bin/glnxa64/' and saved the string to newpath
I then made this the new path: setenv("LD_LIBRARY_PATH", newpath),
where newpath no longer has the reference to the conflicting
folder
And now I am getting the expected result in my MATLAB command window:
FreeCAD 0.18.1, Libs: 0.18.1R
atestname
The comparative paths:
Newpath = '/usr/local/MATLAB/R2018b/sys/opengl/lib/glnxa64:/usr/local/MATLAB/R2018b/sys/os/glnxa64:/usr/local/MATLAB/R2018b/extern/lib/glnxa64:/usr/local/MATLAB/R2018b/runtime/glnxa64:/usr/local/MATLAB/R2018b/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2018b/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/lib/x86_64-linux-gnu/';
Oldpath = '/usr/local/MATLAB/R2018b/sys/opengl/lib/glnxa64:/usr/local/MATLAB/R2018b/sys/os/glnxa64:/usr/local/MATLAB/R2018b/bin/glnxa64:/usr/local/MATLAB/R2018b/extern/lib/glnxa64:/usr/local/MATLAB/R2018b/runtime/glnxa64:/usr/local/MATLAB/R2018b/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2018b/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/lib/x86_64-linux-gnu/';
I hope the title makes sense. To give specifics:
I am using csvtotable (https://github.com/vividvilla/csvtotable) to generate HTML tables from CSVs. I have installed via pip and am able to run a command line command:
csvtotable test1743.csv test1743.html
to generate a HTML page. All good so far.
I wanted to do this from within a Python script I had already written so I heard that subprocess was the way to do this. I looked up how to do it and understood that it can be done using the following:
subprocess.run('csvtotable test1743.csv test1743.html',shell=True)
So I tested this via the command line first by doing
python
from the command line and then running
import subprocess
subprocess.run('csvtotable test1743.csv test1743.html',shell=True)
Success! It worked. Fantastic.
However, when I try to do this from IDLE, it just returns a 1. I have checked the directory thinking that maybe the csv was missing from there, but it still doesn't work.
Am I misunderstanding how subprocess works?
Solved by finding a way to call the function without subprocess. I think the issue may have related to default arguments not being set when it is executed through python and hence why below I have had to specify so many arguments.
Code:
from csvtotable import convert
content = convert.convert("C:\\Users\\admin\\Google Drive\\test1743.csv",delimiter=",",quotechar='"',display_length=-1,overwrite=False,serve=False,pagination=True,virtual_scroll=1000, no_header=False, export=True, export_options=["copy","csv","json","print"])
convert.save("C:\\Users\\admin\\Google Drive\\test1743.html",content)
Note that the argument names had to be changed where they had a - in the name. I just changed any instance e.g. display-length to display_length in convert.py
Ive been using MSVC2010 for some time now combined with Python Tools 1.0
I'm currently working on a python project and my solution looks like this:
Solution
- Pyproj
- EmptyTestRunner
The pyproj contains a MainTest.py file that runs all my tests,
the EmptyTestRunner is just an empty project with a "Post-Build" action that runs the MainTest.py (thus running all my tests written in PyUnit on my python code each time i "re-build")
My problem is that running the python file, it produces normal python output - which because i'm running it as a post-build event, refers it to the MSVC output window.
MSVC2010 doesn't recognize this output as a "compile / linker" error (for example when an exception is raised in my python code which isn't caught) so it doesn't add it to the error window.
Is there a tool that already does this?
Is there a flag passed to the python.exe that can do it?
I've googled the subject throghouly but found nothing resembling it.
I'm about to just simply write a python script that will re-format the output of my pyproj to the msvc format.
thx in advance for any help!
I've done this with Visual Studio 9 (2008), so I don't know if the trick works for 2010, but anyway: the linking between the output window and the error manager in VS is automagical. All it expects is a line of the form:
path\to\file\name(lineno) : error : text of the error
e.g.
c:\dev\myfile.ext(17) : error : 'foo' is not a valid configuration directive
if your script somehow reads a configuration file.
Then when you double click this line, it opens the file at the specified line. The error message ('text of the error' here) also appears in the Error list window.
All other lines of your script's output, if they do not have this format, are not affected, so you may very well throw a lengthy explanation after the magical line - just as the C++ compiler does for template errors.
Whether you choose to modify your script to translate Python errors to a VS-compatible format or use a wrapper script is your decision - I'd favor the first one since you can catch all errors in the __main__ function, and VS-print them (given that you can still print the full Python trace just afterwards).