diff --git a/CHANGELOG.md b/CHANGELOG.md index fc58da4..3eb9389 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +v 0.3.0 [23-Apr-2023] +--------------------- + - Add method post + v 0.2.0 [23-Sep-2022] --------------------- - Add DrawPage. diff --git a/VERSION b/VERSION index 0ea3a94..0d91a54 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.0 +0.3.0 diff --git a/source/easymacro/__init__.py b/source/easymacro/__init__.py index 88ef035..2798484 100644 --- a/source/easymacro/__init__.py +++ b/source/easymacro/__init__.py @@ -11,7 +11,6 @@ from .easydrawpage import LOGalleries def __getattr__(name): classes = { 'active': LODocuments().active, - 'active_sheet': LODocuments().active.active, 'clipboard': ClipBoard, 'cmd': LOMain.commands, 'color': Color(), diff --git a/source/easymacro/easydoc.py b/source/easymacro/easydoc.py index 01cb6de..ca7d915 100644 --- a/source/easymacro/easydoc.py +++ b/source/easymacro/easydoc.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import uno import unohelper from com.sun.star.io import IOException, XOutputStream from .easymain import (log, diff --git a/source/easymacro/easydraw.py b/source/easymacro/easydraw.py index e8dce16..eb21b25 100644 --- a/source/easymacro/easydraw.py +++ b/source/easymacro/easydraw.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 from .easydoc import LODrawImpress +from .easydrawpage import LODrawPage class LODraw(LODrawImpress): @@ -8,3 +9,11 @@ class LODraw(LODrawImpress): def __init__(self, obj): super().__init__(obj) + + def __getitem__(self, index): + if isinstance(index, int): + page = self.obj.DrawPages[index] + else: + page = self.obj.DrawPages.getByName(index) + return LODrawPage(page) + diff --git a/source/easymacro/easydrawpage.py b/source/easymacro/easydrawpage.py index ef62b04..0895238 100644 --- a/source/easymacro/easydrawpage.py +++ b/source/easymacro/easydrawpage.py @@ -2,8 +2,13 @@ from com.sun.star.awt import Size, Point -from .easymain import BaseObject, create_instance, set_properties +from .easymain import ( + BaseObject, + create_instance, + dict_to_property, + set_properties) from .easyshape import LOShape +from .easydoc import IOStream DEFAULT_WH = 3000 @@ -218,19 +223,20 @@ class LODrawPage(BaseObject): y = args.get('Y', 1000) name = args.get('Name', f'image{index}') - image = self.create_instance('com.sun.star.drawing.GraphicObjectShape') + image = self._create_instance('com.sun.star.drawing.GraphicObjectShape') if isinstance(path, str): image.GraphicURL = _P.to_url(path) else: gp = create_instance('com.sun.star.graphic.GraphicProvider') - properties = dict_to_property({'InputStream': path}) + stream = IOStream.input(path) + properties = dict_to_property({'InputStream': stream}) image.Graphic = gp.queryGraphic(properties) self.obj.add(image) image.Size = Size(w, h) image.Position = Point(x, y) image.Name = name - return LOShape(self.obj[index], index) + return LOShape(self.obj[index]) class LOGalleryItem(BaseObject): diff --git a/source/easymacro/easymain.py b/source/easymacro/easymain.py index 805e45f..d50165f 100644 --- a/source/easymacro/easymain.py +++ b/source/easymacro/easymain.py @@ -1,14 +1,19 @@ #!/usr/bin/env python3 +import csv import datetime import getpass +import json import logging import os import platform import re import shutil +import subprocess import sys +import tempfile import threading +import zipfile from pathlib import Path from typing import Any, Union @@ -144,6 +149,7 @@ day = get_app_config(node, 'DD') DATE_OFFSET = datetime.date(year, month, day).toordinal() _info_debug = f"Python: {sys.version}\n\n{platform.platform()}\n\n" + '\n'.join(sys.path) +# ~ doc INFO_DEBUG = f"{NAME} v{VERSION} {LANGUAGE}\n\n{_info_debug}" @@ -501,16 +507,21 @@ class LOMain(): node = PropertyValue(Name='nodepath', Value=node_name) update = cp.createInstanceWithArguments(service, (node,)) + m = create_instance('mytools.Mri') + m.inspect(update) + result = True try: if disable: new_node = update.createInstanceWithArguments(()) new_node.setPropertyValue('Command', command) - update.insertByName(NEW_NODE_NAME, new_node) + if not update.hasByName(NEW_NODE_NAME): + update.insertByName(NEW_NODE_NAME, new_node) else: update.removeByName(NEW_NODE_NAME) update.commitChanges() except Exception as e: + print(e) result = False return result @@ -1225,19 +1236,28 @@ class Paths(object): @classmethod def zip(cls, source: Union[str, tuple, list], target: str='') -> str: + """Zip files or directories + + :param source: Path source file or directory or list of files. + :type source: str or tuple or list + :param target: Path target + :type target: str + :return: Path target + :rtype: str + """ path_zip = target if not isinstance(source, (tuple, list)): - path, _, name, _ = _P(source).info - start = len(path) + 1 + path = Paths(source) + start = len(path.path) + 1 if not target: - path_zip = f'{path}/{name}.zip' + path_zip = f'{path.path}/{path.name}.zip' if isinstance(source, (tuple, list)): - files = [(f, f[len(_P(f).path)+1:]) for f in source] - elif _P.is_file(source): + files = [(f, f[len(Paths(f).path)+1:]) for f in source] + elif Paths.is_file(source): files = ((source, source[start:]),) else: - files = [(f, f[start:]) for f in _P.walk(source)] + files = [(f, f[start:]) for f in Paths.walk(source)] compression = zipfile.ZIP_DEFLATED with zipfile.ZipFile(path_zip, 'w', compression=compression) as z: @@ -1247,9 +1267,20 @@ class Paths(object): @classmethod def unzip(cls, source: str, target: str='', members=None, pwd=None): + """Unzip files + + :param source: Path source file zip. + :type source: str + :param target: Path target folder for extrac content zip. + :type target: str + :param members: Tuple of files in zip for extract. + :type members: tuple + :param pwd: Password of zip. + :type pwd: str + """ path = target if not target: - path = _P(source).path + path = Paths(source).path with zipfile.ZipFile(source) as z: if not pwd is None: pwd = pwd.encode() @@ -1259,7 +1290,14 @@ class Paths(object): return @classmethod - def zip_content(cls, path: str): + def zip_content(cls, path: str) -> list: + """Get files in zip + + :param path: Path source file zip. + :type path: str + :return: Content files + :rtype: list + """ with zipfile.ZipFile(path) as z: names = z.namelist() return names diff --git a/source/easymacro/easytools.py b/source/easymacro/easytools.py index e8da6ed..9e4c5cd 100644 --- a/source/easymacro/easytools.py +++ b/source/easymacro/easytools.py @@ -3,7 +3,7 @@ import csv import datetime import hashlib -import json +import json as jsonpy import os import re import shlex @@ -75,12 +75,14 @@ __all__ = [ 'error', 'errorbox', 'info', + 'question', 'mri', 'msgbox', 'render', 'save_log', 'set_app_config', 'sleep', + 'warning', ] @@ -144,7 +146,7 @@ def save_log(path: str, data: Any) -> bool: try: with open(path, 'a') as f: - f.write(f'{str(Dates.now())} - ') + f.write(f'{str(Dates.now)} - ') pprint(data, stream=f) except Exception as e: error(e) @@ -293,7 +295,10 @@ def errorbox(message: Any, title: str=TITLE) -> int: def sleep(seconds: int): - """Sleep + """Sleep for seconds + + :param seconds: Seconds for sleep. + :type seconds: int """ time.sleep(seconds) return @@ -708,8 +713,7 @@ class Email(): return body def send(self, message): - # ~ file_name = 'attachment; filename={}' - email = MIMEMultipart('alternative') + email = MIMEMultipart() email['From'] = self._sender email['To'] = message['to'] email['Cc'] = message.get('cc', '') @@ -728,7 +732,6 @@ class Email(): paths = (paths,) for path in paths: fn = Paths(path).file_name - # ~ print('NAME', fn) part = MIMEBase('application', 'octet-stream') part.set_payload(Paths.read_bin(path)) encoders.encode_base64(part) @@ -788,8 +791,8 @@ class Email(): :param server: Configuration for send emails :type server: dict - :param server: Dictionary con message or list of messages - :type server: dict or iterator + :param messages: Dictionary con message or list of messages + :type messages: dict or iterator """ if isinstance(messages, dict): messages = (messages,) @@ -1026,13 +1029,22 @@ class Timer(object): class URL(object): """Class for simple url open + + `See mureq `_ """ @classmethod def get(cls, url: str, **kwargs): return mureq.get(url, **kwargs) @classmethod - def post(cls, url: str, body=None, **kwargs): + def post(cls, url: str, body=None, json=None, **kwargs): + if json: + body = jsonpy.dumps(json).encode() + headers = {'Content-Type': 'application/json'} + if 'headers' in kwargs: + kwargs['headers'].update(headers) + else: + kwargs['headers'] = headers return mureq.post(url, body, **kwargs) diff --git a/source/tests/test_config.py b/source/tests/test_config.py index 2734f82..1cc6e69 100644 --- a/source/tests/test_config.py +++ b/source/tests/test_config.py @@ -5,7 +5,7 @@ OS = 'Linux' DESKTOP = 'gnome' PC = 'oficina' -USER = 'mau' +USER = 'elmau' IS_WIN = False IS_MAC = False