mottosso / Qt.py

Minimal Python 2 & 3 shim around all Qt bindings - PySide, PySide2, PyQt4 and PyQt5.
MIT License
908 stars 253 forks source link

compiling resources #88

Open hdd opened 8 years ago

hdd commented 8 years ago

Hi , is there any wrapped command for things like : pyside-rcc and pyside-uic , so does pick up the right one depending on the framework in use ?

hdd commented 8 years ago

wondering, if the tool has to work on more than one (eg: PySide/PySide2) ... should resource should be compiled for both ?

hdd commented 8 years ago

checking now, but should be compatible, so is matter of having some logic on when we compile the resource files.

hdd commented 8 years ago

ok looks like is not, as in the compiled resouce file we end up (eg: pyside) with from

PySide import QtCore

mottosso commented 8 years ago

There isn't any wrapper at the moment, but one could potentially be built in.

I reckon that if the resulting resources can't be used in all bindings, then there probably isn't much point in wrapping it.

If you were to provide a wrapper, it could get tested on each supported platform and each supported binding via Travis and the pull request. Have a look at the currently ongoing pull request for load_ui for example.

fredrikaverpil commented 8 years ago

To my knowledge, pysideuic is its own module and is not part of PySide or PySide2. So far, we haven't attempted to map modules which aren't part of the actual bindings but I can't see why this could not be done.

How would such a module be imported though? Perhaps something like this:

from Qt import pysideuic

But then you'd probably just be better off handling this yourself since there's no equivalent in PyQt land...?

from Qt import __binding__

if __binding__ in ('PySide', 'PySide2'):
    import pysideuic
    do_pyside_stuff()
elif __binding__ in ('PyQt4', 'PyQt5'):
    import uic
    do_pyqt_stuff()
fredrikaverpil commented 8 years ago

Oh, wait, are we talking about the standalone executables such as pyside-uic.exe?

hdd commented 8 years ago

Thinking of fixing this in the various setup.py which are doing the compilation of the resources, replacing in the compiled resource :

.QtGui with Qt.QtGui On Aug 4, 2016 09:26, "Fredrik Averpil" notifications@github.com wrote: > Oh, wait, are we talking about the standalone executables such as > pyside-uic.exe? > > — > You are receiving this because you authored the thread. > Reply to this email directly, view it on GitHub > https://github.com/mottosso/Qt.py/issues/88#issuecomment-237474231, or mute > the thread > https://github.com/notifications/unsubscribe-auth/AAYavSjZZoMKINc4ScTLrN5hI7dqnXY8ks5qcZQJgaJpZM4JbonW > .
mottosso commented 8 years ago

Thinking of fixing this in the various setup.py which are doing the compilation of the resources, replacing in the compiled resource

Possible to show an example of this?

hdd commented 8 years ago

Sure, so when you compile an py from an rcc file, the first lines of the compiled one looks like this:

# -*- coding: utf-8 -*-

# Resource object code
#
# Created: Fri Aug 5 10:17:32 2016
#      by: The Resource Compiler for PySide (Qt v4.8.5)
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore
qt_resource_data = "......................."

This should be changed after the compilation as :

# -*- coding: utf-8 -*-

# Resource object code
#
# Created: Fri Aug 5 10:17:32 2016
#      by: The Resource Compiler for PySide (Qt v4.8.5)
#
# WARNING! All changes made in this file will be lost!

from Qt import QtCore
qt_resource_data = "......................."

I'll give this a shot and let you know. This way we should be able to provide backward compatibility.

hdd commented 8 years ago

so, in the setup.py of the tool I'm working in at the moment, I've been adding a post processing call which does pretty much this, which seems to be working fine :

import fileinput
filepath = '/home/efesto/Desktop/resource.py'
replace = 'from Qt import QtCore'
for line in fileinput.input(filepath, inplace=True):
    if 'import QtCore' in line:
        print line.replace(line, replace)
    else:
        print line

here it is in action: https://gitlab.com/4degrees/riffle/merge_requests/4/diffs#8e2edce0d507e1297474f25c00cae94258db38d8_53_54

hdd commented 8 years ago

wondering, does worth providing a wrapper with popen which kicks the compilation and replace ? it could be retained in a /bin folder under the project as qt-rcc

let me know. Cheers. L

mottosso commented 8 years ago

Interesting. I haven't used these features myself, so I'll have to rely on your experiences.

I'm a bit blurry on how the setup.py comes in to play. Are you saying that you distribute your software with a PyQt or PySide compiled resource file, and re-map it when setup.py is used? That is, during installation?

Could you show a small runnable example?

mottosso commented 8 years ago

To cut to the chase, what I'm looking for is the tests for this feature, before we start talking about an implementation.

The tests will more or less be the minimal, yet complete, examples that you come up with. Once there are tests, it will be more easily understood what your end game is and how we can reach it.

For example.

def test_compile_resources():
  import Qt
  Qt.compile_resources(sourcedir="/path/to/resources", targetfile="/path/to/my_resources.py")
  assert os.path.isfile("path/to/my_resources.py"), "nothing written"

This tells us, in just a few lines:

  1. How to use the end result
  2. What the interface will look like
  3. What the expected result is

The tests could also include importing this file and asserting some of its content, like (1) the variable in which the data resides and (2) the header comment and (3) the use of Qt instead of PySide etc.

mottosso commented 8 years ago

How about this for support coming from Qt.py?

  1. Compile natively with either PyQt or PySide
  2. Search-and-replace PyQt or PySide with Qt and write a new file
  3. Done

That way, developers can work natively and Qt.py doesn't have to wrap any functionality, whilst still being able to provide support for all bindings. Win-win-win.

The interface could look something like this.

$ pyside-uic my_resources # writes my_resources.py
$ python -m Qt --convert my_resources.py # writes my_resources_qt.py
fredrikaverpil commented 8 years ago

I wonder if they produce identical results though? If so, I agree this is a win-win-win 😉

hdd commented 8 years ago

isn't it a bit inconsistent as you are providing wrappers for loading the ui ? This two step process doesn't make much sense, either is done from within the code (Qt.compile_resource(in, out) ), or for me , simply worth keeping it on the various setup.py

mottosso commented 8 years ago

simply worth keeping it on the various setup.py

Fair enough. Let's make it part of the docs on how to do that, and we're done.

hdd commented 8 years ago

cool, then please remember to add this also for the uic files, otherwise people will be having all sorts of issues.

Cheers. L.

mottosso commented 8 years ago

then please remember to add this also for the uic files

What uic files? Sorry, I don't follow.

hdd commented 8 years ago

Ui (uic) files are created through designer-qt4 and the can be compiled (similarly to resources) to py using pyside-uic

This, usually, allow to load the ui faster than through the load ui command.

mottosso commented 8 years ago

I see, ok. Would you be up for writing this part of the README?

fredrikaverpil commented 7 years ago

@hdd please have a look at the latest release which implements the alpha feature --convert: https://github.com/mottosso/Qt.py/releases/tag/0.5.0

mottosso commented 7 years ago

I think Lorenzo is asking about compiling resources, such as png's and jpeg's, into a .py file. --convert only handles .ui files.

fredrikaverpil commented 7 years ago

Oh 😮 allright 😄