def test_get_marker(self):
     # Make sure, sorted dicts get the same marker
     result1 = get_marker()
     result2 = get_marker(options={})
     result3 = get_marker(options={'b': '0', 'a': '1'})
     result4 = get_marker(options={'a': '1', 'b': '0'})
     assert result1 == 'W10'
     assert result2 == 'W10'
     assert result3 == result4
     assert result2 != result3
示例#2
0
 def test_get_marker(self):
     # Make sure, sorted dicts get the same marker
     result1 = get_marker()
     result2 = get_marker(options={})
     result3 = get_marker(options={'b': '0', 'a': '1'})
     result4 = get_marker(options={'a': '1', 'b': '0'})
     assert result1 == 'W10'
     assert result2 == 'W10'
     assert result3 == result4
     assert result2 != result3
示例#3
0
    def get_cached_by_source(self, src_doc_path, options={}):
        """Get the document from cache by source doc and options.

        Find a cached document, which was created from the given
        `src_doc_path` and `options`.

        Returns the path to the document and a cache key you are
        encouraged to use for future access.

        Please note that this method is much more expensive than
        :meth:`get_cached`. Use it only if the `cache_key` returned
        upon registering a doc is absolutely not available any more.

        Returns ``(None, None)`` if no such file can be found or no
        cache dir was set at all.

        .. warning:: The returned path (if any) is part of cache! Do
                     not remove or change the file. Copy it to another
                     location instead.

        .. versionadded:: 1.1

        """
        repr_key = get_marker(options)
        if self.cache_manager is not None:
            return self.cache_manager.get_cached_file_by_source(
                src_doc_path, repr_key)
        return None, None
示例#4
0
    def get_cached_by_source(self, src_doc_path, options={}):
        """Get the document from cache by source doc and options.

        Find a cached document, which was created from the given
        `src_doc_path` and `options`.

        Returns the path to the document and a cache key you are
        encouraged to use for future access.

        Please note that this method is much more expensive than
        :meth:`get_cached`. Use it only if the `cache_key` returned
        upon registering a doc is absolutely not available any more.

        Returns ``(None, None)`` if no such file can be found or no
        cache dir was set at all.

        .. warning:: The returned path (if any) is part of cache! Do
                     not remove or change the file. Copy it to another
                     location instead.

        .. versionadded:: 1.1

        """
        repr_key = get_marker(options)
        if self.cache_manager is not None:
            return self.cache_manager.get_cached_file_by_source(
                src_doc_path, repr_key)
        return None, None
示例#5
0
 def register_fakedoc_in_cache(self, src, options):
     # register a fake doc in cache. Result cache_key is based on
     # path to src document and options given.
     cm = CacheManager(self.cachedir)
     fake_result_path = os.path.join(self.workdir, 'result.html')
     open(fake_result_path, 'w').write('A fake result.')
     marker = get_marker(options)
     cache_key = cm.register_doc(src, fake_result_path, repr_key=marker)
     return cache_key
示例#6
0
def convert_doc(src_doc, options, cache_dir):
    """Convert `src_doc` according to the other parameters.

    `src_doc` is the path to the source document. `options` is a dict
    of options for processing, passed to the processors.

    `cache_dir` may be ``None`` in which no caching is requested
    during processing.

    Generates a converted representation of `src_doc` by calling
    :class:`ulif.openoffice.processor.MetaProcessor` with `options` as
    parameters.

    Afterwards the conversion result is stored in cache (if
    allowed/possible) for speedup of upcoming requests.

    Returns a triple:

      ``(<PATH>, <CACHE_KEY>, <METADATA>)``

    where ``<PATH>`` is the path to the resulting document,
    ``<CACHE_KEY>`` an identifier (string) to retrieve a generated doc
    from cache on future requests, and ``<METADATA>`` is a dict of values
    returned during request (and set by the document processors,
    notably setting the `error` keyword).

    If errors happen or caching is disabled, ``<CACHE_KEY>`` is
    ``None``.
    """
    result_path = None
    cache_key = None
    repr_key = get_marker(options)  # Create unique marker out of options
    metadata = dict(error=False)

    # Generate result
    input_copy_dir = tempfile.mkdtemp()
    input_copy = os.path.join(input_copy_dir, os.path.basename(src_doc))
    shutil.copy2(src_doc, input_copy)
    try:
        proc = MetaProcessor(options=options)  # Removes original doc
        result_path, metadata = proc.process(input_copy)
    except Exception as exc:
        shutil.rmtree(input_copy_dir)
        raise exc

    error_state = metadata.get('error', False)
    if cache_dir and not error_state and result_path is not None:
        # Cache away generated doc
        cache_key = CacheManager(cache_dir).register_doc(
            src_doc, result_path, repr_key)
    return result_path, cache_key, metadata
