Github user mxmrlv commented on a diff in the pull request:

    https://github.com/apache/incubator-ariatosca/pull/31#discussion_r90499710
  
    --- Diff: aria/storage/__init__.py ---
    @@ -37,354 +37,93 @@
         * drivers - module, a pool of Aria standard drivers.
         * StorageDriver - class, abstract model implementation.
     """
    -# todo: rewrite the above package documentation
    -# (something like explaning the two types of storage - models and 
resources)
     
    -from collections import namedtuple
    -
    -from .structures import Storage, Field, Model, IterField, PointerField
    -from .drivers import (
    -    ModelDriver,
    -    ResourceDriver,
    -    FileSystemResourceDriver,
    -    FileSystemModelDriver,
    +from aria.logger import LoggerMixin
    +from . import (
    +    models,
    +    exceptions,
    +    api as storage_api,
    +    structures
     )
    -from . import models, exceptions
    +
     
     __all__ = (
         'ModelStorage',
    -    'ResourceStorage',
    -    'FileSystemModelDriver',
         'models',
         'structures',
    -    'Field',
    -    'IterField',
    -    'PointerField',
    -    'Model',
    -    'drivers',
    -    'ModelDriver',
    -    'ResourceDriver',
    -    'FileSystemResourceDriver',
     )
    -# todo: think about package output api's...
    -# todo: in all drivers name => entry_type
    -# todo: change in documentation str => basestring
     
     
    -class ModelStorage(Storage):
    +class Storage(LoggerMixin):
         """
    -    Managing the models storage.
    +    Represents the storage
         """
    -    def __init__(self, driver, model_classes=(), **kwargs):
    -        """
    -        Simple storage client api for Aria applications.
    -        The storage instance defines the tables/documents/code api.
    -
    -        :param ModelDriver driver: model storage driver.
    -        :param model_classes: the models to register.
    -        """
    -        assert isinstance(driver, ModelDriver)
    -        super(ModelStorage, self).__init__(driver, model_classes, **kwargs)
    -
    -    def __getattr__(self, table):
    -        """
    -        getattr is a shortcut to simple api
    -
    -        for Example:
    -        >> storage = ModelStorage(driver=FileSystemModelDriver('/tmp'))
    -        >> node_table = storage.node
    -        >> for node in node_table:
    -        >>     print node
    -
    -        :param str table: table name to get
    -        :return: a storage object that mapped to the table name
    -        """
    -        return super(ModelStorage, self).__getattr__(table)
    -
    -    def register(self, model_cls):
    -        """
    -        Registers the model type in the resource storage manager.
    -        :param model_cls: the model to register.
    -        """
    -        model_name = generate_lower_name(model_cls)
    -        model_api = _ModelApi(model_name, self.driver, model_cls)
    -        self.registered[model_name] = model_api
    -
    -        for pointer_schema_register in model_api.pointer_mapping.values():
    -            model_cls = pointer_schema_register.model_cls
    -            self.register(model_cls)
    -
    -_Pointer = namedtuple('_Pointer', 'name, is_iter')
    -
    -
    -class _ModelApi(object):
    -    def __init__(self, name, driver, model_cls):
    -        """
    -        Managing the model in the storage, using the driver.
    -
    -        :param basestring name: the name of the model.
    -        :param ModelDriver driver: the driver which supports this model in 
