diff --git a/source/easymacro.py b/source/easymacro.py index d6aaeb4..1df04cf 100644 --- a/source/easymacro.py +++ b/source/easymacro.py @@ -526,11 +526,6 @@ def set_properties(model, properties): return -def array_to_dict(values): - d = {r[0]: r[1] for r in values} - return d - - # ~ Custom classes class ObjectBase(object): diff --git a/source/easymacro2.py b/source/easymacro2.py index 75b38f0..1d0af36 100644 --- a/source/easymacro2.py +++ b/source/easymacro2.py @@ -26,6 +26,7 @@ import platform import socket import subprocess import sys +import threading import time from enum import IntEnum @@ -56,8 +57,11 @@ try: from peewee import Database, DateTimeField, DateField, TimeField, \ __exception_wrapper__ except ImportError as e: + Database = object + DateField = object + TimeField = object + DateTimeField = object print('Install peewee') - peewee = None LOG_FORMAT = '%(asctime)s - %(levelname)s - %(message)s' @@ -114,7 +118,7 @@ INFO_DEBUG = f"{sys.version}\n\n{platform.platform()}\n\n" + '\n'.join(sys.path) SECONDS_DAY = 60 * 60 * 24 - +_MACROS = {} CTX = uno.getComponentContext() SM = CTX.getServiceManager() @@ -160,14 +164,20 @@ d = get_app_config(node, 'DD') DATE_OFFSET = datetime.date(y, m, d).toordinal() +def error(info): + log.error(info) + return + + def debug(*args): data = [str(a) for a in args] log.debug('\t'.join(data)) return -def error(info): - log.error(info) +def info(*args): + data = [str(a) for a in args] + log.info('\t'.join(data)) return @@ -255,6 +265,24 @@ def dict_to_property(values: dict, uno_any: bool=False): return ps +def _array_to_dict(values): + d = {v[0]: v[1] for v in values} + return d + +def _property_to_dict(values): + d = {v.Name: v.Value for v in values} + return d + + +def data_to_dict(data): + if isinstance(data, tuple) and isinstance(data[0], tuple): + return _array_to_dict(data) + + if isinstance(data, tuple) and isinstance(data[0], (PropertyValue, NamedValue)): + return _property_to_dict(data) + return {} + + def _path_url(path: str) -> str: if path.startswith('file://'): return path @@ -319,6 +347,81 @@ def _struct_to_date(value): return d +def _get_url_script(args): + library = args['library'] + module = '.' + name = args['name'] + language = args.get('language', 'Python') + location = args.get('location', 'user') + + if language == 'Python': + module = '.py$' + elif language == 'Basic': + module = f".{module}." + if location == 'user': + location = 'application' + + url = 'vnd.sun.star.script' + url = f'{url}:{library}{module}{name}?language={language}&location={location}' + + return url + + +def _call_macro(args): + #~ https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework_URI_Specification + + url = _get_url_script(args) + args = args.get('args', ()) + + service = 'com.sun.star.script.provider.MasterScriptProviderFactory' + factory = create_instance(service) + script = factory.createScriptProvider('').getScript(url) + result = script.invoke(args, None, None)[0] + + return result + + +def call_macro(args, in_thread=False): + result = None + if in_thread: + t = threading.Thread(target=_call_macro, args=(args,)) + t.start() + else: + result = _call_macro(args) + return result + + +class TimerThread(threading.Thread): + + def __init__(self, event, seconds, macro): + threading.Thread.__init__(self) + self.stopped = event + self.seconds = seconds + self.macro = macro + + def run(self): + info('Timer started... {}'.format(self.macro['name'])) + while not self.stopped.wait(self.seconds): + _call_macro(self.macro) + info('Timer stopped... {}'.format(self.macro['name'])) + return + + +def start_timer(name, seconds, macro): + global _MACROS + _MACROS[name] = threading.Event() + thread = TimerThread(_MACROS[name], seconds, macro) + thread.start() + return + + +def stop_timer(name): + global _MACROS + _MACROS[name].set() + del _MACROS[name] + return + + class LOBaseObject(object): def __init__(self, obj): @@ -1943,11 +2046,8 @@ class LODialog(object): class LOSheets(object): - def __init__(self): - pass - def __getitem__(self, index): - return docs.active[index] + return LODocs().active[index] def __enter__(self): return self @@ -1958,11 +2058,8 @@ class LOSheets(object): class LOCells(object): - def __init__(self): - pass - def __getitem__(self, index): - return docs.active.active[index] + return LODocs().active.active[index] class Paths(object): @@ -1985,6 +2082,10 @@ def __getattr__(name): return Paths() if name == 'docs': return LODocs() + if name == 'sheets': + return LOSheets() + if name == 'cells': + return LOCells() raise AttributeError(f"module '{__name__}' has no attribute '{name}'") @@ -1998,10 +2099,6 @@ def get_fonts(): return device.FontDescriptors -# ~ docs = LODocs() -sheets = LOSheets() -cells = LOCells() - # ~ https://en.wikipedia.org/wiki/Web_colors def get_color(value): COLORS = {