From ff7d162a3a0734b60437d75b35802519be48b9f0 Mon Sep 17 00:00:00 2001 From: Ayman Awartani Date: Wed, 21 Jun 2017 00:50:50 +0300 Subject: [PATCH 1/2] load metadata from file --- odata/metadata.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/odata/metadata.py b/odata/metadata.py index 6cc85b3..a0248d4 100644 --- a/odata/metadata.py +++ b/odata/metadata.py @@ -41,6 +41,7 @@ def __init__(self, service): self.url = service.url + '$metadata/' self.connection = service.default_context.connection self.service = service + self.metadata_local_file_path = None def property_type_to_python(self, edm_type): return self.property_types.get(edm_type, StringProperty) @@ -218,10 +219,19 @@ def get_entity_or_prop_from_type(typename): self.log.info('Loaded {0} entity sets, total {1} types'.format(len(entities), len(all_types))) return base_class, entities, all_types + def read_metadata_from_file(self, file_path): + self.metadata_local_file_path = file_path + def load_document(self): - self.log.info('Loading metadata document: {0}'.format(self.url)) - response = self.connection._do_get(self.url) - return ET.fromstring(response.content) + if self.metadata_local_file_path: + self.log.info('Reading metadata document: {0}'.format(self.metadata_local_file_path)) + with open(self.metadata_local_file_path, 'r') as metadata_file: + content = metadata_file.read() + return ET.fromstring(content) + else: + self.log.info('Loading metadata document: {0}'.format(self.url)) + response = self.connection._do_get(self.url) + return ET.fromstring(response.content) def _parse_action(self, xmlq, action_element, schema_name): action = { From d631ea7b7c2812d1877a231b4ae340ec0d76659d Mon Sep 17 00:00:00 2001 From: Ayman Awartani Date: Wed, 21 Jun 2017 00:56:56 +0300 Subject: [PATCH 2/2] pass filepath through service --- odata/metadata.py | 7 ++----- odata/service.py | 4 ++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/odata/metadata.py b/odata/metadata.py index a0248d4..dcbce09 100644 --- a/odata/metadata.py +++ b/odata/metadata.py @@ -37,11 +37,11 @@ class MetaData(object): 'Edm.Guid': UUIDProperty, } - def __init__(self, service): + def __init__(self, service, metadata_local_file_path=None): self.url = service.url + '$metadata/' self.connection = service.default_context.connection self.service = service - self.metadata_local_file_path = None + self.metadata_local_file_path = metadata_local_file_path def property_type_to_python(self, edm_type): return self.property_types.get(edm_type, StringProperty) @@ -219,9 +219,6 @@ def get_entity_or_prop_from_type(typename): self.log.info('Loaded {0} entity sets, total {1} types'.format(len(entities), len(all_types))) return base_class, entities, all_types - def read_metadata_from_file(self, file_path): - self.metadata_local_file_path = file_path - def load_document(self): if self.metadata_local_file_path: self.log.info('Reading metadata document: {0}'.format(self.metadata_local_file_path)) diff --git a/odata/service.py b/odata/service.py index 6238462..1335c0d 100644 --- a/odata/service.py +++ b/odata/service.py @@ -76,7 +76,7 @@ class ODataService(object): :param auth: Custom Requests auth object to use for credentials :raises ODataConnectionError: Fetching metadata failed. Server returned an HTTP error code """ - def __init__(self, url, base=None, reflect_entities=False, session=None, auth=None): + def __init__(self, url, base=None, reflect_entities=False, session=None, auth=None, metadata_local_file_path=None): self.url = url self.metadata_url = '' self.collections = {} @@ -115,7 +115,7 @@ def __init__(self, url, base=None, reflect_entities=False, session=None, auth=No :type types: dict """ - self.metadata = MetaData(self) + self.metadata = MetaData(self, metadata_local_file_path) self.Base = base or declarative_base() """ Entity base class. Either a custom one given in init or a generated one. Can be used to define entities