util.py :  » Email » BoboMail » bobomail » lib » Python Open Source

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

from htmlentitydefs import entitydefs
import urllib, string


exitlist = []
def atexit(func):
  global exitlist
  exitlist.append(func)

def exitall():
  for func in exitlist:
    func()

import sys
if hasattr(sys, "exitfunc"): atexit(sys.exitfunc)  
sys.exitfunc = exitall

def getusername(user):
  if user.host == "localhost":
    try:
      import pwd
      return string.split(pwd.getpwnam(user.id)[4], ",")[0]
    except: pass
  return ""


def urlquote(s): return urllib.quote_plus(s, "")
def urlunquote(s): return urllib.unquote_plus(s)

def quotedtext(text):
  def quotedline(line):
    return "> %s" % line
  return string.join(map(quotedline, string.split(text, "\n")), "\n")


replace_char = {"\n": "<br>\n", "\t": "&nbsp;" * 4}
for key, value in entitydefs.items():
    replace_char[value] = "&%s;" % key


def escape(s, spaces=0):
  nbsp = entitydefs["nbsp"]
  last = ""
  new = StringIO()
  for char in s:
    if spaces and char == " " and last in [" ", nbsp]:
      char = nbsp
    new.write(replace_char.get(char, char))
    last = char
  newstr = new.getvalue()
  if spaces and newstr and newstr[0] == " ":
      return "&nbsp;%s" % newstr[1:]
  return newstr


def path2pid(path):return string.join(map(str, path), ".")
def pid2path(pid): return map(int, string.split(pid, "."))



def bytesizestr(bytes):
  s = ""
  if bytes > 1024 * 1024:
    mb = bytes / (1024 * 1024)
    s = "%s MB" % mb
  elif bytes > 1024:
    kb = bytes / 1024
    s = "%s KB" % kb
  else:
    s = "%s" % bytes
  return s

           
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.