moksha/connector/utils.py | 17 +++++--- moksha/lib/helpers.py | 92 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 91 insertions(+), 18 deletions(-)
New commits: commit f3465b6479cffa3eb2185e9945945f610961baac Author: Luke Macken lmacken@redhat.com Date: Wed May 13 06:45:07 2009 -0400
Add a deprecation warning to the moksha.connector.utils.DateTimeDisplay object
diff --git a/moksha/connector/utils.py b/moksha/connector/utils.py index 91895ad..168b9de 100644 --- a/moksha/connector/utils.py +++ b/moksha/connector/utils.py @@ -16,17 +16,22 @@ # # Authors: John (J5) Palmieri johnp@redhat.com
-from datetime import datetime -from UserDict import DictMixin - import time import bisect +import warnings + +from datetime import datetime +from UserDict import DictMixin
class DateTimeDisplay(object): """DateTimeDisplay is an object which takes any number of datetime objects and process them for display. """ def __init__(self, *datetime_args): + warnings.warn("moksha.connectors.utils.DateTimeDisplay has been " + "deprecated, use moksha.lib.helpers.DateTimeDisplay " + "instead.", DeprecationWarning) + # All dates are sorted from latest to earliest self._datetime_ordered_list = [] for dt in datetime_args: diff --git a/moksha/lib/helpers.py b/moksha/lib/helpers.py index a78a0c6..120cdb6 100644 --- a/moksha/lib/helpers.py +++ b/moksha/lib/helpers.py @@ -920,6 +920,7 @@ def utc_offset(tz): # utc_offset += '.%d' % ... return utc_offset
+ class DateTimeDisplay(object): """ DateTimeDisplay is an object which takes any number of datetime objects
commit c717becf37f6b47367158ed9ebfed31386ebf2bf Author: Luke Macken lmacken@redhat.com Date: Wed May 13 06:37:53 2009 -0400
Clean up our helpers module, and add a new DateTimeDisplay object.
diff --git a/moksha/lib/helpers.py b/moksha/lib/helpers.py index 6913f14..a78a0c6 100644 --- a/moksha/lib/helpers.py +++ b/moksha/lib/helpers.py @@ -14,18 +14,9 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see http://www.gnu.org/licenses/.
-from webhelpers import date, feedgenerator, html, number, misc, text - -from repoze.what.predicates import (Not, Predicate, All, Any, has_all_permissions, - has_any_permission, has_permission, - in_all_groups, in_any_group, in_group, - is_user, not_anonymous) - -from repoze.what.authorize import check_authorization, NotAuthorizedError - -from webob import Request - +import moksha import copy +import time import pylons import urllib import uuid @@ -34,9 +25,20 @@ import re import os import logging
+from pytz import timezone, utc from webob import Request from decorator import decorator -import moksha +from datetime import datetime +from webhelpers import date, feedgenerator, html, number, misc, text +from webhelpers.date import distance_of_time_in_words +from repoze.what.authorize import check_authorization, NotAuthorizedError +from repoze.what.predicates import (Not, Predicate, All, Any, + has_all_permissions, has_any_permission, + has_permission, in_all_groups, + in_any_group, in_group, is_user, + not_anonymous) + +from moksha.exc import MokshaConfigNotFound
log = logging.getLogger(__name__) scrub_filter = re.compile('[^_a-zA-Z0-9-]') @@ -896,8 +898,13 @@ def strip_script(environ): return path
-def utc_offset(cls, tz): - """ Return the UTC offset for a given timezone """ +def utc_offset(tz): + """ Return the UTC offset for a given timezone. + + >>> utc_offset('US/Eastern') + '-4' + + """ utc_offset = '' now = datetime.now(utc) now = now.astimezone(timezone(tz)) @@ -912,3 +919,45 @@ def utc_offset(cls, tz): #if minutes: # utc_offset += '.%d' % ... return utc_offset + +class DateTimeDisplay(object): + """ + DateTimeDisplay is an object which takes any number of datetime objects + and process them for display:: + + >>> from datetime import datetime + >>> now = datetime(2009, 5, 12) + >>> later = datetime(2009, 5, 13) + >>> d = DateTimeDisplay(now) + >>> d.age(later) + '1 day' + >>> d.age(datetime(2010, 7, 10, 10, 10), granularity='minute') + '1 year, 1 month, 29 days, 10 hours and 10 minutes' + + """ + def __init__(self, timestamp, format='%Y-%m-%d %H:%M:%S'): + self.timestamp = timestamp + if isinstance(timestamp, datetime): + self.datetime = timestamp + elif isinstance(timestamp, basestring): + if hasattr(datetime, 'strptime'): # Python 2.5+ + self.datetime = datetime.strptime(timestamp, format) + else: # Python 2.4 + self.datetime = datetime(*time.strptime(timestamp, format)[:-2]) + else: + raise Exception("You must provide either a datetime object or a" + "string, not %s" % type(timestamp)) + + def age(self, end=None, tz=None, granularity='hour'): + """ + Return the distance of time in words from `self.datetime` to `end`. + """ + if not end: + if tz: + end = datetime.now(utc) + end = end.astimezone(timezone(tz)) + else: + end = datetime.utcnow() + + return distance_of_time_in_words(self.datetime, end, + granularity=granularity)
commit e03eafc95554318b8acca957a9555328a8cf053d Author: Luke Macken lmacken@redhat.com Date: Wed May 13 06:30:06 2009 -0400
Add a utc_offset method to moksha.lib.helpers
diff --git a/moksha/lib/helpers.py b/moksha/lib/helpers.py index 40ca0d8..6913f14 100644 --- a/moksha/lib/helpers.py +++ b/moksha/lib/helpers.py @@ -894,3 +894,21 @@ def strip_script(environ): if path.startswith(prefix): path = path[len(prefix):] return path + + +def utc_offset(cls, tz): + """ Return the UTC offset for a given timezone """ + utc_offset = '' + now = datetime.now(utc) + now = now.astimezone(timezone(tz)) + offset = now.strftime('%z') + if offset.startswith('-'): + offset = offset[1:] + utc_offset += '-' + hours = int(offset[:2]) + utc_offset += str(hours) + # FIXME: account for minutes? + #minutes = int(offset[2:]) + #if minutes: + # utc_offset += '.%d' % ... + return utc_offset
commit 12da5287a37613862e2ec6e2fe7e67b3a3bd7981 Merge: d22b992... 016266e... Author: Luke Macken lmacken@redhat.com Date: Tue May 12 15:47:53 2009 -0400
Merge branch 'master' of git+ssh://git.fedorahosted.org/git/moksha
commit d22b99237f6647bf416d34b1fa84c351714e39f6 Author: Luke Macken lmacken@redhat.com Date: Tue May 12 15:47:49 2009 -0400
Fix broken tabbing
diff --git a/moksha/connector/utils.py b/moksha/connector/utils.py index 0de0b90..91895ad 100644 --- a/moksha/connector/utils.py +++ b/moksha/connector/utils.py @@ -260,9 +260,9 @@ class WeightedSearch(object): HEAVY_WEIGHT = 100
def __init__(self, search_func, cols, cache=None): - self.search_func = search_func - self.cache = cache - self.cols = cols + self.search_func = search_func + self.cache = cache + self.cols = cols
def weigh(self, search_term, weighted_item): search_term_len = len(search_term)
moksha-commits@lists.fedorahosted.org