wimd_pwfile.py :  » GUI » PmwContribD » PmwContribD-r2_0_2 » SampleApps » WhackyChat » Server » 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 » GUI » PmwContribD 
PmwContribD » PmwContribD r2_0_2 » SampleApps » WhackyChat » Server » wimd_pwfile.py
#!/usr/bin/env python
#
# $Id: wimd_pwfile.py,v 1.4 2001/11/03 11:05:22 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
#                         All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

"""Read a WhackyChat server password file.

"""

__rcs_info__ = {
    #
    #  Creation Information
    #
    'module_name'  : '$RCSfile: wimd_pwfile.py,v $',
    'rcs_id'       : '$Id: wimd_pwfile.py,v 1.4 2001/11/03 11:05:22 doughellmann Exp $',
    'creator'      : 'Doug Hellmann <doug@hellfly.net>',
    'project'      : 'UNSPECIFIED',
    'created'      : 'Sun, 01-Apr-2001 13:26:16 EDT',

    #
    #  Current Information
    #
    'author'       : '$Author: doughellmann $',
    'version'      : '$Revision: 1.4 $',
    'date'         : '$Date: 2001/11/03 11:05:22 $',
}

#
# Import system modules
#
import string
import UserDict
import crypt
import whrandom


#
# Import Local modules
#


#
# Module
#


class wimd_pwfile(UserDict.UserDict):
    """
    This class provides easy access to a wimd
    style password file.
    """
    #
    # Define the "enum" to get to various fields.
    #
    MONIKER, PASSWORD, UID, GROUP, UNKNOWN, EMAIL = range(6)
    
    def __init__(self, filename):
        print 'reading password file %s' % filename
        self.filename = filename
        self.random_generator = whrandom.whrandom()
        UserDict.UserDict.__init__(self)
        self.parse_file()
        return

    def parse_file(self):
        file = open(self.filename, 'rt')
        contents = file.readlines()
        user_info = {}
        for line in contents:
            line = string.strip(line)
            if line and line[0] != '#':
                parts = string.split(line, ':')
                user_info[parts[2]] = parts
        self.data = user_info
        file.close()
        return

    def store(self):
        """
        Store the info we have back to the file
        from which we read it.
        """
        output=open(self.filename, 'wt')
        uids = self.keys()
        uids.sort()
        for uid in uids:
            output.write('%s\n' % string.join(self[uid], ':'))
        output.close()
        return
    
    #
    # Methods to return a particular piece of information
    # about a user
    #
    def get_field(self, uid, field):
        return self[uid][field]
    def get_moniker(self, uid):
        return self.get_field(uid, self.MONIKER)
    def get_password(self, uid):
        return self.get_field(uid, self.PASSWORD)
    def get_group(self, uid):
        return self.get_field(uid, self.GROUP)
    def get_unknown(self, uid):
        return self.get_field(uid, self.UNKNOWN)
    def get_email(self, uid):
        return self.get_field(uid, self.EMAIL)
    #
    # Methods to set a particular piece of information
    # about a user
    #
    def set_field(self, uid, field, value):
        rec = self[uid]
        new_rec = tuple(rec[:field]) + (value,) + tuple(rec[field+1:])
        self[uid] = new_rec
        return
    def set_moniker(self, uid, value):
        return self.set_field(uid, self.MONIKER, value)
    def set_password(self, uid, value):
        salt = self.random_generator.choice(string.letters) + \
               self.random_generator.choice(string.letters)
        value = crypt.crypt(value, salt)
        return self.set_field(uid, self.PASSWORD, value)
    def set_group(self, uid, value):
        return self.set_field(uid, self.GROUP, value)
    def set_unknown(self, uid, value):
        return self.set_field(uid, self.UNKNOWN, value)
    def set_email(self, uid, value):
        return self.set_field(uid, self.EMAIL, value)

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