#!/usr/bin/python3
"""This is the DB storage class for AirBnB"""
import os
from models.base_model import Base
from sqlalchemy import (create_engine)
from sqlalchemy.orm import sessionmaker

os_user = os.environ['HBNB_MYSQL_USER']
os_pass = os.environ['HBNB_MYSQL_PWD']
os_host = os.environ['HBNB_MYSQL_HOST']
os_db = os.environ['HBNB_MYSQL_DB']
os_env = os.environ['HBNB_ENV']

class DBStorage:
    """This class storage instances in MySQL
    """
    __engine = None
    __session = None

    def __init__(self):
        self.__engine = create_engine('mysql+mysqldb://{}:{}@{}:3306/{}'
                                      .format(os_user, os_pass, os_host, os_db),
                                      pool_pre_ping=True)
        if os_env == "test":
            drop_all(bind=None)


    def all(self, cls=None):
        """returns a dictionary
        Return:
            returns a dictionary of __object
        """

        if cls:
            print (session.query(cls).all())
            return (session.query(cls).all())
        else:
            return (session.query.all())
            return (session.query.all())

    def new(self, obj):
        """sets __object to given obj
        Args:
            obj: given object
        """
        if obj:
            self.__session.add(obj)

    def save(self):
        """serialize the file path to JSON file path
        """
        self.__session.commit()

    def reload(self):
        """serialize the file path to JSON file path
        """
        Base.metadata.create_all(self.__engine)
        Session = scoped_session(sessionmaker(bind = self.__engine,
                                              expire_on_commit=False))
        self.__session = Session()

    def delete(self, obj=None):
        """Delete obj from _objects"""
        if obj:
            self.__session.delete(obj)
