#!/usr/bin/env python
# CCS Computer Science
#   Change Full Name
#
# Douglas Thrift
#
# $Id$

from __future__ import with_statement
import admin
import common
import ldap
import psycopg2
import re
import sys
import users

if sys.hexversion >= 0x2060000:
	import warnings

	with warnings.catch_warnings():
		warnings.filterwarnings('ignore', 'the sets module is deprecated', DeprecationWarning)

		import MySQLdb
else:
	import MySQLdb

if __name__ == '__main__':
	try:
		user = ''
		string = re.compile('^[a-z0-9]{1,16}$')

		while string.match(user) is None:	
			user = raw_input('User Name: ')

		if users.user(user):
			admin.error('User exists: %s.' % user)

		name = ''
		string = re.compile('^[^:]+$')

		while string.match(name) is None:
			name = raw_input('Full Name: ')

		password = common.new_password()

		if admin.master():
			admin.adduser(user, name, password)

		admin.run(common.add_all(user, name, password))
	except (common.PasswordError, ldap.LDAPError, OSError, MySQLdb.MySQLError, psycopg2.Error, psycopg2.Warning), error:
		admin.error(error)
	except (EOFError, KeyboardInterrupt):
		admin.eof()
