def processor_order(string): proc_tuple = string_to_stringtuple(string) proc_names = get_entry_points('ulif.openoffice.processors').keys() for name in proc_tuple: if name not in proc_names: raise ValueError('Only values in %r are allowed.' % proc_names) return proc_tuple
def test_string_to_stringtuple(self): assert string_to_stringtuple('foo') == ('foo', ) assert string_to_stringtuple('foo, bar') == ('foo', 'bar') assert string_to_stringtuple('foo,bar') == ('foo', 'bar') assert string_to_stringtuple(' foo ') == ('foo', ) assert string_to_stringtuple('') == () assert string_to_stringtuple('foo,,,bar') == ('foo', 'bar') assert string_to_stringtuple(None) == () assert string_to_stringtuple(',,,,') == () # with `strict` empty strings are forbidden self.assertRaises( ValueError, string_to_stringtuple, '', strict=True) self.assertRaises( ValueError, string_to_stringtuple, None, strict=True)
# tests for options module import argparse import pytest from ulif.openoffice.helpers import string_to_stringtuple from ulif.openoffice.options import ( dict_to_argtuple, Argument, ArgumentParserError, ExceptionalArgumentParser, Options, ) from ulif.openoffice.processor import DEFAULT_PROCORDER DEFAULT_PROCORDER = string_to_stringtuple(DEFAULT_PROCORDER) class TestHelpers(object): # tests for option helpers def test_dict_to_argtuple(self): # we can turn dicts into lists of arguments assert dict_to_argtuple({'x': '1', 'y': '2'}) == ('-x', '1', '-y', '2') assert dict_to_argtuple({'y': '2', 'x': '1'}) == ('-x', '1', '-y', '2') class TestArgument(object): # tests for ulif.openoffice Argument class def test_regular(self): # normally we pass in args and keywords arg = Argument('-myproc-opt1', '--myproc-option1', choice=[1, 2, 3]) assert arg.short_name == '-myproc-opt1' assert arg.long_name == '--myproc-option1'
class MetaProcessor(BaseProcessor): """The meta processor handles general workflow. When getting certain options, it constructs a pipeline of document processors. The :class:`MetaProcessor` is a kind of processor dispatcher that finds, setups and calls all requested processors in the requested order. """ #: the meta processor is named 'meta' prefix = 'meta' #: We support a ``-meta-procord`` option which stands for #: ``processororder``. The current default order is: #: ``'unzip,oocp,zip'`` which means: maybe unzip the input, then #: convert it into HTML and afterwards zip the results. args = [ Argument( '-meta-procord', '--meta-processor-order', default=string_to_stringtuple(DEFAULT_PROCORDER), type=processor_order, help='Comma-separated list of processors to run. ' 'Default: "%s"' % DEFAULT_PROCORDER, metavar='PROC_LIST', ), ] @property def avail_procs(self): return get_entry_points('ulif.openoffice.processors') def __init__(self, options={}): from ulif.openoffice.options import Options if not isinstance(options, Options): options = Options(string_dict=options) self.all_options = options self.options = options self.metadata = {} return def process(self, input=None, metadata={'error': False}): """Run all processors defined in options. If all processors run successful, the output of the last along with (maybe modified) metadata is returned. Each processor is fed with the `metadata` dict and an `input` (normally a filepath). Feeding a processor means to call its `process` method. If a processor sets the ``error`` entry of `metadata` to ``True`` this indicates some problem and the whole process is aborted returning ``None`` as output and the `metadata`, maybe containing some smart hints about the reasons. If all processors work correctly, the output of the last processor is returned along with the last `metadata`. The set and order of processors called depends on the ``procord`` option passed in. If this option is set to some value like ``oocp,oocp`` then the ``oocp`` processor (which is the :class:`OOConvProcessor`, registered under ``oocp`` in `setup.py`) is called two times. .. note:: after each processing, the (then old) input is removed. """ metadata = metadata.copy() pipeline = self._build_pipeline() output = None for processor in pipeline: proc_instance = processor(self.all_options) output, metadata = proc_instance.process(input, metadata) if metadata['error'] is True: metadata = self._handle_error(processor, input, output, metadata) return None, metadata if input != output: remove_file_dir(input) input = output return input, metadata def _handle_error(self, proc, input, output, metadata): metadata['error-descr'] = metadata.get( 'error-descr', 'problem while processing %s' % proc.prefix) remove_file_dir(input) remove_file_dir(output) return metadata def _build_pipeline(self): """Build a pipeline of processors according to options. """ result = [] procs = self.avail_procs for proc_name in self.options['meta_processor_order']: result.append(procs[proc_name]) return tuple(result)
# tests for options module import argparse import unittest from ulif.openoffice.helpers import string_to_stringtuple from ulif.openoffice.options import ( dict_to_argtuple, Argument, ArgumentParserError, ExceptionalArgumentParser, Options, ) from ulif.openoffice.processor import DEFAULT_PROCORDER DEFAULT_PROCORDER = string_to_stringtuple(DEFAULT_PROCORDER) class HelperTests(unittest.TestCase): # tests for option helpers def test_dict_to_argtuple(self): # we can turn dicts into lists of arguments assert dict_to_argtuple( {'x': '1', 'y': '2'}) == ('-x', '1', '-y', '2') assert dict_to_argtuple( {'y': '2', 'x': '1'}) == ('-x', '1', '-y', '2') class ArgumentTests(unittest.TestCase): # tests for ulif.openoffice Argument def test_regular(self): # normally we pass in args and keywords arg = Argument( '-myproc-opt1', '--myproc-option1', choice=[1, 2, 3]) assert arg.short_name == '-myproc-opt1' assert arg.long_name == '--myproc-option1'