Exemplo n.º 1
0
def get_flags(col_name, values):
    """
    Convert flag names to their respective numerical values.
    """
    if col_name not in _col_constants_mapping:
        return
    if not isinstance(values, (list, tuple)):
        values = [values]
    const_group = _col_constants_mapping[col_name]
    flags = []
    for value in values:
        if isinstance(value, int):
            flags.append(value)
            continue
        const_name = value.upper().replace(" ", "_")
        const = getattr(const_group, const_name)
        if isinstance(const, (constants.Race, constants.Class)):
            const = const.binary
        flags.append(const)
    if not tuple in map(type, flags):
        return sum(flags)
    else:

        def convert(flags_):
            if isinstance(flags_, (list, tuple)):
                flags_ = list(flags_)
                for index, flag in enumerate(flags_):
                    try:
                        flags_[index] = flag.id
                    except AttributeError:
                        pass
            return flags_

        return flatten(map(convert, flags))
Exemplo n.º 2
0
Arquivo: util.py Projeto: darvid/trine
def get_flags(col_name, values):
    """
    Convert flag names to their respective numerical values.
    """
    if col_name not in _col_constants_mapping:
        return
    if not isinstance(values, (list, tuple)):
        values = [values]
    const_group = _col_constants_mapping[col_name]
    flags = []
    for value in values:
        if isinstance(value, int):
            flags.append(value)
            continue
        const_name = value.upper().replace(" ", "_")
        const = getattr(const_group, const_name)
        if isinstance(const, (constants.Race, constants.Class)):
            const = const.binary
        flags.append(const)
    if not tuple in map(type, flags):
        return sum(flags)
    else:

        def convert(flags_):
            if isinstance(flags_, (list, tuple)):
                flags_ = list(flags_)
                for index, flag in enumerate(flags_):
                    try:
                        flags_[index] = flag.id
                    except AttributeError:
                        pass
            return flags_

        return flatten(map(convert, flags))
Exemplo n.º 3
0
 def _process_where(data, table):
     if not "where" in data:
         return
     for where_col_name, values in data["where"].items():
         if not isinstance(values, list):
             values = [values]
         data["where"][where_col_name] = []
         for value in values:
             if isinstance(value, SelectQueryBuilder):
                 rows = flatten(self._execute(value.build()).fetchall())
                 for field in rows:
                     data["where"][where_col_name].append(
                         get_cmp(table, where_col_name, field))
             else:
                 data["where"][where_col_name].append(
                     get_cmp(table, where_col_name, value))
Exemplo n.º 4
0
 def _process_where(data, table):
     if not "where" in data:
         return
     for where_col_name, values in data["where"].items():
         if not isinstance(values, list):
             values = [values]
         data["where"][where_col_name] = []
         for value in values:
             if isinstance(value, SelectQueryBuilder):
                 rows = flatten(self._execute(value.build()).fetchall())
                 for field in rows:
                     data["where"][where_col_name].append(
                         get_cmp(table, where_col_name, field)
                     )
             else:
                 data["where"][where_col_name].append(
                     get_cmp(table, where_col_name, value)
                 )
Exemplo n.º 5
0
    def process(self):
        """
        Preprocess DBC flags, WHERE clauses, and item lists for vendors.
        """
        def _process_where(data, table):
            if not "where" in data:
                return
            for where_col_name, values in data["where"].items():
                if not isinstance(values, list):
                    values = [values]
                data["where"][where_col_name] = []
                for value in values:
                    if isinstance(value, SelectQueryBuilder):
                        rows = flatten(self._execute(value.build()).fetchall())
                        for field in rows:
                            data["where"][where_col_name].append(
                                get_cmp(table, where_col_name, field))
                    else:
                        data["where"][where_col_name].append(
                            get_cmp(table, where_col_name, value))

        for col_name, values in self.data.items():
            flags = get_flags(col_name, values)

            # DBC flags
            if flags is not None:
                self.data[col_name] = flags

            # vendor items
            elif col_name == "items" and values is not None:
                items = []
                item_table = get_table("ItemTemplate")
                if not isinstance(values, list):
                    values = [values]
                for item in values:
                    query_append = None
                    if isinstance(item, basestring):
                        if item.endswith("^"):
                            item = item[:-1]
                            query_append = lambda q: \
                                q.order_by(desc(item_table.c.ItemLevel))
                        query = select([item_table.c.entry])\
                            .where(get_cmp(item_table, "name", item)[0])
                        if query_append:
                            query = query_append(query)
                        items.append(self._execute(query).fetchone())
                        if items[-1] is None:
                            log.error("could not find entry for '%s'" % item)
                    elif isinstance(item, int):
                        items.append(item)
                    elif isinstance(item, SelectQueryBuilder):
                        items += item_table.bind.execute(
                            item.build()).fetchall()
                self.data["items"] = flatten(items)

            elif col_name == "extended_costs":
                costs = self.data["extended_costs"]
                costs = [costs] if not isinstance(costs, list) else costs
                for cost_spec in costs:
                    _process_where(cost_spec, get_table("ItemTemplate"))

        _process_where(self.data, self.table)
Exemplo n.º 6
0
    def process(self):
        """
        Preprocess DBC flags, WHERE clauses, and item lists for vendors.
        """
        def _process_where(data, table):
            if not "where" in data:
                return
            for where_col_name, values in data["where"].items():
                if not isinstance(values, list):
                    values = [values]
                data["where"][where_col_name] = []
                for value in values:
                    if isinstance(value, SelectQueryBuilder):
                        rows = flatten(self._execute(value.build()).fetchall())
                        for field in rows:
                            data["where"][where_col_name].append(
                                get_cmp(table, where_col_name, field)
                            )
                    else:
                        data["where"][where_col_name].append(
                            get_cmp(table, where_col_name, value)
                        )
        for col_name, values in self.data.items():
            flags = get_flags(col_name, values)

            # DBC flags
            if flags is not None:
                self.data[col_name] = flags

            # vendor items
            elif col_name == "items" and values is not None:
                items = []
                item_table = get_table("ItemTemplate")
                if not isinstance(values, list):
                    values = [values]
                for item in values:
                    query_append = None
                    if isinstance(item, basestring):
                        if item.endswith("^"):
                            item = item[:-1]
                            query_append = lambda q: \
                                q.order_by(desc(item_table.c.ItemLevel))
                        query = select([item_table.c.entry])\
                            .where(get_cmp(item_table, "name", item)[0])
                        if query_append:
                            query = query_append(query)
                        items.append(self._execute(query).fetchone())
                        if items[-1] is None:
                            log.error("could not find entry for '%s'" % item)
                    elif isinstance(item, int):
                        items.append(item)
                    elif isinstance(item, SelectQueryBuilder):
                        items += item_table.bind.execute(item.build()).fetchall()
                self.data["items"] = flatten(items)

            elif col_name == "extended_costs":
                costs = self.data["extended_costs"]
                costs = [costs] if not isinstance(costs, list) else costs
                for cost_spec in costs:
                    _process_where(cost_spec, get_table("ItemTemplate"))

        _process_where(self.data, self.table)