B ㊇cI@sdZddlZddlZddlmZddlmZmZmZddl m Z m Z ddl m Z deiZerpddlmZeed <eeZd d ZGd d d ZdddZGdddZGdddZdS)a Module for loading various model files. This module provides the classes that are used to load models used by botocore. This can include: * Service models (e.g. the model for EC2, S3, DynamoDB, etc.) * Service model extras which customize the service models * Other models associated with a service (pagination, waiters) * Non service-specific config (Endpoint data, retry config) Loading a module is broken down into several steps: * Determining the path to load * Search the data_path for files to load * The mechanics of loading the file * Searching for extras and applying them to the loaded file The last item is used so that other faster loading mechanism besides the default JSON loader can be used. The Search Path =============== Similar to how the PATH environment variable is to finding executables and the PYTHONPATH environment variable is to finding python modules to import, the botocore loaders have the concept of a data path exposed through AWS_DATA_PATH. This enables end users to provide additional search paths where we will attempt to load models outside of the models we ship with botocore. When you create a ``Loader``, there are two paths automatically added to the model search path: * /data/ * ~/.aws/models The first value is the path where all the model files shipped with botocore are located. The second path is so that users can just drop new model files in ``~/.aws/models`` without having to mess around with the AWS_DATA_PATH. The AWS_DATA_PATH using the platform specific path separator to separate entries (typically ``:`` on linux and ``;`` on windows). Directory Layout ================ The Loader expects a particular directory layout. In order for any directory specified in AWS_DATA_PATH to be considered, it must have this structure for service models:: | |-- servicename1 | |-- 2012-10-25 | |-- service-2.json |-- ec2 | |-- 2014-01-01 | | |-- paginators-1.json | | |-- service-2.json | | |-- waiters-2.json | |-- 2015-03-01 | |-- paginators-1.json | |-- service-2.json | |-- waiters-2.json | |-- service-2.sdk-extras.json That is: * The root directory contains sub directories that are the name of the services. * Within each service directory, there's a sub directory for each available API version. * Within each API version, there are model specific files, including (but not limited to): service-2.json, waiters-2.json, paginators-1.json The ``-1`` and ``-2`` suffix at the end of the model files denote which version schema is used within the model. Even though this information is available in the ``version`` key within the model, this version is also part of the filename so that code does not need to load the JSON model in order to determine which version to use. The ``sdk-extras`` and similar files represent extra data that needs to be applied to the model after it is loaded. Data in these files might represent information that doesn't quite fit in the original models, but is still needed for the sdk. For instance, additional operation parameters might be added here which don't represent the actual service api. N) BOTOCORE_ROOT)HAS_GZIP OrderedDictjson)DataNotFoundErrorUnknownServiceError) deep_mergez.json)openz.json.gzcsfdd}|S)aCache the result of a method on a per instance basis. This is not a general purpose caching decorator. In order for this to be used, it must be used on methods on an instance, and that instance *must* provide a ``self._cache`` dictionary. cs\jf|}xt|D] }||7}qW||jkr>|j|S|f||}||j|<|S)N)__name__sorteditems_cache)selfargskwargskeypairdata)funck/private/var/folders/8c/hx9_v10d5x38qmnzt13b7b8j1k3n5b/T/pip-target-x6xd5gna/lib/python/botocore/loaders.py_wrappers     z instance_cache.._wrapperr)rrr)rrinstance_cache~s rc@s(eZdZdZddZddZddZdS) JSONFileLoaderzeLoader JSON files. This class can load the default format of models, which is a JSON file. cCs&x tD]}tj||rdSqWdS)zChecks if the file exists. :type file_path: str :param file_path: The full path to the file to load without the '.json' extension. :return: True if file path exists, False otherwise. TF)_JSON_OPEN_METHODSospathisfile)r file_pathextrrrexistss zJSONFileLoader.existsc CsNtj|sdS||d}|d}WdQRXtd|tj|t dS)Nrbzutf-8zLoading JSON file: %s)object_pairs_hook) rrrreaddecodeloggerdebugrloadsr)r full_path open_methodfppayloadrrr _load_files    zJSONFileLoader._load_filecCs6x0tD]$\}}||||}|dk r |Sq WdS)zAttempt to load the file path. :type file_path: str :param file_path: The full path to the file to load without the '.json' extension. :return: The loaded data if it exists, otherwise None. N)rr r,)rrrr)rrrr load_files zJSONFileLoader.load_fileN)r __module__ __qualname____doc__r r,r-rrrrrs rcCsT|dkrtSg}|tj}x*|D]"}tjtj|}||q$Wt|dS)aCreate a Loader class. This factory function creates a loader given a search string path. :type search_string_path: str :param search_string_path: The AWS_DATA_PATH value. A string of data path values separated by the ``os.path.pathsep`` value, which is typically ``:`` on POSIX platforms and ``;`` on windows. :return: A ``Loader`` instance. N)extra_search_paths)Loadersplitrpathsepr expanduser expandvarsappend)Zsearch_path_stringpathsZ extra_pathsrrrr create_loaders  r9c@seZdZdZeZeje dZ ejej dddZ dgZ d"d d Zed d Zed dZeddZeddZeddZed#ddZddZeddZddZd$ddZd d!ZdS)%r2zFind and load data models. This class will handle searching for and loading data models. The main method used here is ``load_service_model``, which is a convenience method over ``load_data`` and ``determine_latest_version``. r~z.awsmodelssdkNTcCsni|_|dkr|}||_|dk r,||_ng|_|rJ|j|j|jgg|_|rb|j|jt |_ dS)N) r FILE_LOADER_CLASS file_loader _search_pathsextendCUSTOMER_DATA_PATHBUILTIN_DATA_PATH _extras_typesBUILTIN_EXTRAS_TYPESExtrasProcessor_extras_processor)rr1r>cacheZinclude_default_search_pathsZinclude_default_extrasrrr__init__szLoader.__init__cCs|jS)N)r?)rrrr search_pathsszLoader.search_pathscCs|jS)N)rC)rrrr extras_typesszLoader.extras_typesc st}x|D]xfddtD}xZ|D]R}tj|}t|}x4|D],}tj|||}|j|rT||PqTWq2WqWt |S)apList all known services. This will traverse the search path and look for all known services. :type type_name: str :param type_name: The type of the service (service-2, paginators-1, waiters-2, etc). This is needed because the list of available services depends on the service type. For example, the latest API version available for a resource-1.json file may not be the latest API version available for a services-2.json file. :return: A list of all services. The list of services will be sorted. cs&g|]}tjtj|r|qSr)rrisdirjoin).0d) possible_pathrr 1sz2Loader.list_available_services..) set_potential_locationsrlistdirrrLr>r addr ) r type_nameZservicesZpossible_services service_nameZ full_dirnameZ api_versions api_versionZfull_load_pathr)rOrlist_available_servicess       zLoader.list_available_servicescCst|||S)a Find the latest API version available for a service. :type service_name: str :param service_name: The name of the service. :type type_name: str :param type_name: The type of the service (service-2, paginators-1, waiters-2, etc). This is needed because the latest API version available can depend on the service type. For example, the latest API version available for a resource-1.json file may not be the latest API version available for a services-2.json file. :rtype: str :return: The latest API version. If the service does not exist or does not have any available API data, then a ``DataNotFoundError`` exception will be raised. )maxlist_api_versions)rrVrUrrrdetermine_latest_versionAszLoader.determine_latest_versioncCspt}xR|j|dddD]>}x8t|D]*}tj|||}|j|r(||q(WqW|sht |dt |S)ayList all API versions available for a particular service type :type service_name: str :param service_name: The name of the service :type type_name: str :param type_name: The type name for the service (i.e service-2, paginators-1, etc.) :rtype: list :return: A list of API version strings in sorted order. T) must_existis_dir) data_path) rQrRrrSrrLr>r rTrr )rrVrUZknown_api_versionsrOdirnamer(rrrrZXs  zLoader.list_api_versionscCsv||}||kr(t|dt|d|dkr<|||}tj|||}||}||||}|j |||S)aJLoad a botocore service model This is the main method for loading botocore models (e.g. a service model, pagination configs, waiter configs, etc.). :type service_name: str :param service_name: The name of the service (e.g ``ec2``, ``s3``). :type type_name: str :param type_name: The model type. Valid types include, but are not limited to: ``service-2``, ``paginators-1``, ``waiters-2``. :type api_version: str :param api_version: The API version to load. If this is not provided, then the latest API version will be used. :type load_extras: bool :param load_extras: Whether or not to load the tool extras which contain additional data to be added to the model. :raises: UnknownServiceError if there is no known service with the provided service_name. :raises: DataNotFoundError if no data could be found for the service_name/type_name/api_version. :return: The loaded data, as a python type (e.g. dict, list, etc). z, )rVZknown_service_namesN) rXrrLr r[rr load_data _find_extrasrFprocess)rrVrUrWZknown_servicesr(modelZ extras_datarrrload_service_modelvs  zLoader.load_service_modelc csZxT|jD]J}|d|d}tj|||}y||VWqtk rPYqXqWdS)z-Creates an iterator over all the extras data..z-extrasN)rJrrrLr`r)rrVrUrWZ extras_typeZ extras_namer(rrrras zLoader._find_extrascCs>x.||D] }|j|}|dk r ||fSq Wt|ddS)ajSame as ``load_data`` but returns file path as second return value. :type name: str :param name: The data path, i.e ``ec2/2015-03-01/service-2``. :return: Tuple of the loaded data and the path to the data file where the data was loaded from. If no data could be found then a DataNotFoundError is raised. N)r^)rRr>r-r)rnamerOfoundrrrload_data_with_paths   zLoader.load_data_with_pathcCs||\}}|S)aLoad data given a data path. This is a low level method that will search through the various search paths until it's able to load a value. This is typically only needed to load *non* model files (such as _endpoints and _retry). If you need to load model files, you should prefer ``load_service_model``. Use ``load_data_with_path`` to get the data path of the data file as second return value. :type name: str :param name: The data path, i.e ``ec2/2015-03-01/service-2``. :return: The loaded data. If no data could be found then a DataNotFoundError is raised. )rh)rrfr_rrrr`szLoader.load_dataFccspxj|jD]`}tj|r|}|dk r2tj||}|s>|Vq|rVtj|rV|Vqtj|r|VqWdS)N)rIrrrKrLr )rrfr\r]rr(rrrrRs   zLoader._potential_locationscCs tjtj|}||jS)aWhether a given path is within the package's data directory. This method can be used together with load_data_with_path(name) to determine if data has been loaded from a file bundled with the package, as opposed to a file in a separate location. :type path: str :param path: The file path to check. :return: Whether the given path is within the package's data directory. )rrr5r6 startswithrB)rrrrris_builtin_paths zLoader.is_builtin_path)NNNTT)N)NFF)r r.r/r0rr=rrrLrrBr5rArDrHpropertyrIrJrrXr[rZrdrarhr`rRrkrrrrr2s.    +   2  r2c@s eZdZdZddZddZdS)rEz5Processes data from extras files into service models.cCsx|D]}|||qWdS)a$Processes data from a list of loaded extras files into a model :type original_model: dict :param original_model: The service model to load all the extras into. :type extra_models: iterable of dict :param extra_models: A list of loaded extras models. N)_process)rZoriginal_modelZ extra_modelsextrasrrrrbs zExtrasProcessor.processcCsd|krt||ddS)z3Process a single extras model into a service model.mergeN)r)rrcZ extra_modelrrrrm szExtrasProcessor._processN)r r.r/r0rbrmrrrrrEs rE)N)r0loggingrZbotocorerZbotocore.compatrrrZbotocore.exceptionsrrZbotocore.utilsrr rgzipZ gzip_open getLoggerr r%rrr9r2rErrrrgs$    3