db_trans.py :  » Template-Engines » Clearsilver » clearsilver-0.10.5 » python » examples » trans » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Template Engines » Clearsilver 
Clearsilver » clearsilver 0.10.5 » python » examples » trans » db_trans.py


from odb import *
import profiler
import socket

USER = 'root'
PASSWORD = ''
DATABASE = 'trans_data'

class TransStringTable(Table):
    # access patterns:
    #   -> lookup individual entries by string_id
    #   -> lookup entry by string
    def _defineRows(self):
        self.d_addColumn("string_id", kInteger, primarykey=1, autoincrement=1)
        # we can't actually index this... but we can index part with myisam
        self.d_addColumn("string", kBigString, indexed=1)

## hmm, on second thought, storing this is in the database is kind 
## of silly..., since it essentially could change with each run.  It may
## not even be necessary to store this anywhere except in memory while 
## trans is running
class TransLocTable(Table):
    # access patterns:
    #   -> find "same" entry by filename/offset
    #   -> dump all locations for a version
    #   -> maybe: find all locations for a filename
    def _defineRows(self):
        self.d_addColumn("loc_id", kInteger, primarykey=1, autoincrement=1)
        self.d_addColumn("string_id", kInteger, indexed=1)
        self.d_addColumn("version", kInteger, default=0)
        self.d_addColumn("filename", kVarString, 255, indexed=1)
        self.d_addColumn("location", kVarString, 255)
        # this can either be:
        # ofs:x:y
        # hdf:foo.bar.baz

class TransMapTable(Table):
    # access patterns:
    #   -> dump all for a language
    #   -> lookup entry by string_id/lang
    def _defineRows(self):
        self.d_addColumn("string_id", kInteger, primarykey=1)
        self.d_addColumn("lang", kFixedString, 2, primarykey=1)
        self.d_addColumn("string", kBigString)

class DB(Database):
    def __init__(self, db, debug=0):
  self.db = db
        self._cursor = None
        self.debug = debug

        self.addTable("strings", "nt_trans_strings", TransStringTable)
        self.addTable("locs", "nt_trans_locs", TransLocTable)
        self.addTable("maps", "nt_trans_maps", TransMapTable)

    def defaultCursor(self):
        # share one cursor for this db object!
        if self._cursor is None:
            if self.debug:
                self._cursor = profiler.ProfilerCursor(self.db.cursor())
            else:
                self._cursor = self.db.cursor()

        return self._cursor

def trans_connect(host = 'localhost', debug=0):
    # try to optimize connection if on this machine
    if host != 'localhost':
        local_name = socket.gethostname()
        if string.find(local_name, '.') == -1:
            local_name = local_name + ".neotonic.com"
        if local_name == host:
            host = 'localhost'

    if debug: p = profiler.Profiler("SQL", "Connect -- %s:trans" % (host))
    db = MySQLdb.connect(host = host, user=USER, passwd = PASSWORD, db=DATABASE)
    if debug: p.end()

    retval = DB(db, debug=debug)
    return retval
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.