1
# -*- coding: utf-8 -*-
2
# Copyright (c) 2009 - Luca Invernizzi <invernizzi.l@gmail.com>
4
# This program is free software: you can redistribute it and/or modify it under
5
# the terms of the GNU General Public License as published by the Free Software
6
# Foundation, either version 3 of the License, or (at your option) any later
9
# This program is distributed in the hope that it will be useful, but WITHOUT
10
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14
# You should have received a copy of the GNU General Public License along with
15
# this program. If not, see <http://www.gnu.org/licenses/>.
17
from __future__ import with_statement
26
__all__ = ["smartSaveToFile",
36
def smartLoadFromFile(dirname, filename):
37
path=dirname+'/'+filename
38
if os.path.isdir(dirname):
39
if os.path.isfile(path):
41
with open(path, 'r') as file:
42
item = pickle.load(file)
50
def smartSaveToFile(dirname, filename, item, **kwargs):
51
path=dirname+'/'+filename
53
with open(path, 'wb') as file:
54
pickle.dump(item, file)
56
if kwargs.get('critical', False):
57
raise Exception(_("saving critical object failed"))
63
return tuple(map(list, zip(*a)))
66
def filterAttr(list, attr, value):
67
return filter(lambda elem: getattr(elem, attr) == value, list)
70
def iso8601toTime(string):
71
#FIXME: need to handle time with TIMEZONES!
72
string = string.split('.')[0].split('Z')[0]
73
if string.find('T') == -1:
74
return datetime.datetime.strptime(string.split(".")[0], "%Y-%m-%d")
75
return datetime.datetime.strptime(string.split(".")[0], \
79
def timeToIso8601(timeobject):
80
if not hasattr(timeobject, 'strftime'):
82
return timeobject.strftime("%Y-%m-%dT%H:%M:%S")
85
def dateToIso8601(timeobject):
86
if not hasattr(timeobject, 'strftime'):
88
return timeobject.strftime("%Y-%m-%d")
93
return datetime.timedelta(seconds = time.altzone)
95
return datetime.timedelta(seconds = time.timezone)
97
def text_strip_tags(text):
98
r = re.compile(r'<content>(.*)</content>',re.DOTALL)
99
result = re.findall(r, text)
104
r = re.compile(r'(?:^|\s)@[\w]+,*')
107
for match in r.finditer(text):
108
purged_text += text[last_match_end : match.start()]
109
last_match_end = match.end()