File: activity.py

package info (click to toggle)
python-pypump 0.3%2Bgit20130823.1.97bffc6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 328 kB
  • ctags: 333
  • sloc: python: 1,630; makefile: 140; sh: 7
file content (137 lines) | stat: -rw-r--r-- 4,423 bytes parent folder | download
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
##
# Copyright (C) 2013 Jessica T. (Tsyesika) <xray7224@googlemail.com>
# 
# This program is free software: you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by 
# the Free Software Foundation, either version 3 of the License, or 
# (at your option) any later version. 
# 
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
# GNU General Public License for more details. 
# 
# You should have received a copy of the GNU General Public License 
# along with this program. If not, see <http://www.gnu.org/licenses/>.
##

from dateutil.parser import parse as parse
import re
from pypump.models import AbstractModel
from pypump.compatability import *

#TODO clean up and move to own file
class Generator(object):
    """ The client used to generate the activity """
    display_name = None
    id = None

    def __repr__(self):
        return "<Generator: {g_name}>".format(g_name=self.display_name)

    def __init__(self, display_name=display_name, id=id, *args, **kwargs):
        self.display_name = display_name
        self.id = id

        super(Generator, self).__init__(*args, **kwargs)

    @classmethod
    def unserialize(cls, data):
        id = data["id"]
        display_name = data["displayName"] if "displayName" in data else "Unknown client"

        return cls(id=id, display_name=display_name)

#TODO clean up and move to own file
@implement_to_string
class Unknown(AbstractModel):
    """ This class is used when we can't find a matching object type """
    TYPE = None
    display_name = None

    def __repr__(self):
        return self.display_name if self.display_name else self.TYPE

    def __str__(self):
        return str(self.__repr__())

    def __init__(self, object_type=None, display_name=None, *args, **kwargs):
        self.TYPE = object_type
        self.display_name = display_name

        super(Unknown, self).__init__(*args, **kwargs)

    @classmethod
    def unserialize(cls, data):
        object_type = data["objectType"] if "objectType" in data else ""
        display_name = data["displayName"] if "displayName" in data else ""

        return cls(display_name=display_name, object_type=object_type)

@implement_to_string
class Activity(AbstractModel):
    obj = None
    verb = None
    actor = None
    generator = None
    updated = None
    url = None
    published = None
    content = None
    id = None

    def __init__(self, obj, verb, actor, generator,
                 updated, url, published, content, id, *args, **kwargs):

        super(Activity, self).__init__(*args, **kwargs)

        self.obj = obj
        self.verb = verb
        self.actor = actor
        self.generator = generator
        self.updated = updated
        self.url = url
        self.published = published
        self.content = content
        self.id = id
    
    def __repr__(self):
        return '<Activity: {webfinger} {verb}ed {model}>'.format(
                webfinger=self.actor.id[5:], # [5:] to strip of acct:
                verb=self.verb.rstrip("e"), # english: e + ed = ed
                model=self.obj.objectType
                )

    def __str__(self):
        return str(self.__repr__())

    @classmethod
    def unserialize(cls, data):
        """ From JSON -> Activity object """

        dataobj = data["object"]
        obj_type = dataobj["objectType"].capitalize()

        if "author" not in dataobj:
            # author is not set for posted objects in inbox/major, so we add it
            dataobj["author"] = data["actor"]

        try:
            objekt = getattr(cls._pump, obj_type)
            obj = objekt.unserialize(dataobj)
        except AttributeError:
            obj = Unknown.unserialize(dataobj)

        verb = data["verb"]
        actor = cls._pump.Person.unserialize(data["actor"])
        # generator is not always there (at least not for verb:'update' obj:Person)
        generator = Generator.unserialize(data["generator"]) if "generator" in data else None
        updated = parse(data["updated"])
        url = data["url"]
        published = parse(data["published"])
        content = data["content"]
        id = data["id"]

        return cls(obj, verb, actor, generator, updated,
                   url, published, content, id)