Allow user to use a custom pricing file by placing a file to ~/.libcloud/pricing.json.
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/5fa1610c Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/5fa1610c Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/5fa1610c Branch: refs/heads/0.13.x Commit: 5fa1610cdad7400303af8d6cdfb4d19d6bb81d59 Parents: be1bdc0 Author: Tomaz Muraus <[email protected]> Authored: Fri Jun 14 21:50:50 2013 -0700 Committer: Tomaz Muraus <[email protected]> Committed: Thu Aug 1 20:28:49 2013 +0200 ---------------------------------------------------------------------- libcloud/pricing.py | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/5fa1610c/libcloud/pricing.py ---------------------------------------------------------------------- diff --git a/libcloud/pricing.py b/libcloud/pricing.py index 3ed32d2..db3b674 100644 --- a/libcloud/pricing.py +++ b/libcloud/pricing.py @@ -25,27 +25,26 @@ except ImportError: import os.path from os.path import join as pjoin -PRICING_FILE_PATH = 'data/pricing.json' +CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) +DEFAULT_PRICING_FILE_PATH = pjoin(CURRENT_DIRECTORY, 'data/pricing.json') +CUSTOM_PRICING_FILE_PATH = os.path.expanduser('~/.libcloud/pricing.json') -PRICING_DATA = {} +# Pricing data cache +PRICING_DATA = { + 'compute': {}, + 'storage': {} +} VALID_PRICING_DRIVER_TYPES = ['compute', 'storage'] -def clear_pricing_data(): - PRICING_DATA.clear() - PRICING_DATA.update({ - 'compute': {}, - 'storage': {}, - }) -clear_pricing_data() - - def get_pricing_file_path(file_path=None): - pricing_directory = os.path.dirname(os.path.abspath(__file__)) - pricing_file_path = pjoin(pricing_directory, PRICING_FILE_PATH) + if os.path.exists(CUSTOM_PRICING_FILE_PATH) and \ + os.path.isfile(CUSTOM_PRICING_FILE_PATH): + # Custom pricing file is available, use it + return CUSTOM_PRICING_FILE_PATH - return pricing_file_path + return DEFAULT_PRICING_FILE_PATH def get_pricing(driver_type, driver_name, pricing_file_path=None): @@ -58,6 +57,10 @@ def get_pricing(driver_type, driver_name, pricing_file_path=None): @type driver_name: C{str} @param driver_name: Driver name + @type pricing_file_path: C{str} + @param pricing_file_path: Custom path to a price file. If not provided + it uses a default path. + @rtype: C{dict} @return: Dictionary with pricing where a key name is size ID and the value is a price. @@ -126,12 +129,22 @@ def get_size_price(driver_type, driver_name, size_id): def invalidate_pricing_cache(): """ - Invalidate the cache for all the drivers. + Invalidate pricing cache for all the drivers. """ PRICING_DATA['compute'] = {} PRICING_DATA['storage'] = {} +def clear_pricing_data(): + """ + Invalidate pricing cache for all the drivers. + + Note: This method does the same thing as invalidate_pricing_cache and is + here for backward compatibility reasons. + """ + invalidate_pricing_cache() + + def invalidate_module_pricing_cache(driver_type, driver_name): """ Invalidate the cache for the specified driver.
