Changeset 3294

Show
Ignore:
Timestamp:
01/18/08 23:39:41 (16 years ago)
Author:
jerome
Message:

Added modules to store utility functions and application
intialization code, which has nothing to do in classes.
Modified tool.py accordingly (far from being finished)
Use these new modules where necessary.
Now converts all command line arguments to unicode before
beginning to work. Added a proper logging method for already
encoded query strings.

Location:
pykota/trunk
Files:
1 added
25 modified
1 copied

Legend:

Unmodified
Added
Removed
  • pykota/trunk/bin/autopykota

    r3288 r3294  
    2525import sys 
    2626import os 
     27 
     28import pykota.appinit 
     29from pykota.utils import * 
    2730 
    2831from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
     
    140143            retcode = automat.main(args, options) 
    141144    except KeyboardInterrupt :         
    142         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     145        logerr("\nInterrupted with Ctrl+C !\n") 
    143146        retcode = -3 
    144147    except PyKotaCommandLineError, msg :     
    145         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     148        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    146149        retcode = -2 
    147150    except SystemExit :         
  • pykota/trunk/bin/cupspykota

    r3288 r3294  
    4343 
    4444from mx import DateTime 
     45 
     46import pykota.appinit 
     47from pykota.utils import * 
    4548 
    4649from pykota.errors import PyKotaToolError 
     
    13131316        sys.exit(0)                 
    13141317    elif len(sys.argv) not in (6, 7) :     
    1315         sys.stderr.write("ERROR: %s job-id user title copies options [file]\n"\ 
     1318        logerr("ERROR: %s job-id user title copies options [file]\n"\ 
    13161319                              % sys.argv[0]) 
    13171320        sys.exit(1) 
  • pykota/trunk/bin/dumpykota

    r3288 r3294  
    2323 
    2424import sys 
     25 
     26import pykota.appinit 
     27from pykota.utils import * 
    2528 
    2629from pykota.errors import PyKotaCommandLineError 
     
    183186            retcode = dumper.main(args, options) 
    184187    except KeyboardInterrupt :         
    185         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     188        logerr("\nInterrupted with Ctrl+C !\n") 
    186189        retcode = -3 
    187190    except PyKotaCommandLineError, msg :     
    188         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     191        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    189192        retcode = -2 
    190193    except SystemExit :         
  • pykota/trunk/bin/edpykota

    r3288 r3294  
    2323 
    2424import sys 
     25 
     26import pykota.appinit 
     27from pykota.utils import * 
    2528 
    2629from pykota.errors import PyKotaCommandLineError 
     
    352355            retcode = manager.main(args, options) 
    353356    except KeyboardInterrupt :         
    354         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     357        logerr("\nInterrupted with Ctrl+C !\n") 
    355358        retcode = -3 
    356359    except PyKotaCommandLineError, msg :      
    357         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     360        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    358361        retcode = -2 
    359362    except SystemExit :         
  • pykota/trunk/bin/pkbanner

    r3288 r3294  
    4444    hasPIL = True 
    4545     
     46import pykota.appinit 
     47from pykota.utils import * 
     48 
    4649from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    4750from pykota.tool import Tool, crashed, N_ 
     
    325328            retcode = banner.main(args, options) 
    326329    except KeyboardInterrupt :         
    327         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     330        logerr("\nInterrupted with Ctrl+C !\n") 
    328331        retcode = -3 
    329332    except PyKotaCommandLineError, msg :     
    330         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     333        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    331334        retcode = -2 
    332335    except SystemExit :         
  • pykota/trunk/bin/pkbcodes

    r3288 r3294  
    2525import sys 
    2626import pwd 
     27 
     28import pykota.appinit 
     29from pykota.utils import * 
    2730 
    2831from pykota.errors import PyKotaCommandLineError 
     
    199202            retcode = manager.main(args, options) 
    200203    except KeyboardInterrupt :         
    201         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     204        logerr("\nInterrupted with Ctrl+C !\n") 
    202205        retcode = -3 
    203206    except PyKotaCommandLineError, msg :     
    204         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     207        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    205208        retcode = -2 
    206209    except SystemExit :         
  • pykota/trunk/bin/pkinvoice

    r3288 r3294  
    4343else :     
    4444    hasPIL = True 
     45 
     46import pykota.appinit 
     47from pykota.utils import * 
    4548 
    4649from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
     
    403406            retcode = invoiceGenerator.main(args, options) 
    404407    except KeyboardInterrupt :         
    405         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     408        logerr("\nInterrupted with Ctrl+C !\n") 
    406409        retcode = -3 
    407410    except PyKotaCommandLineError, msg :      
    408         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     411        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    409412        retcode = -2 
    410413    except SystemExit :         
  • pykota/trunk/bin/pkmail

    r3288 r3294  
    3030from email.Header import Header 
    3131import email.Utils 
     32 
     33import pykota.appinit 
     34from pykota.utils import * 
    3235 
    3336from pykota.errors import PyKotaCommandLineError 
     
    164167            retcode = mailparser.main(args, options) 
    165168    except KeyboardInterrupt :         
    166         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     169        logerr("\nInterrupted with Ctrl+C !\n") 
    167170        retcode = -3 
    168171    except PyKotaCommandLineError, msg :     
    169         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     172        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    170173        retcode = -2 
    171174    except SystemExit :         
  • pykota/trunk/bin/pknotify

    r3288 r3294  
    3434else :     
    3535    hasPAM = True 
     36 
     37import pykota.appinit 
     38from pykota.utils import * 
    3639 
    3740from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
     
    354357            retcode = notifier.main(args, options) 
    355358    except KeyboardInterrupt :         
    356         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     359        logerr("\nInterrupted with Ctrl+C !\n") 
    357360        retcode = -3 
    358361    except PyKotaCommandLineError, msg :     
    359         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     362        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    360363        print "CANCEL"  # Forces the cancellation of the print job if a command line switch is incorrect 
    361364        retcode = -2 
  • pykota/trunk/bin/pkprinters

    r3288 r3294  
    2525import sys 
    2626import pwd 
     27 
     28import pykota.appinit 
     29from pykota.utils import * 
    2730 
    2831from pykota.errors import PyKotaCommandLineError 
     
    372375            retcode = manager.main(args, options) 
    373376    except KeyboardInterrupt :         
    374         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     377        logerr("\nInterrupted with Ctrl+C !\n") 
    375378        retcode = -3 
    376379    except PyKotaCommandLineError, msg :     
    377         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     380        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    378381        retcode = -2 
    379382    except SystemExit :         
  • pykota/trunk/bin/pkrefund

    r3288 r3294  
    4343else :     
    4444    hasPIL = True 
     45 
     46import pykota.appinit 
     47from pykota.utils import * 
    4548 
    4649from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
     
    440443            retcode = refundmanager.main(args, options) 
    441444    except KeyboardInterrupt :         
    442         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     445        logerr("\nInterrupted with Ctrl+C !\n") 
    443446        retcode = -3 
    444447    except PyKotaCommandLineError, msg :     
    445         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     448        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    446449        retcode = -2 
    447450    except SystemExit :         
  • pykota/trunk/bin/pksetup

    r3290 r3294  
    408408        homedirectory = self.addPyKotaUser() 
    409409        if homedirectory is None : 
    410             sys.stderr.write("Installation can't proceed. You MUST create a system user named 'pykota'.\n") 
     410            logerr("Installation can't proceed. You MUST create a system user named 'pykota'.\n") 
    411411        else :     
    412412            self.upgradeSystem() 
     
    465465            installer = globals()[classname]() 
    466466        except KeyError :     
    467             sys.stderr.write("There's currently no support for the %s distribution, sorry.\n" % sys.argv[1]) 
     467            logerr("There's currently no support for the %s distribution, sorry.\n" % sys.argv[1]) 
    468468            retcode = -1 
    469469        else :     
     
    471471                retcode = installer.setup() 
    472472            except KeyboardInterrupt :              
    473                 sys.stderr.write("\n\n\nWARNING : Setup was aborted at user's request !\n\n") 
     473                logerr("\n\n\nWARNING : Setup was aborted at user's request !\n\n") 
    474474                retcode = -1 
    475475    sys.exit(retcode) 
  • pykota/trunk/bin/pkturnkey

    r3288 r3294  
    2929import signal 
    3030 
     31import pykota.appinit 
     32from pykota.utils import * 
     33 
    3134from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
    3235from pykota.tool import Tool, crashed, N_ 
     
    226229                from pysnmp.proto.api import alpha 
    227230            except ImportError :     
    228                 sys.stderr.write("pysnmp doesn't seem to be installed. SNMP checks will be ignored !\n") 
     231                logerr("pysnmp doesn't seem to be installed. SNMP checks will be ignored !\n") 
    229232                return 0 
    230233        else :         
     
    528531            retcode = manager.main(args, options) 
    529532    except KeyboardInterrupt :         
    530         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     533        logerr("\nInterrupted with Ctrl+C !\n") 
    531534        retcode = -3 
    532535    except PyKotaCommandLineError, msg :     
    533         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     536        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    534537        retcode = -2 
    535538    except SystemExit :         
  • pykota/trunk/bin/pkusers

    r3288 r3294  
    2525import pwd 
    2626import grp 
     27 
     28import pykota.appinit 
     29from pykota.utils import * 
    2730 
    2831from pykota.errors import PyKotaCommandLineError 
     
    440443            retcode = manager.main(args, options) 
    441444    except KeyboardInterrupt :         
    442         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     445        logerr("\nInterrupted with Ctrl+C !\n") 
    443446        retcode = -3 
    444447    except PyKotaCommandLineError, msg :     
    445         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     448        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    446449        retcode = -2 
    447450    except SystemExit :         
  • pykota/trunk/bin/pykosd

    r3288 r3294  
    3030    import pyosd 
    3131except ImportError :     
    32     sys.stderr.write("Sorry ! You need both xosd and the Python OSD library (pyosd) for this software to work.\n") 
     32    logerr("Sorry ! You need both xosd and the Python OSD library (pyosd) for this software to work.\n") 
    3333    sys.exit(-1) 
     34 
     35import pykota.appinit 
     36from pykota.utils import * 
    3437 
    3538from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
     
    192195        retcode = 0 
    193196    except KeyboardInterrupt :         
    194         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     197        logerr("\nInterrupted with Ctrl+C !\n") 
    195198        retcode = -3 
    196199    except PyKotaCommandLineError, msg :     
    197         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     200        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    198201        retcode = -2 
    199202    except SystemExit :         
  • pykota/trunk/bin/pykotme

    r3288 r3294  
    2525import os 
    2626import pwd 
     27 
     28import pykota.appinit 
     29from pykota.utils import * 
    2730 
    2831from pykota.errors import PyKotaCommandLineError 
     
    156159            retcode = sender.main(args, options) 
    157160    except KeyboardInterrupt :         
    158         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     161        logerr("\nInterrupted with Ctrl+C !\n") 
    159162        retcode = -3 
    160163    except PyKotaCommandLineError, msg :     
    161         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     164        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    162165        retcode = -2 
    163166    except SystemExit :         
  • pykota/trunk/bin/repykota

    r3288 r3294  
    2727 
    2828from mx import DateTime 
     29 
     30import pykota.appinit 
     31from pykota.utils import * 
    2932 
    3033from pykota.errors import PyKotaToolError, PyKotaCommandLineError 
     
    157160            retcode = reportTool.main(args, options) 
    158161    except KeyboardInterrupt :         
    159         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     162        logerr("\nInterrupted with Ctrl+C !\n") 
    160163        retcode = -3 
    161164    except PyKotaCommandLineError, msg :     
    162         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     165        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    163166        retcode = -2 
    164167    except SystemExit :         
  • pykota/trunk/bin/warnpykota

    r3288 r3294  
    2525import os 
    2626import pwd 
     27 
     28import pykota.appinit 
     29from pykota.utils import * 
    2730 
    2831from pykota.errors import PyKotaCommandLineError 
     
    156159            retcode = sender.main(args, options) 
    157160    except KeyboardInterrupt :         
    158         sys.stderr.write("\nInterrupted with Ctrl+C !\n") 
     161        logerr("\nInterrupted with Ctrl+C !\n") 
    159162        retcode = -3 
    160163    except PyKotaCommandLineError, msg :     
    161         sys.stderr.write("%s : %s\n" % (sys.argv[0], msg)) 
     164        logerr("%s : %s\n" % (sys.argv[0], msg)) 
    162165        retcode = -2 
    163166    except SystemExit :         
  • pykota/trunk/pykota/storage.py

    r3291 r3294  
    581581        self.close() 
    582582         
     583    def querydebug(self, qmsg) :     
     584        """Logs a database query, where all queries are already UTF-8 encoded.""" 
     585        self.tool.logdebug(qmsg.decode("UTF-8", "replace")) 
     586         
    583587    def getFromCache(self, cachetype, key) : 
    584588        """Tries to extract something from the cache.""" 
     
    728732        return gpquotas         
    729733         
    730     def databaseToUnicode(self, text) : 
    731         """Converts from database format (UTF-8) to unicode.""" 
    732         if text is not None : 
    733             return text.decode("UTF-8", "replace") 
    734         else :  
    735             return None 
    736          
    737     def unicodeToDatabase(self, text) : 
    738         """Converts from unicode to database format (UTF-8).""" 
    739         if text is not None :  
    740             return text.encode("UTF-8", "replace") 
    741         else :     
    742             return None 
    743              
    744734    def cleanDates(self, startdate, enddate) :     
    745735        """Clean the dates to create a correct filter.""" 
  • pykota/trunk/pykota/storages/ldapstorage.py

    r3291 r3294  
    4242                           StorageJob, StorageLastJob, StorageUserPQuota, \ 
    4343                           StorageGroupPQuota, StorageBillingCode 
     44                            
     45from pykota.utils import *                            
    4446 
    4547try : 
     
    160162                    result = [(base, entry)] 
    161163                else : 
    162                     self.tool.logdebug("QUERY : Filter : %s, BaseDN : %s, Scope : %s, Attributes : %s" % (key, base, scope, fields)) 
     164                    self.querydebug("QUERY : Filter : %s, BaseDN : %s, Scope : %s, Attributes : %s" % (key, base, scope, fields)) 
    163165                    result = self.database.search_s(base, scope, key, fields) 
    164166            except ldap.NO_SUCH_OBJECT, msg :         
     
    171173                self.secondStageInit() 
    172174            else :      
    173                 self.tool.logdebug("QUERY : Result : %s" % result) 
     175                self.querydebug("QUERY : Result : %s" % result) 
    174176                result = [ (dn, cidict(attrs)) for (dn, attrs) in result ] 
    175177                if self.useldapcache : 
    176178                    for (dn, attributes) in result : 
    177                         self.tool.logdebug("LDAP cache store %s => %s" % (dn, attributes)) 
     179                        self.querydebug("LDAP cache store %s => %s" % (dn, attributes)) 
    178180                        self.ldapcache[dn] = attributes 
    179181                return result 
     
    186188        for tryit in range(3) : 
    187189            try : 
    188                 self.tool.logdebug("QUERY : ADD(%s, %s)" % (dn, str(fields))) 
     190                self.querydebug("QUERY : ADD(%s, %s)" % (dn, fields)) 
    189191                entry = ldap.modlist.addModlist(fields) 
    190                 self.tool.logdebug("%s" % entry) 
     192                self.querydebug("%s" % entry) 
    191193                self.database.add_s(dn, entry) 
    192194            except ldap.ALREADY_EXISTS, msg :         
     
    200202            else : 
    201203                if self.useldapcache : 
    202                     self.tool.logdebug("LDAP cache add %s => %s" % (dn, fields)) 
     204                    self.querydebug("LDAP cache add %s => %s" % (dn, fields)) 
    203205                    self.ldapcache[dn] = fields 
    204206                return dn 
     
    210212        for tryit in range(3) : 
    211213            try : 
    212                 self.tool.logdebug("QUERY : Delete(%s)" % dn) 
     214                self.querydebug("QUERY : Delete(%s)" % dn) 
    213215                self.database.delete_s(dn) 
    214216            except ldap.NO_SUCH_OBJECT :     
     
    223225                if self.useldapcache : 
    224226                    try : 
    225                         self.tool.logdebug("LDAP cache del %s" % dn) 
     227                        self.querydebug("LDAP cache del %s" % dn) 
    226228                        del self.ldapcache[dn] 
    227229                    except KeyError :     
     
    239241                    if self.ldapcache.has_key(dn) : 
    240242                        old = self.ldapcache[dn] 
    241                         self.tool.logdebug("LDAP cache hit %s => %s" % (dn, old)) 
     243                        self.querydebug("LDAP cache hit %s => %s" % (dn, old)) 
    242244                        oldentry = {} 
    243245                        for (k, v) in old.items() : 
     
    245247                                oldentry[k] = v 
    246248                    else :     
    247                         self.tool.logdebug("LDAP cache miss %s" % dn) 
     249                        self.querydebug("LDAP cache miss %s" % dn) 
    248250                        oldentry = self.doSearch("objectClass=*", base=dn, scope=ldap.SCOPE_BASE)[0][1] 
    249251                else :         
     
    254256                            oldvalue = v["convert"](oldentry.get(k, [0])[0]) 
    255257                        except ValueError :     
    256                             self.tool.logdebug("Error converting %s with %s(%s)" % (oldentry.get(k), k, v)) 
     258                            self.querydebug("Error converting %s with %s(%s)" % (oldentry.get(k), k, v)) 
    257259                            oldvalue = 0 
    258260                        if v["operator"] == '+' : 
     
    262264                        fields[k] = str(newvalue) 
    263265                fields = self.normalizeFields(fields) 
    264                 self.tool.logdebug("QUERY : Modify(%s, %s ==> %s)" % (dn, oldentry, fields)) 
     266                self.querydebug("QUERY : Modify(%s, %s ==> %s)" % (dn, oldentry, fields)) 
    265267                entry = ldap.modlist.modifyModlist(oldentry, fields, ignore_oldexistent=ignoreold) 
    266268                modentry = [] 
     
    268270                    if mtyp and (mtyp.lower() != "createtimestamp") : 
    269271                        modentry.append((mop, mtyp, mval)) 
    270                 self.tool.logdebug("MODIFY : %s ==> %s ==> %s" % (fields, entry, modentry)) 
     272                self.querydebug("MODIFY : %s ==> %s ==> %s" % (fields, entry, modentry)) 
    271273                if modentry : 
    272274                    self.database.modify_s(dn, modentry) 
     
    288290                            except KeyError :     
    289291                                pass 
    290                     self.tool.logdebug("LDAP cache update %s => %s" % (dn, cachedentry)) 
     292                    self.querydebug("LDAP cache update %s => %s" % (dn, cachedentry)) 
    291293                return dn 
    292294        raise PyKotaStorageError, message 
     
    303305                self.tool.printInfo("Object %s has no %s attribute !" % (dn, attribute), "error") 
    304306            else : 
    305                 attrval = self.databaseToUnicode(attrval) 
     307                attrval = databaseToUnicode(attrval) 
    306308                if patterns : 
    307309                    if (not isinstance(patterns, type([]))) and (not isinstance(patterns, type(()))) : 
     
    318320        result = self.doSearch(ldapfilter, ["pykotaBillingCode"], base=self.info["billingcodebase"]) 
    319321        if result : 
    320             return [self.databaseToUnicode(bc) for bc in self.filterNames(result, "pykotaBillingCode", billingcode)] 
     322            return [databaseToUnicode(bc) for bc in self.filterNames(result, "pykotaBillingCode", billingcode)] 
    321323        else :     
    322324            return [] 
     
    351353    def getUserNbJobsFromHistory(self, user) : 
    352354        """Returns the number of jobs the user has in history.""" 
    353         result = self.doSearch("(&(pykotaUserName=%s)(objectClass=pykotaJob))" % self.unicodeToDatabase(user.Name), None, base=self.info["jobbase"]) 
     355        result = self.doSearch("(&(pykotaUserName=%s)(objectClass=pykotaJob))" % unicodeToDatabase(user.Name), None, base=self.info["jobbase"]) 
    354356        return len(result) 
    355357         
     
    357359        """Extracts user information given its name.""" 
    358360        user = StorageUser(self, username) 
    359         username = self.unicodeToDatabase(username) 
     361        username = unicodeToDatabase(username) 
    360362        result = self.doSearch("(&(objectClass=pykotaAccount)(|(pykotaUserName=%s)(%s=%s)))" % (username, self.info["userrdn"], username), ["pykotaUserName", "pykotaLimitBy", self.info["usermail"], "description"], base=self.info["userbase"]) 
    361363        if result : 
    362364            fields = result[0][1] 
    363365            user.ident = result[0][0] 
    364             user.Description = self.databaseToUnicode(fields.get("description", [None])[0]) 
     366            user.Description = databaseToUnicode(fields.get("description", [None])[0]) 
    365367            user.Email = fields.get(self.info["usermail"], [None])[0] 
    366368            user.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0] 
     
    395397                        description = "" 
    396398                    else :     
    397                         description = self.databaseToUnicode(base64.decodestring(description)) 
     399                        description = databaseToUnicode(base64.decodestring(description)) 
    398400                    if amount.endswith(" #") :     
    399401                        amount = amount[:-2] # TODO : should be catched earlier, the bug is above I think 
     
    405407        """Extracts group information given its name.""" 
    406408        group = StorageGroup(self, groupname) 
    407         groupname = self.unicodeToDatabase(groupname) 
     409        groupname = unicodeToDatabase(groupname) 
    408410        result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % (groupname, self.info["grouprdn"], groupname), ["pykotaGroupName", "pykotaLimitBy", "description"], base=self.info["groupbase"]) 
    409411        if result : 
    410412            fields = result[0][1] 
    411413            group.ident = result[0][0] 
    412             group.Name = fields.get("pykotaGroupName", [self.databaseToUnicode(groupname)])[0]  
    413             group.Description = self.databaseToUnicode(fields.get("description", [None])[0]) 
     414            group.Name = fields.get("pykotaGroupName", [databaseToUnicode(groupname)])[0]  
     415            group.Description = databaseToUnicode(fields.get("description", [None])[0]) 
    414416            group.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0] 
    415417            group.AccountBalance = 0.0 
     
    425427        """Extracts printer information given its name : returns first matching printer.""" 
    426428        printer = StoragePrinter(self, printername) 
    427         printername = self.unicodeToDatabase(printername) 
     429        printername = unicodeToDatabase(printername) 
    428430        result = self.doSearch("(&(objectClass=pykotaPrinter)(|(pykotaPrinterName=%s)(%s=%s)))" \ 
    429431                      % (printername, self.info["printerrdn"], printername), \ 
     
    435437            fields = result[0][1]       # take only first matching printer, ignore the rest 
    436438            printer.ident = result[0][0] 
    437             printer.Name = fields.get("pykotaPrinterName", [self.databaseToUnicode(printername)])[0]  
     439            printer.Name = fields.get("pykotaPrinterName", [databaseToUnicode(printername)])[0]  
    438440            printer.PricePerJob = float(fields.get("pykotaPricePerJob", [0.0])[0]) 
    439441            printer.PricePerPage = float(fields.get("pykotaPricePerPage", [0.0])[0]) 
     
    445447                printer.PassThrough = 0 
    446448            printer.uniqueMember = fields.get("uniqueMember", []) 
    447             printer.Description = self.databaseToUnicode(fields.get("description", [""])[0])  
     449            printer.Description = databaseToUnicode(fields.get("description", [""])[0])  
    448450            printer.Exists = True 
    449451        return printer     
     
    458460                base = self.info["userquotabase"] 
    459461            result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaUserName=%s)(pykotaPrinterName=%s))" % \ 
    460                                       (self.unicodeToDatabase(user.Name), self.unicodeToDatabase(printer.Name)), \ 
     462                                      (unicodeToDatabase(user.Name), unicodeToDatabase(printer.Name)), \ 
    461463                                      ["pykotaPageCounter", "pykotaLifePageCounter", "pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaWarnCount", "pykotaMaxJobSize"], \ 
    462464                                      base=base) 
     
    503505                base = self.info["groupquotabase"] 
    504506            result = self.doSearch("(&(objectClass=pykotaGroupPQuota)(pykotaGroupName=%s)(pykotaPrinterName=%s))" % \ 
    505                                       (self.unicodeToDatabase(group.Name), self.unicodeToDatabase(printer.Name)), \ 
     507                                      (unicodeToDatabase(group.Name), unicodeToDatabase(printer.Name)), \ 
    506508                                      ["pykotaSoftLimit", "pykotaHardLimit", "pykotaDateLimit", "pykotaMaxJobSize"], \ 
    507509                                      base=base) 
     
    535537                grouppquota.PageCounter = 0 
    536538                grouppquota.LifePageCounter = 0 
    537                 usernamesfilter = "".join(["(pykotaUserName=%s)" % self.unicodeToDatabase(member.Name) for member in self.getGroupMembers(group)]) 
     539                usernamesfilter = "".join(["(pykotaUserName=%s)" % unicodeToDatabase(member.Name) for member in self.getGroupMembers(group)]) 
    538540                if usernamesfilter : 
    539541                    usernamesfilter = "(|%s)" % usernamesfilter 
     
    543545                    base = self.info["userquotabase"] 
    544546                result = self.doSearch("(&(objectClass=pykotaUserPQuota)(pykotaPrinterName=%s)%s)" % \ 
    545                                           (self.unicodeToDatabase(printer.Name), usernamesfilter), \ 
     547                                          (unicodeToDatabase(printer.Name), usernamesfilter), \ 
    546548                                          ["pykotaPageCounter", "pykotaLifePageCounter"], base=base) 
    547549                if result : 
     
    555557        """Extracts a printer's last job information.""" 
    556558        lastjob = StorageLastJob(self, printer) 
    557         pname = self.unicodeToDatabase(printer.Name) 
     559        pname = unicodeToDatabase(printer.Name) 
    558560        result = self.doSearch("(&(objectClass=pykotaLastjob)(|(pykotaPrinterName=%s)(%s=%s)))" % \ 
    559561                                  (pname, self.info["printerrdn"], pname), \ 
     
    591593                lastjob.ident = result[0][0] 
    592594                lastjob.JobId = fields.get("pykotaJobId")[0] 
    593                 lastjob.UserName = self.databaseToUnicode(fields.get("pykotaUserName")[0]) 
     595                lastjob.UserName = databaseToUnicode(fields.get("pykotaUserName")[0]) 
    594596                lastjob.PrinterPageCounter = int(fields.get("pykotaPrinterPageCounter", [0])[0]) 
    595597                try : 
     
    602604                    lastjob.JobPrice = None 
    603605                lastjob.JobAction = fields.get("pykotaAction", [""])[0] 
    604                 lastjob.JobFileName = self.databaseToUnicode(fields.get("pykotaFileName", [""])[0])  
    605                 lastjob.JobTitle = self.databaseToUnicode(fields.get("pykotaTitle", [""])[0])  
     606                lastjob.JobFileName = databaseToUnicode(fields.get("pykotaFileName", [""])[0])  
     607                lastjob.JobTitle = databaseToUnicode(fields.get("pykotaTitle", [""])[0])  
    606608                lastjob.JobCopies = int(fields.get("pykotaCopies", [0])[0]) 
    607                 lastjob.JobOptions = self.databaseToUnicode(fields.get("pykotaOptions", [""])[0])  
     609                lastjob.JobOptions = databaseToUnicode(fields.get("pykotaOptions", [""])[0])  
    608610                lastjob.JobHostName = fields.get("pykotaHostName", [""])[0] 
    609611                lastjob.JobSizeBytes = fields.get("pykotaJobSizeBytes", [0L])[0] 
    610                 lastjob.JobBillingCode = self.databaseToUnicode(fields.get("pykotaBillingCode", [None])[0]) 
     612                lastjob.JobBillingCode = databaseToUnicode(fields.get("pykotaBillingCode", [None])[0]) 
    611613                lastjob.JobMD5Sum = fields.get("pykotaMD5Sum", [None])[0] 
    612614                lastjob.JobPages = fields.get("pykotaPages", [""])[0] 
     
    630632        """Returns the group's members list.""" 
    631633        groupmembers = [] 
    632         gname = self.unicodeToDatabase(group.Name) 
     634        gname = unicodeToDatabase(group.Name) 
    633635        result = self.doSearch("(&(objectClass=pykotaGroup)(|(pykotaGroupName=%s)(%s=%s)))" % \ 
    634636                                  (gname, self.info["grouprdn"], gname), \ 
     
    637639        if result : 
    638640            for username in result[0][1].get(self.info["groupmembers"], []) : 
    639                 groupmembers.append(self.getUser(self.databaseToUnicode(username))) 
     641                groupmembers.append(self.getUser(databaseToUnicode(username))) 
    640642        return groupmembers         
    641643         
     
    643645        """Returns the user's groups list.""" 
    644646        groups = [] 
    645         uname = self.unicodeToDatabase(user.Name) 
     647        uname = unicodeToDatabase(user.Name) 
    646648        result = self.doSearch("(&(objectClass=pykotaGroup)(%s=%s))" % \ 
    647649                                  (self.info["groupmembers"], uname), \ 
     
    650652        if result : 
    651653            for (groupid, fields) in result : 
    652                 groupname = self.databaseToUnicode((fields.get("pykotaGroupName", [None]) or fields.get(self.info["grouprdn"], [None]))[0]) 
     654                groupname = databaseToUnicode((fields.get("pykotaGroupName", [None]) or fields.get(self.info["grouprdn"], [None]))[0]) 
    653655                group = self.getFromCache("GROUPS", groupname) 
    654656                if group is None : 
     
    681683            for (printerid, fields) in result : 
    682684                if printerid != printer.ident : # In case of integrity violation. 
    683                     parentprinter = self.getPrinter(self.databaseToUnicode(fields.get("pykotaPrinterName")[0])) 
     685                    parentprinter = self.getPrinter(databaseToUnicode(fields.get("pykotaPrinterName")[0])) 
    684686                    if parentprinter.Exists : 
    685687                        pgroups.append(parentprinter) 
     
    703705                    patdict[p] = None 
    704706            for (printerid, fields) in result : 
    705                 printername = self.databaseToUnicode(fields.get("pykotaPrinterName", [""])[0] or fields.get(self.info["printerrdn"], [""])[0]) 
     707                printername = databaseToUnicode(fields.get("pykotaPrinterName", [""])[0] or fields.get(self.info["printerrdn"], [""])[0]) 
    706708                if patdict.has_key(printername) or self.tool.matchString(printername, patterns) : 
    707709                    printer = StoragePrinter(self, printername) 
     
    716718                        printer.PassThrough = 0 
    717719                    printer.uniqueMember = fields.get("uniqueMember", []) 
    718                     printer.Description = self.databaseToUnicode(fields.get("description", [""])[0])  
     720                    printer.Description = databaseToUnicode(fields.get("description", [""])[0])  
    719721                    printer.Exists = True 
    720722                    printers.append(printer) 
     
    739741                    patdict[p] = None 
    740742            for (userid, fields) in result : 
    741                 username = self.databaseToUnicode(fields.get("pykotaUserName", [""])[0] or fields.get(self.info["userrdn"], [""])[0]) 
     743                username = databaseToUnicode(fields.get("pykotaUserName", [""])[0] or fields.get(self.info["userrdn"], [""])[0]) 
    742744                if patdict.has_key(username) or self.tool.matchString(username, patterns) : 
    743745                    user = StorageUser(self, username) 
     
    745747                    user.Email = fields.get(self.info["usermail"], [None])[0] 
    746748                    user.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0] 
    747                     user.Description = self.databaseToUnicode(fields.get("description", [""])[0])  
    748                     uname = self.unicodeToDatabase(username) 
     749                    user.Description = databaseToUnicode(fields.get("description", [""])[0])  
     750                    uname = unicodeToDatabase(username) 
    749751                    result = self.doSearch("(&(objectClass=pykotaAccountBalance)(|(pykotaUserName=%s)(%s=%s)))" % \ 
    750752                                              (uname, self.info["balancerdn"], uname), \ 
     
    780782                                description = "" 
    781783                            else :     
    782                                 description = self.databaseToUnicode(base64.decodestring(description)) 
     784                                description = databaseToUnicode(base64.decodestring(description)) 
    783785                            if amount.endswith(" #") :     
    784786                                amount = amount[:-2] # TODO : should be catched earlier, the bug is above I think 
     
    806808                    patdict[p] = None 
    807809            for (groupid, fields) in result : 
    808                 groupname = self.databaseToUnicode(fields.get("pykotaGroupName", [""])[0] or fields.get(self.info["grouprdn"], [""])[0]) 
     810                groupname = databaseToUnicode(fields.get("pykotaGroupName", [""])[0] or fields.get(self.info["grouprdn"], [""])[0]) 
    809811                if patdict.has_key(groupname) or self.tool.matchString(groupname, patterns) : 
    810812                    group = StorageGroup(self, groupname) 
    811813                    group.ident = groupid 
    812                     group.Name = fields.get("pykotaGroupName", [self.databaseToUnicode(groupname)])[0]  
     814                    group.Name = fields.get("pykotaGroupName", [databaseToUnicode(groupname)])[0]  
    813815                    group.LimitBy = fields.get("pykotaLimitBy", ["quota"])[0] 
    814                     group.Description = self.databaseToUnicode(fields.get("description", [""])[0])  
     816                    group.Description = databaseToUnicode(fields.get("description", [""])[0])  
    815817                    group.AccountBalance = 0.0 
    816818                    group.LifeTimePaid = 0.0 
     
    827829        """Returns the list of users who uses a given printer, along with their quotas.""" 
    828830        usersandquotas = [] 
    829         pname = self.unicodeToDatabase(printer.Name) 
    830         names = [self.unicodeToDatabase(n) for n in names] 
     831        pname = unicodeToDatabase(printer.Name) 
     832        names = [unicodeToDatabase(n) for n in names] 
    831833        if self.info["userquotabase"].lower() == "user" : 
    832834            base = self.info["userbase"] 
     
    839841        if result : 
    840842            for (userquotaid, fields) in result : 
    841                 user = self.getUser(self.databaseToUnicode(fields.get("pykotaUserName")[0])) 
     843                user = self.getUser(databaseToUnicode(fields.get("pykotaUserName")[0])) 
    842844                userpquota = StorageUserPQuota(self, user, printer) 
    843845                userpquota.ident = userquotaid 
     
    872874        """Returns the list of groups which uses a given printer, along with their quotas.""" 
    873875        groupsandquotas = [] 
    874         pname = self.unicodeToDatabase(printer.Name) 
    875         names = [self.unicodeToDatabase(n) for n in names] 
     876        pname = unicodeToDatabase(printer.Name) 
     877        names = [unicodeToDatabase(n) for n in names] 
    876878        if self.info["groupquotabase"].lower() == "group" : 
    877879            base = self.info["groupbase"] 
     
    884886        if result : 
    885887            for (groupquotaid, fields) in result : 
    886                 group = self.getGroup(self.databaseToUnicode(fields.get("pykotaGroupName")[0])) 
     888                group = self.getGroup(databaseToUnicode(fields.get("pykotaGroupName")[0])) 
    887889                grouppquota = self.getGroupPQuota(group, printer) 
    888890                groupsandquotas.append((group, grouppquota)) 
     
    895897        if oldentry.Exists : 
    896898            return oldentry # we return the existing entry 
    897         printername = self.unicodeToDatabase(printer.Name) 
     899        printername = unicodeToDatabase(printer.Name) 
    898900        fields = { self.info["printerrdn"] : printername, 
    899901                   "objectClass" : ["pykotaObject", "pykotaPrinter"], 
     
    902904                   "pykotaPassThrough" : (printer.PassThrough and "t") or "f", 
    903905                   "pykotaMaxJobSize" : str(printer.MaxJobSize or 0), 
    904                    "description" : self.unicodeToDatabase(printer.Description or ""), 
     906                   "description" : unicodeToDatabase(printer.Description or ""), 
    905907                   "pykotaPricePerPage" : str(printer.PricePerPage or 0.0), 
    906908                   "pykotaPricePerJob" : str(printer.PricePerJob or 0.0), 
     
    916918        if oldentry.Exists : 
    917919            return oldentry # we return the existing entry 
    918         uname = self.unicodeToDatabase(user.Name) 
     920        uname = unicodeToDatabase(user.Name) 
    919921        newfields = { 
    920922                       "pykotaUserName" : uname, 
    921923                       "pykotaLimitBy" : (user.LimitBy or "quota"), 
    922                        "description" : self.unicodeToDatabase(user.Description or ""), 
     924                       "description" : unicodeToDatabase(user.Description or ""), 
    923925                       self.info["usermail"] : user.Email or "", 
    924926                    }    
     
    991993        if oldentry.Exists : 
    992994            return oldentry # we return the existing entry 
    993         gname = self.unicodeToDatabase(group.Name) 
     995        gname = unicodeToDatabase(group.Name) 
    994996        newfields = {  
    995997                      "pykotaGroupName" : gname, 
    996998                      "pykotaLimitBy" : (group.LimitBy or "quota"), 
    997                       "description" : self.unicodeToDatabase(group.Description or "") 
     999                      "description" : unicodeToDatabase(group.Description or "") 
    9981000                    }  
    9991001        mustadd = 1 
     
    10401042                if not fields.has_key(self.info["groupmembers"]) : 
    10411043                    fields[self.info["groupmembers"]] = [] 
    1042                 fields[self.info["groupmembers"]].append(self.unicodeToDatabase(user.Name)) 
     1044                fields[self.info["groupmembers"]].append(unicodeToDatabase(user.Name)) 
    10431045                self.doModify(group.ident, fields) 
    10441046                group.Members.append(user) 
     
    10531055                    fields[self.info["groupmembers"]] = [] 
    10541056                try :     
    1055                     fields[self.info["groupmembers"]].remove(self.unicodeToDatabase(user.Name)) 
     1057                    fields[self.info["groupmembers"]].remove(unicodeToDatabase(user.Name)) 
    10561058                except ValueError : 
    10571059                    pass # TODO : Strange, shouldn't it be there ? 
     
    10671069            return oldentry # we return the existing entry 
    10681070        uuid = self.genUUID() 
    1069         uname = self.unicodeToDatabase(upq.User.Name) 
    1070         pname = self.unicodeToDatabase(upq.Printer.Name) 
     1071        uname = unicodeToDatabase(upq.User.Name) 
     1072        pname = unicodeToDatabase(upq.Printer.Name) 
    10711073        fields = { "cn" : uuid, 
    10721074                   "objectClass" : ["pykotaObject", "pykotaUserPQuota"], 
     
    10951097            return oldentry # we return the existing entry 
    10961098        uuid = self.genUUID() 
    1097         gname = self.unicodeToDatabase(gpq.Group.Name) 
    1098         pname = self.unicodeToDatabase(gpq.Printer.Name) 
     1099        gname = unicodeToDatabase(gpq.Group.Name) 
     1100        pname = unicodeToDatabase(gpq.Printer.Name) 
    10991101        fields = { "cn" : uuid, 
    11001102                   "objectClass" : ["pykotaObject", "pykotaGroupPQuota"], 
     
    11161118                   "pykotaPassThrough" : (printer.PassThrough and "t") or "f", 
    11171119                   "pykotaMaxJobSize" : str(printer.MaxJobSize or 0), 
    1118                    "description" : self.unicodeToDatabase(printer.Description or ""), 
     1120                   "description" : unicodeToDatabase(printer.Description or ""), 
    11191121                   "pykotaPricePerPage" : str(printer.PricePerPage or 0.0), 
    11201122                   "pykotaPricePerJob" : str(printer.PricePerJob or 0.0), 
     
    11261128        newfields = { 
    11271129                       "pykotaLimitBy" : (user.LimitBy or "quota"), 
    1128                        "description" : self.unicodeToDatabase(user.Description or ""),  
     1130                       "description" : unicodeToDatabase(user.Description or ""),  
    11291131                       self.info["usermail"] : user.Email or "", 
    11301132                    }    
     
    11411143        newfields = { 
    11421144                       "pykotaLimitBy" : (group.LimitBy or "quota"), 
    1143                        "description" : self.unicodeToDatabase(group.Description or ""),  
     1145                       "description" : unicodeToDatabase(group.Description or ""),  
    11441146                    }    
    11451147        self.doModify(group.ident, newfields) 
     
    11781180        payments = [] 
    11791181        for payment in user.Payments : 
    1180             payments.append("%s # %s # %s" % (payment[0], str(payment[1]), base64.encodestring(self.unicodeToDatabase(payment[2])).strip())) 
    1181         payments.append("%s # %s # %s" % (str(DateTime.now()), str(amount), base64.encodestring(self.unicodeToDatabase(comment)).strip())) 
     1182            payments.append("%s # %s # %s" % (payment[0], str(payment[1]), base64.encodestring(unicodeToDatabase(payment[2])).strip())) 
     1183        payments.append("%s # %s # %s" % (str(DateTime.now()), str(amount), base64.encodestring(unicodeToDatabase(comment)).strip())) 
    11821184        fields = { 
    11831185                   "pykotaPayments" : payments, 
     
    11951197    def writeJobNew(self, printer, user, jobid, pagecounter, action, jobsize=None, jobprice=None, filename=None, title=None, copies=None, options=None, clienthost=None, jobsizebytes=None, jobmd5sum=None, jobpages=None, jobbilling=None, precomputedsize=None, precomputedprice=None) : 
    11961198        """Adds a job in a printer's history.""" 
    1197         uname = self.unicodeToDatabase(user.Name) 
    1198         pname = self.unicodeToDatabase(printer.Name) 
     1199        uname = unicodeToDatabase(user.Name) 
     1200        pname = unicodeToDatabase(printer.Name) 
    11991201        if (not self.disablehistory) or (not printer.LastJob.Exists) : 
    12001202            uuid = self.genUUID() 
     
    12141216                   "pykotaPrinterPageCounter" : str(pagecounter), 
    12151217                   "pykotaAction" : action, 
    1216                    "pykotaFileName" : ((filename is None) and "None") or self.unicodeToDatabase(filename),  
    1217                    "pykotaTitle" : ((title is None) and "None") or self.unicodeToDatabase(title),  
     1218                   "pykotaFileName" : ((filename is None) and "None") or unicodeToDatabase(filename),  
     1219                   "pykotaTitle" : ((title is None) and "None") or unicodeToDatabase(title),  
    12181220                   "pykotaCopies" : str(copies),  
    1219                    "pykotaOptions" : ((options is None) and "None") or self.unicodeToDatabase(options),  
     1221                   "pykotaOptions" : ((options is None) and "None") or unicodeToDatabase(options),  
    12201222                   "pykotaHostName" : str(clienthost),  
    12211223                   "pykotaJobSizeBytes" : str(jobsizebytes), 
    12221224                   "pykotaMD5Sum" : str(jobmd5sum), 
    12231225                   "pykotaPages" : jobpages,            # don't add this attribute if it is not set, so no string conversion 
    1224                    "pykotaBillingCode" : self.unicodeToDatabase(jobbilling), # don't add this attribute if it is not set, so no string conversion 
     1226                   "pykotaBillingCode" : unicodeToDatabase(jobbilling), # don't add this attribute if it is not set, so no string conversion 
    12251227                   "pykotaPrecomputedJobSize" : str(precomputedsize), 
    12261228                   "pykotaPrecomputedJobPrice" : str(precomputedprice), 
     
    13141316        where = [] 
    13151317        if user is not None : 
    1316             where.append("(pykotaUserName=%s)" % self.unicodeToDatabase(user.Name)) 
     1318            where.append("(pykotaUserName=%s)" % unicodeToDatabase(user.Name)) 
    13171319        if printer is not None : 
    1318             where.append("(pykotaPrinterName=%s)" % self.unicodeToDatabase(printer.Name)) 
     1320            where.append("(pykotaPrinterName=%s)" % unicodeToDatabase(printer.Name)) 
    13191321        if hostname is not None : 
    13201322            where.append("(pykotaHostName=%s)" % hostname) 
    13211323        if billingcode is not None : 
    1322             where.append("(pykotaBillingCode=%s)" % self.unicodeToDatabase(billingcode)) 
     1324            where.append("(pykotaBillingCode=%s)" % unicodeToDatabase(billingcode)) 
    13231325        if jobid is not None : 
    1324             where.append("(pykotaJobId=%s)" % jobid) # TODO : jobid is text, so self.unicodeToDatabase(jobid) but do all of them as well. 
     1326            where.append("(pykotaJobId=%s)" % jobid) # TODO : jobid is text, so unicodeToDatabase(jobid) but do all of them as well. 
    13251327        if where :     
    13261328            where = "(&%s)" % "".join([precond] + where) 
     
    13631365                    job.JobPrice = None 
    13641366                job.JobAction = fields.get("pykotaAction", [""])[0] 
    1365                 job.JobFileName = self.databaseToUnicode(fields.get("pykotaFileName", [""])[0])  
    1366                 job.JobTitle = self.databaseToUnicode(fields.get("pykotaTitle", [""])[0])  
     1367                job.JobFileName = databaseToUnicode(fields.get("pykotaFileName", [""])[0])  
     1368                job.JobTitle = databaseToUnicode(fields.get("pykotaTitle", [""])[0])  
    13671369                job.JobCopies = int(fields.get("pykotaCopies", [0])[0]) 
    1368                 job.JobOptions = self.databaseToUnicode(fields.get("pykotaOptions", [""])[0])  
     1370                job.JobOptions = databaseToUnicode(fields.get("pykotaOptions", [""])[0])  
    13691371                job.JobHostName = fields.get("pykotaHostName", [""])[0] 
    13701372                job.JobSizeBytes = fields.get("pykotaJobSizeBytes", [0L])[0] 
    1371                 job.JobBillingCode = self.databaseToUnicode(fields.get("pykotaBillingCode", [None])[0]) 
     1373                job.JobBillingCode = databaseToUnicode(fields.get("pykotaBillingCode", [None])[0]) 
    13721374                job.JobMD5Sum = fields.get("pykotaMD5Sum", [None])[0] 
    13731375                job.JobPages = fields.get("pykotaPages", [""])[0] 
     
    13891391                   ((end is None) and (job.JobDate >= start)) or \ 
    13901392                   ((job.JobDate >= start) and (job.JobDate <= end)) : 
    1391                     job.UserName = self.databaseToUnicode(fields.get("pykotaUserName")[0]) 
    1392                     job.PrinterName = self.databaseToUnicode(fields.get("pykotaPrinterName")[0]) 
     1393                    job.UserName = databaseToUnicode(fields.get("pykotaUserName")[0]) 
     1394                    job.PrinterName = databaseToUnicode(fields.get("pykotaPrinterName")[0]) 
    13931395                    job.Exists = True 
    13941396                    jobs.append(job) 
     
    14001402    def deleteUser(self, user) :     
    14011403        """Completely deletes an user from the Quota Storage.""" 
    1402         uname = self.unicodeToDatabase(user.Name) 
     1404        uname = unicodeToDatabase(user.Name) 
    14031405        todelete = []     
    14041406        result = self.doSearch("(&(objectClass=pykotaJob)(pykotaUserName=%s))" % uname, base=self.info["jobbase"]) 
     
    14181420            # if last job of current printer was printed by the user 
    14191421            # to delete, we also need to delete the printer's last job entry. 
    1420             printer = self.getPrinter(self.databaseToUnicode(fields["pykotaPrinterName"][0])) 
     1422            printer = self.getPrinter(databaseToUnicode(fields["pykotaPrinterName"][0])) 
    14211423            if printer.LastJob.UserName == user.Name : 
    14221424                todelete.append(printer.LastJob.lastjobident) 
     
    14531455    def deleteGroup(self, group) :     
    14541456        """Completely deletes a group from the Quota Storage.""" 
    1455         gname = self.unicodeToDatabase(group.Name) 
     1457        gname = unicodeToDatabase(group.Name) 
    14561458        if self.info["groupquotabase"].lower() == "group" : 
    14571459            base = self.info["groupbase"] 
     
    15241526    def deleteUserPQuota(self, upquota) :     
    15251527        """Completely deletes an user print quota entry from the database.""" 
    1526         uname = self.unicodeToDatabase(upquota.User.Name) 
    1527         pname = self.unicodeToDatabase(upquota.Printer.Name) 
     1528        uname = unicodeToDatabase(upquota.User.Name) 
     1529        pname = unicodeToDatabase(upquota.Printer.Name) 
    15281530        result = self.doSearch("(&(objectClass=pykotaJob)(pykotaUserName=%s)(pykotaPrinterName=%s))" \ 
    15291531                                   % (uname, pname), \ 
     
    15411543    def deletePrinter(self, printer) :     
    15421544        """Completely deletes a printer from the Quota Storage.""" 
    1543         pname = self.unicodeToDatabase(printer.Name) 
     1545        pname = unicodeToDatabase(printer.Name) 
    15441546        result = self.doSearch("(&(objectClass=pykotaLastJob)(pykotaPrinterName=%s))" % pname, base=self.info["lastjobbase"]) 
    15451547        for (ident, fields) in result : 
     
    17641766        """Extracts billing code information given its label : returns first matching billing code.""" 
    17651767        code = StorageBillingCode(self, label) 
    1766         ulabel = self.unicodeToDatabase(label) 
     1768        ulabel = unicodeToDatabase(label) 
    17671769        result = self.doSearch("(&(objectClass=pykotaBilling)(pykotaBillingCode=%s))" % \ 
    17681770                                  ulabel, \ 
     
    17721774            fields = result[0][1]       # take only first matching code, ignore the rest 
    17731775            code.ident = result[0][0] 
    1774             code.BillingCode = self.databaseToUnicode(fields.get("pykotaBillingCode", [ulabel])[0]) 
     1776            code.BillingCode = databaseToUnicode(fields.get("pykotaBillingCode", [ulabel])[0]) 
    17751777            code.PageCounter = int(fields.get("pykotaPageCounter", [0])[0]) 
    17761778            code.Balance = float(fields.get("pykotaBalance", [0.0])[0]) 
    1777             code.Description = self.databaseToUnicode(fields.get("description", [""])[0])  
     1779            code.Description = databaseToUnicode(fields.get("description", [""])[0])  
    17781780            code.Exists = True 
    17791781        return code     
     
    17881790        fields = { "objectClass" : ["pykotaObject", "pykotaBilling"], 
    17891791                   "cn" : uuid, 
    1790                    "pykotaBillingCode" : self.unicodeToDatabase(bcode.BillingCode), 
     1792                   "pykotaBillingCode" : unicodeToDatabase(bcode.BillingCode), 
    17911793                   "pykotaPageCounter" : str(bcode.PageCounter or 0), 
    17921794                   "pykotaBalance" : str(bcode.Balance or 0.0), 
    1793                    "description" : self.unicodeToDatabase(bcode.Description or ""),  
     1795                   "description" : unicodeToDatabase(bcode.Description or ""),  
    17941796                 }  
    17951797        self.doAdd(dn, fields) 
     
    18001802        """Sets the new description for a billing code.""" 
    18011803        fields = { 
    1802                    "description" : self.unicodeToDatabase(bcode.Description or ""),  
     1804                   "description" : unicodeToDatabase(bcode.Description or ""),  
    18031805                   "pykotaPageCounter" : str(bcode.PageCounter or 0), 
    18041806                   "pykotaBalance" : str(bcode.Balance or 0.0), 
     
    18221824                    patdict[p] = None 
    18231825            for (codeid, fields) in result : 
    1824                 codename = self.databaseToUnicode(fields.get("pykotaBillingCode", [""])[0]) 
     1826                codename = databaseToUnicode(fields.get("pykotaBillingCode", [""])[0]) 
    18251827                if patdict.has_key(codename) or self.tool.matchString(codename, patterns) : 
    18261828                    code = StorageBillingCode(self, codename) 
     
    18281830                    code.PageCounter = int(fields.get("pykotaPageCounter", [0])[0]) 
    18291831                    code.Balance = float(fields.get("pykotaBalance", [0.0])[0]) 
    1830                     code.Description = self.databaseToUnicode(fields.get("description", [""])[0])  
     1832                    code.Description = databaseToUnicode(fields.get("description", [""])[0])  
    18311833                    code.Exists = True 
    18321834                    codes.append(code) 
  • pykota/trunk/pykota/storages/mysqlstorage.py

    r3288 r3294  
    9898        try : 
    9999            before = time.time() 
    100             self.tool.logdebug("QUERY : %s" % query) 
     100            self.querydebug("QUERY : %s" % query) 
    101101            self.cursor.execute(query) 
    102102        except self.database.Error, msg :     
     
    137137        try : 
    138138            before = time.time() 
    139             self.tool.logdebug("QUERY : %s" % query) 
     139            self.querydebug("QUERY : %s" % query) 
    140140            self.cursor.execute(query) 
    141141        except self.database.Error, msg :     
  • pykota/trunk/pykota/storages/pgstorage.py

    r3291 r3294  
    2929from pykota.storage import BaseStorage 
    3030from pykota.storages.sql import SQLStorage 
     31 
     32from pykota.utils import *                            
    3133 
    3234try : 
     
    99101        try : 
    100102            before = time.time() 
    101             self.tool.logdebug("QUERY : %s" % query) 
     103            self.querydebug("QUERY : %s" % query) 
    102104            result = self.database.query(query) 
    103105        except PGError, msg :     
     
    121123        try : 
    122124            before = time.time() 
    123             self.tool.logdebug("QUERY : %s" % query) 
     125            self.querydebug("QUERY : %s" % query) 
    124126            result = self.database.query(query) 
    125127        except PGError, msg :     
     
    154156                    field = fields[j] 
    155157                    if type(field) == StringType : 
    156                         fields[j] = self.databaseToUnicode(field)  
     158                        fields[j] = databaseToUnicode(field)  
    157159                entries[i] = tuple(fields)     
    158160            return entries 
  • pykota/trunk/pykota/storages/sqlitestorage.py

    r3288 r3294  
    3535    # TODO : to translate or not to translate ? 
    3636    raise PyKotaStorageError, "This python version (%s) doesn't seem to have the PySQLite module installed correctly." % sys.version.split()[0] 
    37  
     37     
    3838class Storage(BaseStorage, SQLStorage) : 
    3939    def __init__(self, pykotatool, host, dbname, user, passwd) : 
     
    8282        try : 
    8383            before = time.time() 
    84             self.tool.logdebug("QUERY : %s" % query) 
     84            self.querydebug("QUERY : %s" % query) 
    8585            self.cursor.execute(query) 
    8686        except self.database.Error, msg :     
     
    119119        try : 
    120120            before = time.time() 
    121             self.tool.logdebug("QUERY : %s" % query) 
     121            self.querydebug("QUERY : %s" % query) 
    122122            self.cursor.execute(query) 
    123123        except self.database.Error, msg :     
  • pykota/trunk/pykota/storages/sql.py

    r3293 r3294  
    2626                           StorageJob, StorageLastJob, StorageUserPQuota, \ 
    2727                           StorageGroupPQuota, StorageBillingCode 
     28                            
     29from pykota.utils import *                            
    2830 
    2931class SQLStorage : 
     
    3638        user.LifeTimePaid = record.get("lifetimepaid") 
    3739        user.Email = record.get("email") 
    38         user.Description = self.databaseToUnicode(record.get("description")) 
     40        user.Description = databaseToUnicode(record.get("description")) 
    3941        user.OverCharge = record.get("overcharge", 1.0) 
    4042        user.Exists = True 
     
    4850        group.AccountBalance = record.get("balance") 
    4951        group.LifeTimePaid = record.get("lifetimepaid") 
    50         group.Description = self.databaseToUnicode(record.get("description")) 
     52        group.Description = databaseToUnicode(record.get("description")) 
    5153        group.Exists = True 
    5254        return group 
     
    6466        else : 
    6567            printer.PassThrough = False 
    66         printer.Description = self.databaseToUnicode(record.get("description") or "") # TODO : is 'or ""' still needed ? 
     68        printer.Description = databaseToUnicode(record.get("description") or "") # TODO : is 'or ""' still needed ? 
    6769        printer.Exists = True 
    6870        return printer 
     
    7678        job.JobPrice = record.get("jobprice") 
    7779        job.JobAction = record.get("action") 
    78         job.JobFileName = self.databaseToUnicode(record.get("filename") or "")  
    79         job.JobTitle = self.databaseToUnicode(record.get("title") or "")  
     80        job.JobFileName = databaseToUnicode(record.get("filename") or "")  
     81        job.JobTitle = databaseToUnicode(record.get("title") or "")  
    8082        job.JobCopies = record.get("copies") 
    81         job.JobOptions = self.databaseToUnicode(record.get("options") or "")  
     83        job.JobOptions = databaseToUnicode(record.get("options") or "")  
    8284        job.JobDate = record.get("jobdate") 
    8385        job.JobHostName = record.get("hostname") 
     
    8587        job.JobMD5Sum = record.get("md5sum") 
    8688        job.JobPages = record.get("pages") 
    87         job.JobBillingCode = self.databaseToUnicode(record.get("billingcode") or "") 
     89        job.JobBillingCode = databaseToUnicode(record.get("billingcode") or "") 
    8890        job.PrecomputedJobSize = record.get("precomputedjobsize") 
    8991        job.PrecomputedJobPrice = record.get("precomputedjobprice") 
    90         job.UserName = self.databaseToUnicode(record.get("username")) 
    91         job.PrinterName = self.databaseToUnicode(record.get("printername")) 
     92        job.UserName = databaseToUnicode(record.get("username")) 
     93        job.PrinterName = databaseToUnicode(record.get("printername")) 
    9294        if job.JobTitle == job.JobFileName == job.JobOptions == "hidden" : 
    9395            (job.JobTitle, job.JobFileName, job.JobOptions) = (_("Hidden because of privacy concerns"),) * 3 
     
    138140        code = StorageBillingCode(self, billingcode) 
    139141        code.ident = record.get("id") 
    140         code.Description = self.databaseToUnicode(record.get("description") or "") # TODO : is 'or ""' still needed ? 
     142        code.Description = databaseToUnicode(record.get("description") or "") # TODO : is 'or ""' still needed ? 
    141143        code.Balance = record.get("balance") or 0.0 
    142144        code.PageCounter = record.get("pagecounter") or 0 
     
    149151            expressions = [] 
    150152            for (k, v) in only.items() : 
    151                 expressions.append("%s=%s" % (k, self.doQuote(self.unicodeToDatabase(v)))) 
     153                expressions.append("%s=%s" % (k, self.doQuote(unicodeToDatabase(v)))) 
    152154            return " AND ".join(expressions)      
    153155        return ""         
     
    299301                self.tool.printInfo("Object %s has no %s attribute !" % (repr(record), attribute), "error") 
    300302            else : 
    301                 attrval = self.databaseToUnicode(attrval) 
     303                attrval = databaseToUnicode(attrval) 
    302304                if patterns : 
    303305                    if (not isinstance(patterns, type([]))) and (not isinstance(patterns, type(()))) : 
     
    351353        """Extracts user information given its name.""" 
    352354        result = self.doSearch("SELECT * FROM users WHERE username=%s"\ 
    353                       % self.doQuote(self.unicodeToDatabase(username))) 
     355                      % self.doQuote(unicodeToDatabase(username))) 
    354356        if result : 
    355357            return self.storageUserFromRecord(username, result[0]) 
     
    360362        """Extracts group information given its name.""" 
    361363        result = self.doSearch("SELECT groups.*,COALESCE(SUM(balance), 0.0) AS balance, COALESCE(SUM(lifetimepaid), 0.0) AS lifetimepaid FROM groups LEFT OUTER JOIN users ON users.id IN (SELECT userid FROM groupsmembers WHERE groupid=groups.id) WHERE groupname=%s GROUP BY groups.id,groups.groupname,groups.limitby,groups.description" \ 
    362                       % self.doQuote(self.unicodeToDatabase(groupname))) 
     364                      % self.doQuote(unicodeToDatabase(groupname))) 
    363365        if result : 
    364366            return self.storageGroupFromRecord(groupname, result[0]) 
     
    369371        """Extracts printer information given its name.""" 
    370372        result = self.doSearch("SELECT * FROM printers WHERE printername=%s" \ 
    371                       % self.doQuote(self.unicodeToDatabase(printername))) 
     373                      % self.doQuote(unicodeToDatabase(printername))) 
    372374        if result : 
    373375            return self.storagePrinterFromRecord(printername, result[0]) 
     
    378380        """Extracts a billing code information given its name.""" 
    379381        result = self.doSearch("SELECT * FROM billingcodes WHERE billingcode=%s" \ 
    380                       % self.doQuote(self.unicodeToDatabase(label))) 
     382                      % self.doQuote(unicodeToDatabase(label))) 
    381383        if result : 
    382384            return self.storageBillingCodeFromRecord(label, result[0]) 
     
    416418        if result : 
    417419            for record in result : 
    418                 user = self.storageUserFromRecord(self.databaseToUnicode(record.get("username")), \ 
     420                user = self.storageUserFromRecord(databaseToUnicode(record.get("username")), \ 
    419421                                                  record) 
    420422                groupmembers.append(user) 
     
    428430        if result : 
    429431            for record in result : 
    430                 groups.append(self.getGroup(self.databaseToUnicode(record.get("groupname")))) 
     432                groups.append(self.getGroup(databaseToUnicode(record.get("groupname")))) 
    431433        return groups         
    432434         
     
    438440            for record in result : 
    439441                if record["groupid"] != printer.ident : # in case of integrity violation 
    440                     parentprinter = self.getPrinter(self.databaseToUnicode(record.get("printername"))) 
     442                    parentprinter = self.getPrinter(databaseToUnicode(record.get("printername"))) 
    441443                    if parentprinter.Exists : 
    442444                        pgroups.append(parentprinter) 
     
    460462                    patdict[p] = None 
    461463            for record in result : 
    462                 pname = self.databaseToUnicode(record["printername"]) 
     464                pname = databaseToUnicode(record["printername"]) 
    463465                if patdict.has_key(pname) or self.tool.matchString(pname, patterns) : 
    464466                    printer = self.storagePrinterFromRecord(pname, record) 
     
    484486                    patdict[p] = None 
    485487            for record in result : 
    486                 uname = self.databaseToUnicode(record["username"]) 
     488                uname = databaseToUnicode(record["username"]) 
    487489                if patdict.has_key(uname) or self.tool.matchString(uname, patterns) : 
    488490                    user = self.storageUserFromRecord(uname, record) 
     
    508510                    patdict[p] = None 
    509511            for record in result : 
    510                 gname = self.databaseToUnicode(record["groupname"]) 
     512                gname = databaseToUnicode(record["groupname"]) 
    511513                if patdict.has_key(gname) or self.tool.matchString(gname, patterns) : 
    512514                    group = self.storageGroupFromRecord(gname, record) 
     
    529531                    patdict[p] = None 
    530532            for record in result : 
    531                 codename = self.databaseToUnicode(record["billingcode"]) 
     533                codename = databaseToUnicode(record["billingcode"]) 
    532534                if patdict.has_key(codename) or self.tool.matchString(codename, patterns) : 
    533535                    code = self.storageBillingCodeFromRecord(codename, record) 
     
    542544        if result : 
    543545            for record in result : 
    544                 uname = self.databaseToUnicode(record.get("username")) 
     546                uname = databaseToUnicode(record.get("username")) 
    545547                if self.tool.matchString(uname, names) : 
    546548                    user = self.storageUserFromRecord(uname, record) 
     
    557559        if result : 
    558560            for record in result : 
    559                 gname = self.databaseToUnicode(record.get("groupname")) 
     561                gname = databaseToUnicode(record.get("groupname")) 
    560562                if self.tool.matchString(gname, names) : 
    561563                    group = self.getGroup(gname) 
     
    570572            return oldentry 
    571573        self.doModify("INSERT INTO printers (printername, passthrough, maxjobsize, description, priceperpage, priceperjob) VALUES (%s, %s, %s, %s, %s, %s)" \ 
    572                           % (self.doQuote(self.unicodeToDatabase(printer.Name)), \ 
     574                          % (self.doQuote(unicodeToDatabase(printer.Name)), \ 
    573575                             self.doQuote((printer.PassThrough and "t") or "f"), \ 
    574576                             self.doQuote(printer.MaxJobSize or 0), \ 
    575                              self.doQuote(self.unicodeToDatabase(printer.Description)), \ 
     577                             self.doQuote(unicodeToDatabase(printer.Description)), \ 
    576578                             self.doQuote(printer.PricePerPage or 0.0), \ 
    577579                             self.doQuote(printer.PricePerJob or 0.0))) 
     
    585587            return oldentry 
    586588        self.doModify("INSERT INTO billingcodes (billingcode, balance, pagecounter, description) VALUES (%s, %s, %s, %s)" \ 
    587                            % (self.doQuote(self.unicodeToDatabase(bcode.BillingCode)),  
     589                           % (self.doQuote(unicodeToDatabase(bcode.BillingCode)),  
    588590                              self.doQuote(bcode.Balance or 0.0), \ 
    589591                              self.doQuote(bcode.PageCounter or 0), \ 
    590                               self.doQuote(self.unicodeToDatabase(bcode.Description)))) 
     592                              self.doQuote(unicodeToDatabase(bcode.Description)))) 
    591593        bcode.isDirty = False 
    592594        return None # the entry created doesn't need further modification 
     
    598600            return oldentry 
    599601        self.doModify("INSERT INTO users (username, limitby, balance, lifetimepaid, email, overcharge, description) VALUES (%s, %s, %s, %s, %s, %s, %s)" % \ 
    600                                      (self.doQuote(self.unicodeToDatabase(user.Name)), \ 
     602                                     (self.doQuote(unicodeToDatabase(user.Name)), \ 
    601603                                      self.doQuote(user.LimitBy or 'quota'), \ 
    602604                                      self.doQuote(user.AccountBalance or 0.0), \ 
     
    604606                                      self.doQuote(user.Email), \ 
    605607                                      self.doQuote(user.OverCharge), \ 
    606                                       self.doQuote(self.unicodeToDatabase(user.Description)))) 
     608                                      self.doQuote(unicodeToDatabase(user.Description)))) 
    607609        if user.PaymentsBacklog : 
    608610            for (value, comment) in user.PaymentsBacklog : 
     
    618620            return oldentry 
    619621        self.doModify("INSERT INTO groups (groupname, limitby, description) VALUES (%s, %s, %s)" % \ 
    620                               (self.doQuote(self.unicodeToDatabase(group.Name)), \ 
     622                              (self.doQuote(unicodeToDatabase(group.Name)), \ 
    621623                               self.doQuote(group.LimitBy or "quota"), \ 
    622                                self.doQuote(self.unicodeToDatabase(group.Description)))) 
     624                               self.doQuote(unicodeToDatabase(group.Description)))) 
    623625        group.isDirty = False 
    624626        return None # the entry created doesn't need further modification 
     
    677679                              % (self.doQuote((printer.PassThrough and "t") or "f"), \ 
    678680                                 self.doQuote(printer.MaxJobSize or 0), \ 
    679                                  self.doQuote(self.unicodeToDatabase(printer.Description)), \ 
     681                                 self.doQuote(unicodeToDatabase(printer.Description)), \ 
    680682                                 self.doQuote(printer.PricePerPage or 0.0), \ 
    681683                                 self.doQuote(printer.PricePerJob or 0.0), \ 
     
    690692                                  self.doQuote(user.Email), \ 
    691693                                  self.doQuote(user.OverCharge), \ 
    692                                   self.doQuote(self.unicodeToDatabase(user.Description)), \ 
     694                                  self.doQuote(unicodeToDatabase(user.Description)), \ 
    693695                                  self.doQuote(user.ident))) 
    694696                                   
     
    697699        self.doModify("UPDATE groups SET limitby=%s, description=%s WHERE id=%s" \ 
    698700                               % (self.doQuote(group.LimitBy or 'quota'), \ 
    699                                   self.doQuote(self.unicodeToDatabase(group.Description)), \ 
     701                                  self.doQuote(unicodeToDatabase(group.Description)), \ 
    700702                                  self.doQuote(group.ident))) 
    701703         
     
    717719                            % (self.doQuote(bcode.Balance or 0.0), \ 
    718720                               self.doQuote(bcode.PageCounter or 0), \ 
    719                                self.doQuote(self.unicodeToDatabase(bcode.Description)), \ 
     721                               self.doQuote(unicodeToDatabase(bcode.Description)), \ 
    720722                               self.doQuote(bcode.ident))) 
    721723        
     
    735737        """Adds a new payment to the payments history.""" 
    736738        if user.ident is not None : 
    737             self.doModify("INSERT INTO payments (userid, amount, description) VALUES (%s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(amount), self.doQuote(self.unicodeToDatabase(comment)))) 
     739            self.doModify("INSERT INTO payments (userid, amount, description) VALUES (%s, %s, %s)" % (self.doQuote(user.ident), self.doQuote(amount), self.doQuote(unicodeToDatabase(comment)))) 
    738740        else :     
    739             self.doModify("INSERT INTO payments (userid, amount, description) VALUES ((SELECT id FROM users WHERE username=%s), %s, %s)" % (self.doQuote(self.unicodeToDatabase(user.Name)), self.doQuote(amount), self.doQuote(self.unicodeToDatabase(comment)))) 
     741            self.doModify("INSERT INTO payments (userid, amount, description) VALUES ((SELECT id FROM users WHERE username=%s), %s, %s)" % (self.doQuote(unicodeToDatabase(user.Name)), self.doQuote(amount), self.doQuote(unicodeToDatabase(comment)))) 
    740742         
    741743    def writeLastJobSize(self, lastjob, jobsize, jobprice) :         
     
    748750            # For legal reasons, we want to hide the title, filename and options 
    749751            title = filename = options = "hidden" 
    750         filename = self.unicodeToDatabase(filename) 
    751         title = self.unicodeToDatabase(title) 
    752         options = self.unicodeToDatabase(options) 
    753         jobbilling = self.unicodeToDatabase(jobbilling) 
     752        filename = unicodeToDatabase(filename) 
     753        title = unicodeToDatabase(title) 
     754        options = unicodeToDatabase(options) 
     755        jobbilling = unicodeToDatabase(jobbilling) 
    754756        if (not self.disablehistory) or (not printer.LastJob.Exists) : 
    755757            if jobsize is not None : 
     
    814816            where.append("hostname=%s" % self.doQuote(hostname)) 
    815817        if billingcode is not None :     
    816             where.append("billingcode=%s" % self.doQuote(self.unicodeToDatabase(billingcode))) 
     818            where.append("billingcode=%s" % self.doQuote(unicodeToDatabase(billingcode))) 
    817819        if jobid is not None :     
    818             where.append("jobid=%s" % self.doQuote(jobid)) # TODO : jobid is text, so self.unicodeToDatabase(jobid) but do all of them as well. 
     820            where.append("jobid=%s" % self.doQuote(jobid)) # TODO : jobid is text, so unicodeToDatabase(jobid) but do all of them as well. 
    819821        if start is not None :     
    820822            where.append("jobdate>=%s" % self.doQuote(start)) 
  • pykota/trunk/pykota/tool.py

    r3288 r3294  
    2929import getopt 
    3030import smtplib 
    31 import gettext 
    3231import locale 
    3332import socket 
     
    109108        self.logger = logger.openLogger("stderr") 
    110109         
    111         # locale stuff 
    112         try : 
    113             locale.setlocale(locale.LC_ALL, (lang, charset)) 
    114         except (locale.Error, IOError) : 
    115             locale.setlocale(locale.LC_ALL, None) 
     110        # Saves a copy of the locale settings 
    116111        (self.language, self.charset) = locale.getlocale() 
    117         self.language = self.language or "C" 
    118         try : 
    119             self.charset = self.charset or locale.getpreferredencoding() 
    120         except locale.Error :     
    121             self.charset = sys.stdout.encoding or sys.getfilesystemencoding() 
    122          
    123         # Dirty hack : if the charset is ASCII, we can safely use UTF-8 instead 
    124         # This has the advantage of allowing transparent support for recent 
    125         # versions of CUPS which (en-)force charset to UTF-8 when printing. 
    126         # This should be needed only when printing, but is probably (?) safe 
    127         # to do when using interactive commands. 
    128         if self.charset.upper() in ('ASCII', 'ANSI_X3.4-1968') : 
    129             self.charset = "UTF-8" 
    130          
    131         # translation stuff 
    132         try : 
    133             try : 
    134                 trans = gettext.translation("pykota", languages=["%s.%s" % (self.language, self.charset)], codeset=self.charset) 
    135             except TypeError : # Python <2.4 
    136                 trans = gettext.translation("pykota", languages=["%s.%s" % (self.language, self.charset)]) 
    137             trans.install(unicode=True) 
    138         except : 
    139             gettext.NullTranslations().install(unicode=True) 
    140      
     112         
    141113        # pykota specific stuff 
    142114        self.documentation = doc 
     
    362334                    # and reset the list of arguments to the lines read from 
    363335                    # the file. 
    364                     argsfile = open(parsed["arguments"] or parsed["A"], "r") 
     336                    argsfile = open(parsed["arguments"] or parsed["A"], "r") # TODO : charset decoding 
    365337                    argv = [ l.strip() for l in argsfile.readlines() ] 
    366338                    argsfile.close() 
  • pykota/trunk/pykota/utils.py

    r3275 r3294  
    1919# $Id$ 
    2020# 
     21 
     22"""This module defines some utility functions which make no sense as methods.""" 
     23 
     24import sys 
     25import locale 
     26import gettext 
     27 
     28def initlocale(lang="", cset=None) : 
     29    """Initializes the locale stuff.""" 
     30    try : 
     31        locale.setlocale(locale.LC_ALL, (lang, cset)) 
     32    except (locale.Error, IOError) : 
     33        locale.setlocale(locale.LC_ALL, None) 
     34    (language, charset) = locale.getlocale() 
     35    language = language or "C" 
     36    try : 
     37        charset = charset or locale.getpreferredencoding() 
     38    except locale.Error :     
     39        charset = sys.stdout.encoding or sys.getfilesystemencoding() 
     40 
     41    # Dirty hack : if the charset is ASCII, we can safely use UTF-8 instead 
     42    # This has the advantage of allowing transparent support for recent 
     43    # versions of CUPS which (en-)force charset to UTF-8 when printing. 
     44    # This should be needed only when printing, but is probably (?) safe 
     45    # to do when using interactive commands. 
     46    if charset.upper() in ('ASCII', 'ANSI_X3.4-1968') : 
     47        charset = "UTF-8" 
     48    return (language, charset) 
     49 
     50def initgettext(lang, cset) : 
     51    """Initializes gettext translations for PyKota.""" 
     52    try : 
     53        try : 
     54            trans = gettext.translation("pykota", \ 
     55                                        languages=["%s.%s" % (lang,  
     56                                                              cset)],  
     57                                        codeset=cset) 
     58        except TypeError : # Python <2.4 
     59            trans = gettext.translation("pykota",  
     60                                        languages=["%s.%s" % (lang,  
     61                                                              cset)]) 
     62        trans.install(unicode=True) 
     63    except : 
     64        gettext.NullTranslations().install(unicode=True) 
     65 
     66def databaseToUnicode(text) : 
     67    """Converts from database format (UTF-8) to unicode.""" 
     68    if text is not None : 
     69        return text.decode("UTF-8", "replace") 
     70    else :  
     71        return None 
     72     
     73def unicodeToDatabase(text) : 
     74    """Converts from unicode to database format (UTF-8).""" 
     75    if text is not None :  
     76        return text.encode("UTF-8", "replace") 
     77    else :     
     78        return None 
     79             
     80def logerr(text) : 
     81    """Logs an unicode text to stderr.""" 
     82    sys.stderr.write(text.encode(sys.stdout.encoding, "replace")) 
     83