From 9aa946e06e2bc8561ae656ab7b8460ed4a3df0fa Mon Sep 17 00:00:00 2001 From: El Mau Date: Mon, 28 Feb 2022 19:02:42 -0600 Subject: [PATCH] Doc for paths --- docs/source/paths.rst | 98 +++++++++++++++++++++++++++++---- source/easymacro.py | 122 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 206 insertions(+), 14 deletions(-) diff --git a/docs/source/paths.rst b/docs/source/paths.rst index 24a9b20..bb7e95d 100644 --- a/docs/source/paths.rst +++ b/docs/source/paths.rst @@ -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) diff --git a/source/easymacro.py b/source/easymacro.py index 327acab..ee4ec08 100644 --- a/source/easymacro.py +++ b/source/easymacro.py @@ -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 `_ + """ + 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