public
Description: Web-based translation and translation management tool
Homepage: http://translate.sf.net/
Clone URL: git://github.com/julen/pootle.git
pootle / pootle.py
100755 640 lines (610 sloc) 26.227 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2004-2006 Zuza Software Foundation
#
# This file is part of translate.
#
# translate is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# translate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with translate; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
from jToolkit.web import server
from jToolkit.web import templateserver
from jToolkit.web import session
from jToolkit import prefs
from jToolkit import localize
from jToolkit.widgets import widgets
from jToolkit.widgets import spellui
from jToolkit.widgets import thumbgallery
from jToolkit.web import simplewebserver
from Pootle import indexpage
from Pootle import adminpages
from Pootle import translatepage
from Pootle import pagelayout
from Pootle import projects
from Pootle import potree
from Pootle import pootlefile
from Pootle import users
from Pootle import filelocations
from translate.misc import optrecurse
# Versioning information
from Pootle import __version__ as pootleversion
from translate import __version__ as toolkitversion
from jToolkit import __version__ as jtoolkitversion
from Pootle import statistics
try:
  from xml.etree import ElementTree
except ImportError:
  from elementtree import ElementTree
# We don't need kid in this file, but this will show quickly if it is not
# installed. jToolkit won't complain, so we have to stop here if we don't have kid
import kid
import sys
import os
import re
import random
import pprint
 
