450 lines
7.5 KiB
ReStructuredText
450 lines
7.5 KiB
ReStructuredText
|
|
Paths and files
|
|
---------------
|
|
|
|
Remember, always import library.
|
|
|
|
.. code-block:: python
|
|
|
|
import easymacro as app
|
|
|
|
|
|
Get info path
|
|
^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/myfile.ods'
|
|
p = app.paths(path)
|
|
|
|
app.debug(p.path)
|
|
app.debug(p.file_name)
|
|
app.debug(p.name)
|
|
app.debug(p.ext)
|
|
app.debug(p.size)
|
|
app.debug(p.url)
|
|
|
|
Or get information in a tuple
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/myfile.ods'
|
|
p = app.paths(path)
|
|
|
|
app.msgbox(p.info)
|
|
|
|
|
|
Exists path
|
|
^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/test'
|
|
app.msgbox(app.paths.exists(path))
|
|
|
|
|
|
Path is file
|
|
^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/myfile.ott'
|
|
app.msgbox(app.paths.is_file(path))
|
|
|
|
|
|
Path is dir
|
|
^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau'
|
|
app.msgbox(app.paths.is_dir(path))
|
|
|
|
|
|
Get path home
|
|
^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path = app.paths.home
|
|
app.msgbox(path)
|
|
|
|
|
|
Get path documents
|
|
^^^^^^^^^^^^^^^^^^
|
|
|
|
* Configurate in LibreOffice Paths
|
|
|
|
.. code-block:: python
|
|
|
|
path = app.paths.documents
|
|
app.msgbox(path)
|
|
|
|
|
|
Get path temp
|
|
^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path = app.paths.temp_dir
|
|
app.msgbox(path)
|
|
|
|
|
|
Get path from LibreOffice configuration
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
* Default get path documents.
|
|
|
|
.. code-block:: python
|
|
|
|
path = app.paths.config()
|
|
app.msgbox(path)
|
|
|
|
* All options in `API XPathSettings`_
|
|
|
|
.. code-block:: python
|
|
|
|
path = app.paths.config('Config')
|
|
app.msgbox(path)
|
|
|
|
|
|
Get path executable python
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path_python = app.paths.python
|
|
app.msgbox(path_python)
|
|
|
|
|
|
Save and read text data
|
|
^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
* Default encoding is UTF8
|
|
|
|
.. code-block:: python
|
|
|
|
data = """Do you want to know who you are? Don't ask. Act!
|
|
Action will delineate and define you.
|
|
|
|
Thomas Jefferson
|
|
"""
|
|
|
|
path = '/home/mau/temp.txt'
|
|
app.paths.save(path, data)
|
|
|
|
data = app.paths.read(path)
|
|
app.msgbox(data)
|
|
|
|
* Change encoding
|
|
|
|
.. code-block:: python
|
|
|
|
app.paths.save(path, data, 'iso-8859-1')
|
|
|
|
|
|
Save and read binary data
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
data = b'Binary data'
|
|
path = '/home/mau/temp.bin'
|
|
app.paths.save_bin(path, data)
|
|
|
|
data = app.paths.read_bin(path)
|
|
app.msgbox(data)
|
|
|
|
|
|
Join paths
|
|
^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path_home = app.paths.home
|
|
file_name = 'test.ods'
|
|
path = app.paths.join(path_home, file_name)
|
|
|
|
app.msgbox(path)
|
|
|
|
|
|
Get a temporal path
|
|
^^^^^^^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path_temp = app.paths.tmp()
|
|
app.msgbox(path_temp)
|
|
|
|
* Get with extension.
|
|
|
|
.. code-block:: python
|
|
|
|
path_temp = app.paths.tmp('.txt')
|
|
app.msgbox(path_temp)
|
|
|
|
* Save data in a temporal path
|
|
|
|
.. code-block:: python
|
|
|
|
data = """He who receives an idea from me,
|
|
receives instruction himself without lessening mine;
|
|
as he who lights his taper at mine,
|
|
receives light without darkening me.
|
|
|
|
Thomas Jefferson
|
|
"""
|
|
path_tmp = app.paths.save_tmp(data)
|
|
|
|
app.msgbox(path_tmp)
|
|
|
|
|
|
Get a temporal dir
|
|
^^^^^^^^^^^^^^^^^^
|
|
|
|
* All content and directory is deleted when exit context.
|
|
|
|
.. code-block:: python
|
|
|
|
data = """Do you want to know who you are? Don't ask. Act!
|
|
Action will delineate and define you.
|
|
|
|
Thomas Jefferson
|
|
"""
|
|
|
|
with app.paths.dir_tmp() as dt:
|
|
app.debug(app.paths.exists(dt))
|
|
path = app.paths.join(dt, 'test.txt')
|
|
app.paths.save(path, data)
|
|
|
|
app.debug(app.paths.exists(dt))
|
|
|
|
|
|
Get path for save
|
|
^^^^^^^^^^^^^^^^^
|
|
|
|
* Default open in user documents.
|
|
|
|
.. code-block:: python
|
|
|
|
path = app.paths.get()
|
|
app.msgbox(path)
|
|
|
|
* Open in other path.
|
|
|
|
.. code-block:: python
|
|
|
|
path_tmp = app.paths.temp_dir
|
|
path = app.paths.get(path_tmp)
|
|
app.msgbox(path)
|
|
|
|
* Add one filter
|
|
|
|
.. code-block:: python
|
|
|
|
path = app.paths.get(filters='xml')
|
|
app.msgbox(path)
|
|
|
|
* Add multiple filters
|
|
|
|
.. code-block:: python
|
|
|
|
path = app.paths.get(filters='xml,txt')
|
|
|
|
|
|
Select directory
|
|
^^^^^^^^^^^^^^^^
|
|
|
|
* Default open in user documents.
|
|
|
|
.. code-block:: python
|
|
|
|
path_dir = app.paths.get_dir()
|
|
app.msgbox(path_dir)
|
|
|
|
* Open in other path.
|
|
|
|
.. code-block:: python
|
|
|
|
path_tmp = app.paths.temp_dir
|
|
path_dir = app.paths.get_dir(path_tmp)
|
|
app.msgbox(path_dir)
|
|
|
|
|
|
Get path exists file
|
|
^^^^^^^^^^^^^^^^^^^^
|
|
|
|
* Default open in user documents.
|
|
|
|
.. code-block:: python
|
|
|
|
path_file = app.paths.get_file()
|
|
app.msgbox(path_file)
|
|
|
|
* Change init dir.
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau'
|
|
path_file = app.paths.get_file(path)
|
|
app.msgbox(path_file)
|
|
|
|
* Add filter or filters.
|
|
|
|
.. code-block:: python
|
|
|
|
path_file = app.paths.get_file(filters='ods')
|
|
|
|
# or
|
|
|
|
path_file = app.paths.get_file(filters='ods,odt')
|
|
|
|
* Can select multiple files.
|
|
|
|
.. code-block:: python
|
|
|
|
path_file = app.paths.get_file(multiple=True)
|
|
|
|
|
|
Replace extension
|
|
^^^^^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/myFile.ods'
|
|
path_new = app.paths.replace_ext(path, 'pdf')
|
|
app.msgbox(path_new)
|
|
|
|
|
|
Path in format URL <-> System
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/myFile.ods'
|
|
path_url = app.paths.to_url(path)
|
|
app.msgbox(path_url)
|
|
|
|
path = app.paths.to_system(path_url)
|
|
app.msgbox(path)
|
|
|
|
|
|
Delete files and directories
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
**CAUTION**: Delete files and directories immediately, always confirm this action.
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/temp.bin'
|
|
result = app.paths.kill(path)
|
|
app.msgbox(result)
|
|
|
|
* Delete directory and all content.
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/safe_for_delete'
|
|
result = app.paths.kill(path)
|
|
app.msgbox(result)
|
|
|
|
|
|
Get files
|
|
^^^^^^^^^
|
|
|
|
This method use library `pathlib`
|
|
|
|
* Get files not recursively
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/Documents'
|
|
files = app.paths.files(path)
|
|
for f in files:
|
|
app.debug(f)
|
|
|
|
* Add filter
|
|
|
|
.. code-block:: python
|
|
|
|
files = app.paths.files(path, '*.pdf')
|
|
|
|
* Get from this directory and all subdirectories, recursively
|
|
|
|
.. code-block:: python
|
|
|
|
files = app.paths.files(path, '**/*.pdf')
|
|
|
|
This method use `os.walk`
|
|
|
|
* Get content files recursively
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/Documents'
|
|
files = app.paths.walk(path)
|
|
for f in files:
|
|
app.debug(f)
|
|
|
|
* Add filters
|
|
|
|
.. code-block:: python
|
|
|
|
files = app.paths.walk(path, 'ods')
|
|
|
|
# or filters
|
|
|
|
files = app.paths.walk(path, 'ods|odt')
|
|
|
|
|
|
Get directories
|
|
^^^^^^^^^^^^^^^
|
|
|
|
This method use library `pathlib`
|
|
|
|
* Get directories in path not recursively
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/Documents'
|
|
folders = app.paths.dirs(path)
|
|
for f in folders:
|
|
app.debug(f)
|
|
|
|
This method use `os.walk`
|
|
|
|
* Get directories in path recursively
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/Documents'
|
|
folders = app.paths.walk_dirs(path)
|
|
for f in folders:
|
|
app.debug(f)
|
|
|
|
* Get info in a tuple (ID_FOLDER, ID_PARENT, NAME)
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/Documents'
|
|
folders = app.paths.walk_dirs(path, True)
|
|
for f in folders:
|
|
app.debug(f)
|
|
|
|
|
|
Get install path from id extension
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
id_ext = 'net.elmau.zaz.EasyMacro'
|
|
path = app.paths.from_id(id_ext)
|
|
app.debug(path)
|
|
|
|
.. code-block:: bash
|
|
|
|
24/06/2021 21:47:29 - DEBUG - /home/mau/.config/libreoffice/4/user/uno_packages/cache/uno_packages/lu20665x29msz.tmp_/ZAZEasyMacro_v0.1.0.oxt
|
|
|
|
|
|
.. _API XPathSettings: http://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1util_1_1XPathSettings.html
|