QtCreator - Import folders into resources? - c++

Is it somehow possible to import a folder structure into a resources file?
I have folders such as:
- JS
- Lots of JS files
- Images
- Lots of images
etc.
I can only seem to import one file at a time, I have around 80 files.
Any ideas?

I am not aware of anything, but this python script should give you some idea for a quick'n dirty workaround:
main.py
import os
print('<RCC>')
print('<qresource prefix="/">')
for root, dirs, files in os.walk(sys.argv[1]):
for name in files:
print('<file>%s</file>' % name)
print('</qresource>')
print('/RCC')
Then, you can invoke the script as follows:
python main.py myfolder
Diclaimer: the code does not check against len(sys.argv), etc, but it would be pretty simple to extend and hand-craft it further. It does not write into a dedicated file either, but you can redirect the output, and so on.

Related

Importing module not in path relative to current file

I'm working on a system where I can't add a new module by adding it's path to sys.path. Instead, I want to place the module in the same folder as the files using it, and then import the module on runtime using imp or importlib (or similar).
I've tried to use both imp and importlib, but can not get it to work. Time will tell if I'm just misinterpreting how and what params to specify when using either of the two libraries.
The folder structure for my project is defined like this:
root-folder-in-sys-path/
- file1.py
- file2.py
- file3.py
- my-module/
--- __init__.py
--- helper1.py
--- helper2.py
As my example indicates, the root folder is part of sys.path. The files (file1.py etc.) is part of the system and is from where I need access to the module. Only files that contains classes of a specific type is added, so it will not be possible just to add the module files in the root to load them, as they will be ignored. Best case would be if helper1.py to helper-n.py is made available - otherwise It is ok if only one is loaded.
Thanks.
I was able to come up with a solution that loads anyone of the helpers. I guess it could easily be made into a package with many more modules this way as well.
To for example access helper1.py in file1.py, this will work:
import os.path, imp
path = os.path.abspath(os.path.join(os.path.dirname(__file__), "my-module/helper1.py"))
im = imp.load_source("helper1", path)
If you find a better solution, then please let me know!

webapp2 - how do I include library

I am testing Google Cloud and first I wanted to develop something on my PC before I use in on google cloud.
I am using APACHE and configured it in that way, that when I am going to the page localhost/wsgi_app I see my page which physically is stored in folder /svc/http/webapp2. File wsgi_app.py which contains my app is stored in subfolder webapp2. All works fine. I provide this information just in case it might play any role.
The issue I have is with import from library.
I did it on Django and now try to move it to webapp2.
The first lines of my program look like this:
import webapp2
import MySQLdb
import json
I have file called test.py which contains some classess and funtions.
it is in the same folder as wsgi_app.py.
I want to include it, however this seems not to work:
import webapp2
import MySQLdb
import json
from test import *
my test.py contains definition of the class 'Quote', but when I call the page I see error
NameError: global name 'Quote' is not defined
When I put the inside of the file test.py in the file wsgi_app.py all works fine.
My goal is to separate the code into several files.
Any idea why :
from test import *
does not work ?
It worked on any other program I wrote, so why not here?
issue solved.
issue is not related to weapp2 but to WSGI and the path where python search for files.
Most simple solution is to add something like this ;
execfile("/srv/http/test.py")
it will import the file.
other than this here is quite good article
http://blog.dscpl.com.au/2014/09/python-module-search-path-and-modwsgi.html

pytest: robust file path of a txt file used in tests

During a pytest fixture, what is the best way to robustly get the location of a text file for users that may specify different working directories at runtime e.g. I want a person using the cmd line in the test fixture directory find the file as well as an integration server which may work in the project's root. Can I somehow include the text file in a module? What are best practices for including and getting access to non .py files?
I am aware of BASE_DIR = os.path.dirname(os.path.dirname(__file__)), but I am not sure if this will always refer to the same directory given a particular way of running the test suite.
os.path.dirname(os.path.abspath(__file__)) (which is what I think you meant above) worked fine for me so far - it should work as long as Python can figure the path of the file, and with pytest I can't imagine a scenario where that wouldn't be true.

Ember cli Managing dependencies for custom folders

I have an ember app, and a folder with a file playGame/game.js. This file includes game logic, and I want to import it for asset compilation.
If this file is under app/playGame/game.js and my Brocfile is like this:
app.import('app/playGame/game.js')
this gives the error, path or pattern app/playGame/game.js didn't match any files..
but if I put the file under bower_components/playGame/game.js and my Brocfile:
app.import('bower_components/playGame/game.js'), this compiles successfully.
What is the problem and solution here?
There are two parts to this:
Where should I put my file to import it as an asset?
Why isn't putting it in my app-folder working?
The way to do what you want is to create a folder called vendor in your root, put the file somewhere in there, and then import it in your Brocfile.js like so:
app.import('vendor/playGame/game.js');
This is documented on ember-cli.com, although somewhat hidden.
You could also put it in bower_components, but that folder is for things installed with bower, and could theoretically be deleted (in fact, this is a common recommendation to various issues). Things in bower_components is also not checked in to version control by default, which you probably want to do in this case.
This should solve your issue.
Now, why doesn't it work to put it in /app?
app is a special folder. From the documentation:
Contains your Ember application’s code. Javascript files in this
folder are compiled through the ES6 module transpiler and concatenated
into a file called app.js.
This is what makes it possible for you to import stuff from within your app. The folders in app is available directly under your <appname> namespace, along with some other files and folders like config/environment.
Example:
import myWidget from 'my-app/widgets/my-widget';`
The referenced file is /app/widgets/my-widget.js.
The ember-cli website has some more resources for how to use modules. Read those if this doesn't make any sense.
To sum up:
You could put your file in app, but that would make it part of your transpiled package, and you'd have to use it that way internally with an export and everything else that comes with it. It would end up as part of <appname>.js
You could put your file in vendor and import it in your Brocfile.js as explained above. It would be part of vendor.js and load before your app code.

The right way to add unique libraries to heroku (django app)

I'm trying to deploy a django app to heroku.
I have several python libraries which are not on PyPi and so I can't just declare them in requirements.txt file
In local development I've used:
import sys
sys.path += [os.path.dirname(os.path.dirname(__file__))+"\\project-name\\lib"]
inside manage.py and it works fine there.
Obviously it doesn't work on heroku and I get import errors.
What is the recommended way to add libraries like that on heroku?
Thanks.
One way to do it is include the libraries in the repository itself, from which you can import them. That means simply moving the actual folder for each library into your main Django project folder.
- DjangoProject
- AppFolder1
- AppFolder2 ...
- python-library1
- python-library2
When the repository is pushed to Heroku your libraries will be pushed as part of the project.
From here, your imports of these libraries within a view/model etc within any app's folder would
import python-library1
from python-library2 import a_function, a_class
The reason why I suggest the directory structure above is that, most likely, you would not have go back and change any import codes.
If you have a large number of libraries and would like to keep the direcory structure simpler, simply create a folder with a name such as "importables" in the main DjangoProject folder and change the import statements to something such as...
from importables import python-library1
from importables.python-library2 import a_function, a_class
It's not exactly beautiful, but a quick way to get the job done. If you aren't sure where the libraries you'd like to include are located, there's a few ways to quickly see their location using Python (How do I find the location of Python module sources?).