def isTransformable(source_format, target_format): """ test if a transform from source_format to target_format. would be feasible """ try: _getCommand(source_format, target_format) except (ValueError, LookupError): return False return True
def convert(self, qdc, PID, target_format): """Convert the QDC Metadata from Fedora to any format supported by bibutils. Uses MODS as an intermediate format. """ mods = self._make_mods(qdc, PID) if target_format == 'xml': indent.indent(mods) xml = ET.tostring(mods, encoding='utf-8') result = xml else: xml = ET.tostring(mods, encoding='utf-8') command = _getCommand('xml', target_format) p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=False) (fi, fo, fe) = (p.stdin, p.stdout, p.stderr) # fi.write(_encode(xml)) fi.write(xml) fi.close() result = fo.read() fo.close() error = fe.read() fe.close() return result
def test_suite(): from unittest import TestSuite, makeSuite suite = TestSuite() if _getCommand('bib', 'xml', False): suite.addTest(makeSuite(TestRenderXML)) return suite
def render(self, data, source_format, target_format, output_encoding=None): """ Transform data from 'source_format' to 'target_format' We have nothing, so we do nothing :) >>> if _getCommand('bib', 'end', None) is not None: ... result = ExternalTransformUtility().render('', 'bib', 'end') ... assert result == '' >>> data = ''' ... @Book{bookreference.2008-02-04.7570607450, ... author = {Werner, kla{\"u}s}, ... title = {H{\"a}rry Motter}, ... year = {1980}, ... publisher = {Diogenes} ... }''' This should work. (If external bibutils are installed!) We transform the `bib`-format into the `end`-format >>> if _hasCommands(commands.get('bib2end')): ... result = ExternalTransformUtility().render(data, 'bib', 'end') ... # We need to take care of any stray Windows carriage returns. ... result = result.replace('\r', '') ... assert ''' ... %0 Book ... %A Werner, kla"us title =. H"arry Motter ... %D 1980 ... %I Diogenes ... %F bookreference.2008-02-04.7570607450 '''.strip() in result This one is not allowed. No valid transformer exists for `foo` and `bar` (foo2bar) >>> ExternalTransformUtility().render(data, 'foo', 'bar') Traceback (most recent call last): ... ValueError: No transformation from 'foo' to 'bar' found. """ command = _getCommand(source_format, target_format) if not command: return '' orig_path = os.environ['PATH'] if os.environ.has_key('BIBUTILS_PATH'): os.environ['PATH'] = os.pathsep.join([orig_path, os.environ['BIBUTILS_PATH']]) ts = time.time() # This is a stinking workaround with hanging subprocesses on Linux. # We had the case where "end2xml | xml2bib " was just hanging # while reading the results from the output pipeline. So we fall # back in a safe way to os.system() on Linux if sys.platform == 'linux2': input_filename = tempfile.mktemp() error_filename = tempfile.mktemp() output_filename = tempfile.mktemp() file(input_filename, 'wb').write(_encode(data)) command = 'cat "%s" | %s 2>"%s" 1>"%s"' % (input_filename, command, error_filename, output_filename) st = os.system(command) error = file(output_filename, 'rb').read() result = file(output_filename, 'rb').read() os.unlink(input_filename) os.unlink(output_filename) os.unlink(error_filename) else: ts = time.time() log.info(command) p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=False) (fi, fo, fe) = (p.stdin, p.stdout, p.stderr) fi.write(_encode(data)) fi.close() result = fo.read() fo.close() error = fe.read() fe.close() log.info('Execution time: %2.2f seconds' % (time.time() - ts)) if error: # command could be like 'ris2xml', or 'ris2xml | xml2bib'. It # seems unlikely, but we'll code for an arbitrary number of # pipes... command_list = command.split(' | ') for each in command_list: if each in error and not result: log.error("'%s' not found. Make sure 'bibutils' is installed.", command) if output_encoding is None: return result else: return _convertToOutputEncoding(result, output_encoding=output_encoding) os.environ['PATH'] = orig_path
def available(self): return bool(_getCommand(self.source_format, self.target_format, False))