class PootleServer(users.OptionalLoginAppServer, templateserver.TemplateServer):
  """the Server that serves the Pootle Pages"""
  def __init__(self, instance, webserver, sessioncache=None, errorhandler=None, loginpageclass=users.LoginPage):
    if sessioncache is None:
      sessioncache = session.SessionCache(sessionclass=users.PootleSession)
    self.potree = potree.POTree(instance)
    super(PootleServer, self).__init__(instance, webserver, sessioncache, errorhandler, loginpageclass)
    self.templatedir = filelocations.templatedir
    self.setdefaultoptions()
 
  def loadurl(self, filename, context):
    """loads a url internally for overlay code"""
    # print "call to load %s with context:\n%s" % (filename, pprint.pformat(context))
    filename = os.path.join(self.templatedir, filename+os.extsep+"html")
    if os.path.exists(filename):
      return open(filename, "r").read()
    return None
 
  def saveprefs(self):
    """saves any changes made to the preferences"""
    # TODO: this is a hack, fix it up nicely :-)
    prefsfile = self.instance.__root__.__dict__["_setvalue"].im_self
    prefsfile.savefile()
 
  def setdefaultoptions(self):
    """sets the default options in the preferences"""
    changed = False
    if not hasattr(self.instance, "title"):
      setattr(self.instance, "title", "Pootle Demo")
      changed = True
    if not hasattr(self.instance, "description"):
      defaultdescription = "This is a demo installation of pootle. The administrator can customize the description in the preferences."
      setattr(self.instance, "description", defaultdescription)
      changed = True
    if not hasattr(self.instance, "baseurl"):
      setattr(self.instance, "baseurl", "/")
      changed = True
    if not hasattr(self.instance, "enablealtsrc"):
      setattr(self.instance, "enablealtsrc", False)
      changed = True
    if changed:
      self.saveprefs()
 
  def changeoptions(self, argdict):
    """changes options on the instance"""
    for key, value in argdict.iteritems():
      if not key.startswith("option-"):
        continue
      optionname = key.replace("option-", "", 1)
      setattr(self.instance, optionname, value)
    self.saveprefs()
 
  def initlanguage(self, req, session):
    """Initialises the session language from the request"""
    availablelanguages = self.potree.getlanguagecodes('pootle')
    acceptlanguageheader = req.headers_in.getheader('Accept-Language')
    if not acceptlanguageheader:
      return
 
    for langpref in acceptlanguageheader.split(","):
      langpref = pagelayout.localelanguage(langpref)
      pos = langpref.find(";")
      if pos >= 0:
        langpref = langpref[:pos]
      if langpref in availablelanguages:
        session.setlanguage(langpref)
        return
      elif langpref.startswith("en"):
        session.setlanguage(None)
        return
    session.setlanguage(None)
        
  def inittranslation(self, localedir=None, localedomains=None, defaultlanguage=None):
    """initializes live translations using the Pootle PO files"""
    self.localedomains = ['jToolkit', 'pootle']
    self.localedir = None
    self.languagelist = self.potree.getlanguagecodes('pootle')
    self.languagenames = self.potree.getlanguages()
    self.defaultlanguage = defaultlanguage
    if self.defaultlanguage is None:
      self.defaultlanguage = getattr(self.instance, "defaultlanguage", "en")
    if self.potree.hasproject(self.defaultlanguage, 'pootle'):
      try:
        self.translation = self.potree.getproject(self.defaultlanguage, 'pootle')
        return
      except Exception, e:
        self.errorhandler.logerror("Could not initialize translation:\n%s" % str(e))
    # if no translation available, set up a blank translation
    super(PootleServer, self).inittranslation()
    # the inherited method overwrites self.languagenames, so we have to redo it
    self.languagenames = self.potree.getlanguages()
 
  def gettranslation(self, language):
    """returns a translation object for the given language (or default if language is None)"""
    if language is None:
      return self.translation
    else:
      try:
        return self.potree.getproject(language, 'pootle')
      except Exception, e:
        if not language.startswith('en'):
          self.errorhandler.logerror("Could not get translation for language %r:\n%s" % (language,str(e)))
        return self.translation
 
  def refreshstats(self, args):
    """refreshes all the available statistics..."""
    if args:
      def filtererrorhandler(functionname, str1, str2, e):
        print "error in filter %s: %r, %r, %s" % (functionname, str1, str2, e)
        return False
      checkerclasses = [projects.checks.StandardChecker, projects.checks.StandardUnitChecker]
      stdchecker = projects.checks.TeeChecker(checkerclasses=checkerclasses, errorhandler=filtererrorhandler)
      for arg in args:
        if not os.path.exists(arg):
          print "file not found:", arg
        if os.path.isdir(arg):
          if not arg.endswith(os.sep):
            arg += os.sep
          projectcode, languagecode = self.potree.getcodesfordir(arg)
          dummyproject = projects.DummyStatsProject(arg, stdchecker, projectcode, languagecode)
          def refreshdir(dummy, dirname, fnames):
            reldirname = dirname.replace(dummyproject.podir, "")
            for fname in fnames:
              fpath = os.path.join(reldirname, fname)
              fullpath = os.path.join(dummyproject.podir, fpath)
              #TODO: PO specific
              if fname.endswith(".po") and not os.path.isdir(fullpath):
                if not os.path.exists(fullpath):
                  print "file does not exist:", fullpath
                  return
                print "refreshing stats for", fpath
                pootlefile.pootlefile(dummyproject, fpath).statistics.updatequickstats()
          os.path.walk(arg, refreshdir, None)
          if projectcode and languagecode:
            dummyproject.savequickstats()
        elif os.path.isfile(arg):
          dummyproject = projects.DummyStatsProject(".", stdchecker)
          print "refreshing stats for", arg
          projects.pootlefile.pootlefile(dummyproject, arg)
    else:
      print "refreshing stats for all files in all projects"
      self.potree.refreshstats()
 
  def generateactivationcode(self):
    """generates a unique activation code"""
    return "".join(["%02x" % int(random.random()*0x100) for i in range(16)])
 
  def generaterobotsfile(self):
    """generates the robots.txt file"""
    langcodes = self.potree.getlanguagecodes()
    excludedfiles = ["login.html", "register.html", "activate.html"]
    content = "User-agent: *\n"
    for excludedfile in excludedfiles:
      content += "Disallow: /%s\n" % excludedfile
    for langcode in langcodes:
      content += "Disallow: /%s/\n" % langcode
    return content
 
  def getpage(self, pathwords, session, argdict):
    """return a page that will be sent to the user"""
    #Ensure we get unicode from argdict
    #TODO: remove when jToolkit does this
    newargdict = {}
    for key, value in argdict.iteritems():
      if isinstance(key, str):
       key = key.decode("utf-8")
      if isinstance(value, str):
       value = value.decode("utf-8")
      newargdict[key] = value
    argdict = newargdict
 
    # Strip of the base url
    baseurl = re.sub('http://[^/]', '', self.instance.baseurl)
    # Split up and remove empty parts
    basepathwords = filter(None, baseurl.split('/'))
    while pathwords and basepathwords and basepathwords[0] == pathwords[0]:
      basepathwords = basepathwords[1:]
      pathwords = pathwords[1:]
 
    if pathwords:
      top = pathwords[0]
    else:
      top = ""
    if top == 'js':
      pathwords = pathwords[1:]
      jsfile = os.path.join(filelocations.htmldir, 'js', *pathwords)
      if not os.path.exists(jsfile):
        jsfile = os.path.join(filelocations.jtoolkitdir, 'js', *pathwords)
        if not os.path.exists(jsfile):
          return None
      jspage = widgets.PlainContents(None)
      jspage.content_type = "application/x-javascript"
      jspage.sendfile_path = jsfile
      jspage.allowcaching = True
      return jspage
    elif pathwords and pathwords[-1].endswith(".css"):
      cssfile = os.path.join(filelocations.htmldir, *pathwords)
      if not os.path.exists(cssfile):
        cssfile = os.path.join(filelocations.jtoolkitdir, *pathwords)
        if not os.path.exists(cssfile):
          return None
      csspage = widgets.PlainContents(None)
      csspage.content_type = "text/css"
      csspage.sendfile_path = cssfile
      csspage.allowcaching = True
      return csspage
    elif top in ['selenium', 'tests']:
      picturefile = os.path.join(filelocations.htmldir, *pathwords)
      picture = widgets.SendFile(picturefile)
      if picturefile.endswith(".html"):
        picture.content_type = 'text/html'
      elif picturefile.endswith(".js"):
        picture.content_type = 'text/javascript'
      picture.allowcaching = True
      return picture
    elif top == 'images':
      pathwords = pathwords[1:]
      picturefile = os.path.join(filelocations.htmldir, 'images', *pathwords)
      picture = widgets.SendFile(picturefile)
      picture.content_type = thumbgallery.getcontenttype(pathwords[-1])
      picture.allowcaching = True
      return picture
    elif pathwords and pathwords[-1].endswith(".ico"):
      picturefile = os.path.join(filelocations.htmldir, *pathwords)
      picture = widgets.SendFile(picturefile)
      picture.content_type = 'image/ico'
      picture.allowcaching = True
      return picture
    elif top == "robots.txt":
      robotspage = widgets.PlainContents(self.generaterobotsfile())
      robotspage.content_type = 'text/plain'
      robotspage.allowcaching = True
      return robotspage
    elif top == "testtemplates.html":
      return templateserver.TemplateServer.getpage(self, pathwords, session, argdict)
    elif not top or top == "index.html":
      return indexpage.PootleIndex(self.potree, session)
    elif top == 'about.html':
      return indexpage.AboutPage(session)
    elif top == "login.html":
      if session.isopen:
        returnurl = argdict.get('returnurl', None) or getattr(self.instance, 'homepage', 'home/')
        return server.Redirect(returnurl)
      message = None
      if 'username' in argdict:
        session.username = argdict["username"]
        message = session.localize("Login failed")
      return users.LoginPage(session, languagenames=self.languagenames, message=message)
    elif top == "register.html":
      return self.registerpage(session, argdict)
    elif top == "activate.html":
      return self.activatepage(session, argdict)
    elif top == "projects":
      pathwords = pathwords[1:]
      if pathwords:
        top = pathwords[0]
      else:
        top = ""
      if not top or top == "index.html":
        return indexpage.ProjectsIndex(self.potree, session)
      else:
        projectcode = top
        if not self.potree.hasproject(None, projectcode):
          return None
        pathwords = pathwords[1:]
        if pathwords:
          top = pathwords[0]
        else:
          top = ""
        if not top or top == "index.html":
          return indexpage.ProjectLanguageIndex(self.potree, projectcode, session)
        elif top == "admin.html":
          return adminpages.ProjectAdminPage(self.potree, projectcode, session, argdict)
    elif top == "languages":
      pathwords = pathwords[1:]
      if pathwords:
        top = pathwords[0]
      else:
        top = ""
      if not top or top == "index.html":
        return indexpage.LanguagesIndex(self.potree, session)
    elif top == "home":
      pathwords = pathwords[1:]
      if pathwords:
        top = pathwords[0]
      else:
        top = ""
      if not session.isopen:
        templatename = "redirect"
        templatevars = {
            "pagetitle": session.localize("Redirecting to login..."),
            "refresh": 1,
            "refreshurl": "login.html",
            "message": session.localize("Need to log in to access home page"),
            }
        pagelayout.completetemplatevars(templatevars, session)
        return server.Redirect("../login.html", withtemplate=(templatename, templatevars))
      if not top or top == "index.html":
        return indexpage.UserIndex(self.potree, session)
      elif top == "options.html":
        message = None
        try:
          if "changeoptions" in argdict:
            session.setoptions(argdict)
          if "changepersonal" in argdict:
            session.setpersonaloptions(argdict)
            message = session.localize("Personal details updated")
          if "changeinterface" in argdict:
            session.setinterfaceoptions(argdict)
        except users.RegistrationError, errormessage:
          message = errormessage
        return users.UserOptions(self.potree, session, message)
    elif top == "admin":
      pathwords = pathwords[1:]
      if pathwords:
        top = pathwords[0]
      else:
        top = ""
      if not session.isopen:
        templatename = "redirect"
        templatevars = {
            "pagetitle": session.localize("Redirecting to login..."),
            "refresh": 1,
            "refreshurl": "login.html",
            "message": session.localize("Need to log in to access admin page"),
            }
        pagelayout.completetemplatevars(templatevars, session)
        return server.Redirect("../login.html", withtemplate=(templatename, templatevars))
      if not session.issiteadmin():
        templatename = "redirect"
        templatevars = {
            "pagetitle": session.localize("Redirecting to home..."),
            "refresh": 1,
            "refreshurl": "login.html",
            "message": self.localize("You do not have the rights to administer pootle."),
            }
        pagelayout.completetemplatevars(templatevars, session)
        return server.Redirect("../index.html", withtemplate=(templatename, templatevars))
      if not top or top == "index.html":
        if "changegeneral" in argdict:
          self.changeoptions(argdict)
        return adminpages.AdminPage(self.potree, session, self.instance)
      elif top == "users.html":
        if "changeusers" in argdict:
          self.changeusers(session, argdict)
        return adminpages.UsersAdminPage(self, session.loginchecker.users, session, self.instance)
      elif top == "languages.html":
        if "changelanguages" in argdict:
          self.potree.changelanguages(argdict)
        return adminpages.LanguagesAdminPage(self.potree, session, self.instance)
      elif top == "projects.html":
        if "changeprojects" in argdict:
          self.potree.changeprojects(argdict)
        return adminpages.ProjectsAdminPage(self.potree, session, self.instance)
    elif top == "templates" or self.potree.haslanguage(top):
      languagecode = top
      pathwords = pathwords[1:]
      if pathwords:
        top = pathwords[0]
        bottom = pathwords[-1]
      else:
        top = ""
        bottom = ""
      if not top or top == "index.html":
        return indexpage.LanguageIndex(self.potree, languagecode, session)
      if self.potree.hasproject(languagecode, top):
        projectcode = top
        project = self.potree.getproject(languagecode, projectcode)
        pathwords = pathwords[1:]
        if pathwords:
          top = pathwords[0]
        else:
          top = ""
        if not top or top == "index.html":
          return indexpage.ProjectIndex(project, session, argdict)
        elif top == "admin.html":
          return adminpages.TranslationProjectAdminPage(self.potree, project, session, argdict)
        elif bottom == "translate.html":
          if len(pathwords) > 1:
            dirfilter = os.path.join(*pathwords[:-1])
          else:
            dirfilter = ""
          try:
            return translatepage.TranslatePage(project, session, argdict, dirfilter)
          except projects.RightsError, stoppedby:
            argdict["message"] = str(stoppedby)
            return indexpage.ProjectIndex(project, session, argdict, dirfilter)
        elif bottom == "spellcheck.html":
          # the full review page
          argdict["spellchecklang"] = languagecode
          return spellui.SpellingReview(session, argdict, js_url="/js/spellui.js")
        elif bottom == "spellingstandby.html":
          # a simple 'loading' page
          return spellui.SpellingStandby()
        elif bottom.endswith("." + project.fileext):
          pofilename = os.path.join(*pathwords)
          if argdict.get("translate", 0):
            try:
              return translatepage.TranslatePage(project, session, argdict, dirfilter=pofilename)
            except projects.RightsError, stoppedby:
              if len(pathwords) > 1:
                dirfilter = os.path.join(*pathwords[:-1])
              else:
                dirfilter = ""
              argdict["message"] = str(stoppedby)
              return indexpage.ProjectIndex(project, session, argdict, dirfilter=dirfilter)
          elif argdict.get("index", 0):
            return indexpage.ProjectIndex(project, session, argdict, dirfilter=pofilename)
          else:
            pofile = project.getpofile(pofilename, freshen=False)
            page = widgets.SendFile(pofile.filename)
            page.etag = str(pofile.pomtime)
            encoding = getattr(pofile, "encoding", "UTF-8")
            page.content_type = "text/plain; charset=%s" % encoding
            return page
        elif bottom.endswith(".csv") or bottom.endswith(".xlf") or bottom.endswith(".ts") or bottom.endswith(".po") or bottom.endswith(".mo"):
          destfilename = os.path.join(*pathwords)
          basename, extension = os.path.splitext(destfilename)
          pofilename = basename + os.extsep + project.fileext
          extension = extension[1:]
          if extension == "mo":
            if not "pocompile" in project.getrights(session):
              return None
          etag, filepath_or_contents = project.convert(pofilename, extension)
          if etag:
            page = widgets.SendFile(filepath_or_contents)
            page.etag = str(etag)
          else:
            page = widgets.PlainContents(filepath_or_contents)
          if extension == "po":
            page.content_type = "text/x-gettext-translation; charset=UTF-8"
          elif extension == "csv":
            page.content_type = "text/csv; charset=UTF-8"
          elif extension == "xlf":
            page.content_type = "application/x-xliff; charset=UTF-8"
          elif extension == "ts":
            page.content_type = "application/x-linguist; charset=UTF-8"
          elif extension == "mo":
            page.content_type = "application/x-gettext-translation"
          return page
        elif bottom.endswith(".zip"):
          if not "archive" in project.getrights(session):
            return None
          if len(pathwords) > 1:
            dirfilter = os.path.join(*pathwords[:-1])
          else:
            dirfilter = None
          goal = argdict.get("goal", None)
          if goal:
            goalfiles = project.getgoalfiles(goal)
            pofilenames = []
            for goalfile in goalfiles:
              pofilenames.extend(project.browsefiles(goalfile))
          else:
            pofilenames = project.browsefiles(dirfilter)
          archivecontents = project.getarchive(pofilenames)
          page = widgets.PlainContents(archivecontents)
          page.content_type = "application/zip"
          return page
        elif bottom.endswith(".sdf") or bottom.endswith(".sgi"):
          if not "pocompile" in project.getrights(session):
            return None
          oocontents = project.getoo()
          page = widgets.PlainContents(oocontents)
          page.content_type = "text/tab-seperated-values"
          return page
        elif bottom == "index.html":
          if len(pathwords) > 1:
            dirfilter = os.path.join(*pathwords[:-1])
          else:
            dirfilter = None
          return indexpage.ProjectIndex(project, session, argdict, dirfilter)
        else:
          return indexpage.ProjectIndex(project, session, argdict, os.path.join(*pathwords))
    return None
 
