File: store.rst

package info (click to toggle)
python-pypump 0.7-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 512 kB
  • sloc: python: 3,153; makefile: 152
file content (84 lines) | stat: -rw-r--r-- 2,558 bytes parent folder | download | duplicates (2)
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
Store
======

Using store objects is the way PyPump handles the storing of certain
data it gets that needs to be saved to disk. There is several pieces
of data that might be stored such as:

- Client ID and secret
- OAuth request token and secret
- OAuth access token and secret

There might be others in the future too. The store object has an
interface like a dictionary. PyPump provides a disk JSON store which
allows you to easily just save the data to disk. You should note that
this data should be considered sensative as with it someone has access
to the users pump.io account.

Implementation
--------------

You probably want to provide your own storage object. There are two
extra methods other than dictionary methods you need to implement
are::

  @classmethod
  def load(cls, webfinger, pump):
      """
      This should return an instance of the store object full of any
      data that has been saved. It's your responsibility to set the
      `prefix` attribute on the store object.

      webfinger: String containing webfinger of user.
      pump: PyPump object loading the store object.
      """
      store = cls()
      store.prefix = webfinger
      return store

  def save(self):
      """
      This should save all the data to the storage.
      """
      pass

There is a `prefix` attribute will contain the webfinger of the user
the data belongs to. All the data stored and loaded should relate to
this webfinger.

The save is called frequently and multiple times. The AbstractStore
class will call the save method everytime something is set/changed on
the object.

PyPump
------

There are several ways to provide PyPump with a store object. You can
pass it in when you create the PyPump object e.g::

  >>> my_store = MyStore.load()
  >>> pump = PyPump(store=my_store, ...)

If no storage object is passed, PyPump will call the .create_store
method on itself. This will by default call .load(webfinger, pypump)
on whatever class is in store_class on PyPump. You can provide your
own class there::

  >>> class MyPump(PyPump):
  ...     store_class = MyStore
  ...
  >>> pump = MyPump(...)

This will use the MyStore class. If you want to do something else you
can always override the .create_store method::

  class MyPump(PyPump):
      def create_store(self):
          """ This should create and return the store object """
          return MyStore.load(
              webfinger=self.client.webfinger,
              pump=self
          )

For convenience, PyPump comes with a simple JSON store class,
`pypump.store.Store`.