loaders.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. # Copyright 2012-2015 ksyun.com, Inc. or its affiliates. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"). You
  4. # may not use this file except in compliance with the License. A copy of
  5. # the License is located at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # or in the "license" file accompanying this file. This file is
  10. # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. # ANY KIND, either express or implied. See the License for the specific
  12. # language governing permissions and limitations under the License.
  13. """Module for loading various model files.
  14. This module provides the classes that are used to load models used
  15. by kscore. This can include:
  16. * Service models (e.g. the model for EC2, S3, DynamoDB, etc.)
  17. * Other models associated with a service (pagination, waiters)
  18. * Non service-specific config (Endpoint data, retry config)
  19. Loading a module is broken down into several steps:
  20. * Determining the path to load
  21. * Search the data_path for files to load
  22. * The mechanics of loading the file
  23. The last item is used so that other faster loading mechanism
  24. besides the default JSON loader can be used.
  25. The Search Path
  26. ===============
  27. Similar to how the PATH environment variable is to finding executables
  28. and the PYTHONPATH environment variable is to finding python modules
  29. to import, the kscore loaders have the concept of a data path exposed
  30. through AWS_DATA_PATH.
  31. This enables end users to provide additional search paths where we
  32. will attempt to load models outside of the models we ship with
  33. kscore. When you create a ``Loader``, there are two paths
  34. automatically added to the model search path:
  35. * <kscore root>/data/
  36. * ~/.aws/models
  37. The first value is the path where all the model files shipped with
  38. kscore are located.
  39. The second path is so that users can just drop new model files in
  40. ``~/.aws/models`` without having to mess around with the AWS_DATA_PATH.
  41. The AWS_DATA_PATH using the platform specific path separator to
  42. separate entries (typically ``:`` on linux and ``;`` on windows).
  43. Directory Layout
  44. ================
  45. The Loader expects a particular directory layout. In order for any
  46. directory specified in AWS_DATA_PATH to be considered, it must have
  47. this structure for service models::
  48. <root>
  49. |
  50. |-- servicename1
  51. | |-- 2012-10-25
  52. | |-- service-2.json
  53. |-- ec2
  54. | |-- 2014-01-01
  55. | | |-- paginators-1.json
  56. | | |-- service-2.json
  57. | | |-- waiters-2.json
  58. | |-- 2015-03-01
  59. | |-- paginators-1.json
  60. | |-- service-2.json
  61. | |-- waiters-2.json
  62. That is:
  63. * The root directory contains sub directories that are the name
  64. of the services.
  65. * Within each service directory, there's a sub directory for each
  66. available API version.
  67. * Within each API version, there are model specific files, including
  68. (but not limited to): service-2.json, waiters-2.json, paginators-1.json
  69. The ``-1`` and ``-2`` suffix at the end of the model files denote which version
  70. schema is used within the model. Even though this information is available in
  71. the ``version`` key within the model, this version is also part of the filename
  72. so that code does not need to load the JSON model in order to determine which
  73. version to use.
  74. """
  75. import os
  76. from kscore import KSCORE_ROOT
  77. from kscore.compat import json
  78. from kscore.compat import yaml
  79. from kscore.compat import OrderedDict
  80. from kscore.exceptions import DataNotFoundError, UnknownServiceError
  81. def instance_cache(func):
  82. """Cache the result of a method on a per instance basis.
  83. This is not a general purpose caching decorator. In order
  84. for this to be used, it must be used on methods on an
  85. instance, and that instance *must* provide a
  86. ``self._cache`` dictionary.
  87. """
  88. def _wrapper(self, *args, **kwargs):
  89. key = (func.__name__,) + args
  90. for pair in sorted(kwargs.items()):
  91. key += pair
  92. if key in self._cache:
  93. return self._cache[key]
  94. data = func(self, *args, **kwargs)
  95. self._cache[key] = data
  96. return data
  97. return _wrapper
  98. class JSONFileLoader(object):
  99. """Loader JSON files.
  100. This class can load the default format of models, which is a JSON file.
  101. """
  102. def exists(self, file_path):
  103. """Checks if the file exists.
  104. :type file_path: str
  105. :param file_path: The full path to the file to load without
  106. the '.json' extension.
  107. :return: True if file path exists, False otherwise.
  108. """
  109. return os.path.isfile(file_path + '.json')
  110. def load_file(self, file_path):
  111. """Attempt to load the file path.
  112. :type file_path: str
  113. :param file_path: The full path to the file to load without
  114. the '.json' extension.
  115. :return: The loaded data if it exists, otherwise None.
  116. """
  117. full_path = file_path + '.json'
  118. if not os.path.isfile(full_path):
  119. return
  120. # By default the file will be opened with locale encoding on Python 3.
  121. # We specify "utf8" here to ensure the correct behavior.
  122. with open(full_path, 'rb') as fp:
  123. payload = fp.read().decode('utf-8')
  124. return json.loads(payload, object_pairs_hook=OrderedDict)
  125. class YAMLFileLoader(object):
  126. """Loader YAML files.
  127. This class can load the default format of models, which is a YAML file.
  128. """
  129. def exists(self, file_path):
  130. """Checks if the file exists.
  131. :type file_path: str
  132. :param file_path: The full path to the file to load without
  133. the '.json' extension.
  134. :return: True if file path exists, False otherwise.
  135. """
  136. return os.path.isfile(file_path + '.yaml')
  137. def load_file(self, file_path):
  138. """Attempt to load the file path.
  139. :type file_path: str
  140. :param file_path: The full path to the file to load without
  141. the '.json' extension.
  142. :return: The loaded data if it exists, otherwise None.
  143. """
  144. full_path = file_path + '.yaml'
  145. if not os.path.isfile(full_path):
  146. return
  147. # By default the file will be opened with locale encoding on Python 3.
  148. # We specify "utf8" here to ensure the correct behavior.
  149. with open(full_path, 'rb') as fp:
  150. payload = fp.read().decode('utf-8')
  151. return OrderedDict(yaml.load(payload, Loader=yaml.Loader))
  152. def create_loader(search_path_string=None, dynamic_loader=None):
  153. """Create a Loader class.
  154. This factory function creates a loader given a search string path.
  155. :type search_string_path: str
  156. :param search_string_path: The AWS_DATA_PATH value. A string
  157. of data path values separated by the ``os.path.pathsep`` value,
  158. which is typically ``:`` on POSIX platforms and ``;`` on
  159. windows.
  160. :return: A ``Loader`` instance.
  161. """
  162. if dynamic_loader.lower() == "yaml":
  163. loader = YAMLFileLoader()
  164. elif dynamic_loader.lower() == "json":
  165. loader = JSONFileLoader()
  166. else:
  167. loader = None
  168. if search_path_string is None:
  169. return Loader(file_loader=loader)
  170. paths = []
  171. extra_paths = search_path_string.split(os.pathsep)
  172. for path in extra_paths:
  173. path = os.path.expanduser(os.path.expandvars(path))
  174. paths.append(path)
  175. return Loader(extra_search_paths=paths, file_loader=loader)
  176. class Loader(object):
  177. """Find and load data models.
  178. This class will handle searching for and loading data models.
  179. The main method used here is ``load_service_model``, which is a
  180. convenience method over ``load_data`` and ``determine_latest_version``.
  181. """
  182. FILE_LOADER_CLASS = JSONFileLoader
  183. # The included models in kscore/data/ that we ship with kscore.
  184. BUILTIN_DATA_PATH = os.path.join(KSCORE_ROOT, 'data')
  185. # For convenience we automatically add ~/.aws/models to the data path.
  186. CUSTOMER_DATA_PATH = os.path.join(os.path.expanduser('~'),
  187. '.ks', 'models')
  188. def __init__(self, extra_search_paths=None, file_loader=None,
  189. cache=None, include_default_search_paths=True):
  190. self._cache = {}
  191. if file_loader is None:
  192. file_loader = self.FILE_LOADER_CLASS()
  193. self.file_loader = file_loader
  194. if extra_search_paths is not None:
  195. self._search_paths = extra_search_paths
  196. else:
  197. self._search_paths = []
  198. if include_default_search_paths:
  199. self._search_paths.extend([self.CUSTOMER_DATA_PATH,
  200. self.BUILTIN_DATA_PATH])
  201. @property
  202. def search_paths(self):
  203. return self._search_paths
  204. @instance_cache
  205. def list_available_services(self, type_name):
  206. """List all known services.
  207. This will traverse the search path and look for all known
  208. services.
  209. :type type_name: str
  210. :param type_name: The type of the service (service-2,
  211. paginators-1, waiters-2, etc). This is needed because
  212. the list of available services depends on the service
  213. type. For example, the latest API version available for
  214. a resource-1.json file may not be the latest API version
  215. available for a services-2.json file.
  216. :return: A list of all services. The list of services will
  217. be sorted.
  218. """
  219. services = set()
  220. for possible_path in self._potential_locations():
  221. # Any directory in the search path is potentially a service.
  222. # We'll collect any initial list of potential services,
  223. # but we'll then need to further process these directories
  224. # by searching for the corresponding type_name in each
  225. # potential directory.
  226. possible_services = [
  227. d for d in os.listdir(possible_path)
  228. if os.path.isdir(os.path.join(possible_path, d))]
  229. for service_name in possible_services:
  230. full_dirname = os.path.join(possible_path, service_name)
  231. api_versions = os.listdir(full_dirname)
  232. for api_version in api_versions:
  233. full_load_path = os.path.join(full_dirname,
  234. api_version,
  235. type_name)
  236. if self.file_loader.exists(full_load_path):
  237. services.add(service_name)
  238. break
  239. return sorted(services)
  240. @instance_cache
  241. def determine_latest_version(self, service_name, type_name):
  242. """Find the latest API version available for a service.
  243. :type service_name: str
  244. :param service_name: The name of the service.
  245. :type type_name: str
  246. :param type_name: The type of the service (service-2,
  247. paginators-1, waiters-2, etc). This is needed because
  248. the latest API version available can depend on the service
  249. type. For example, the latest API version available for
  250. a resource-1.json file may not be the latest API version
  251. available for a services-2.json file.
  252. :rtype: str
  253. :return: The latest API version. If the service does not exist
  254. or does not have any available API data, then a
  255. ``DataNotFoundError`` exception will be raised.
  256. """
  257. return max(self.list_api_versions(service_name, type_name))
  258. @instance_cache
  259. def list_api_versions(self, service_name, type_name):
  260. """List all API versions available for a particular service type
  261. :type service_name: str
  262. :param service_name: The name of the service
  263. :type type_name: str
  264. :param type_name: The type name for the service (i.e service-2,
  265. paginators-1, etc.)
  266. :rtype: list
  267. :return: A list of API version strings in sorted order.
  268. """
  269. known_api_versions = set()
  270. for possible_path in self._potential_locations(service_name,
  271. must_exist=True,
  272. is_dir=True):
  273. for dirname in os.listdir(possible_path):
  274. full_path = os.path.join(possible_path, dirname, type_name)
  275. # Only add to the known_api_versions if the directory
  276. # contains a service-2, paginators-1, etc. file corresponding
  277. # to the type_name passed in.
  278. if self.file_loader.exists(full_path):
  279. known_api_versions.add(dirname)
  280. if not known_api_versions:
  281. raise DataNotFoundError(data_path=service_name)
  282. return sorted(known_api_versions)
  283. @instance_cache
  284. def load_customer_model(self, path, service_name, type_name, customer_name, api_version=None):
  285. if api_version is None:
  286. api_version = self.determine_latest_version(
  287. service_name, type_name)
  288. full_path = os.path.join(path, service_name, api_version, customer_name)
  289. return self.load_customer(full_path)
  290. @instance_cache
  291. def load_service_model(self, service_name, type_name, api_version=None):
  292. """Load a kscore service model
  293. This is the main method for loading kscore models (e.g. a service
  294. model, pagination configs, waiter configs, etc.).
  295. :type service_name: str
  296. :param service_name: The name of the service (e.g ``ec2``, ``s3``).
  297. :type type_name: str
  298. :param type_name: The model type. Valid types include, but are not
  299. limited to: ``service-2``, ``paginators-1``, ``waiters-2``.
  300. :type api_version: str
  301. :param api_version: The API version to load. If this is not
  302. provided, then the latest API version will be used.
  303. :raises: UnknownServiceError if there is no known service with
  304. the provided service_name.
  305. :raises: DataNotFoundError if no data could be found for the
  306. service_name/type_name/api_version.
  307. :return: The loaded data, as a python type (e.g. dict, list, etc).
  308. """
  309. # Wrapper around the load_data. This will calculate the path
  310. # to call load_data with.
  311. known_services = self.list_available_services(type_name)
  312. if service_name not in known_services:
  313. raise UnknownServiceError(
  314. service_name=service_name,
  315. known_service_names=', '.join(sorted(known_services)))
  316. if api_version is None:
  317. api_version = self.determine_latest_version(
  318. service_name, type_name)
  319. full_path = os.path.join(service_name, api_version, type_name)
  320. return self.load_data(full_path)
  321. @instance_cache
  322. def load_customer(self, path):
  323. found = self.file_loader.load_file(path)
  324. if found is not None:
  325. return found
  326. # We didn't find anything that matched on any path.
  327. raise DataNotFoundError(data_path=path)
  328. @instance_cache
  329. def load_data(self, name):
  330. """Load data given a data path.
  331. This is a low level method that will search through the various
  332. search paths until it's able to load a value. This is typically
  333. only needed to load *non* model files (such as _endpoints and
  334. _retry). If you need to load model files, you should prefer
  335. ``load_service_model``.
  336. :type name: str
  337. :param name: The data path, i.e ``ec2/2015-03-01/service-2``.
  338. :return: The loaded data. If no data could be found then
  339. a DataNotFoundError is raised.
  340. """
  341. for possible_path in self._potential_locations(name):
  342. found = self.file_loader.load_file(possible_path)
  343. if found is not None:
  344. return found
  345. # We didn't find anything that matched on any path.
  346. raise DataNotFoundError(data_path=name)
  347. def _potential_locations(self, name=None, must_exist=False,
  348. is_dir=False):
  349. # Will give an iterator over the full path of potential locations
  350. # according to the search path.
  351. for path in self.search_paths:
  352. if os.path.isdir(path):
  353. full_path = path
  354. if name is not None:
  355. full_path = os.path.join(path, name)
  356. if not must_exist:
  357. yield full_path
  358. else:
  359. if is_dir and os.path.isdir(full_path):
  360. yield full_path
  361. elif os.path.exists(full_path):
  362. yield full_path