Example #1
0
    def do_search(cls, table, order, filter):
        c = cm()

        if filter:
            for i in range(len(filter)):

                regex = re.compile("(!=|>=|<=|=|>|<)", re.I)
                sep = regex.search(filter[i]).groups()
                if sep:
                    separator = sep[0]
                else:
                    Console.error("Please specify a valid filter")
                    return

                split = filter[i].split(separator)

                if '[' in filter[i]:
                    parameters = Parameter.expand(split[1])
                    parameters = ['{} = \'{}\''.format(split[0], parameters[j]) for j in range(len(parameters))]
                    parameters = """ OR """.join(parameters)
                    where = '( {} )'.format(parameters)
                    filter[i] = where
                else:
                    where = '{} {} \'{}\''.format(split[0], separator, split[1])
                    filter[i] = where

            where = """ AND """.join(filter)
            where = ' WHERE {}'.format(where)
        else:
            where = ""

        _table = table.upper()
        sql = text(""" SELECT * FROM {}{}""".format(_table, where))

        try:
            if table == 'vm' or table == 'flavor' or table == 'image':
                model = globals()[_table]
                r = c.session.query(model).from_statement(sql).all()
            else:
                Console.error("Please specify a valid table")
                return
        except Exception:
            Console.error("Please specify a valid search")
            return

        if r:

            print "{} TABLE".format(_table)
            result = c.object_to_dict(r)
            if order:
                _order = order.split(',')
            else:
                _order = None
            output = models.dict_printer(result, order=_order, header=None, output="table", sort_keys=True)
            print(output)
        else:
            print("Nothing found")
Example #2
0
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import cloudmesh_client.common.tables as tables
from cloudmesh_client.db.models import VM,FLAVOR,DEFAULT,IMAGE
import cloudmesh_client.db.models
import cloudmesh_client.db.models as models


filename = Config.path_expand("~/test/db.db")
endpoint = 'sqlite:///{:}'.format(filename)
engine = create_engine(endpoint)
Base = declarative_base(bind=engine)

Session = sessionmaker(bind=engine)
session = Session()

r = session.query(VM).all()
for obj in r:
    print obj.name

result = dict()
for u in r:
    _id = u.id
    values = {}
    for key in u.__dict__.keys():
        if not key.startswith("_sa"):
            values[key] = u.__dict__[key]
    result[_id] = values

output = models.dict_printer(result, order=None, header=None, output="dict", sort_keys=True)
print(output)
Example #3
0
    def do_search(cls, table, order, filter):
        c = cm()

        if filter:
            for i in range(len(filter)):

                regex = re.compile("(!=|>=|<=|=|>|<)", re.I)
                sep = regex.search(filter[i]).groups()
                if sep:
                    separator = sep[0]
                else:
                    Console.error("Please specify a valid filter")
                    return

                split = filter[i].split(separator)

                if '[' in filter[i]:
                    parameters = Parameter.expand(split[1])
                    parameters = [
                        '{} = \'{}\''.format(split[0], parameters[j])
                        for j in range(len(parameters))
                    ]
                    parameters = """ OR """.join(parameters)
                    where = '( {} )'.format(parameters)
                    filter[i] = where
                else:
                    where = '{} {} \'{}\''.format(split[0], separator,
                                                  split[1])
                    filter[i] = where

            where = """ AND """.join(filter)
            where = ' WHERE {}'.format(where)
        else:
            where = ""

        _table = table.upper()
        sql = text(""" SELECT * FROM {}{}""".format(_table, where))

        try:
            if table == 'vm' or table == 'flavor' or table == 'image':
                model = globals()[_table]
                r = c.session.query(model).from_statement(sql).all()
            else:
                Console.error("Please specify a valid table")
                return
        except Exception:
            Console.error("Please specify a valid search")
            return

        if r:

            print "{} TABLE".format(_table)
            result = c.object_to_dict(r)
            if order:
                _order = order.split(',')
            else:
                _order = None
            output = models.dict_printer(result,
                                         order=_order,
                                         header=None,
                                         output="table",
                                         sort_keys=True)
            print(output)
        else:
            print("Nothing found")
Example #4
0
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
import cloudmesh_client.common.tables as tables
from cloudmesh_client.db.models import VM,FLAVOR,DEFAULT,IMAGE
import cloudmesh_client.db.models
import cloudmesh_client.db.models as models
from __future__ import print_function

filename = Config.path_expand("~/test/db.db")
endpoint = 'sqlite:///{:}'.format(filename)
engine = create_engine(endpoint)
Base = declarative_base(bind=engine)

Session = sessionmaker(bind=engine)
session = Session()

r = session.query(VM).all()
for obj in r:
    print (obj.name)

result = dict()
for u in r:
    _id = u.id
    values = {}
    for key in u.__dict__.keys():
        if not key.startswith("_sa"):
            values[key] = u.__dict__[key]
    result[_id] = values

output = models.dict_printer(result, order=None, header=None, output="dict", sort_keys=True)
print(output)