Erik Grinaker is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this againRevelation / src / lib / misc.py
- Tag
- revelation-0.3.2
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 | #
# Revelation 0.3.2 - a password manager for GNOME 2
# http://oss.codepoet.no/revelation/
# $Id$
#
# Module with miscellaneous functionality
#
#
# Copyright (c) 2003-2004 Erik Grinaker
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
import time, random, gconf, gtk
def escape_markup(string):
"Escapes a string so it can be placed in a markup string"
string = string.replace("&", "&")
string = string.replace("<", "<")
string = string.replace(">", ">")
return string
def generate_password():
"Generates a random password"
def get_random_item(list):
return list[int(random.random() * len(list))]
client = gconf.client_get_default()
pwlen = client.get_int("/apps/revelation/passwordgen/length")
# set up list of chars to exclude
exclude = []
if client.get_bool("/apps/revelation/passwordgen/avoid_ambiguous") == gtk.TRUE:
exclude = [ "0", "O", "I", "l", "1", "S", "5", "q", "g" ]
# calculate the minimum share of the password for each character class
sharedigits = int(round(pwlen * 0.15))
sharechars = int(round(pwlen * 0.24))
sharecaps = int(round(pwlen * 0.24))
# set up a set of all usable ascii characters
digits = range(48, 58)
chars = range(65, 91)
caps = range(97, 123)
full = []
full.extend(digits)
full.extend(chars)
full.extend(caps)
# generate characters for the password
pwchars = []
while len(pwchars) < pwlen:
# get digits
if len(pwchars) < sharedigits:
char = chr(get_random_item(digits))
# get chars
elif len(pwchars) < sharechars + sharedigits:
char = chr(get_random_item(chars))
# get caps
elif len(pwchars) < sharecaps + sharechars + sharedigits:
char = chr(get_random_item(caps))
# get random characters
else:
char = chr(get_random_item(full))
if char not in exclude:
pwchars.append(char)
# shuffle the password
password = ""
for i in range(len(pwchars)):
password += pwchars.pop(int(random.random() * len(pwchars)))
return password
def timediff_simple(start, end = None):
"Returns an approximate time difference in human-readable format"
if end is None:
end = time.time()
if end < start:
return None
diff = int(end - start)
gmstart = time.gmtime(start)
gmend = time.gmtime(end)
# get a human-readable time period
if diff >= int(365.25 * 24 * 60 * 60):
period = int(diff / 365.25 / 24 / 60 / 60)
unit = "year"
elif gmend.tm_mon - gmstart.tm_mon >= 2 or (gmend.tm_mon - gmstart.tm_mon == 1 and gmend.tm_mday >= gmstart.tm_mday):
period = gmend.tm_mon - gmstart.tm_mon
unit = "month"
elif diff >= 7 * 24 * 60 * 60:
period = diff / 7 / 24 / 60 / 60
unit = "week"
elif diff >= 24 * 60 * 60:
period = diff / 24 / 60 / 60
unit = "day"
elif diff >= 60 * 60:
period = diff / 60 / 60
unit = "hour"
elif diff >= 60:
period = diff / 60
unit = "minute"
else:
period = diff
unit = "second"
if period == 1:
return str(period) + " " + unit
else:
return str(period) + " " + unit + "s"
|