~ubuntu-branches/ubuntu/saucy/trac-xmlrpc/saucy

« back to all changes in this revision

Viewing changes to trunk/tracrpc/util.py

  • Committer: Package Import Robot
  • Author(s): W. Martin Borgert, Jakub Wilk
  • Date: 2011-12-31 18:27:57 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20111231182757-oe70jynzu0lk5hny
Tags: 1.1.2+r10706-1
* New upstream version (Closes: #653726). Works with Trac 0.12.
* Fixed lintians.
  [Jakub Wilk <jwilk@debian.org>]
* Replace deprecated > operator with >=.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
(c) 2009      ::: www.CodeResort.com - BV Network AS (simon-code@bvnetwork.no)
7
7
"""
8
8
 
9
 
import datetime
10
 
import time
11
 
import xmlrpclib
12
 
 
13
 
from trac.util.datefmt import utc
14
 
 
15
 
### PUBLIC
16
 
 
17
 
def to_xmlrpc_datetime(dt):
18
 
    """ Convert a datetime.datetime object to a xmlrpclib DateTime object """
19
 
    return xmlrpclib.DateTime(dt.utctimetuple())
20
 
 
21
 
def from_xmlrpc_datetime(data):
22
 
    """Return datetime (in utc) from XMLRPC datetime string (is always utc)"""
23
 
    t = list(time.strptime(data.value, "%Y%m%dT%H:%M:%S")[0:6])
24
 
    return apply(datetime.datetime, t, {'tzinfo': utc})
25
 
 
26
 
### INTERNAL / COMPAT
 
9
from trac.util.compat import any
 
10
 
 
11
try:
 
12
  from cStringIO import StringIO
 
13
except ImportError:
 
14
  from StringIO import StringIO
27
15
 
28
16
try:
29
17
    # Method only available in Trac 0.11.3 or higher
38
26
            message = '\n%s\n%s' % (to_unicode('\n'.join(traceback_only)),
39
27
                                        message)
40
28
        return message
 
29
 
 
30
try:
 
31
    # Constant available from Trac 0.12dev r8612
 
32
    from trac.util.text import empty
 
33
except ImportError:
 
34
    empty = None
 
35
 
 
36
def accepts_mimetype(req, mimetype):
 
37
    if isinstance(mimetype, basestring):
 
38
      mimetype = (mimetype,)
 
39
    accept = req.get_header('Accept')
 
40
    if accept is None :
 
41
        # Don't make judgements if no MIME type expected and method is GET
 
42
        return req.method == 'GET'
 
43
    else :
 
44
        accept = accept.split(',')
 
45
        return any(x.strip().startswith(y) for x in accept for y in mimetype)
 
46
 
 
47
def prepare_docs(text, indent=4):
 
48
    r"""Remove leading whitespace"""
 
49
    return ''.join(l[indent:] for l in text.splitlines(True))
 
50
 
 
51
try:
 
52
    # Micro-second support added to 0.12dev r9210
 
53
    from trac.util.datefmt import to_utimestamp, from_utimestamp
 
54
except ImportError:
 
55
    from trac.util.datefmt import to_timestamp, to_datetime, utc
 
56
    to_utimestamp = to_timestamp
 
57
    from_utimestamp = lambda x: to_datetime(x, utc)