Introduction file2py is a module that helps to handle conversion of
binary (as well as text) files to python's source file. Simply embed it
base64. Supports adding/deleting files, templates that facilitate the
further use. It comes with command line tool file2py
and predefined
templates like qt, pyside, pyqt.
You can use pip:
pip install file2py
or directly from Github
pip install git+https://github.com/kwarunek/file2py.git
or manually
git clone https://github.com/kwarunek/file2py.git cd file2py python setup.py install
To convert file to py simply specified target file with -f option and files to add:
file2py -f my_first.py some.jpg some.png
There is alias image2py which do the pretty much the same:
image2py -f my_second.py some.jpg some.png
If target file exists and it is result of file2py, all converted files will be appended, otherwise an error will occur.
image2py -f my_first.py some_other_file.jpg **Warning**: Files with the same name will be overwritten, instead of append.
By default BasicTemplate
will be used. With -t option you can
choose one of:
- pyside (add imports and function helpers PySide)
- pyqt (add imports and function helpers PyQt4)
- qt (combined two above with fallbacks)
- basic
For example:
image2py -t qt -f icons.py small.png, mini.ico
image2py -f my_imgs.py one.jpg two.png three.gif
>>> import my_imgs
>>> dir(my_imgs)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'base64', 'data', 'get_data', 'get_decoded', 'list_files', 'template']
>>> my_imgs.list_files()
['one.jpg', 'three.gif', 'two.png']
>>> my_imgs.get_data('one.jpg') #get file's data (base64 encoded)
# some base64 data
>>> my_imgs.get_decoded('one.jpg') #get file's data (raw, base64 decoded)
# some raw data
>>> my_imgs.template #holds the name of template
'BasicTemplate'
>>> my_imgs.data #holds data dict
{'one.jpg': 'base64 data', 'three.gif': 'base64 data', 'two.png': 'base64 data'}
Qt, PySide, PyQt templates provide additional functions:
getAsQByteArray(filename)
returns data as QtCore.QByteArray objectgetAsQPixmap(filename)
returns data as QtGui.QPixmap objectgetAsQIcon(filename)
returns data as QtGui.QIcon objectgetAsQImage(filename)
returns data as QtGui.QImage object
file2py provide two objects:
Converter
templates
(submodule contaningBasicTemplate
,QtTemplate
, ...)
All templates should extend BasicTemplate
. For more information see docstrings:).
- Fork https://github.com/kwarunek/file2py
- Improve
- Pull request
- more tests
- more templates
- add delete to command line tool
MIT