the storage.
    -        :param Model model_cls: table/document class model.
    -        """
    -        assert isinstance(driver, ModelDriver)
    -        assert issubclass(model_cls, Model)
    -        self.name = name
    -        self.driver = driver
    -        self.model_cls = model_cls
    -        self.pointer_mapping = {}
    -        self._setup_pointers_mapping()
    -
    -    def _setup_pointers_mapping(self):
    -        for field_name, field_cls in vars(self.model_cls).items():
    -            if not(isinstance(field_cls, PointerField) and field_cls.type):
    -                continue
    -            pointer_key = _Pointer(field_name, 
is_iter=isinstance(field_cls, IterField))
    -            self.pointer_mapping[pointer_key] = self.__class__(
    -                name=generate_lower_name(field_cls.type),
    -                driver=self.driver,
    -                model_cls=field_cls.type)
    -
    -    def __iter__(self):
    -        return self.iter()
    +    def __init__(self, api, items=(), api_params=None, **kwargs):
    +        self._api_params = api_params or {}
    +        super(Storage, self).__init__(**kwargs)
    +        self.api = api
    +        self.registered = {}
    +        for item in items:
    +            self.register(item)
    +        self.logger.debug('{name} object is ready: {0!r}'.format(
    +            self, name=self.__class__.__name__))
     
         def __repr__(self):
    -        return '{self.name}(driver={self.driver}, 
model={self.model_cls})'.format(self=self)
    -
    -    def create(self):
    -        """
    -        Creates the model in the storage.
    -        """
    -        with self.driver as connection:
    -            connection.create(self.name)
    -
    -    def get(self, entry_id, **kwargs):
    -        """
    -        Getter for the model from the storage.
    -
    -        :param basestring entry_id: the id of the table/document.
    -        :return: model instance
    -        :rtype: Model
    -        """
    -        with self.driver as connection:
    -            data = connection.get(
    -                name=self.name,
    -                entry_id=entry_id,
    -                **kwargs)
    -            data.update(self._get_pointers(data, **kwargs))
    -        return self.model_cls(**data)
    +        return 
'{name}(api={self.api})'.format(name=self.__class__.__name__, self=self)
     
    -    def store(self, entry, **kwargs):
    -        """
    -        Setter for the model in the storage.
    -
    -        :param Model entry: the table/document to store.
    -        """
    -        assert isinstance(entry, self.model_cls)
    -        with self.driver as connection:
    -            data = entry.fields_dict
    -            data.update(self._store_pointers(data, **kwargs))
    -            connection.store(
    -                name=self.name,
    -                entry_id=entry.id,
    -                entry=data,
    -                **kwargs)
    -
    -    def delete(self, entry_id, **kwargs):
    -        """
    -        Delete the model from storage.
    -
    -        :param basestring entry_id: id of the entity to delete from 
storage.
    -        """
    -        entry = self.get(entry_id)
    -        with self.driver as connection:
    -            self._delete_pointers(entry, **kwargs)
    -            connection.delete(
    -                name=self.name,
    -                entry_id=entry_id,
    -                **kwargs)
    -
    -    def iter(self, **kwargs):
    -        """
    -        Generator over the entries of model in storage.
    -        """
    -        with self.driver as connection:
    -            for data in connection.iter(name=self.name, **kwargs):
    -                data.update(self._get_pointers(data, **kwargs))
    -                yield self.model_cls(**data)
    +    def __getattr__(self, item):
    +        try:
    +            return self.registered[item]
    +        except KeyError:
    +            return super(Storage, self).__getattribute__(item)
     
    -    def update(self, entry_id, **kwargs):
    +    def register(self, entry):
             """
    -        Updates and entry in storage.
    -
    -        :param str entry_id: the id of the table/document.
    -        :param kwargs: the fields to update.
    +        Register the entry to the storage
    +        :param name:
             :return:
             """
    -        with self.driver as connection:
    -            connection.update(
    -                name=self.name,
    -                entry_id=entry_id,
    -                **kwargs
    -            )
    -
    -    def _get_pointers(self, data, **kwargs):
    -        pointers = {}
    -        for field, schema in self.pointer_mapping.items():
    -            if field.is_iter:
    -                pointers[field.name] = [
    -                    schema.get(entry_id=pointer_id, **kwargs)
    -                    for pointer_id in data[field.name]
    -                    if pointer_id]
    -            elif data[field.name]:
    -                pointers[field.name] = 
schema.get(entry_id=data[field.name], **kwargs)
    -        return pointers
    -
    -    def _store_pointers(self, data, **kwargs):
    -        pointers = {}
    -        for field, model_api in self.pointer_mapping.items():
    -            if field.is_iter:
    -                pointers[field.name] = []
    -                for iter_entity in data[field.name]:
    -                    pointers[field.name].append(iter_entity.id)
    -                    model_api.store(iter_entity, **kwargs)
    -            else:
    -                pointers[field.name] = data[field.name].id
    -                model_api.store(data[field.name], **kwargs)
    -        return pointers
    +        raise NotImplementedError('Subclass must implement abstract 
register method')
     
    -    def _delete_pointers(self, entry, **kwargs):
    -        for field, schema in self.pointer_mapping.items():
    -            if field.is_iter:
    -                for iter_entry in getattr(entry, field.name):
    -                    schema.delete(iter_entry.id, **kwargs)
    -            else:
    -                schema.delete(getattr(entry, field.name).id, **kwargs)
     
    -
    -class ResourceApi(object):
    +class ResourceStorage(Storage):
         """
    -    Managing the resource in the storage, using the driver.
    -
    -    :param basestring name: the name of the resource.
    -    :param ResourceDriver driver: the driver which supports this resource 