示例#7
0
def convert_doc(src_doc, options, cache_dir):
    """Convert `src_doc` according to the other parameters.

    `src_doc` is the path to the source document. `options` is a dict
    of options for processing, passed to the processors.

    `cache_dir` may be ``None`` in which no caching is requested
    during processing.

    Generates a converted representation of `src_doc` by calling
    :class:`ulif.openoffice.processor.MetaProcessor` with `options` as
    parameters.

    Afterwards the conversion result is stored in cache (if
    allowed/possible) for speedup of upcoming requests.

    Returns a triple:

      ``(<PATH>, <CACHE_KEY>, <METADATA>)``

    where ``<PATH>`` is the path to the resulting document,
    ``<CACHE_KEY>`` an identifier (string) to retrieve a generated doc
    from cache on future requests, and ``<METADATA>`` is a dict of values
    returned during request (and set by the document processors,
    notably setting the `error` keyword).

    If errors happen or caching is disabled, ``<CACHE_KEY>`` is
    ``None``.
    """
    result_path = None
    cache_key = None
    repr_key = get_marker(options)  # Create unique marker out of options
    metadata = dict(error=False)

    # Generate result
    input_copy_dir = tempfile.mkdtemp()
    input_copy = os.path.join(input_copy_dir, os.path.basename(src_doc))
    shutil.copy2(src_doc, input_copy)
    try:
        proc = MetaProcessor(options=options)  # Removes original doc
        result_path, metadata = proc.process(input_copy)
    except Exception as exc:
        shutil.rmtree(input_copy_dir)
        raise exc

    error_state = metadata.get('error', False)
    if cache_dir and not error_state and result_path is not None:
        # Cache away generated doc
        cache_key = CacheManager(cache_dir).register_doc(
            src_doc, result_path, repr_key)
    return result_path, cache_key, metadata
示例#8
0
 def test_show_with_cache(self, conv_env):
     # we can retrieve cached files
     app = RESTfulDocConverter(cache_dir=str(conv_env / "cache"))
     conv_env.join("sample_in.txt").write("Fake source.")
     conv_env.join("sample_out.pdf").write("Fake result.")
     marker = get_marker(dict(foo='bar', bar='baz'))
     doc_id = app.cache_manager.register_doc(
         source_path=str(conv_env.join("sample_in.txt")),
         to_cache=str(conv_env.join("sample_out.pdf")),
         repr_key=marker)
     assert doc_id == '3fe6f0d4c5e62ff9a1deca0a8a65fe8d_1_1'
     url = 'http://localhost/docs/3fe6f0d4c5e62ff9a1deca0a8a65fe8d_1_1'
     req = Request.blank(url)
     resp = app(req)
     assert resp.status == "200 OK"
     assert resp.content_type == "application/pdf"
示例#9
0
 def test_show_with_cache(self, conv_env):
     # we can retrieve cached files
     app = RESTfulDocConverter(cache_dir=str(conv_env / "cache"))
     conv_env.join("sample_in.txt").write("Fake source.")
     conv_env.join("sample_out.pdf").write("Fake result.")
     marker = get_marker(dict(foo='bar', bar='baz'))
     doc_id = app.cache_manager.register_doc(
         source_path=str(conv_env.join("sample_in.txt")),
         to_cache=str(conv_env.join("sample_out.pdf")),
         repr_key=marker)
     assert doc_id == '3fe6f0d4c5e62ff9a1deca0a8a65fe8d_1_1'
     url = 'http://localhost/docs/3fe6f0d4c5e62ff9a1deca0a8a65fe8d_1_1'
     req = Request.blank(url)
     resp = app(req)
     assert resp.status == "200 OK"
     assert resp.content_type == "application/pdf"
示例#10
0
 def test_show_with_cache(self):
     # we can retrieve cached files
     app = RESTfulDocConverter(cache_dir=self.cachedir)
     fake_src = os.path.join(self.workdir, 'sample_in.txt')
     fake_result = os.path.join(self.workdir, 'sample_out.pdf')
     open(fake_src, 'w').write('Fake source.')
     open(fake_result, 'w').write('Fake result.')
     marker = get_marker(dict(foo='bar', bar='baz'))
     doc_id = app.cache_manager.register_doc(
         source_path=fake_src, to_cache=fake_result, repr_key=marker)
     self.assertEqual('3fe6f0d4c5e62ff9a1deca0a8a65fe8d_1_1', doc_id)
     doc_id = '%s_%s' % (doc_id, marker)
     url = 'http://localhost/docs/3fe6f0d4c5e62ff9a1deca0a8a65fe8d_1_1'
     req = Request.blank(url)
     resp = app(req)
     self.assertEqual(resp.status, '200 OK')
     self.assertEqual(resp.content_type, 'application/pdf')