/loggerhead/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/loggerhead/trunk

« back to all changes in this revision

Viewing changes to loggerhead/util.py

  • Committer: Martin Pool
  • Date: 2011-11-23 08:33:12 UTC
  • mto: This revision was merged to the branch mainline in revision 461.
  • Revision ID: mbp@canonical.com-20111123083312-4stzhfv5843jxv2k
Bump version to 1.18.1; compatible with bzr 2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#
17
17
# You should have received a copy of the GNU General Public License
18
18
# along with this program; if not, write to the Free Software
19
 
# Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335  USA
 
19
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
20
#
21
21
 
22
22
import base64
35
35
except ImportError:
36
36
    from elementtree import ElementTree as ET
37
37
 
38
 
from breezy import urlutils
 
38
from bzrlib import urlutils
39
39
 
40
40
from simpletal.simpleTALUtils import HTMLStructureCleaner
41
41
 
52
52
# Display of times.
53
53
 
54
54
# date_day -- just the day
55
 
# date_time -- full date with time (UTC)
 
55
# date_time -- full date with time
56
56
#
 
57
# displaydate -- for use in sentences
57
58
# approximatedate -- for use in tables
58
59
#
59
 
# approximatedate return an elementtree <span> Element
60
 
# with the full date (UTC) in a tooltip.
 
60
# displaydate and approximatedate return an elementtree <span> Element
 
61
# with the full date in a tooltip.
61
62
 
62
63
 
63
64
def date_day(value):
66
67
 
67
68
def date_time(value):
68
69
    if value is not None:
69
 
        # Note: this assumes that the value is UTC in some fashion.
70
 
        return value.strftime('%Y-%m-%d %H:%M:%S UTC')
 
70
        return value.strftime('%Y-%m-%d %H:%M:%S')
71
71
    else:
72
72
        return 'N/A'
73
73
 
74
74
 
 
75
def _displaydate(date):
 
76
    delta = abs(datetime.datetime.now() - date)
 
77
    if delta > datetime.timedelta(1, 0, 0):
 
78
        # far in the past or future, display the date
 
79
        return 'on ' + date_day(date)
 
80
    return _approximatedate(date)
 
81
 
 
82
 
75
83
def _approximatedate(date):
76
84
    delta = datetime.datetime.now() - date
77
85
    if abs(delta) > datetime.timedelta(1, 0, 0):
118
126
    return _wrap_with_date_time_title(date, _approximatedate(date))
119
127
 
120
128
 
 
129
def displaydate(date):
 
130
    return _wrap_with_date_time_title(date, _displaydate(date))
 
131
 
 
132
 
121
133
class Container(object):
122
134
    """
123
135
    Convert a dict into an object with attributes.
214
226
def html_escape(s):
215
227
    """Transform dangerous (X)HTML characters into entities.
216
228
 
217
 
    Like cgi.escape, except also escaping \" and '. This makes it safe to use
 
229
    Like cgi.escape, except also escaping " and '. This makes it safe to use
218
230
    in both attribute and element content.
219
231
 
220
232
    If you want to safely fill a format string with escaped values, use
332
344
def human_size(size, min_divisor=0):
333
345
    size = int(size)
334
346
    if (size == 0) and (min_divisor == 0):
335
 
        return 'Empty'
336
 
    if (size < 1024) and (min_divisor == 0):
337
 
        return str(size) + ' bytes'
 
347
        return '0'
 
348
    if (size < 512) and (min_divisor == 0):
 
349
        return str(size)
338
350
 
339
351
    if (size >= P95_GIG) or (min_divisor >= GIG):
340
352
        divisor = GIG
355
367
    if (base < 100) and (dot != 0):
356
368
        out += '.%d' % (dot,)
357
369
    if divisor == KILO:
358
 
        out += ' KB'
 
370
        out += 'K'
359
371
    elif divisor == MEG:
360
 
        out += ' MB'
 
372
        out += 'M'
361
373
    elif divisor == GIG:
362
 
        out += ' GB'
 
374
        out += 'G'
363
375
    return out
364
376
 
365
377
 
472
484
    for index, dir_name in enumerate(dir_parts):
473
485
        inner_breadcrumbs.append({
474
486
            'dir_name': dir_name,
475
 
            'path': '/'.join(dir_parts[:index + 1]),
 
487
            'file_id': inv.path2id('/'.join(dir_parts[:index + 1])),
476
488
            'suffix': '/' + view,
477
489
        })
478
490
    return inner_breadcrumbs
543
555
#         for re-ordering an existing page by different sort
544
556
 
545
557
t_context = threading.local()
546
 
_valid = (
547
 
    'start_revid', 'filter_file_id', 'q', 'remember', 'compare_revid', 'sort')
 
558
_valid = ('start_revid', 'file_id', 'filter_file_id', 'q', 'remember',
 
559
          'compare_revid', 'sort')
548
560
 
549
561
 
550
562
def set_context(map):