def create(self, req): # post a new doc options = dict([(name, val) for name, val in list(req.params.items()) if name not in ('CREATE', 'doc', 'docid')]) if 'out_fmt' in list(req.params.keys()): options['oocp-out-fmt'] = options['out_fmt'] del options['out_fmt'] if 'CREATE' in list(req.params.keys()): if options.get('oocp-out-fmt', 'html') == 'pdf': options['meta-procord'] = 'unzip,oocp,zip' doc = req.POST['doc'] # write doc to filesystem tmp_dir = tempfile.mkdtemp() src_path = os.path.join(tmp_dir, doc.filename) with open(src_path, 'wb') as f: for chunk in iter(lambda: doc.file.read(8 * 1024), b''): f.write(chunk) # do the conversion result_path, id_tag, metadata = convert_doc( src_path, options, self.cache_dir) # deliver the created file resp = make_response(result_path) if id_tag is not None: # we can only signal new resources if cache is enabled resp.status = '201 Created' resp.location = self._url(req, 'doc', id=id_tag, qualified=True) return resp
def create(self, req): # post a new doc options = dict([(name, val) for name, val in req.params.items() if name not in ('CREATE', 'doc', 'docid')]) if 'out_fmt' in req.params.keys(): options['oocp-out-fmt'] = options['out_fmt'] del options['out_fmt'] if 'CREATE' in req.params.keys(): if options.get('oocp-out-fmt', 'html') == 'pdf': options['meta-procord'] = 'unzip,oocp,zip' doc = req.POST['doc'] # write doc to filesystem tmp_dir = tempfile.mkdtemp() src_path = os.path.join(tmp_dir, doc.filename) with open(src_path, 'wb') as f: for chunk in iter(lambda: doc.file.read(8 * 1024), b''): f.write(chunk) # do the conversion result_path, id_tag, metadata = convert_doc(src_path, options, self.cache_dir) # deliver the created file resp = make_response(result_path) if id_tag is not None: # we can only signal new resources if cache is enabled resp.status = '201 Created' resp.location = self._url(req, 'doc', id=id_tag, qualified=True) return resp
def test_cached(self): # with a cache_dir, the result is cached result_path, cache_key, metadata = convert_doc( self.src_doc, options={}, cache_dir=self.cachedir) self.resultdir = os.path.dirname(result_path) assert result_path[-16:] == '/sample.html.zip' # cache keys are same for equal input files assert cache_key == '164dfcf01584bd0e3595b62fb53cf12c_1_1' assert metadata == {'error': False, 'oocp_status': 0}
def test_options(self): # options given are respected options = {'meta-procord': 'unzip,oocp', 'oocp-out-fmt': 'pdf'} result_path, cache_key, metadata = convert_doc( self.src_doc, options=options, cache_dir=None) self.resultdir = os.path.dirname(result_path) assert result_path[-11:] == '/sample.pdf' assert metadata == {'error': False, 'oocp_status': 0}
def test_nocache(self): # by default we get a zip'd HTML representation result_path, cache_key, metadata = convert_doc( self.src_doc, options={}, cache_dir=None) assert 'Cmd result: 0' in self.log_catcher.get_log_messages() self.resultdir = os.path.dirname(result_path) assert result_path[-16:] == '/sample.html.zip' assert cache_key is None # no cache, no cache_key assert metadata == {'error': False, 'oocp_status': 0}
def test_options(self, workdir, lo_server): # options given are respected workdir.join('src').chdir() src_doc = workdir.join('src').join('sample.txt') options = {'meta-procord': 'unzip,oocp', 'oocp-out-fmt': 'pdf'} result_path, cache_key, metadata = convert_doc( os.path.basename(str(src_doc)), options=options, cache_dir=None) assert os.path.basename(result_path) == "sample.pdf" assert metadata == {'error': False, 'oocp_status': 0}
def test_nocache(self, workdir, conv_logger, lo_server): # by default we get a zip'd HTML representation workdir.join('src').chdir() src_doc = workdir.join('src').join('sample.txt') result_path, cache_key, metadata = convert_doc( os.path.basename(str(src_doc)), options={}, cache_dir=None) assert 'Cmd result: 0' in conv_logger.getvalue() assert os.path.basename(result_path) == "sample.html.zip" assert cache_key is None # no cache, no cache_key assert metadata == {'error': False, 'oocp_status': 0}
def test_cached(self, workdir, conv_logger, lo_server): # with a cache_dir, the result is cached workdir.join('src').chdir() src_doc = workdir.join('src').join('sample.txt') result_path, cache_key, metadata = convert_doc( os.path.basename(str(src_doc)), options={}, cache_dir=str(workdir / "cache")) assert os.path.basename(result_path) == "sample.html.zip" # cache keys are same for equal input files assert cache_key == '396199333edbf40ad43e62a1c1397793_1_1' assert metadata == {'error': False, 'oocp_status': 0}
def test_options(self, workdir, lo_server): # options given are respected workdir.join('src').chdir() src_doc = workdir.join('src').join('sample.txt') options = {'meta-procord': 'unzip,oocp', 'oocp-out-fmt': 'pdf'} result_path, cache_key, metadata = convert_doc(os.path.basename( str(src_doc)), options=options, cache_dir=None) assert os.path.basename(result_path) == "sample.pdf" assert metadata == {'error': False, 'oocp_status': 0}
def test_nocache(self, workdir, conv_logger, lo_server): # by default we get a zip'd HTML representation workdir.join('src').chdir() src_doc = workdir.join('src').join('sample.txt') result_path, cache_key, metadata = convert_doc(os.path.basename( str(src_doc)), options={}, cache_dir=None) assert 'Cmd result: 0' in conv_logger.getvalue() assert os.path.basename(result_path) == "sample.html.zip" assert cache_key is None # no cache, no cache_key assert metadata == {'error': False, 'oocp_status': 0}
def test_only_one_file_considered_as_input(self, workdir, lo_server): # we only consider one input file, not other files in same dir options = {'meta-procord': 'oocp', 'oocp-out-fmt': 'html'} workdir.join('src').chdir() src_doc = workdir.join('src').join('sample.txt') workdir.join('src').join('other.foo').write('some-content') result_path, cache_key, metadata = convert_doc(os.path.basename( str(src_doc)), options=options, cache_dir=None) result_list = str(os.listdir(os.path.dirname(result_path))) assert 'other.foo' not in result_list assert 'sample.html' in result_list
def test_only_one_file_considered_as_input(self, workdir, lo_server): # we only consider one input file, not other files in same dir options = { 'meta-procord': 'oocp', 'oocp-out-fmt': 'html' } workdir.join('src').chdir() src_doc = workdir.join('src').join('sample.txt') workdir.join('src').join('other.foo').write('some-content') result_path, cache_key, metadata = convert_doc( os.path.basename(str(src_doc)), options=options, cache_dir=None) result_list = str(os.listdir(os.path.dirname(result_path))) assert 'other.foo' not in result_list assert 'sample.html' in result_list
def test_basename_only_input(self): # also source paths with a basename only are accepted options = {'meta-procord': 'oocp', 'oocp-out-fmt': 'pdf'} # change to the dir where the src doc resides (set back by teardown) os.chdir(os.path.dirname(self.src_doc)) result_path, cache_key, metadata = convert_doc( os.path.basename(self.src_doc), options=options, cache_dir=None) assert 'Cmd result: 0' in self.log_catcher.get_log_messages() self.resultdir = os.path.dirname(result_path) assert result_path[-11:] == '/sample.pdf' assert metadata == {'error': False, 'oocp_status': 0} # the original source doc still exists assert os.path.exists(self.src_doc)
def test_basename_only_input(self, workdir, conv_logger, lo_server): # also source paths with a basename only are accepted options = {'meta-procord': 'oocp', 'oocp-out-fmt': 'pdf'} # change to the dir where the src doc resides workdir.join('src').chdir() src_doc = workdir.join('src').join('sample.txt') result_path, cache_key, metadata = convert_doc( os.path.basename(str(src_doc)), options=options, cache_dir=None) assert "Cmd result: 0" in conv_logger.getvalue() assert os.path.basename(result_path) == "sample.pdf" assert metadata == {'error': False, 'oocp_status': 0} # the original source doc still exists assert src_doc.exists()
def test_basename_only_input(self, workdir, conv_logger, lo_server): # also source paths with a basename only are accepted options = {'meta-procord': 'oocp', 'oocp-out-fmt': 'pdf'} # change to the dir where the src doc resides workdir.join('src').chdir() src_doc = workdir.join('src').join('sample.txt') result_path, cache_key, metadata = convert_doc(os.path.basename( str(src_doc)), options=options, cache_dir=None) assert "Cmd result: 0" in conv_logger.getvalue() assert os.path.basename(result_path) == "sample.pdf" assert metadata == {'error': False, 'oocp_status': 0} # the original source doc still exists assert src_doc.exists()
def convert_locally(self, src_path, options): """Convert document in `path`. Expects a local path to the document to convert. The `options` are a dictionary of options as accepted by all converter components in this package. The cache (if set) will be updated. Returns path of converted document, a cache key and a dictionary of metadata. The cache key is ``None`` if no cache was used. """ result_path, cache_key, metadata = convert_doc(src_path, options, self.cache_dir) return result_path, cache_key, metadata
def convert_locally(self, src_path, options): """Convert document in `path`. Expects a local path to the document to convert. The `options` are a dictionary of options as accepted by all converter components in this package. The cache (if set) will be updated. Returns path of converted document, a cache key and a dictionary of metadata. The cache key is ``None`` if no cache was used. """ result_path, cache_key, metadata = convert_doc( src_path, options, self.cache_dir) return result_path, cache_key, metadata