# Path: canary/dtuple.py
# class TupleDescriptor:
# class DatabaseTuple:
#   def __init__(self, desc):
#   def __len__(self):
#   def __repr__(self):
#   def __str__(self):
#   def __init__(self, desc, data):
#   def __str__(self):
#   def __repr__(self):
#   def __cmp__(self, other):
#   def __getattr__(self, name):
#   def __setattr__(self, name, value):
#   def __getitem__(self, key):
#   def __setitem__(self, key, value):
#   def __len__(self):
#   def __getslice__(self, i, j):
#   def __setslice__(self, i, j, list):
#   def _keys_(self):
#   def _has_key_(self, key):
#   def _items_(self):
#   def _count_(self, item):
#   def _index_(self, item):
#   def _getvalue_(self,name):
#   def asMapping(self):
#   def asTuple(self):
#   def asList(self):
# 
# Path: canary/utils.py
# class DTable:
#     """
#     Convenience class for rdbms-persisted object types.  Allows simple
#     loading of data for single-valued fields using set() and for multi-valued
#     fields using append().
#     """
#     
#     def set (self, field, value):
#         """
#         Dynamic field setting for simpler database lookups
#         """
#         if value == None:
#             setattr(self, str(field), str(''))
#         else:
#             setattr(self, str(field), value)
#        
#     #def append (self, field, value):
#     #    list_attr = getattr(self, str(field))
#     #    if not value in list_attr:
#     #        list_attr.append(value)
#     
#        
#     def get_new_uid (self, context):
#         cursor = context.get_cursor()
#         try:
#             cursor.execute('SELECT LAST_INSERT_ID() AS new_uid')
#             row = cursor.fetchone()
#             new_uid = int(row[0])
#         except:
#             new_uid = -1
#         return new_uid
#     
# 
#     def load (self, context):
#         cursor = context.get_cursor()
#         if self.uid:
#             cursor.execute("""SELECT * FROM """ + self.TABLE_NAME +
#                 """
#                 WHERE uid LIKE %s
#                 """, (self.uid))
#             fields = [d[0] for d in cursor.description]
#             desc = dtuple.TupleDescriptor([[f] for f in fields])
#             row = cursor.fetchone()
#             if row:
#                 row = dtuple.DatabaseTuple(desc, row)
#                 for field in fields:
#                     self.set(field, row[field])
#             else:
#                 context.logger.debug('No %s "%s"', self.TABLE_NAME, self.uid)
#                 print 'No %s "%s"' % (self.TABLE_NAME, self.uid)
#                 raise 'InvalidId'
#                 
# 
#     def delete (self, context):
#         cursor = context.get_cursor()
#         try:
#             if self.uid >= 0:
#                 cursor.execute("""DELETE FROM """ + self.TABLE_NAME +
#                     """
#                     WHERE uid = %s
#                     """, self.uid)
#         except Exception, e:
#             context.logger.error(e)


import logging
import random
import sha
import canary
from canary import dtuple
from canary.utils import DTable
                
        # Load sets
        cursor.execute("""
            SELECT uid
            FROM user_sets
            WHERE user_id = %s
            ORDER BY LOWER(name)
            """, self.uid)
        fields = [d[0] for d in cursor.description]
        desc = dtuple.TupleDescriptor([[f] for f in fields])
        set_ids = []
        for row in cursor.fetchall():
            row = dtuple.DatabaseTuple(desc, row)
            set_ids.append(row['uid'])
        try:
            set_map = context.cache_get_multi(list('%s:%s' % (UserSet.CACHE_KEY, id) for id in set_ids))
            self.sets.extend(list(set_map['%s:%s' % (UserSet.CACHE_KEY, id)] for id in set_ids))
        except:
            for id in set_ids:
                self.sets.append(UserSet(context, id))

        # Load record->set assignments
        for set in self.sets:
            cursor.execute("""
                SELECT record_id
                FROM user_set_records
                WHERE user_set_id = %s
                ORDER BY uid
                """, set.uid)
            fields = [d[0] for d in cursor.description]

