<s> import os <EOL> from urlparse import urlparse <EOL> from jinja2 import Environment <EOL> import requests <EOL> from gonzo . config import config_proxy as config <EOL> from gonzo . exceptions import DataError <EOL> def get_parsed_document ( entity_name , uri = None , config_params_key = None , <EOL> additional_params = None ) : <EOL> """<STR_LIT>""" <EOL> user_data_params = build_params_dict ( entity_name , config_params_key , <EOL> additional_params ) <EOL> return get_document ( uri , user_data_params ) <EOL> def build_params_dict ( entity_name , config_params_key , additional_params = None ) : <EOL> """<STR_LIT>""" <EOL> params = { <EOL> '<STR_LIT>' : entity_name , <EOL> '<STR_LIT>' : entity_name , <EOL> '<STR_LIT>' : config . get_cloud ( ) [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : "<STR_LIT>" % ( entity_name , config . get_cloud ( ) [ '<STR_LIT>' ] ) , <EOL> } <EOL> if config_params_key in config . get_cloud ( ) : <EOL> params . update ( config . get_cloud ( ) [ config_params_key ] ) <EOL> if additional_params is not None : <EOL> params . update ( additional_params ) <EOL> return params <EOL> def get_document ( uri , params = None ) : <EOL> """<STR_LIT>""" <EOL> if uri is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> try : <EOL> urlparse ( uri ) <EOL> data = fetch_from_url ( uri ) <EOL> except requests . exceptions . MissingSchema : <EOL> uri = os . path . expanduser ( uri ) <EOL> uri = os . path . abspath ( uri ) <EOL> if os . path . isabs ( uri ) : <EOL> try : <EOL> data = open ( uri , '<STR_LIT:r>' ) . read ( ) <EOL> except IOError as err : <EOL> err_msg = "<STR_LIT>" . format ( err ) <EOL> raise DataError ( err_msg ) <EOL> else : <EOL> err_msg = "<STR_LIT>" . format ( uri ) <EOL> raise DataError ( err_msg ) <EOL> except requests . exceptions . ConnectionError as err : <EOL> err_msg = "<STR_LIT>" . format ( err ) <EOL> raise DataError ( err_msg ) <EOL> data_tpl = Environment ( ) . from_string ( data ) <EOL> return data_tpl . render ( params ) <EOL> def fetch_from_url ( url ) : <EOL> resp = requests . get ( url ) <EOL> if resp . status_code != requests . codes . ok : <EOL> raise requests . exceptions . ConnectionError ( "<STR_LIT>" ) <EOL> return resp . text </s>
