#!/usr/bin/python # # Copyright 2009 Google Inc. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # """Handy utility functions.""" __author__ = 'api.sgrinberg@gmail.com (Stan Grinberg)' import codecs import csv import datetime import htmlentitydefs import math import os import re import sys import traceback from urlparse import urlparse from urlparse import urlunparse from aw_api import LIB_HOME from aw_api import SanityCheck from aw_api.AuthToken import AuthToken from aw_api.Buffer import Buffer from aw_api.Errors import Error def ReadFile(f_path): """Load data from a given file. Args: f_path: str absolute path to the file to read. Returns: str data loaded from the file, None otherwise. """ data = '' if f_path: try: fh = open(f_path, 'r') try: data = fh.read() finally: fh.close() except IOError: return '' return data def GetDataFromCsvFile(file_name): """Get data from CSV file, given name of the file from "aw_api/data". Args: file_name: str file name. Returns: list data from CSV file. """ f_path = os.path.join(LIB_HOME, 'data', file_name) rows = [] for row in csv.reader(ReadFile(f_path).split('\n')): if row: rows.append(row) return rows[1:] def GetCategories(): """Get a list of available categories. Returns: list available categories. """ return GetDataFromCsvFile('categories.csv') def GetCountries(): """Get a list of available countries. Returns: list available countries. """ return GetDataFromCsvFile('countries.csv') def GetCurrencies(): """Get a list of available currencies. Returns: list available currencies. """ return GetDataFromCsvFile('currencies.csv') def GetErrorCodes(): """Get a list of available error codes. Returns: list available error codes. """ return GetDataFromCsvFile('error_codes.csv') def GetLanguages(): """Get a list of available languages. Returns: list available languages. """ return GetDataFromCsvFile('languages.csv') def GetOpsRates(): """Get a list of available API operations rates. Returns: list available API operations rates. """ return GetDataFromCsvFile('ops_rates.csv') def GetMethodCost(version, service, method, ops_num, is_sandbox=False, is_validate_only=False): """Get cost of the method in units. Args: version: str version to which method belongs. service: str service to which method belongs. method: str method for which to return cost. ops_num: int number of operations for which to calculate cost. [optional] is_sandbox: bool whether to calculate cost for sandbox instance. is_validate_only: bool whether to calculate cost for validateOnly request. Returns: int cost of the method in units. """ cost = 0 for line in GetOpsRates(): # TODO(api.sgrinberg): Implement support for calculating method cost for # operatins with different operators. if (line and line[0] == version and line[1] == service and line[2].lower() == method.lower()): cost = float(line[3]) if is_validate_only: cost = 0.05 if bool(line[4]): cost *= ops_num break return int(math.ceil(cost)) def GetTimezones(): """Get a list of available timezones. Returns: list available timezones. """ return GetDataFromCsvFile('timezones.csv') def GetUsCities(): """Get a list of available US cities. Returns: list available US cities. """ return GetDataFromCsvFile('us_cities.csv') def GetUsMetros(): """Get a list of available US metros. Returns: list available US metros. """ return GetDataFromCsvFile('us_metros.csv') def GetWorldCities(): """Get a list of available world cities. Returns: list available world cities. """ return GetDataFromCsvFile('world_cities.csv') def GetWorldRegions(): """Get a list of available world regions. Returns: list available world regions. """ return GetDataFromCsvFile('world_regions.csv') def PurgeLogs(logs_home): """Clear all logs generated by this client library. This will remove all stored SOAP XML and request data. WARNING: This will remove all data from the logs generated by the client library. Args: logs_home: str absolute path to logs directory. """ logs = (os.path.join(logs_home, 'aw_api_lib.log'), os.path.join(logs_home, 'soap_xml.log'), os.path.join(logs_home, 'request_info.log')) for log in logs: PurgeLog(log) def PurgeLog(log): """Clear content of a given log file. If the file cannot be opened, Error is raised. Args: log: str absolute path to a log file. """ try: fh = open(log, 'w') try: fh.write('') finally: fh.close() except IOError, e: raise Error(e) # TODO(api.sgrinberg): Rewrite this method using HTML parser library. def GetErrorFromHtml(data): """Return error message from HTML page. Args: data: str HTML data. Returns: str error message. """ pattern = re.compile('\n') data = pattern.sub('', data) # Fetch error message. pattern = re.compile('
|' '.*
(.*)