# Path: canary/dtuple.py
# class TupleDescriptor:
# class DatabaseTuple:
#   def __init__(self, desc):
#   def __len__(self):
#   def __repr__(self):
#   def __str__(self):
#   def __init__(self, desc, data):
#   def __str__(self):
#   def __repr__(self):
#   def __cmp__(self, other):
#   def __getattr__(self, name):
#   def __setattr__(self, name, value):
#   def __getitem__(self, key):
#   def __setitem__(self, key, value):
#   def __len__(self):
#   def __getslice__(self, i, j):
#   def __setslice__(self, i, j, list):
#   def _keys_(self):
#   def _has_key_(self, key):
#   def _items_(self):
#   def _count_(self, item):
#   def _index_(self, item):
#   def _getvalue_(self,name):
#   def asMapping(self):
#   def asTuple(self):
#   def asList(self):
# 
# Path: canary/utils.py
# class DTable:
#     """
#     Convenience class for rdbms-persisted object types.  Allows simple
#     loading of data for single-valued fields using set() and for multi-valued
#     fields using append().
#     """
#     
#     def set (self, field, value):
#         """
#         Dynamic field setting for simpler database lookups
#         """
#         if value == None:
#             setattr(self, str(field), str(''))
#         else:
#             setattr(self, str(field), value)
#        
#     #def append (self, field, value):
#     #    list_attr = getattr(self, str(field))
#     #    if not value in list_attr:
#     #        list_attr.append(value)
#     
#        
#     def get_new_uid (self, context):
#         cursor = context.get_cursor()
#         try:
#             cursor.execute('SELECT LAST_INSERT_ID() AS new_uid')
#             row = cursor.fetchone()
#             new_uid = int(row[0])
#         except:
#             new_uid = -1
#         return new_uid
#     
# 
#     def load (self, context):
#         cursor = context.get_cursor()
#         if self.uid:
#             cursor.execute("""SELECT * FROM """ + self.TABLE_NAME +
#                 """
#                 WHERE uid LIKE %s
#                 """, (self.uid))
#             fields = [d[0] for d in cursor.description]
#             desc = dtuple.TupleDescriptor([[f] for f in fields])
#             row = cursor.fetchone()
#             if row:
#                 row = dtuple.DatabaseTuple(desc, row)
#                 for field in fields:
#                     self.set(field, row[field])
#             else:
#                 context.logger.debug('No %s "%s"', self.TABLE_NAME, self.uid)
#                 print 'No %s "%s"' % (self.TABLE_NAME, self.uid)
#                 raise 'InvalidId'
#                 
# 
#     def delete (self, context):
#         cursor = context.get_cursor()
#         try:
#             if self.uid >= 0:
#                 cursor.execute("""DELETE FROM """ + self.TABLE_NAME +
#                     """
#                     WHERE uid = %s
#                     """, self.uid)
#         except Exception, e:
#             context.logger.error(e)


# Path: canary/user.py
import logging
import random
import sha
import canary
from canary import dtuple
from canary.utils import DTable

                
        # Load sets
        cursor.execute("""
            SELECT uid
            FROM user_sets
            WHERE user_id = %s
            ORDER BY LOWER(name)
            """, self.uid)
        fields = [d[0] for d in cursor.description]
        desc = dtuple.TupleDescriptor([[f] for f in fields])
        set_ids = []
        for row in cursor.fetchall():
            row = dtuple.DatabaseTuple(desc, row)
            set_ids.append(row['uid'])
        try:
            set_map = context.cache_get_multi(list('%s:%s' % (UserSet.CACHE_KEY, id) for id in set_ids))
            self.sets.extend(list(set_map['%s:%s' % (UserSet.CACHE_KEY, id)] for id in set_ids))
        except:
            for id in set_ids:
                self.sets.append(UserSet(context, id))

        # Load record->set assignments
        for set in self.sets:
            cursor.execute("""
                SELECT record_id
                FROM user_set_records
                WHERE user_set_id = %s
                ORDER BY uid
                """, set.uid)
            fields = [d[0] for d in cursor.description]

            desc = dtuple.TupleDescriptor([[f] for f in fields])