# Path: wh.py
# def xlt(s): # translate
#   return s
#   t=wx.GetTranslation(s)
#   # if dict set and t == s: log untranslated
#   return t
# 
# def modPath(name, mod):
#   """
#   str modPath(filename, module)
#   
#   prepend module's path to filename
#   """
#   if mod:
#     if not isinstance(mod, StringType):
#       mod=mod.__module__
#     ri=mod.rfind('.')
#     if ri > 0:
#       return os.path.join(loaddir, mod[0:ri].replace('.', '/'), name)
# 
#   return os.path.join(loaddir, name)


import psycopg2
import select
import logger
import adm
import re
import threading
from wh import xlt, modPath
from Crypto.PublicKey._slowmath import rsa_construct
  def Disconnect(self):
    for conn in self.connections:
      conn.disconnect()
    self.connections=[]
    
  def RemoveConnection(self, conn):
    try:    self.connections.remove(conn)
    except: pass
  
  def GetCursor(self):
    conn=None
    with self.lock:
      for c in self.connections:
        if not c.inUse:
          conn=c
#          logger.trace(2, 4, "USING     %s", str(c))
          c.inUse=True
          break
    if not conn:
      conn=self.CreateConnection()
#      logger.trace(2, 4, "CREATING  %s", str(c))
    return conn.GetCursor()

  
  def CreateConnection(self):
    try:
      conn=pgConnection(self.dsn, self)
      return conn
    except Exception as e:
      self.lastError = unicode(e)

# Path: wh.py
# def xlt(s): # translate
#   return s
#   t=wx.GetTranslation(s)
#   # if dict set and t == s: log untranslated
#   return t
# 
# def modPath(name, mod):
#   """
#   str modPath(filename, module)
#   
#   prepend module's path to filename
#   """
#   if mod:
#     if not isinstance(mod, StringType):
#       mod=mod.__module__
#     ri=mod.rfind('.')
#     if ri > 0:
#       return os.path.join(loaddir, mod[0:ri].replace('.', '/'), name)
# 
#   return os.path.join(loaddir, name)


# Path: modPg/_pgsql.py
import psycopg2
import select
import logger
import adm
import re
import threading
from wh import xlt, modPath
from Crypto.PublicKey._slowmath import rsa_construct

  def Disconnect(self):
    for conn in self.connections:
      conn.disconnect()
    self.connections=[]
    
  def RemoveConnection(self, conn):
    try:    self.connections.remove(conn)
    except: pass
  
  def GetCursor(self):
    conn=None
    with self.lock:
      for c in self.connections:
        if not c.inUse:
          conn=c
#          logger.trace(2, 4, "USING     %s", str(c))
          c.inUse=True
          break
    if not conn:
      conn=self.CreateConnection()
#      logger.trace(2, 4, "CREATING  %s", str(c))
    return conn.GetCursor()

  
  def CreateConnection(self):
    try:
      conn=pgConnection(self.dsn, self)
      return conn
    except Exception as e:
      self.lastError = unicode(e)

      raise adm.ConnectionException(self.node, xlt("Connect"), self.lastError)   