Add download_pricing_file function to libcloud.pricing module.
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/2e929567 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/2e929567 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/2e929567 Branch: refs/heads/trunk Commit: 2e929567d2696615b5f50937d9d46e2276e9e2df Parents: cde33ec Author: Tomaz Muraus <[email protected]> Authored: Wed Jul 31 19:48:01 2013 +0200 Committer: Tomaz Muraus <[email protected]> Committed: Thu Aug 1 14:30:03 2013 +0200 ---------------------------------------------------------------------- libcloud/pricing.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/2e929567/libcloud/pricing.py ---------------------------------------------------------------------- diff --git a/libcloud/pricing.py b/libcloud/pricing.py index db3b674..6e5befe 100644 --- a/libcloud/pricing.py +++ b/libcloud/pricing.py @@ -13,17 +13,31 @@ # See the License for the specific language governing permissions and # limitations under the License. from __future__ import with_statement + """ A class which handles loading the pricing files. """ +import os.path +from os.path import join as pjoin + try: import simplejson as json except ImportError: import json -import os.path -from os.path import join as pjoin +from libcloud.utils.connection import get_response_object + +__all__ = [ + 'get_pricing', + 'get_size_price', + 'set_pricing', + 'clear_pricing_data', + 'download_pricing_file' +] + +# Default URL to the pricing file +DEFAULT_FILE_URL = 'https://git-wip-us.apache.org/repos/asf?p=libcloud.git;a=blob_plain;f=libcloud/data/pricing.json' CURRENT_DIRECTORY = os.path.dirname(os.path.abspath(__file__)) DEFAULT_PRICING_FILE_PATH = pjoin(CURRENT_DIRECTORY, 'data/pricing.json') @@ -157,3 +171,46 @@ def invalidate_module_pricing_cache(driver_type, driver_name): """ if driver_name in PRICING_DATA[driver_type]: del PRICING_DATA[driver_type][driver_name] + + +def download_pricing_file(file_url=DEFAULT_FILE_URL, + file_path=CUSTOM_PRICING_FILE_PATH): + """ + Download pricing file from the file_url and save it to file_path. + + @type file_url: C{str} + @param file_url: URL pointing to the pricing file. + + @type file_path: C{str} + @param file_path: Path where a download pricing file will be saved. + """ + dir_name = os.path.dirname(file_path) + + if not os.path.exists(dir_name): + # Verify a valid path is provided + msg = ('Can\'t write to %s, directory %s, doesn\'t exist' % + (file_path, dir_name)) + raise ValueError(msg) + + if os.path.exists(file_path) and os.path.isdir(file_path): + msg = ('Can\'t write to %s file path because it\'s a' + ' directory' % (file_path)) + raise ValueError(msg) + + response = get_response_object(file_url) + body = response.body + + # Verify pricing file is valid + try: + data = json.loads(body) + except json.decoder.JSONDecodeError: + msg = 'Provided URL doesn\'t contain valid pricing data' + raise Exception(msg) + + if not data.get('updated', None): + msg = 'Provided URL doesn\'t contain valid pricing data' + raise Exception(msg) + + # No need to stream it since file is small + with open(file_path, 'w') as file_handle: + file_handle.write(body)
