Add unokit
Browse filesFiles changed (13)
- unokit/setup.pyAdded+10
- unokit/unokit/__init__.pyAdded+36
- unokit/unokit/adapters.pyAdded+141
- unokit/unokit/configuration.pyAdded+41
- unokit/unokit/contexts.pyAdded+47
- unokit/unokit/services.pyAdded+52
- unokit/unokit/singletons.pyAdded+58
- unokit/unokit/tests/__init__.pyAdded
- unokit/unokit/tests/test_configuration.pyAdded+33
- unokit/unokit/tests/test_singletons.pyAdded+40
- unokit/unokit/tests/test_ucb.pyAdded+35
- unokit/unokit/ucb.pyAdded+56
- unokit/unokit/util.pyAdded+88
12345678910import osimport os.pathcwd = os.getcwd()os.chdir(os.path.dirname(__file__))try: from setuptools import setup, find_packages setup(name='unokit', packages=find_packages())finally: os.chdir(cwd)123456789101112131415161718192021222324252627282930313233343536# -*- coding: utf-8 -*-## pyhwp : hwp file format parser in python# Copyright (C) 2010,2011,2012 https://github.com/mete0r## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#import unohelper
class Base(unohelper.Base):
def __init__(self, context): self.context = context
def component_context(f): import unokit.contexts def wrapper(self, *args, **kwargs): unokit.contexts.push(self.context) try: return f(self, *args, **kwargs) finally: unokit.contexts.pop() return wrapper123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141# -*- coding: utf-8 -*-## pyhwp : hwp file format parser in python# Copyright (C) 2010,2011,2012 https://github.com/mete0r## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#import unoimport unohelperfrom com.sun.star.io import XInputStream, XSeekable, XOutputStream
class InputStreamFromFileLike(unohelper.Base, XInputStream, XSeekable): ''' Implementation of XInputStream, XSeekable based on a file-like object
Implements com.sun.star.io.XInputStream and com.sun.star.io.XSeekable
:param f: a file-like object ''' def __init__(self, f, dontclose=False): self.f = f self.dontclose = dontclose
def readBytes(self, aData, nBytesToRead): data = self.f.read(nBytesToRead) return len(data), uno.ByteSequence(data)
readSomeBytes = readBytes
def skipBytes(self, nBytesToSkip): self.f.read(nBytesToSkip)
def available(self): return 0
def closeInput(self): if not self.dontclose: self.f.close()
def seek(self, location): self.f.seek(location)
def getPosition(self): pos = self.f.tell() return pos
def getLength(self): pos = self.f.tell() try: self.f.seek(0, 2) length = self.f.tell() return length finally: self.f.seek(pos)
class OutputStreamToFileLike(unohelper.Base, XOutputStream): ''' Implementation of XOutputStream based on a file-like object.
Implements com.sun.star.io.XOutputStream.
:param f: a file-like object ''' def __init__(self, f, dontclose=False): self.f = f self.dontclose = dontclose
def writeBytes(self, bytesequence): self.f.write(bytesequence.value)
def flush(self): self.f.flush()
def closeOutput(self): if not self.dontclose: self.f.close()
class FileFromStream(object): ''' A file-like object based on XInputStream/XOuputStream/XSeekable
:param stream: a stream object which implements com.sun.star.io.XInputStream, com.sun.star.io.XOutputStream or com.sun.star.io.XSeekable ''' def __init__(self, stream): self.stream = stream
if hasattr(stream, 'readBytes'): def read(size=None): if size is None: data = '' while True: bytes = uno.ByteSequence('') n_read, bytes = stream.readBytes(bytes, 4096) if n_read == 0: return data data += bytes.value bytes = uno.ByteSequence('') n_read, bytes = stream.readBytes(bytes, size) return bytes.value self.read = read
if hasattr(stream, 'seek'): self.tell = stream.getPosition
def seek(offset, whence=0): if whence == 0: pass elif whence == 1: offset += stream.getPosition() elif whence == 2: offset += stream.getLength() stream.seek(offset) self.seek = seek
if hasattr(stream, 'writeBytes'): def write(s): stream.writeBytes(uno.ByteSequence(s)) self.write = write
def flush(): stream.flush() self.flush = flush
def close(self): if hasattr(self.stream, 'closeInput'): self.stream.closeInput() elif hasattr(self.stream, 'closeOutput'): self.stream.closeOutput()1234567891011121314151617181920212223242526272829303132333435363738394041# -*- coding: utf-8 -*-## pyhwp : hwp file format parser in python# Copyright (C) 2010,2011,2012 https://github.com/mete0r## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#
def open_config(nodepath): from unokit.services import css from unokit.util import dict_to_propseq provider = css.configuration.ConfigurationProvider() param = dict_to_propseq(dict(nodepath=nodepath)) configaccess = 'com.sun.star.configuration.ConfigurationAccess' return provider.createInstanceWithArguments(configaccess, param)
def get_soffice_product_info(): config = open_config('/org.openoffice.Setup')
# see schema in libreoffice/officecfg/registry/schema/org/office/Setup.xcs
version = tuple(int(x) for x in config.Product.ooSetupVersionAboutBox.split('.')) if hasattr(config.Product, 'ooSetupVersionAboutBoxSuffix'): # seems for libreoffice >= 3.5 only version += (config.Product.ooSetupVersionAboutBoxSuffix,) return dict(vendor=config.Product.ooVendor, name=config.Product.ooName, version=version, locale=config.L10N.ooLocale)1234567891011121314151617181920212223242526272829303132333435363738394041424344454647# -*- coding: utf-8 -*-## pyhwp : hwp file format parser in python# Copyright (C) 2010,2011,2012 https://github.com/mete0r## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#import unoimport threading
tls = threading.local()localcontext = uno.getComponentContext()
def get_stack(): try: return tls.context_stack except AttributeError: tls.context_stack = [] return tls.context_stack
def push(context): return get_stack().append(context)
def pop(): return get_stack().pop()
def get_current(): stack = get_stack() if len(stack) == 0: return localcontext return stack[-1]12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152# -*- coding: utf-8 -*-## pyhwp : hwp file format parser in python# Copyright (C) 2010,2011,2012 https://github.com/mete0r## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#
def create_service(name, *args): import unokit.contexts context = unokit.contexts.get_current() sm = context.ServiceManager if len(args) > 0: return sm.createInstanceWithArgumentsAndContext(name, args, context) else: return sm.createInstanceWithContext(name, context)
class NamespaceNode(object): def __init__(self, dotted_name): self.dotted_name = dotted_name
def __getattr__(self, name): return NamespaceNode(self.dotted_name + '.' + name)
def __call__(self, *args): return create_service(self.dotted_name, *args)
def __iter__(self): import unokit.contexts context = unokit.contexts.get_current() sm = context.ServiceManager prefix = self.dotted_name + '.' for name in sm.AvailableServiceNames: if name.startswith(prefix): basename = name[len(prefix):] if basename.find('.') == -1: yield basename
css = NamespaceNode('com.sun.star')12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758# -*- coding: utf-8 -*-## pyhwp : hwp file format parser in python# Copyright (C) 2010,2011,2012 https://github.com/mete0r## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#
def get_singleton(name): import unokit.contexts context = unokit.contexts.get_current() return context.getValueByName('/singletons/'+name)
def iter_singleton_names(): import unokit.contexts context = unokit.contexts.get_current() names = (name[len('/singletons/'):] for name in context.ElementNames if (name.startswith('/singletons/') and not name.endswith('/service'))) return names
class NamespaceNode(object): def __init__(self, dotted_name): self.dotted_name = dotted_name
def __getattr__(self, name): import unokit.contexts context = unokit.contexts.get_current() dotted_name = self.dotted_name + '.' + name full_name = '/singletons/' + dotted_name if full_name in context.ElementNames: return context.getValueByName(full_name) return NamespaceNode(self.dotted_name + '.' + name)
def __iter__(self): prefix = self.dotted_name + '.' for name in iter_singleton_names(): if name.startswith(prefix): basename = name[len(prefix):] if basename.find('.') == -1: yield basename
css = NamespaceNode('com.sun.star')123456789101112131415161718192021222324252627282930313233# -*- coding: utf-8 -*-## pyhwp : hwp file format parser in python# Copyright (C) 2010,2011,2012 https://github.com/mete0r## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#from unittest import TestCase
class TestBase(TestCase): pass
class GetSofficeProductInfoTest(TestBase): def test_basic(self): from unokit.configuration import get_soffice_product_info info = get_soffice_product_info() self.assertTrue('name' in info) self.assertTrue('vendor' in info) self.assertTrue('version' in info) self.assertTrue('locale' in info)12345678910111213141516171819202122232425262728293031323334353637383940# -*- coding: utf-8 -*-## pyhwp : hwp file format parser in python# Copyright (C) 2010,2011,2012 https://github.com/mete0r## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#from unittest import TestCase
class TestBase(TestCase): pass
class TestSingletons(TestBase): def test_extman(self): from unokit.singletons import css extman = css.deployment.ExtensionManager self.assertTrue(extman is not None)
def test_pkginfo_prov(self): from unokit.singletons import css pkginfo_prov = css.deployment.PackageInformationProvider self.assertTrue(pkginfo_prov is not None)
for ext_id, ext_ver in pkginfo_prov.ExtensionList: ext_loc = pkginfo_prov.getPackageLocation(ext_id) print ext_id, ext_ver, ext_loc self.assertTrue(ext_loc != '')1234567891011121314151617181920212223242526272829303132333435# -*- coding: utf-8 -*-## pyhwp : hwp file format parser in python# Copyright (C) 2010,2011,2012 https://github.com/mete0r## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#from unittest import TestCase
class TestBase(TestCase): pass
class OpenURLTest(TestBase): def test_basic(self): #import os.path #from uno import systemPathToFileUrl from unokit.ucb import open_url
#path = os.path.abspath('fixtures/sample-5017.hwp') #url = systemPathToFileUrl(path) inputstream = open_url('http://google.com') inputstream.closeInput()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556# -*- coding: utf-8 -*-## pyhwp : hwp file format parser in python# Copyright (C) 2010,2011,2012 https://github.com/mete0r## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#
def open_url(url): ''' open InputStream from a URL.
:param url: a URL to open an InputStream. :returns: an instance of InputStream '''
# see http://wiki.openoffice.org/wiki/Documentation/DevGuide/UCB/Using_the_UCB_API
from unokit.services import css ucb = css.ucb.UniversalContentBroker('Local', 'Office') content_id = ucb.createContentIdentifier(url) content = ucb.queryContent(content_id)
import unohelper from com.sun.star.io import XActiveDataSink class DataSink(unohelper.Base, XActiveDataSink): def setInputStream(self, stream): self.stream = stream def getInputStream(self): return self.stream datasink = DataSink()
from com.sun.star.ucb import Command, OpenCommandArgument2 openargs = OpenCommandArgument2() openargs.Mode = 2 # OpenMode.DOCUMENT openargs.Priority = 32768 openargs.Sink = datasink
command = Command() command.Name = 'open' command.Handle = -1 command.Argument = openargs
content.execute(command, 0, None) return datasink.stream12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788# -*- coding: utf-8 -*-## pyhwp : hwp file format parser in python# Copyright (C) 2010,2011,2012 https://github.com/mete0r## This program is free software: you can redistribute it and/or modify# it under the terms of the GNU Affero General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU Affero General Public License for more details.## You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.#import uno
def unofy_value(value): if isinstance(value, dict): value = dict_to_propseq(value) elif isinstance(value, list): value = tuple(value) return value
def xenumeration_list(xenum): return list(iterate(xenum))
def dict_to_propseq(d): from com.sun.star.beans import PropertyValue DIRECT_VALUE = uno.Enum('com.sun.star.beans.PropertyState', 'DIRECT_VALUE') return tuple(PropertyValue(k, 0, unofy_value(d[k]), DIRECT_VALUE) for k in d)
def propseq_to_dict(propvalues): return dict((p.Name, p.Value) for p in propvalues)
def enumerate(xenumaccess): ''' Enumerate an instance of com.sun.star.container.XEnumerationAccess ''' if hasattr(xenumaccess, 'createEnumeration'): xenum = xenumaccess.createEnumeration() return iterate(xenum) else: return iter([])
def iterate(xenum): ''' Iterate an instance of com.sun.star.container.XEnumeration ''' if hasattr(xenum, 'hasMoreElements'): while xenum.hasMoreElements(): yield xenum.nextElement()
def dump(obj): from binascii import b2a_hex if hasattr(obj, 'ImplementationId'): print 'Implementation Id:', b2a_hex(obj.ImplementationId.value) print
if hasattr(obj, 'ImplementationName'): print 'Implementation Name:', obj.ImplementationName print
if hasattr(obj, 'SupportedServiceNames'): print 'Supported Services:' for x in obj.SupportedServiceNames: print '', x print
if hasattr(obj, 'Types'): print 'Types:' for x in obj.Types: print '', x.typeClass.value, x.typeName print
def dumpdir(obj): print 'dir:' for e in sorted(dir(obj)): print '', e printunokit/setup.pyAdded+10
unokit/unokit/__init__.pyAdded+36
unokit/unokit/adapters.pyAdded+141
unokit/unokit/configuration.pyAdded+41
unokit/unokit/contexts.pyAdded+47
unokit/unokit/services.pyAdded+52
unokit/unokit/singletons.pyAdded+58
unokit/unokit/tests/__init__.pyAdded
unokit/unokit/tests/test_configuration.pyAdded+33
unokit/unokit/tests/test_singletons.pyAdded+40
unokit/unokit/tests/test_ucb.pyAdded+35
unokit/unokit/ucb.pyAdded+56
unokit/unokit/util.pyAdded+88