Add support for search and replace

This commit is contained in:
Mauricio Baeza 2019-10-23 23:09:23 -05:00
parent e8dc0c8e99
commit 264dce2449
1 changed files with 83 additions and 12 deletions

View File

@ -41,12 +41,13 @@ import time
import traceback import traceback
import zipfile import zipfile
from collections import OrderedDict # ~ from collections import OrderedDict
from collections.abc import MutableMapping # ~ from collections.abc import MutableMapping
from functools import wraps from functools import wraps
from operator import itemgetter from operator import itemgetter
from pathlib import Path, PurePath from pathlib import Path, PurePath
from pprint import pprint from pprint import pprint
from enum import IntEnum
from urllib.request import Request, urlopen from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError from urllib.error import URLError, HTTPError
from string import Template from string import Template
@ -91,6 +92,13 @@ from com.sun.star.awt import XKeyListener
from com.sun.star.awt import XItemListener from com.sun.star.awt import XItemListener
from com.sun.star.awt import XFocusListener from com.sun.star.awt import XFocusListener
class FontSlant(IntEnum):
NONE = 0
OBLIQUE = 1
ITALIC = 2
DONTKNOW = 3
REVERSE_OBLIQUE = 4
REVERSE_ITALIC = 5
try: try:
from fernet import Fernet, InvalidToken from fernet import Fernet, InvalidToken
@ -336,18 +344,17 @@ def run_in_thread(fn):
return run return run
def now(): def now(only_time=False):
return datetime.datetime.now() now = datetime.datetime.now()
if only_time:
return now.time()
return now
def today(): def today():
return datetime.date.today() return datetime.date.today()
def time():
return datetime.datetime.now().time()
def get_date(year, month, day, hour=-1, minute=-1, second=-1): def get_date(year, month, day, hour=-1, minute=-1, second=-1):
if hour > -1 or minute > -1 or second > -1: if hour > -1 or minute > -1 or second > -1:
h = hour h = hour
@ -600,9 +607,6 @@ class LODocument(object):
def _init_values(self): def _init_values(self):
self._type_doc = get_type_doc(self.obj) self._type_doc = get_type_doc(self.obj)
# ~ if self._type_doc == 'base':
# ~ self._cc = self.obj.DatabaseDocument.getCurrentController()
# ~ else:
self._cc = self.obj.getCurrentController() self._cc = self.obj.getCurrentController()
return return
@ -1364,6 +1368,42 @@ class LOWriter(LODocument):
self._cc.select(text) self._cc.select(text)
return return
def search(self, options):
descriptor = self.obj.createSearchDescriptor()
descriptor.setSearchString(options.get('Search', ''))
descriptor.SearchCaseSensitive = options.get('CaseSensitive', False)
descriptor.SearchWords = options.get('Words', False)
if 'Attributes' in options:
attr = dict_to_property(options['Attributes'])
descriptor.setSearchAttributes(attr)
if hasattr(descriptor, 'SearchRegularExpression'):
descriptor.SearchRegularExpression = options.get('RegularExpression', False)
if hasattr(descriptor, 'SearchType') and 'Type' in options:
descriptor.SearchType = options['Type']
if options.get('First', False):
found = self.obj.findFirst(descriptor)
else:
found = self.obj.findAll(descriptor)
return found
def replace(self, options):
descriptor = self.obj.createReplaceDescriptor()
descriptor.setSearchString(options['Search'])
descriptor.setReplaceString(options['Replace'])
descriptor.SearchCaseSensitive = options.get('CaseSensitive', False)
descriptor.SearchWords = options.get('Words', False)
if 'Attributes' in options:
attr = dict_to_property(options['Attributes'])
descriptor.setSearchAttributes(attr)
if hasattr(descriptor, 'SearchRegularExpression'):
descriptor.SearchRegularExpression = options.get('RegularExpression', False)
if hasattr(descriptor, 'SearchType') and 'Type' in options:
descriptor.SearchType = options['Type']
found = self.obj.replaceAll(descriptor)
return found
class LOTextRange(object): class LOTextRange(object):
@ -1908,6 +1948,36 @@ class LOCellRange(object):
chart.cell = self chart.cell = self
return chart return chart
def search(self, options):
descriptor = self.obj.Spreadsheet.createSearchDescriptor()
descriptor.setSearchString(options.get('Search', ''))
descriptor.SearchCaseSensitive = options.get('CaseSensitive', False)
descriptor.SearchWords = options.get('Words', False)
if hasattr(descriptor, 'SearchRegularExpression'):
descriptor.SearchRegularExpression = options.get('RegularExpression', False)
if hasattr(descriptor, 'SearchType') and 'Type' in options:
descriptor.SearchType = options['Type']
if options.get('First', False):
found = self.obj.findFirst(descriptor)
else:
found = self.obj.findAll(descriptor)
return found
def replace(self, options):
descriptor = self.obj.Spreadsheet.createReplaceDescriptor()
descriptor.setSearchString(options['Search'])
descriptor.setReplaceString(options['Replace'])
descriptor.SearchCaseSensitive = options.get('CaseSensitive', False)
descriptor.SearchWords = options.get('Words', False)
if hasattr(descriptor, 'SearchRegularExpression'):
descriptor.SearchRegularExpression = options.get('RegularExpression', False)
if hasattr(descriptor, 'SearchType') and 'Type' in options:
descriptor.SearchType = options['Type']
found = self.obj.replaceAll(descriptor)
return found
class EventsListenerBase(unohelper.Base, XEventListener): class EventsListenerBase(unohelper.Base, XEventListener):
@ -3576,7 +3646,7 @@ def get_document(title=''):
return doc return doc
for d in desktop.getComponents(): for d in desktop.getComponents():
if d.Title == title: if hasattr(d, 'Title') and d.Title == title:
doc = d doc = d
break break
@ -3967,6 +4037,7 @@ def url_open(url, options={}, json=False):
req = Request(url) req = Request(url)
try: try:
response = urlopen(req) response = urlopen(req)
# ~ response.info()
except HTTPError as e: except HTTPError as e:
error(e) error(e)
except URLError as e: except URLError as e: