# 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)


from cStringIO import StringIO
from elementtree import ElementTree as etree
from elementtree.ElementTree import Element, SubElement
from canary import dtuple
from canary.utils import DTable
import traceback
import urllib
import canary
# $Id$





def find_resolver (context, ip_address='127.0.0.1'):
    """For a given web user's IP address, find an appropriate resolver from
    OCLC's registry.  We don't want to clobber OCLC's server, though.  So first 
    check local cache, though, and if it's not available locally then query OCLC
    and be sure to save the Resolver when we're done."""
    
    r = Resolver(context, -1, ip_address)
    if r.base_url:
        return r
        
    r.load_from_db(context)
    if r.base_url:
        return r
        
    r.load_from_xml(context)
    r.save(context)
    if r.base_url == 'http://worldcatlibraries.org/registry/gateway':
        return None
    else:
        return r
    


# 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/resolver.py
from cStringIO import StringIO
from elementtree import ElementTree as etree
from elementtree.ElementTree import Element, SubElement
from canary import dtuple
from canary.utils import DTable
import traceback
import urllib
import canary

# $Id$





def find_resolver (context, ip_address='127.0.0.1'):
    """For a given web user's IP address, find an appropriate resolver from
    OCLC's registry.  We don't want to clobber OCLC's server, though.  So first 
    check local cache, though, and if it's not available locally then query OCLC
    and be sure to save the Resolver when we're done."""
    
    r = Resolver(context, -1, ip_address)
    if r.base_url:
        return r
        
    r.load_from_db(context)
    if r.base_url:
        return r
        
    r.load_from_xml(context)
    r.save(context)
    if r.base_url == 'http://worldcatlibraries.org/registry/gateway':
        return None
    else:
        return r
    


class Resolver (canary.context.Cacheable, DTable):