def test_get_format_element(self): """bibformat - format elements parsing and returned structure""" #Test loading with different kind of names, for element with spaces in name, without bfe_ element_1 = bibformat_engine.get_format_element( "test 1", with_built_in_params=True) self.assert_(element_1 is not None) element_1_bis = bibformat_engine.get_format_element( "bfe_tEst_1.py", with_built_in_params=True) self.assertEqual(element_1, element_1_bis) #Test loading with different kind of names, for element without spaces in name, wit bfe_ element_2 = bibformat_engine.get_format_element( "test 2", with_built_in_params=True) self.assert_(element_2 is not None) element_2_bis = bibformat_engine.get_format_element( "bfe_tEst_2.py", with_built_in_params=True) self.assertEqual(element_2, element_2_bis) #Test loading incorrect elements try: element_3 = bibformat_engine.get_format_element( "test 3", with_built_in_params=True) except bibformat_engine.InvenioBibFormatError, e: self.assertEqual(str(e), 'Format element test 3 could not be found.')
def test_get_format_element(self): """bibformat - format elements parsing and returned structure""" bibformat_engine.CFG_BIBFORMAT_ELEMENTS_PATH = CFG_BIBFORMAT_ELEMENTS_PATH bibformat_engine.CFG_BIBFORMAT_ELEMENTS_IMPORT_PATH = CFG_BIBFORMAT_ELEMENTS_IMPORT_PATH #Test loading with different kind of names, for element with spaces in name, without bfe_ element_1 = bibformat_engine.get_format_element("test 1", with_built_in_params=True) self.assert_(element_1 is not None) element_1_bis = bibformat_engine.get_format_element("bfe_tEst_1.py", with_built_in_params=True) self.assertEqual(element_1, element_1_bis) #Test loading with different kind of names, for element without spaces in name, wit bfe_ element_2 = bibformat_engine.get_format_element("test 2", with_built_in_params=True) self.assert_(element_2 is not None) element_2_bis = bibformat_engine.get_format_element("bfe_tEst_2.py", with_built_in_params=True) self.assertEqual(element_2, element_2_bis) #Test loading incorrect elements element_3 = bibformat_engine.get_format_element("test 3", with_built_in_params=True) self.assertEqual(element_3, None) element_4 = bibformat_engine.get_format_element("test 4", with_built_in_params=True) self.assertEqual(element_4, None) unknown_element = bibformat_engine.get_format_element("TEST_NO_ELEMENT", with_built_in_params=True) self.assertEqual(unknown_element, None) #Test element without docstring element_5 = bibformat_engine.get_format_element("test_5", with_built_in_params=True) self.assert_(element_5 is not None) self.assertEqual(element_5['attrs']['description'], '') self.assert_({'name':"param1", 'description':"(no description provided)", 'default':""} in element_5['attrs']['params'] ) self.assertEqual(element_5['attrs']['seealso'], []) #Test correct parsing: #Test type of element self.assertEqual(element_1['type'], "python") #Test name = element filename, with underscore instead of spaces, #without BFE_ and uppercase self.assertEqual(element_1['attrs']['name'], "TEST_1") #Test description parsing self.assertEqual(element_1['attrs']['description'], "Prints test") #Test @see: parsing self.assertEqual(element_1['attrs']['seealso'], ["element2.py", "unknown_element.py"]) #Test @param parsing self.assert_({'name':"param1", 'description':"desc 1", 'default':""} in element_1['attrs']['params'] ) self.assert_({'name':"param2", 'description':"desc 2", 'default':"default value"} in element_1['attrs']['params'] ) #Test non existing element non_existing_element = bibformat_engine.get_format_element("BFE_NON_EXISTING_ELEMENT") self.assertEqual(non_existing_element, None)
def insert_element_code(match): error = [] function_name = match.group("function_name") try: format_element = get_format_element(function_name, verbose) except Exception: error.append('Invalid function name %s' % (function_name, )) params_str = [] if format_element is not None: params = {} # Look for function parameters given in format template code all_params = match.group('params') if all_params is not None: function_params_iterator = pattern_function_params.\ finditer(all_params) for param_match in function_params_iterator: sep = param_match.group('sep') name = param_match.group('param') value = param_match.group('value') params[name] = value params_str.append(name + '=' + sep + value + sep) # Replace element with function call with params. result = '{{ bfe_%s(bfo, %s) }}' % (function_name.lower(), ', '.join(params_str)) return result print '\n'.join(error)
def _bfe_element(bfo, **kwargs): # convert to utf-8 for legacy app kwargs = dict((k, v.encode('utf-8') if isinstance(v, unicode) else v) for k, v in kwargs.iteritems()) format_element = get_format_element(name) (out, dummy) = eval_format_element(format_element, bfo, kwargs) # returns unicode for jinja2 return out.decode('utf-8')
def test_get_format_element(self): """bibformat - format elements parsing and returned structure""" #Test loading with different kind of names, for element with spaces in name, without bfe_ element_1 = bibformat_engine.get_format_element("test 1", with_built_in_params=True) self.assert_(element_1 is not None) element_1_bis = bibformat_engine.get_format_element("bfe_tEst_1.py", with_built_in_params=True) self.assertEqual(element_1, element_1_bis) #Test loading with different kind of names, for element without spaces in name, wit bfe_ element_2 = bibformat_engine.get_format_element("test 2", with_built_in_params=True) self.assert_(element_2 is not None) element_2_bis = bibformat_engine.get_format_element("bfe_tEst_2.py", with_built_in_params=True) self.assertEqual(element_2, element_2_bis) #Test loading incorrect elements try: element_3 = bibformat_engine.get_format_element("test 3", with_built_in_params=True) except bibformat_engine.InvenioBibFormatError, e: self.assertEqual(str(e), 'Format element test 3 could not be found.')
def test_get_format_element_attrs_from_function(self): """ bibformat - correct parsing of attributes in 'format' docstring""" element_1 = bibformat_engine.get_format_element("test 1", with_built_in_params=True) function = element_1['code'] attrs = bibformat_engine.get_format_element_attrs_from_function(function, element_1['attrs']['name'], with_built_in_params=True) self.assertEqual(attrs['name'], "TEST_1") #Test description parsing self.assertEqual(attrs['description'], "Prints test") #Test @see: parsing self.assertEqual(attrs['seealso'], ["element2.py", "unknown_element.py"])
def call_function(self, function_name, parameters=None): ''' Call an external element which is a Python file, using BibFormat @param function_name: the name of the function to call @param parameters: a dictionary of the parameters to pass as key=value pairs @return: a string value, which is the result of the function call ''' if parameters is None: parameters = {} bfo = BibFormatObject(self.recID) format_element = get_format_element(function_name) (value, errors) = eval_format_element(format_element, bfo, parameters) #to do: check errors from function call return value
def test_get_format_element_attrs_from_function(self): """ bibformat - correct parsing of attributes in 'format' docstring""" element_1 = bibformat_engine.get_format_element( "test 1", with_built_in_params=True) function = element_1['code'] attrs = bibformat_engine.get_format_element_attrs_from_function( function, element_1['attrs']['name'], with_built_in_params=True) self.assertEqual(attrs['name'], "TEST_1") #Test description parsing self.assertEqual(attrs['description'], "Prints test") #Test @see: parsing self.assertEqual(attrs['seealso'], ["element2.py", "unknown_element.py"])
def test_get_format_element_attrs_from_function(self): """ bibformat - correct parsing of attributes in 'format' docstring""" bibformat_engine.CFG_BIBFORMAT_ELEMENTS_PATH = CFG_BIBFORMAT_ELEMENTS_PATH bibformat_engine.CFG_BIBFORMAT_ELEMENTS_IMPORT_PATH = CFG_BIBFORMAT_ELEMENTS_IMPORT_PATH element_1 = bibformat_engine.get_format_element("test 1", with_built_in_params=True) function = element_1["code"] attrs = bibformat_engine.get_format_element_attrs_from_function( function, element_1["attrs"]["name"], with_built_in_params=True ) self.assertEqual(attrs["name"], "TEST_1") # Test description parsing self.assertEqual(attrs["description"], "Prints test") # Test @see: parsing self.assertEqual(attrs["seealso"], ["element2.py", "unknown_element.py"])
#Test loading with different kind of names, for element without spaces in name, wit bfe_ element_2 = bibformat_engine.get_format_element("test 2", with_built_in_params=True) self.assert_(element_2 is not None) element_2_bis = bibformat_engine.get_format_element("bfe_tEst_2.py", with_built_in_params=True) self.assertEqual(element_2, element_2_bis) #Test loading incorrect elements try: element_3 = bibformat_engine.get_format_element("test 3", with_built_in_params=True) except bibformat_engine.InvenioBibFormatError, e: self.assertEqual(str(e), 'Format element test 3 could not be found.') else: self.fail("Should have raised InvenioBibFormatError") try: element_4 = bibformat_engine.get_format_element("test 4", with_built_in_params=True) except SyntaxError: self.assertEqual(str(e), 'Format element test 3 could not be found.') else: self.fail("Should have raised SyntaxError") try: unknown_element = bibformat_engine.get_format_element("TEST_NO_ELEMENT", with_built_in_params=True) except bibformat_engine.InvenioBibFormatError, e: self.assertEqual(str(e), 'Format element TEST_NO_ELEMENT could not be found.') else: self.fail("Should have raised InvenioBibFormatError") #Test element without docstring element_5 = bibformat_engine.get_format_element("test_5", with_built_in_params=True) self.assert_(element_5 is not None)
## You should have received a copy of the GNU General Public License ## along with Invenio; if not, write to the Free Software Foundation, Inc., ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. """BibFormat element - Prints all the publication information in one go LaTeX formats require some elements to be displayed differently depending on whether other elements were present or not. It wasn't obvious how to do that in the Invenio templating system, so this format element is a facade for those others and handles the decision logic. """ from invenio.bibformat_elements import bfe_report_numbers, bfe_INSPIRE_publi_coden from invenio.bibformat_engine import eval_format_element, get_format_element pubnoteFE = get_format_element('bfe_inspire_publi_info', with_built_in_params=True) arxivFE = get_format_element('bfe_inspire_arxiv', with_built_in_params=True) def format_element(bfo, pubnotestyle="eu", pubnotemark="html", pubnotepre=" ", pubnotesuf=" ", pubnotesep=", ", arxivlinks="yes", arxivcategory="yes", arxivprepubnote="<br />", arxivsufpubnote="<br />", arxivprenopub="<br />", arxivsufnopub="<br />", reportpre="", reportsuf="", reportlimit="1", reportsep='', reportext=''): """Aggregates pubnote, arxive, and %% CITATION %% display""" out = '' pubnote = '' pubnote_w = '' arxiv = '' arxiv_w = '' repno = '' repno_w = '' pcnt_pre = arxivprenopub + "%%CITATION = "
element_2_bis = bibformat_engine.get_format_element( "bfe_tEst_2.py", with_built_in_params=True) self.assertEqual(element_2, element_2_bis) #Test loading incorrect elements try: element_3 = bibformat_engine.get_format_element( "test 3", with_built_in_params=True) except bibformat_engine.InvenioBibFormatError, e: self.assertEqual(str(e), 'Format element test 3 could not be found.') else: self.fail("Should have raised InvenioBibFormatError") try: element_4 = bibformat_engine.get_format_element( "test 4", with_built_in_params=True) except SyntaxError: self.assertEqual(str(e), 'Format element test 3 could not be found.') else: self.fail("Should have raised SyntaxError") try: unknown_element = bibformat_engine.get_format_element( "TEST_NO_ELEMENT", with_built_in_params=True) except bibformat_engine.InvenioBibFormatError, e: self.assertEqual( str(e), 'Format element TEST_NO_ELEMENT could not be found.') else: self.fail("Should have raised InvenioBibFormatError")