diff --git a/bindings/python/deprecation.py b/bindings/python/deprecation.py index 81d4cdc362..46d0a71f0b 100644 --- a/bindings/python/deprecation.py +++ b/bindings/python/deprecation.py @@ -8,6 +8,31 @@ # @ingroup python_bindings from functools import wraps +import inspect +from warnings import warn_explicit, warn + +# General purpose deprecation decorator. Lifted from +# https://gist.github.com/kgriffs/8202106 + +def deprecated(message): + ''' + Flags a method as deprecated. + param message: Text emitted as part of warning. Include instructions to replace the deprecated function e.g. "use waldo_pepper() isnstead." + ''' + def decorator(func): + @wraps(func) + def wrapper(*args, **kwargs): + warning_msg = 'Call to deprecated function {}. {}'.format( + func.__name__, message) + frame = inspect.current_frame().f_back + + warn_explicit(message, + category=DeprecationWarnig, + filename=inspect.getfile(frame.f_code), + lineno=frame.f_lineno) + return func(*args, **kwargs) + return wrapper + return decorator # use of is_new, force_new and ignore_lock is deprecated, use mode instead # the following decorators enable backward compatibility for the deprecation period @@ -30,8 +55,7 @@ def deprecated_args_session(ignore_lock_or_mode=None, is_new=None, if deprecation: # if any(item in ("is_new", "ignore_lock", "force_new") for item in kwargs): - import warnings - warnings.warn( + warn( "Use of ignore_lock, is_new or force_new arguments is deprecated. Use mode argument instead. Have a look at gnucash.SessionOpenMode.", category=DeprecationWarning, stacklevel=3 @@ -65,4 +89,3 @@ def deprecated_args_session_begin(original_function): mode = deprecated_args_session(ignore_lock_or_mode, is_new, force_new, mode, ignore_lock) return(original_function(self, new_uri=new_uri, mode=mode)) return new_function - diff --git a/bindings/python/gnucash_core.py b/bindings/python/gnucash_core.py index cd2d557297..f088fd03f2 100644 --- a/bindings/python/gnucash_core.py +++ b/bindings/python/gnucash_core.py @@ -53,7 +53,8 @@ from gnucash.gnucash_core_c import gncInvoiceLookup, gncInvoiceGetInvoiceFromTxn from gnucash.deprecation import ( deprecated_args_session, deprecated_args_session_init, - deprecated_args_session_begin + deprecated_args_session_begin, + deprecated ) try: @@ -502,6 +503,14 @@ class GncPriceDB(GnuCashCoreClass): See also https://code.gnucash.org/docs/head/gnc-pricedb_8h.html ''' +@deprecated("Use gnc_pricedb_latest_before_t64") +def gnc_pricedb_lookup_latest_before_t64(self, commodity, currency, date): + return self.lookup_nearest_before_t64(commodit, currency,date) + +GncPriceDB.add_method('gnc_pricedb_lookup_latest_before_t64', 'lookup_latest_before_t64') + +GncPriceDB.lookup_latest_before_t64 = method_function_returns_instance(GncPriceDB.lookup_latest_before_t64, GncPrice) + GncPriceDB.add_methods_with_prefix('gnc_pricedb_') PriceDB_dict = { 'lookup_latest' : GncPrice, @@ -514,7 +523,6 @@ methods_return_instance(GncPriceDB,PriceDB_dict) GncPriceDB.get_prices = method_function_returns_instance_list( GncPriceDB.get_prices, GncPrice ) - class GncCommodity(GnuCashCoreClass): pass class GncCommodityTable(GnuCashCoreClass):