diff options
| author | Barry Warsaw | 2011-04-01 18:56:17 -0400 |
|---|---|---|
| committer | Barry Warsaw | 2011-04-01 18:56:17 -0400 |
| commit | cd18f50624f33a9556492168933d5fe088d51a04 (patch) | |
| tree | cf9f9176e040f8af5b39240c0cd97c2b48c29952 /src/mailman/utilities | |
| parent | 477d81701e0e3ef166b786de9ec9bc1f148eaffe (diff) | |
| parent | 33ad44bc97f08df71f227f6f2a006e770a75c353 (diff) | |
| download | mailman-cd18f50624f33a9556492168933d5fe088d51a04.tar.gz mailman-cd18f50624f33a9556492168933d5fe088d51a04.tar.zst mailman-cd18f50624f33a9556492168933d5fe088d51a04.zip | |
Diffstat (limited to 'src/mailman/utilities')
| -rw-r--r-- | src/mailman/utilities/datetime.py | 11 | ||||
| -rw-r--r-- | src/mailman/utilities/uid.py | 67 |
2 files changed, 74 insertions, 4 deletions
diff --git a/src/mailman/utilities/datetime.py b/src/mailman/utilities/datetime.py index 7e727346d..9dcd21f1e 100644 --- a/src/mailman/utilities/datetime.py +++ b/src/mailman/utilities/datetime.py @@ -36,25 +36,27 @@ __all__ = [ import datetime +from mailman.testing.layers import MockAndMonkeyLayer + class DateFactory: """A factory for today() and now() that works with testing.""" - # Set to True to produce predictable dates and times. - testing_mode = False # The predictable time. predictable_now = None predictable_today = None def now(self, tz=None): + # We can't automatically fast-forward because some tests require us to + # stay on the same day for a while, e.g. autorespond.txt. return (self.predictable_now - if self.testing_mode + if MockAndMonkeyLayer.testing_mode else datetime.datetime.now(tz)) def today(self): return (self.predictable_today - if self.testing_mode + if MockAndMonkeyLayer.testing_mode else datetime.date.today()) @classmethod @@ -72,3 +74,4 @@ factory = DateFactory() factory.reset() today = factory.today now = factory.now +MockAndMonkeyLayer.register_reset(factory.reset) diff --git a/src/mailman/utilities/uid.py b/src/mailman/utilities/uid.py new file mode 100644 index 000000000..cc2cf3b12 --- /dev/null +++ b/src/mailman/utilities/uid.py @@ -0,0 +1,67 @@ +# Copyright (C) 2011 by the Free Software Foundation, Inc. +# +# This file is part of GNU Mailman. +# +# GNU Mailman 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 3 of the License, or (at your option) +# any later version. +# +# GNU Mailman 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 +# GNU Mailman. If not, see <http://www.gnu.org/licenses/>. + +"""Unique ID generation. + +Use these functions to create unique ids rather than inlining calls to hashlib +and whatnot. These are better instrumented for testing purposes. +""" + +from __future__ import absolute_import, unicode_literals + +__metaclass__ = type +__all__ = [ + 'UniqueIDFactory', + 'factory', + ] + + +import os +import time +import hashlib + +from mailman.testing.layers import MockAndMonkeyLayer +from mailman.utilities.passwords import SALT_LENGTH + + + +class UniqueIDFactory: + """A factory for unique ids.""" + + # The predictable id. + predictable_id = None + + def new_uid(self, bytes=None): + if MockAndMonkeyLayer.testing_mode: + uid = self.predictable_id + self.predictable_id += 1 + return unicode(uid) + salt = os.urandom(SALT_LENGTH) + h = hashlib.sha1(repr(time.time())) + h.update(salt) + if bytes is not None: + h.update(bytes) + return unicode(h.hexdigest(), 'us-ascii') + + def reset(self): + self.predictable_id = 1 + + + +factory = UniqueIDFactory() +factory.reset() +MockAndMonkeyLayer.register_reset(factory.reset) |
