~ubuntu-branches/debian/stretch/gtg/stretch

« back to all changes in this revision

Viewing changes to GTG/plugins/rtm_sync/utility.py

  • Committer: Bazaar Package Importer
  • Author(s): Luca Falavigna
  • Date: 2009-12-12 16:04:44 UTC
  • mfrom: (1.1.2 upstream) (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091212160444-gcmi5g9rfnh8zxl0
Tags: 0.2-1
* New upstream release.
  - xdg module is no longer required at build time (Closes: #560566).
* Remove unneeded build-dependencies.
* Add python-dbus to Depends.
* Bump dependency on python-gtk2 to be at least 2.14 (Closes: #559498).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright (c) 2009 - Luca Invernizzi <invernizzi.l@gmail.com>
 
3
#
 
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
 
7
# version.
 
8
#
 
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
 
12
# details.
 
13
#
 
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/>.
 
16
 
 
17
from __future__ import with_statement
 
18
 
 
19
import pickle
 
20
import os
 
21
import datetime
 
22
import time
 
23
import re
 
24
from GTG import _
 
25
 
 
26
__all__ = ["smartSaveToFile",
 
27
           "smartLoadFromFile",
 
28
           "filterAttr",
 
29
           "iso8601toTime",
 
30
           "timeToIso8601",
 
31
           "dateToIso8601",
 
32
           "unziplist",
 
33
           "timezone"]
 
34
 
 
35
 
 
36
def smartLoadFromFile(dirname, filename):
 
37
    path=dirname+'/'+filename
 
38
    if os.path.isdir(dirname):
 
39
        if os.path.isfile(path):
 
40
            try:
 
41
                with open(path, 'r') as file:
 
42
                    item = pickle.load(file)
 
43
            except:
 
44
                return None
 
45
            return item
 
46
    else:
 
47
        os.makedirs(dirname)
 
48
 
 
49
 
 
50
def smartSaveToFile(dirname, filename, item, **kwargs):
 
51
    path=dirname+'/'+filename
 
52
    try:
 
53
        with open(path, 'wb') as file:
 
54
            pickle.dump(item, file)
 
55
    except:
 
56
        if kwargs.get('critical', False):
 
57
            raise Exception(_("saving critical object failed"))
 
58
 
 
59
 
 
60
def unziplist(a):
 
61
    if len(a) == 0:
 
62
        return [], []
 
63
    return tuple(map(list, zip(*a)))
 
64
 
 
65
 
 
66
def filterAttr(list, attr, value):
 
67
    return filter(lambda elem: getattr(elem, attr) == value, list)
 
68
 
 
69
 
 
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], \
 
76
                                      "%Y-%m-%dT%H:%M:%S")
 
77
 
 
78
 
 
79
def timeToIso8601(timeobject):
 
80
    if not hasattr(timeobject, 'strftime'):
 
81
        return ""
 
82
    return timeobject.strftime("%Y-%m-%dT%H:%M:%S")
 
83
 
 
84
 
 
85
def dateToIso8601(timeobject):
 
86
    if not hasattr(timeobject, 'strftime'):
 
87
        return ""
 
88
    return timeobject.strftime("%Y-%m-%d")
 
89
 
 
90
 
 
91
def timezone():
 
92
    if time.daylight:
 
93
        return datetime.timedelta(seconds = time.altzone)
 
94
    else:
 
95
        return datetime.timedelta(seconds = time.timezone)
 
96
 
 
97
def text_strip_tags(text):
 
98
    r = re.compile(r'<content>(.*)</content>',re.DOTALL)
 
99
    result = re.findall(r, text)
 
100
    if len(result) == 0:
 
101
        #something is wrong
 
102
        return text
 
103
    text = result[0]
 
104
    r = re.compile(r'(?:^|\s)@[\w]+,*')
 
105
    last_match_end = 0
 
106
    purged_text = ""
 
107
    for match in r.finditer(text):
 
108
        purged_text += text[last_match_end : match.start()]
 
109
        last_match_end = match.end()
 
110
    return purged_text 
 
111