python/types.py
changeset 0 6f7a81934006
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 # Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 #
       
     3 # This program is free software; you can redistribute it and/or modify
       
     4 # it under the terms of the GNU General Public License as published by
       
     5 # the Free Software Foundation; either version 2 of the License, or
       
     6 # (at your option) any later version.
       
     7 #
       
     8 # This program is distributed in the hope that it will be useful,
       
     9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 # GNU General Public License for more details.
       
    12 #
       
    13 # You should have received a copy of the GNU General Public License
       
    14 # along with this program; if not, write to the Free Software
       
    15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 import string
       
    18 import time
       
    19 
       
    20 class Limit:
       
    21     unlimited = -1
       
    22     def __init__(self, str):
       
    23         if str[0] == '-' or string.lower(str) == 'unlimited':
       
    24             str = self.unlimited
       
    25         self.value = int(str)
       
    26     def __str__(self):
       
    27         if self.value == self.unlimited:
       
    28             return 'unlimited'
       
    29         return str(self.value)
       
    30     def __repr__(self):
       
    31         if self.value == self.unlimited:
       
    32             val = '-'
       
    33         else:
       
    34             val = str(self.value)
       
    35         return "Limit('%s')" % val
       
    36 
       
    37 class Flag:
       
    38     def __init__(self, value):
       
    39         self.value = value
       
    40     def __str__(self):
       
    41         if self.value:
       
    42             return 'true'
       
    43         return 'false'
       
    44     def __repr__(self):
       
    45         return "Flag(%s)" % repr(self.value)
       
    46     def html(self, fieldname):
       
    47         if self.value:
       
    48             strue = ' selected'
       
    49             sfalse = ''
       
    50         else:
       
    51             strue = ''
       
    52             sfalse = ' selected'
       
    53         return ("<select name='%s'>"
       
    54                 "<option value=1%s>True"
       
    55                 "<option value=0%s>False"
       
    56                 "</select>" % ( fieldname, strue, sfalse ) )
       
    57 
       
    58 class Time:
       
    59     def __init__(self, value):
       
    60         self.time = int(value)
       
    61     def __str__(self):
       
    62         return time.asctime(time.localtime(self.time))
       
    63     def __repr__(self):
       
    64         return 'Time(%d)' % self.time
       
    65 
       
    66 ATTR_MAILBOX_ENABLED = 8
       
    67 
       
    68 class VUser:
       
    69     # Default values
       
    70     mailbox_enabled = 1
       
    71     password = ''
       
    72     mailbox = ''
       
    73     forwards = []
       
    74     personal = ''
       
    75     hard_quota = -1
       
    76     soft_quota = -1
       
    77     message_size_limit = -1
       
    78     message_count_limit = -1
       
    79     creation_time = 0
       
    80     expiry_time = -1
       
    81     
       
    82     def __init__(self, bin=None):
       
    83         if bin:
       
    84             self.from_binary(bin)
       
    85 
       
    86     def from_binary(self, bin):
       
    87         if bin[0] <> chr(2):
       
    88             raise ValueError, "Virtual user data has invalid format number"
       
    89         i = 1
       
    90         while bin[i] <> chr(0):
       
    91             flag = ord(bin[i])
       
    92             val = ord(bin[i+1])
       
    93             i = i + 2
       
    94             if flag == ATTR_MAILBOX_ENABLED:
       
    95                 self.mailbox_enabled = Flag(val)
       
    96             else:
       
    97                 raise ValueError, "Invalid flag number %d in vuser data" % flag
       
    98         bin = string.split(bin[i+1:], '\0')
       
    99         self.password = bin[0]
       
   100         self.mailbox = bin[1]
       
   101         self.forwards = []
       
   102         i = 2
       
   103         while bin[i]:
       
   104             self.forwards.append(bin[i])
       
   105             i = i + 1
       
   106         self.personal = bin[i+1]
       
   107         self.hard_quota = Limit(bin[i+2])
       
   108         self.soft_quota = Limit(bin[i+3])
       
   109         self.message_size_limit = Limit(bin[i+4])
       
   110         self.message_count_limit = Limit(bin[i+5])
       
   111         self.creation_time = Time(bin[i+6])
       
   112         self.expiry_time = Limit(bin[i+7])
       
   113 
       
   114     def __getitem__(self, key):
       
   115         """Allow subscript access to the attributes of this object.
       
   116 
       
   117         This method allows the data in the virtual user to be
       
   118         accessed as though it were a dictionary.  This allows the use of
       
   119         such expressions as '%(mailbox)s'%user."""
       
   120         return getattr(self, key)
       
   121 
       
   122     def keys(self): return self.__dict__.keys()
       
   123     def values(self): return self.__dict__.values()
       
   124     def items(self): return self.__dict__.items()
       
   125     def dict(self): return self.__dict__
       
   126 
       
   127 class NamedVUser:
       
   128     def __init__(self, username, vuser):
       
   129         self.username = username
       
   130         self.vuser = vuser
       
   131     def __cmp__(self, other):
       
   132         return cmp(self.username, other.username)
       
   133     def __getitem__(self, key):
       
   134         if key == 'username':
       
   135             return self.username
       
   136         else:
       
   137             return self.vuser[key]
       
   138     def dict(self):
       
   139         d = self.vuser.dict()
       
   140         d['username'] = self.username
       
   141         return d