class PootleOptionParser(simplewebserver.WebOptionParser):
  def __init__(self):
    versionstring = "%%prog %s\njToolkit %s\nTranslate Toolkit %s\nKid %s\nElementTree %s\nPython %s (on %s/%s)" % (pootleversion.ver, jtoolkitversion.ver, toolkitversion.ver, kid.__version__, ElementTree.VERSION, sys.version, sys.platform, os.name)
    simplewebserver.WebOptionParser.__init__(self, version=versionstring)
    self.set_default('prefsfile', filelocations.prefsfile)
    self.set_default('instance', 'Pootle')
    self.set_default('htmldir', filelocations.htmldir)
    self.add_option('', "--refreshstats", dest="action", action="store_const", const="refreshstats",
        default="runwebserver", help="refresh the stats files instead of running the webserver")
    psycomodes=["none", "full", "profile"]
    self.add_option('', "--statsdb_file", action="store", type="string", dest="statsdb_file",
                    default=None, help="Specifies the location of the SQLite stats db file.")
    self.add_option('', "--profile", action="store", type="string", dest="profile",
                    help="Perform profiling, storing the result to the supplied filename.")
    try:
      import psyco
      self.add_option('', "--psyco", dest="psyco", default=None, choices=psycomodes, metavar="MODE",
                      help="use psyco to speed up the operation, modes: %s" % (", ".join(psycomodes)))
    except ImportError, e:
      return
 
