Doc for paths

This commit is contained in:
El Mau 2022-02-28 19:02:42 -06:00
parent ae710f4854
commit 9aa946e06e
2 changed files with 206 additions and 14 deletions

View File

@ -64,15 +64,6 @@ Get document path
app.debug(p.documents)
Get temporary directory
-----------------------
.. code-block:: python
p = app.path
app.debug(p.temp_dir)
Get path user profile
---------------------
@ -168,6 +159,95 @@ Verify if application exists
app.debug(app.path.exists_app(app_name))
Make temporary file
-------------------
It will be destroyed as soon as it is closed.
.. code-block:: python
f = app.path.temp_file()
f.write(app.NAME)
f.close()
If used in context, It will be destroyed too.
.. code-block:: python
with app.path.temp_file() as f:
app.debug(f.name)
f.write('test')
Make temporary directory
------------------------
On completion of the context or destruction of the temporary directory object, the newly created temporary directory and all its contents are removed from the filesystem.
.. code-block:: python
with app.path.temp_dir() as d:
app.debug(app.path.exists(d))
app.debug(d)
Get path for save
-----------------
Default open in user documents
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
path = app.path.get()
app.msgbox(path)
Open in other path
^^^^^^^^^^^^^^^^^^
.. code-block:: python
path_tmp = app.path.config('Temp')
path = app.path.get(path_tmp)
app.msgbox(path)
Add one filter
^^^^^^^^^^^^^^
.. code-block:: python
path = app.path.get(filters='xml')
app.msgbox(path)
Add multiple filters
^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
path = app.path.get(filters='xml,txt')
Select directory
----------------
Default open in user documents
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code-block:: python
path = app.path.get_dir()
app.debug(path)
Open in other path
^^^^^^^^^^^^^^^^^^
.. code-block:: python
path_tmp = app.path.config('Temp')
path_dir = app.paths.get_dir(path_tmp)
app.debug(path_dir)

View File

@ -51,6 +51,7 @@ import uno
from com.sun.star.awt import MessageBoxButtons as MSG_BUTTONS
from com.sun.star.awt.MessageBoxResults import YES
from com.sun.star.beans import PropertyValue, NamedValue
from com.sun.star.ui.dialogs import TemplateDescription
# Global variables
@ -91,6 +92,18 @@ FILES = {
}
DIRS = {}
MESSAGES = {
'es': {
'OK': 'Aceptar',
'Cancel': 'Cancelar',
'Select path': 'Seleccionar ruta',
'Select directory': 'Seleccionar directorio',
'Select file': 'Seleccionar archivo',
'Incorrect user or password': 'Nombre de usuario o contraseña inválidos',
'Allow less secure apps in GMail': 'Activa: Permitir aplicaciones menos segura en GMail',
}
}
CTX = uno.getComponentContext()
SM = CTX.getServiceManager()
@ -176,6 +189,16 @@ day = get_app_config(node, 'DD')
DATE_OFFSET = datetime.date(year, month, day).toordinal()
def _(msg):
if LANG == 'en':
return msg
if not LANG in MESSAGES:
return msg
return MESSAGES[LANG][msg]
def set_app_config(node_name: str, key: str, new_value: Any) -> Any:
"""Update value for key in node name.
@ -956,11 +979,6 @@ class Paths(object):
"""Get user save documents"""
return self.config()
@_classproperty
def temp_dir(self):
"""Get temporary directory in system"""
return tempfile.gettempdir()
@_classproperty
def user_profile(self):
"""Get path user profile"""
@ -1065,6 +1083,100 @@ class Paths(object):
result = bool(shutil.which(name_app))
return result
@classmethod
def temp_file(self):
"""Make temporary file"""
return tempfile.NamedTemporaryFile(mode='w')
@classmethod
def temp_dir(self):
"""Make temporary directory"""
return tempfile.TemporaryDirectory(ignore_cleanup_errors=True)
@classmethod
def get(cls, init_dir: str='', filters: str='') -> str:
"""Get path for save
:param init_dir: Initial default path
:type init_dir: str
:param filters: Filter for show type files: 'xml' or 'txt,xml'
:type filters: str
:return: Selected path
:rtype: str
`See API <https://api.libreoffice.org/docs/idl/ref/namespacecom_1_1sun_1_1star_1_1ui_1_1dialogs_1_1TemplateDescription.html>`_
"""
if not init_dir:
init_dir = cls.documents
init_dir = cls.to_url(init_dir)
file_picker = create_instance(cls.FILE_PICKER)
file_picker.setTitle(_('Select path'))
file_picker.setDisplayDirectory(init_dir)
file_picker.initialize((TemplateDescription.FILEOPEN_SIMPLE,))
if filters:
for f in filters.split(','):
file_picker.appendFilter(f.upper(), f'*.{f.lower()}')
path = ''
if file_picker.execute():
path = cls.to_system(file_picker.getSelectedFiles()[0])
return path
@classmethod
def get_dir(cls, init_dir: str='') -> str:
"""Get path dir
:param init_dir: Initial default path
:type init_dir: str
:param filters: Filter for show type files: 'xml' or 'txt,xml'
:type filters: str
:return: Selected path
:rtype: str
"""
folder_picker = create_instance(cls.FOLDER_PICKER)
if not init_dir:
init_dir = cls.documents
init_dir = cls.to_url(init_dir)
folder_picker.setTitle(_('Select directory'))
folder_picker.setDisplayDirectory(init_dir)
path = ''
if folder_picker.execute():
path = cls.to_system(folder_picker.getDirectory())
return path
@classmethod
def get_file(cls, init_dir: str='', filters: str='', multiple: bool=False):
"""
Get path file
init_folder: folder default open
filters: 'xml' or 'xml,txt'
multiple: True for multiple selected
"""
if not init_dir:
init_dir = cls.documents
init_dir = cls.to_url(init_dir)
file_picker = create_instance(cls.FILE_PICKER)
file_picker.setTitle(_('Select file'))
file_picker.setDisplayDirectory(init_dir)
file_picker.setMultiSelectionMode(multiple)
if filters:
filters = [(f.upper(), f'*.{f.lower()}') for f in filters.split(',')]
file_picker.setCurrentFilter(filters[0][0])
for f in filters:
file_picker.appendFilter(f[0], f[1])
path = ''
if file_picker.execute():
files = file_picker.getSelectedFiles()
path = [cls.to_system(f) for f in files]
if not multiple:
path = path[0]
return path
# ~ Save/read data
@classmethod