예제 #1
0
파일: tricks.py 프로젝트: leofigs/bag
def models_and_tables_in(arg):
    '''``arg`` may be a resource spec, a module or a dictionary.

    Returns 2 lists containing the model classes and tables in it::

        models, tables = models_and_tables_in(globals())
    '''
    if not isinstance(arg, dict):
        arg = resolve(arg)  # ensure arg is a python module
        arg = arg.__dict__
    models = [o for o in arg.values() if is_model_class(o)]
    tables = [o for o in arg.values() if isinstance(o, Table)]
    return models, tables
예제 #2
0
def models_and_tables_in(arg):
    '''``arg`` may be a resource spec, a module or a dictionary.

    Returns 2 lists containing the model classes and tables in it::

        models, tables = models_and_tables_in(globals())
    '''
    if not isinstance(arg, dict):
        arg = resolve(arg)  # ensure arg is a python module
        arg = arg.__dict__
    models = [o for o in arg.values() if is_model_class(o)]
    tables = [o for o in arg.values() if isinstance(o, Table)]
    return models, tables
예제 #3
0
    def _process_table(self, resource_spec, ignore_attribs, sas):
        """Intended for association tables."""
        from sqlalchemy import select
        table = resolve(resource_spec)
        cols = list(enumerate(table.c.keys()))
        for row in sas.execute(select([table])).fetchall():
            self.add("yield [")
            self.indent()

            self.add("'{}',".format(resource_spec))
            for index, colname in cols:
                if colname in ignore_attribs:
                    continue
                self.add("{},".format(
                    self._serialize_property_value(row[index])))

            self.dedent()
            self.add(']')
예제 #4
0
    def _process_table(self, resource_spec, ignore_attribs):
        '''Intended for association tables.'''
        sas = self.session_factory()
        table = resolve(resource_spec)
        cols = list(enumerate(table.c.keys()))
        for row in sas.execute(select([table])).fetchall():
            self.add("yield [")
            self.indent()

            self.add("'{}',".format(resource_spec))
            for index, colname in cols:
                if colname in ignore_attribs:
                    continue
                self.add("{},".format(
                    self._serialize_property_value(row[index])))

            self.dedent()
            self.add(']')
        sas.close()
예제 #5
0
    def _process_table(self, resource_spec, ignore_attribs):
        '''Intended for association tables.'''
        sas = self.session_factory()
        table = resolve(resource_spec)
        cols = list(enumerate(table.c.keys()))
        for row in sas.execute(select([table])).fetchall():
            self.add("yield [")
            self.indent()

            self.add("'{}',".format(resource_spec))
            for index, colname in cols:
                if colname in ignore_attribs:
                    continue
                self.add("{},".format(
                    self._serialize_property_value(row[index])))

            self.dedent()
            self.add(']')
        sas.close()
예제 #6
0
 def _load_row(self, original_values):
     values = copy(original_values)
     table = resolve(values.pop(0))  # TODO Cache
     cols = list(enumerate(table.c.keys()))  # TODO Cache
     for index, col in cols:
         fk = foreign_key_from_col(table.c[col])
         if fk:
             # Replace the old FK value with the NEW id stored in mapp
             old_fk_value = values[index]
             if old_fk_value is None:
                 continue
             try:
                 values[index] = self._get_new_id(fk, old_fk_value)
             except KeyError as e:
                 print('Delaying {} row for lack of {}'.format(
                     table.name, e))
                 # Store this job so it will be retried later:
                 self._delay_creation(e.args[0], self._load_row,
                                      original_values)
                 return False
     self.sas.execute(insert(table, values=values))
     return True
예제 #7
0
 def _load_row(self, original_values):
     values = copy(original_values)
     table = resolve(values.pop(0))  # TODO Cache
     cols = list(enumerate(table.c.keys()))  # TODO Cache
     for index, col in cols:
         fk = foreign_key_from_col(table.c[col])
         if fk:
             # Replace the old FK value with the NEW id stored in mapp
             old_fk_value = values[index]
             if old_fk_value is None:
                 continue
             try:
                 values[index] = self._get_new_id(fk, old_fk_value)
             except KeyError as e:
                 print('Delaying {} row for lack of {}'.format(
                       table.name, e))
                 # Store this job so it will be retried later:
                 self._delay_creation(
                     e.args[0], self._load_row, original_values)
                 return False
     self.sas.execute(insert(table, values=values))
     return True
예제 #8
0
 def resolve(self, key, section=None, default=REQUIRED):
     """For values that point to Python objects, return the object."""
     resource_spec = self.read(key, section, default)
     return None if resource_spec is None else resolve(resource_spec)
예제 #9
0
def resolve_setting(settings, key, default=REQUIRED):
    resource_spec = read_setting(settings, key, default)
    return None if resource_spec is None else resolve(resource_spec)