def checkversions():
  """Checks that version dependencies are met"""
  if not hasattr(toolkitversion, "build") or toolkitversion.build < 11000:
    raise RuntimeError("requires Translate Toolkit version >= 1.1. Current installed version is: %s" % toolkitversion.ver)
 
def usepsyco(options):
  # options.psyco == None means the default, which is "full", but don't give a warning...
  # options.psyco == "none" means don't use psyco at all...
  if getattr(options, "psyco", "none") == "none":
    return
  try:
    import psyco
  except ImportError:
    if options.psyco is not None:
      optrecurse.RecursiveOptionParser(formats={}).warning("psyco unavailable", options, sys.exc_info())
    return
  if options.psyco is None:
    options.psyco = "full"
  if options.psyco == "full":
    psyco.full()
  elif options.psyco == "profile":
    psyco.profile()
  # tell psyco the functions it cannot compile, to prevent warnings
  import encodings
  psyco.cannotcompile(encodings.search_function)
 
def profile_runner(server, options):
  import cProfile
  import Pootle.profiling.lsprofcalltree as lsprofcalltree
 
  def write_cache_grind(profiler, file):
    k_cache_grind = lsprofcalltree.KCacheGrind(profiler)
    k_cache_grind.output(file)
    file.close()
 
  def do_profile_run(file):
    profiler = cProfile.Profile()
    try:
      profiler.runcall(simplewebserver.run, server, options)
    finally:
      write_cache_grind(profiler, file)
  
  try:
    profile_file = open(options.profile, "w+")
    do_profile_run(profile_file)
  except IOError, _e:
    print "Could not open profiling file %s" % (options.profile,)
 
def get_runner(options):
  if getattr(options, "profile", None) != None:
    return profile_runner
  else:
    return simplewebserver.run
 
def set_stats_db(server, options):
  def get_stats():
    if options.statsdb_file != None:
      return options.statsdb_file
    else:
      return getattr(server.instance, 'stats_db', None)
  
  statistics.STATS_DB_FILE = get_stats()
 
def main():
  # run the web server
  checkversions()
  parser = PootleOptionParser()
  options, args = parser.parse_args()
  options.errorlevel = options.logerrors
  usepsyco(options)
  if options.action != "runwebserver":
    options.servertype = "dummy"
  server = parser.getserver(options)
  set_stats_db(server, options)
  server.options = options
  if options.action == "runwebserver":
    run = get_runner(options)
    run(server, options)
  elif options.action == "refreshstats":
    server.refreshstats(args)
 
if __name__ == '__main__':
  main()