in the storage.
    +    Represents resource storage.
         """
    -    def __init__(self, driver, resource_name):
    -        """
    -        Managing the resources in the storage, using the driver.
    -
    -        :param ResourceDriver driver: the driver which supports this model 
in the storage.
    -        :param basestring resource_name: the type of the entry this 
resourceAPI manages.
    -        """
    -        assert isinstance(driver, ResourceDriver)
    -        self.driver = driver
    -        self.resource_name = resource_name
    -
    -    def __repr__(self):
    -        return '{name}(driver={self.driver}, 
resource={self.resource_name})'.format(
    -            name=self.__class__.__name__, self=self)
    -
    -    def create(self):
    -        """
    -        Create the resource dir in the storage.
    -        """
    -        with self.driver as connection:
    -            connection.create(self.resource_name)
    -
    -    def data(self, entry_id, path=None, **kwargs):
    +    def register(self, name):
             """
    -        Retrieve the content of a storage resource.
    -
    -        :param basestring entry_id: the id of the entry.
    -        :param basestring path: path of the resource on the storage.
    -        :param kwargs: resources to be passed to the driver..
    -        :return the content of a single file:
    -        """
    -        with self.driver as connection:
    -            return connection.data(
    -                entry_type=self.resource_name,
    -                entry_id=entry_id,
    -                path=path,
    -                **kwargs)
    -
    -    def download(self, entry_id, destination, path=None, **kwargs):
    -        """
    -        Download a file/dir from the resource storage.
    -
    -        :param basestring entry_id: the id of the entry.
    -        :param basestring destination: the destination of the file/dir.
    -        :param basestring path: path of the resource on the storage.
    -        """
    -        with self.driver as connection:
    -            connection.download(
    -                entry_type=self.resource_name,
    -                entry_id=entry_id,
    -                destination=destination,
    -                path=path,
    -                **kwargs)
    -
    -    def upload(self, entry_id, source, path=None, **kwargs):
    -        """
    -        Upload a file/dir from the resource storage.
    -
    -        :param basestring entry_id: the id of the entry.
    -        :param basestring source: the source path of the file to upload.
    -        :param basestring path: the destination of the file, relative to 
the root dir
    -                                of the resource
    +        Register the resource type to resource storage.
    +        :param name:
    +        :return:
             """
    -        with self.driver as connection:
    -            connection.upload(
    -                entry_type=self.resource_name,
    -                entry_id=entry_id,
    -                source=source,
    -                path=path,
    -                **kwargs)
    +        self.registered[name] = self.api(name=name, **self._api_params)
    --- End diff --
    
    move __init__ to core


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to