/usr/lib/python2.7/dist-packages/kopano/utils.py is in python-kopano 8.5.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | """
Part of the high-level python bindings for Kopano
Copyright 2005 - 2016 Zarafa and its licensors (see LICENSE file for details)
Copyright 2016 - Kopano and its licensors (see LICENSE file for details)
"""
import datetime
import struct
import sys
from MAPI import (
WrapCompressedRTFStream, PT_UNICODE, ROW_ADD, MAPI_MODIFY,
)
from MAPI.Defs import (
PROP_TYPE
)
from MAPI.Tags import (
PR_RTF_COMPRESSED, PR_ACL_TABLE, PR_MEMBER_ENTRYID, PR_MEMBER_RIGHTS
)
from MAPI.Tags import (
IID_IStream, IID_IECMessageRaw, IID_IExchangeModifyTable
)
from MAPI.Struct import (
MAPIErrorNotFound, MAPIErrorInterfaceNotSupported, SPropValue, ROWENTRY,
MAPIErrorNoAccess
)
from .compat import unhex as _unhex, hex as _hex
from .errors import Error, NotFoundError
if sys.hexversion >= 0x03000000:
from . import table as _table
from . import permission as _permission
from . import user as _user
from . import group as _group
else:
import table as _table
import permission as _permission
import user as _user
import group as _group
def stream(mapiobj, proptag):
stream = mapiobj.OpenProperty(proptag, IID_IStream, 0, 0)
if proptag == PR_RTF_COMPRESSED:
stream = WrapCompressedRTFStream(stream, 0)
block_size = 0x100000 # 1MB
data = []
while True:
temp = stream.Read(block_size)
data.append(temp)
if len(temp) < block_size:
break
data = b''.join(data)
if PROP_TYPE(proptag) == PT_UNICODE:
data = data.decode('utf-32le') # under windows them be utf-16le?
return data
def _openentry_helper(mapistore, entryid, flags): # avoid underwater action for archived items
try:
return mapistore.OpenEntry(entryid, IID_IECMessageRaw, flags)
except MAPIErrorInterfaceNotSupported:
return mapistore.OpenEntry(entryid, None, flags)
def openentry_raw(mapistore, entryid, flags): # try to open read/write, falling back to read-only
try:
return _openentry_helper(mapistore, entryid, flags | MAPI_MODIFY)
except MAPIErrorNoAccess:
return _openentry_helper(mapistore, entryid, flags)
def unpack_short(s, pos):
return struct.unpack_from('<H', s, pos)[0]
def unpack_long(s, pos):
return struct.unpack_from('<L', s, pos)[0]
def pack_long(i):
return struct.pack('<L', i)
def rectime_to_unixtime(t):
return (t - 194074560) * 60
def unixtime_to_rectime(t):
return int(t / 60) + 194074560
def permissions(obj):
try:
acl_table = obj.mapiobj.OpenProperty(PR_ACL_TABLE, IID_IExchangeModifyTable, 0, 0)
except MAPIErrorNotFound:
return
table = _table.Table(
obj.server,
obj.mapiobj,
acl_table.GetTable(0),
PR_ACL_TABLE,
)
for row in table.dict_rows():
yield _permission.Permission(acl_table, row, obj.server)
def permission(obj, member, create):
for permission in obj.permissions():
if permission.member == member:
return permission
if create:
acl_table = obj.mapiobj.OpenProperty(PR_ACL_TABLE, IID_IExchangeModifyTable, 0, 0)
if isinstance(member, _user.User): # XXX *.id_ or something..?
memberid = member.userid
elif isinstance(member, _group.Group):
memberid = member.groupid
else:
memberid = member.companyid
acl_table.ModifyTable(0, [ROWENTRY(ROW_ADD, [SPropValue(PR_MEMBER_ENTRYID, _unhex(memberid)), SPropValue(PR_MEMBER_RIGHTS, 0)])])
return obj.permission(member)
else:
raise NotFoundError("no permission entry for '%s'" % member.name)
def bytes_to_human(b):
suffixes = ['b', 'kb', 'mb', 'gb', 'tb', 'pb']
if b == 0: return '0 b'
i = 0
len_suffixes = len(suffixes) - 1
while b >= 1024 and i < len_suffixes:
b /= 1024
i += 1
f = ('%.2f' % b).rstrip('0').rstrip('.')
return '%s %s' % (f, suffixes[i])
def human_to_bytes(s):
"""
Author: Giampaolo Rodola' <g.rodola [AT] gmail [DOT] com>
License: MIT
"""
s = s.lower()
init = s
num = ""
while s and s[0:1].isdigit() or s[0:1] == '.':
num += s[0]
s = s[1:]
num = float(num)
letter = s.strip()
for sset in [('b', 'k', 'm', 'g', 't', 'p', 'e', 'z', 'y'),
('b', 'kb', 'mb', 'gb', 'tb', 'pb', 'eb', 'zb', 'yb'),
('b', 'kib', 'mib', 'gib', 'tib', 'pib', 'eib', 'zib', 'yib')]:
if letter in sset:
break
else:
raise ValueError("can't interpret %r" % init)
prefix = {sset[0]: 1}
for i, s in enumerate(sset[1:]):
prefix[s] = 1 << (i + 1) * 10
return int(num * prefix[letter])
def _in_dst(date, dststartmonth, dststartweek, dststarthour, dstendmonth, dstendweek, dstendhour):
dststart = datetime.datetime(date.year, dststartmonth, 1) + \
datetime.timedelta(seconds=dststartweek*7*24*60*60 + dststarthour*60*60)
dstend = datetime.datetime(date.year, dstendmonth, 1) + \
datetime.timedelta(seconds=dstendweek*7*24*60*60 + dstendhour*60*60)
if dststart <= dstend:
if dststart < date < dstend:
return True
else:
if data < dstend or data > dststart:
return True
# XXX check doc for exact format, check php version
def _get_timezone(date, tz_data, align_dst=False):
if tz_data is None:
return 0
timezone, _, timezonedst, _, dstendmonth, dstendweek, dstendhour, _, _, _, dststartmonth, dststartweek, dststarthour, _, _ = struct.unpack('<lllllHHllHlHHlH', tz_data)
dst = _in_dst(date, dststartmonth, dststartweek, dststarthour, dstendmonth, dstendweek, dstendhour)
# TODO use DST-aware datetimes?
if align_dst and not _in_dst(datetime.datetime.now(),
dststartmonth, dststartweek, dststarthour,
dstendmonth, dstendweek, dstendhour):
dst = not dst
if dst:
return timezone + timezonedst
else:
return timezone
def _from_gmt(date, tz_data):
return date - datetime.timedelta(minutes=_get_timezone(date, tz_data))
def _to_gmt(date, tz_data, align_dst=False):
return date + datetime.timedelta(minutes=_get_timezone(date, tz_data, align_dst=align_dst))
def arg_objects(arg, supported_classes, method_name):
if isinstance(arg, supported_classes):
objects = [arg]
else:
try:
objects = list(arg)
except TypeError:
raise Error('invalid argument to %s' % method_name)
if [o for o in objects if not isinstance(o, supported_classes)]:
raise Error('invalid argument to %s' % method_name)
return objects
|