Skip to content
Snippets Groups Projects
Commit 51124079ebbd authored by Florent Aide's avatar Florent Aide
Browse files

dos2unix

parent d1d529bd23aa
No related branches found
No related tags found
No related merge requests found
from py3o.renderers.juno.main import Convertor, start_jvm
from py3o.renderers.juno.main import Convertor, start_jvm
import jpype
from jpype import startJVM
from jpype import JPackage
import os
import platform
import logging
import pkg_resources
log = logging.getLogger(__name__)
# define some hard coded jar resource we will try to find when starting the JVM
ureitems = ["juh.jar", "jurt.jar", "ridl.jar", "unoloader.jar", "java_uno.jar"]
basisitems = ["unoil.jar", ]
def get_oo_context(office_version):
"""returns a dict with the java_classpath_sep, ure_subpath
and oooclasses_subpath key pointing the (hopefully) correct sub paths
for the current running plateform. The implementer still needs
to provide the real base path for his plateform elsewhere in its
code though...
@param office_version: the version number of the desired
OpenOffice/LibreOffice
@type office_version: string
"""
context = dict()
if os.name in ('nt', 'os2', 'ce'): # Windows
# tested to work with OpenOffice 3.2, we need to confirm
# it works with LibreOffice
context['java_classpath_sep'] = ";"
context['ure_subpath'] = os.path.join('URE', 'java')
context['oooclasses_subpath'] = os.path.join(
"Basis", "program", "classes"
)
else:
context['java_classpath_sep'] = ":"
# TODO: do that properly
# for now, hard-code the stuff on CentOS5.5 x86_64 with OOo 3.1
# this is also working on RedHat RHEL 5 and on Ubuntu
context['oooclasses_subpath'] = os.path.join("program", "classes")
if os.name == 'posix' and platform.dist()[0] in ('Ubuntu', 'debian'):
context['ure_subpath'] = 'java'
else:
context['ure_subpath'] = os.path.join('ure', 'share', 'java')
return context
def start_jvm(jvm, oobase, urebase, office_version, max_mem):
"""this small function should be called only once. At the beginning
of your program.
It takes care of starting the JVM that will be used by our
convertor library with our requirements in terms of
classpath and memory usage
returns nothing
@param jvm: the jvm path to use :
ie c:/Program Files/Java/jre1.5.0_05/bin/client/jvm.dll
@type jvm: string
@param oobase: the base directory where we will
find basis3.3/program/classes where we find the unoil.jar package
@type oobase: string
@param urebase: the base directory where we will find ure/share/java inside
which we should find java_uno.jar, juh.jar, jurt.jar, unoloader.jar
@type oobase: string
@param office_version: the office version we want to use for rendering
@type office_version: string
@param max_mem: the maximum amount of mega bytes to allocate to
our JVM
@type max_mem: integer
"""
context = get_oo_context(office_version)
java_classpath_sep = context.get('java_classpath_sep')
ure_subpath = context.get('ure_subpath')
oooclasses_subpath = context.get('oooclasses_subpath')
# this is our internally compiled java class
jar = pkg_resources.resource_filename(
'py3o.renderers.juno', 'py3oconverter.jar'
)
oojars = list()
for ureitem in ureitems:
oojars.append(os.path.join(urebase, ure_subpath, ureitem))
for basisitem in basisitems:
oojars.append(os.path.join(oobase, oooclasses_subpath, basisitem))
convertor_lib = os.path.abspath(jar)
java_classpath = '-Djava.class.path=%s' % (convertor_lib)
for oojar in oojars:
java_classpath += "%s%s" % (java_classpath_sep, oojar)
# -Xms is initial memory for java, -Xmx is maximum memory for java
# java_initmem = "-Xms%sM" % max_mem
java_maxmem = "-Xmx%sM" % max_mem
jvm_abs = os.path.abspath(jvm)
logging.debug('Starting JVM: %s with options: %s %s' % (
jvm_abs, java_classpath, java_maxmem))
startJVM(jvm_abs, java_classpath, java_maxmem)
class Convertor(object):
def __init__(self, host, port):
"""init our java lib with the host and port for the open office server
"""
if not jpype.isThreadAttachedToJVM():
jpype.attachThreadToJVM()
jconvertor_package = JPackage('py3oconverter').Convertor
self.jconvertor = jconvertor_package(host, port)
def convert(self, infilename, outfilename, filtername):
"""convert the input file using a certain filter produce a result
in outputfile.
:param infilename: a file name that must exist and be readable,
containing the input document to be converted
:type infilename: string
:param outfilename: a filename that must exit and be writeable,
inside which the convertor will save the result
:type outfilename: string
:param filtername: a LibreOffice filter name to use for conversion
:type filtername: string
:returns: nothing
:raises: jpype._jexception
"""
# use our java lib...
self.jconvertor.convert(infilename, outfilename, filtername)
import jpype
from jpype import startJVM
from jpype import JPackage
import os
import platform
import logging
import pkg_resources
log = logging.getLogger(__name__)
# define some hard coded jar resource we will try to find when starting the JVM
ureitems = ["juh.jar", "jurt.jar", "ridl.jar", "unoloader.jar", "java_uno.jar"]
basisitems = ["unoil.jar", ]
def get_oo_context():
"""returns a dict with the java_classpath_sep, ure_subpath
and oooclasses_subpath key pointing the (hopefully) correct sub paths
for the current running plateform. The implementer still needs
to provide the real base path for his plateform elsewhere in its
code though...
@param office_version: the version number of the desired
OpenOffice/LibreOffice
@type office_version: string
"""
context = dict()
if os.name in ('nt', 'os2', 'ce'): # Windows
# tested to work with OpenOffice 3.2, we need to confirm
# it works with LibreOffice
context['java_classpath_sep'] = ";"
context['ure_subpath'] = os.path.join('URE', 'java')
context['oooclasses_subpath'] = os.path.join(
"Basis", "program", "classes"
)
else:
context['java_classpath_sep'] = ":"
context['oooclasses_subpath'] = os.path.join("program", "classes")
# TODO: add more platforms
if os.name == 'posix' and platform.dist()[0] in ('Ubuntu', 'debian'):
context['ure_subpath'] = 'java'
else:
context['ure_subpath'] = os.path.join('ure', 'share', 'java')
return context
def start_jvm(jvm, oobase, urebase, max_mem):
"""this small function should be called only once. At the beginning
of your program.
It takes care of starting the JVM that will be used by our
convertor library with our requirements in terms of
classpath and memory usage
returns nothing
@param jvm: the jvm path to use :
ie c:/Program Files/Java/jre1.5.0_05/bin/client/jvm.dll
@type jvm: string
@param oobase: the base directory where we will find the
program/classes/unoil.jar package
@type oobase: string
@param urebase: the base directory where we will find ure/share/java inside
which we should find java_uno.jar, juh.jar, jurt.jar, unoloader.jar
@type oobase: string
@param max_mem: the maximum amount of mega bytes to allocate to
our JVM
@type max_mem: integer
"""
context = get_oo_context()
java_classpath_sep = context.get('java_classpath_sep')
ure_subpath = context.get('ure_subpath')
oooclasses_subpath = context.get('oooclasses_subpath')
# this is our internally compiled java class
jar = pkg_resources.resource_filename(
'py3o.renderers.juno', 'py3oconverter.jar'
)
oojars = list()
for ureitem in ureitems:
oojars.append(os.path.join(urebase, ure_subpath, ureitem))
for basisitem in basisitems:
oojars.append(os.path.join(oobase, oooclasses_subpath, basisitem))
convertor_lib = os.path.abspath(jar)
java_classpath = '-Djava.class.path=%s' % (convertor_lib)
for oojar in oojars:
java_classpath += "%s%s" % (java_classpath_sep, oojar)
# -Xms is initial memory for java, -Xmx is maximum memory for java
# java_initmem = "-Xms%sM" % max_mem
java_maxmem = "-Xmx%sM" % max_mem
jvm_abs = os.path.abspath(jvm)
logging.debug('Starting JVM: %s with options: %s %s' % (
jvm_abs, java_classpath, java_maxmem))
startJVM(jvm_abs, java_classpath, java_maxmem)
class Convertor(object):
def __init__(self, host, port):
"""init our java lib with the host and port for the open office server
"""
if not jpype.isThreadAttachedToJVM():
jpype.attachThreadToJVM()
jconvertor_package = JPackage('py3oconverter').Convertor
self.jconvertor = jconvertor_package(host, port)
def convert(self, infilename, outfilename, filtername):
"""convert the input file using a certain filter produce a result
in outputfile.
:param infilename: a file name that must exist and be readable,
containing the input document to be converted
:type infilename: string
:param outfilename: a filename that must exit and be writeable,
inside which the convertor will save the result
:type outfilename: string
:param filtername: a LibreOffice filter name to use for conversion
:type filtername: string
:returns: nothing
:raises: jpype._jexception
"""
# use our java lib...
self.jconvertor.convert(infilename, outfilename, filtername)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment