Doc for paths
This commit is contained in:
parent
0b06a0b0ce
commit
71d40fa6fa
|
@ -312,4 +312,237 @@ Can select multiple files
|
||||||
path_files = app.path.get_file(multiple=True)
|
path_files = app.path.get_file(multiple=True)
|
||||||
|
|
||||||
|
|
||||||
|
Get files
|
||||||
|
---------
|
||||||
|
|
||||||
|
Get files not recursively
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
path = '/home/mau/Documents'
|
||||||
|
files = app.path.files(path)
|
||||||
|
for f in files:
|
||||||
|
app.debug(f)
|
||||||
|
|
||||||
|
|
||||||
|
Add filter
|
||||||
|
^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
files = app.path.files(path, '*.pdf')
|
||||||
|
|
||||||
|
|
||||||
|
Get content files, recursively
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
files = app.path.files(path, '**/*.pdf')
|
||||||
|
|
||||||
|
|
||||||
|
Get content files recursively
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
This method use `os.walk`
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
path = '/home/mau/Documents'
|
||||||
|
files = app.path.walk(path)
|
||||||
|
for f in files:
|
||||||
|
app.debug(f)
|
||||||
|
|
||||||
|
|
||||||
|
Add filter
|
||||||
|
^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
files = app.path.walk(path, 'ods')
|
||||||
|
|
||||||
|
# or filters
|
||||||
|
|
||||||
|
files = app.path.walk(path, 'ods|odt')
|
||||||
|
|
||||||
|
|
||||||
|
Get directories
|
||||||
|
---------------
|
||||||
|
|
||||||
|
Get directories not recursively
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
This method use library `pathlib`
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
path = '/home/mau/Documents'
|
||||||
|
folders = app.path.dirs(path)
|
||||||
|
for f in folders:
|
||||||
|
app.debug(f)
|
||||||
|
|
||||||
|
|
||||||
|
Get directories recursively
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
This method use `os.walk`
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
path = '/home/mau/Documents'
|
||||||
|
folders = app.path.walk_dirs(path)
|
||||||
|
for f in folders:
|
||||||
|
app.debug(f)
|
||||||
|
|
||||||
|
|
||||||
|
Get info in a tuple
|
||||||
|
^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
Like (ID_FOLDER, ID_PARENT, NAME)
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
path = '/home/mau/Documents'
|
||||||
|
folders = app.path.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.path.ext(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
|
||||||
|
|
||||||
|
|
||||||
|
Replace extension
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
path = '/home/mau/myFile.ods'
|
||||||
|
path_new = app.path.replace_ext(path, 'pdf')
|
||||||
|
app.debug(path_new)
|
||||||
|
|
||||||
|
|
||||||
|
Open any type file
|
||||||
|
------------------
|
||||||
|
|
||||||
|
Open with default application in OS.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
path = '/home/mau/file.pdf'
|
||||||
|
app.path.open(path)
|
||||||
|
|
||||||
|
path = '/home/mau/index.html'
|
||||||
|
app.path.open(path)
|
||||||
|
|
||||||
|
|
||||||
|
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.path.save(path, data)
|
||||||
|
|
||||||
|
data = app.path.read(path)
|
||||||
|
app.msgbox(data)
|
||||||
|
|
||||||
|
Change encoding
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
app.path.save(path, data, 'iso-8859-1')
|
||||||
|
|
||||||
|
|
||||||
|
Save and read binary data
|
||||||
|
-------------------------
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
data = b'Binary data'
|
||||||
|
path = '/home/mau/temp.bin'
|
||||||
|
app.path.save_bin(path, data)
|
||||||
|
|
||||||
|
data = app.path.read_bin(path)
|
||||||
|
app.msgbox(data)
|
||||||
|
|
||||||
|
|
||||||
|
Save and read json
|
||||||
|
------------------
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
path = '/home/mau/data.json'
|
||||||
|
data = {
|
||||||
|
'type': 'calc',
|
||||||
|
'name': 'myfile.ods',
|
||||||
|
}
|
||||||
|
app.path.to_json(path, data)
|
||||||
|
|
||||||
|
data = app.path.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.path.to_csv(path, data)
|
||||||
|
|
||||||
|
data = app.path.from_csv(path)
|
||||||
|
|
||||||
|
app.msgbox(data)
|
||||||
|
|
||||||
|
|
||||||
|
Delete files and directories
|
||||||
|
----------------------------
|
||||||
|
|
||||||
|
**CAUTION**: This method delete files and directories without confirmation, always ask to user first.
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
path = '/home/mau/temp.bin'
|
||||||
|
result = app.path.kill(path)
|
||||||
|
app.msgbox(result)
|
||||||
|
|
||||||
|
|
||||||
|
Delete directory and all content
|
||||||
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
path = '/home/mau/safe_for_delete'
|
||||||
|
result = app.path.kill(path)
|
||||||
|
app.msgbox(result)
|
||||||
|
|
||||||
|
|
||||||
|
.. _python csv: https://docs.python.org/3.7/library/csv.html
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
# ~ along with easymacro. If not, see <https://www.gnu.org/licenses/>.
|
# ~ along with easymacro. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
import csv
|
||||||
import datetime
|
import datetime
|
||||||
import getpass
|
import getpass
|
||||||
import hashlib
|
import hashlib
|
||||||
|
@ -25,6 +26,7 @@ import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import platform
|
import platform
|
||||||
|
import re
|
||||||
import shlex
|
import shlex
|
||||||
import shutil
|
import shutil
|
||||||
import socket
|
import socket
|
||||||
|
@ -1222,18 +1224,6 @@ class Paths(object):
|
||||||
files = [str(p) for p in Path(path).glob(pattern) if p.is_file()]
|
files = [str(p) for p in Path(path).glob(pattern) if p.is_file()]
|
||||||
return files
|
return files
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def dirs(cls, path):
|
|
||||||
"""Get directories in path
|
|
||||||
|
|
||||||
:param path: Path to scan
|
|
||||||
:type path: str
|
|
||||||
:return: Directories in path
|
|
||||||
:rtype: list
|
|
||||||
"""
|
|
||||||
dirs = [str(p) for p in Path(path).iterdir() if p.is_dir()]
|
|
||||||
return dirs
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def walk(cls, path, filters=''):
|
def walk(cls, path, filters=''):
|
||||||
"""Get all files in path recursively
|
"""Get all files in path recursively
|
||||||
|
@ -1254,6 +1244,18 @@ class Paths(object):
|
||||||
paths += [cls.join(folder, f) for f in files]
|
paths += [cls.join(folder, f) for f in files]
|
||||||
return paths
|
return paths
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def dirs(cls, path):
|
||||||
|
"""Get directories in path
|
||||||
|
|
||||||
|
:param path: Path to scan
|
||||||
|
:type path: str
|
||||||
|
:return: Directories in path
|
||||||
|
:rtype: list
|
||||||
|
"""
|
||||||
|
dirs = [str(p) for p in Path(path).iterdir() if p.is_dir()]
|
||||||
|
return dirs
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def walk_dirs(cls, path, tree=False):
|
def walk_dirs(cls, path, tree=False):
|
||||||
"""Get directories recursively
|
"""Get directories recursively
|
||||||
|
@ -1268,7 +1270,6 @@ class Paths(object):
|
||||||
folders = []
|
folders = []
|
||||||
if tree:
|
if tree:
|
||||||
i = 0
|
i = 0
|
||||||
p = 0
|
|
||||||
parents = {path: 0}
|
parents = {path: 0}
|
||||||
for root, dirs, _ in os.walk(path):
|
for root, dirs, _ in os.walk(path):
|
||||||
for name in dirs:
|
for name in dirs:
|
||||||
|
|
Loading…
Reference in New Issue