This repository has been archived by the owner on Nov 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
95 lines (74 loc) · 2.58 KB
/
tools.py
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
# -*- coding: utf-8 -*-
# vim:tw=80:ts=2:sw=2:colorcolumn=81:nosmartindent
from ConfigParser import ConfigParser
import os
import email.header, email.mime.nonmultipart, logging
__version__ = '0.0.1'
class DMSConfigParser(ConfigParser):
def get(self, section, option):
""" Get a parameter if the returning value is a list, convert string value
to a python list. This code is taken from
http://stackoverflow.com/a/335754/61855"""
value = ConfigParser.get(self, section, option)
if ((value!=None) and (value!='') and
(value[0] == "[") and (value[-1] == "]")):
return eval(value)
else:
return value
def getDefaultPath(n):
""" Returns the absolute path of a file which resides in the script
directory"""
return os.path.join(os.path.split(os.path.realpath(__file__))[0],n)
def decodeHeader(s):
"""Decodes a encoded header. This code is taken partly from
http://stackoverflow.com/a/7331577/61855"""
dh = email.header.decode_header(s)
return ' '.join([ unicode(t[0], t[1] or 'ASCII') for t in dh ])
class MIMEUTF8QPText(email.mime.nonmultipart.MIMENonMultipart):
def __init__(self, payload):
email.mime.nonmultipart.MIMENonMultipart.__init__(self, 'text', 'plain',
charset='utf-8')
utf8qp=email.charset.Charset('utf-8')
utf8qp.body_encoding=email.charset.QP
self.set_payload(payload, charset=utf8qp)
def getCompleter(tags):
logging.debug('Returning Completer for tags %s', tags)
def complete(text, state):
l=[x+" " for x in tags if x.lower().startswith(text.lower())]+[None]
return l[state]
return complete
import time as _time
from datetime import timedelta, tzinfo
ZERO = timedelta(0)
HOUR = timedelta(hours=1)
STDOFFSET = timedelta(seconds = -_time.timezone)
if _time.daylight:
DSTOFFSET = timedelta(seconds = -_time.altzone)
else:
DSTOFFSET = STDOFFSET
DSTDIFF = DSTOFFSET - STDOFFSET
class LocalTimezone(tzinfo):
def utcoffset(self, dt):
if self._isdst(dt):
return DSTOFFSET
else:
return STDOFFSET
def dst(self, dt):
if self._isdst(dt):
return DSTDIFF
else:
return ZERO
def tzname(self, dt):
return _time.tzname[self._isdst(dt)]
def _isdst(self, dt):
tt = (dt.year, dt.month, dt.day,
dt.hour, dt.minute, dt.second,
dt.weekday(), 0, 0)
stamp = _time.mktime(tt)
tt = _time.localtime(stamp)
return tt.tm_isdst > 0
Local = LocalTimezone()
def formatDate(d, localtime=False, usegmt=False):
t = d.timetuple()
ts = _time.mktime(t)
return email.utils.formatdate(ts, localtime, usegmt)