615 lines
10 KiB
ReStructuredText
615 lines
10 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 extension from id
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
.. 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
|
|
|
|
|
|
Verify if application exists
|
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
app_name = 'nosoffice'
|
|
app.msgbox(app.paths.exists_app(app_name))
|
|
|
|
app_name = 'soffice'
|
|
app.msgbox(app.paths.exists_app(app_name))
|
|
|
|
|
|
Open any type file
|
|
^^^^^^^^^^^^^^^^^^
|
|
|
|
Open with default application in OS.
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/Downloads/file.pdf'
|
|
app.paths.open(path)
|
|
|
|
path = '/home/mau/Projects/libre_office/zaz/doc/build/index.html'
|
|
app.paths.open(path)
|
|
|
|
Save and read json
|
|
^^^^^^^^^^^^^^^^^^
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/data.json'
|
|
data = {
|
|
'type': 'calc',
|
|
'name': 'myfile.ods',
|
|
}
|
|
app.paths.to_json(path, data)
|
|
|
|
data = app.paths.from_json(path)
|
|
|
|
app.msgbox(data)
|
|
|
|
|
|
Save and read csv
|
|
^^^^^^^^^^^^^^^^^
|
|
|
|
You can used the same way that `python csv`_
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/data.csv'
|
|
data = (
|
|
(1, 'one', app.now()),
|
|
(2, 'two', app.now()),
|
|
(3, 'three', app.now()),
|
|
)
|
|
app.paths.to_csv(path, data)
|
|
|
|
data = app.paths.from_csv(path)
|
|
|
|
app.msgbox(data)
|
|
|
|
|
|
Zip, unzip
|
|
^^^^^^^^^^
|
|
|
|
For default, save in the same directory with the same name + `.zip`
|
|
|
|
* Zip file
|
|
|
|
.. code-block:: python
|
|
|
|
source = '/home/mau/logo.svg'
|
|
path_zip = app.paths.zip(source)
|
|
app.debug(path_zip)
|
|
|
|
* Zip directory
|
|
|
|
.. code-block:: python
|
|
|
|
source = '/home/mau/test'
|
|
path_zip = app.paths.zip(source)
|
|
app.debug(path_zip)
|
|
|
|
* Set target
|
|
|
|
.. code-block:: python
|
|
|
|
source = '/home/mau/logo.svg'
|
|
target = '/home/mau/myfile.zip'
|
|
path_zip = app.paths.zip(source, target)
|
|
app.debug(path_zip)
|
|
|
|
* Read content of zip
|
|
|
|
.. code-block:: python
|
|
|
|
path_zip = '/home/mau/test.zip'
|
|
names = app.paths.zip_content(path_zip)
|
|
for n in names:
|
|
app.debug(n)
|
|
|
|
* Unzip in the same directory
|
|
|
|
.. code-block:: python
|
|
|
|
path_zip = '/home/mau/test.zip'
|
|
app.paths.unzip(path_zip)
|
|
|
|
* Unzip in other directory
|
|
|
|
.. code-block:: python
|
|
|
|
path_zip = '/home/mau/test.zip'
|
|
target = '/srv/mau'
|
|
app.paths.unzip(path_zip, target)
|
|
|
|
* Extract one or more files
|
|
|
|
.. code-block:: python
|
|
|
|
path_zip = '/home/mau/test.zip'
|
|
members = 'calc.ods'
|
|
app.paths.unzip(path_zip, members=members)
|
|
|
|
# or
|
|
|
|
members = ('calc.ods', 'subdir/file.md')
|
|
|
|
* Merge zips
|
|
|
|
.. code-block:: python
|
|
|
|
zips = (
|
|
'/home/mau/test.zip',
|
|
'/home/mau/pictures.zip',
|
|
)
|
|
target = '/srv/mau/file.zip'
|
|
result = app.paths.merge_zip(target, zips)
|
|
app.msgbox(result)
|
|
|
|
|
|
Copy files
|
|
^^^^^^^^^^
|
|
|
|
Copy with the same name
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/image.png'
|
|
target = '/home/mau/Pictures'
|
|
path_new = app.paths.copy(path, target)
|
|
app.msgbox(path_new)
|
|
|
|
Copy with other name
|
|
|
|
.. code-block:: python
|
|
|
|
path = '/home/mau/image.png'
|
|
target = '/home/mau/Pictures'
|
|
new_name = 'other_name.png'
|
|
path_new = app.paths.copy(path, target, name)
|
|
app.msgbox(path_new)
|
|
|
|
|
|
.. _API XPathSettings: http://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1util_1_1XPathSettings.html
|
|
.. _python csv: https://docs.python.org/3.7/library/csv.html
|