Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement plugins #212

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ at: https://github.com/dagwieers/unoconv/issues

And do mention that you already tried the above hints to troubleshoot the issue.

== Plugins
During the conversion process, you may want to run plugins against the document using
the LibreOffice api. To do so, you need to create your own class that implments a run method,
and place your class in the PYTHONPATH (or adding your path to the PYTHONPATH).

See the SamplePlugin.py file for trival example

And run unoconv with the --plugins= option

unoconv -f pdf --plugins=SamplePlugin


== Interesting information
If you're interested to help out with development, here are some pointers to
Expand Down
6 changes: 6 additions & 0 deletions SamplePlugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class SamplePlugin:

def run(self, document):
print ('plugin')
return

20 changes: 19 additions & 1 deletion unoconv
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import os
import subprocess
import sys
import time
import importlib

__version__ = "$Revision$"
# $Source$
Expand Down Expand Up @@ -518,6 +519,7 @@ class Options:
self.output = None
self.password = None
self.pipe = None
self.plugins = []
self.port = '2002'
self.server = '127.0.0.1'
self.showlist = False
Expand All @@ -531,7 +533,7 @@ class Options:
opts, args = getopt.getopt (args, 'c:Dd:e:f:hi:Llo:np:s:T:t:vV',
['connection=', 'debug', 'doctype=', 'export=', 'format=',
'help', 'import', 'listener', 'no-launch', 'output=',
'outputpath', 'password=', 'pipe=', 'port=', 'server=',
'outputpath', 'password=', 'pipe=', 'plugins=', 'port=', 'server=',
'timeout=', 'show', 'stdout', 'template', 'verbose',
'version'] )
except getopt.error as exc:
Expand Down Expand Up @@ -599,6 +601,8 @@ class Options:
self.password = arg
elif opt in ['--pipe']:
self.pipe = arg
elif opt in ['--plugins']:
self.plugins = arg.split(',')
elif opt in ['-p', '--port']:
self.port = arg
elif opt in ['-s', '--server']:
Expand Down Expand Up @@ -692,6 +696,7 @@ unoconv options:
-n, --no-launch fail if no listener is found (default: launch one)
-o, --output=name output basename, filename or directory
--pipe=name alternative method of connection using a pipe
--plugins=plugins list of plugin names to run, comma seperated
-p, --port=port specify the port (default: 2002)
to be used by client or listener
--password=string provide a password to decrypt the document
Expand Down Expand Up @@ -861,6 +866,19 @@ class Convertor:
print('unoconv: template file `%s\' does not exist.' % op.template, file=sys.stderr)
exitcode = 1

### Run plugins
phase = "plugins"
if len(op.plugins) > 0:
for plugin_name in op.plugins:
try:
module = importlib.import_module("%s" % plugin_name)
instance = getattr(module, plugin_name)()
instance.run(document)
except ImportError:
print("unoconv: skipping plugin '%s', could not import" % plugin_name)
except AttributeError:
print("unoconv: plugin '%s', does not implement run method" % plugin_name)

### Update document links
phase = "update-links"
try:
Expand Down