File: gitfdw.py

package info (click to toggle)
postgresql-multicorn 1.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 864 kB
  • ctags: 611
  • sloc: ansic: 2,690; python: 1,829; sql: 645; makefile: 93; sh: 29
file content (37 lines) | stat: -rw-r--r-- 1,228 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
"""
A Git foreign data wrapper

"""

from . import ForeignDataWrapper
import brigit


class GitFdw(ForeignDataWrapper):
    """A Git foreign data wrapper.

    The git foreign data wrapper accepts the following options:

    path        --  the absolute path to the git repo. It must be readable by
                    the user running postgresql (usually, postgres).
    encoding    --  the file encoding. Defaults to "utf-8".

    """

    def __init__(self, fdw_options, fdw_columns):
        super(GitFdw, self).__init__(fdw_options, fdw_columns)
        self.path = fdw_options["path"]
        self.encoding = fdw_options.get("encoding", "utf-8")

    def execute(self, quals, columns):
        def enc(unicode_str):
            """Encode the string in the self given encoding."""
            return unicode_str.encode(self.encoding)
        for log in  brigit.Git(self.path).pretty_log():
            yield {
                'author_name': enc(log["author"]['name']),
                'author_email': enc(log["author"]['email']),
                'message': enc(log['message']),
                'hash': enc(log['hash']),
                'date': log['datetime'].isoformat()
            }