• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

georgia-tech-db / eva / f09e3b44-ef34-4638-a136-6dc1a5893c0a

26 Oct 2023 11:49PM UTC coverage: 92.368% (-0.8%) from 93.203%
f09e3b44-ef34-4638-a136-6dc1a5893c0a

push

circle-ci

web-flow
Merge branch 'georgia-tech-db:master' into master

1152 of 1152 new or added lines in 80 files covered. (100.0%)

11836 of 12814 relevant lines covered (92.37%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/evadb/third_party/databases/github/github_handler.py
1
# coding=utf-8
2
# Copyright 2018-2023 EvaDB
3
#
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
7
#
8
#     http://www.apache.org/licenses/LICENSE-2.0
9
#
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
15
import github
×
16
import pandas as pd
×
17

18
from evadb.third_party.databases.github.table_column_info import STARGAZERS_COLUMNS
×
19
from evadb.third_party.databases.types import (
×
20
    DBHandler,
21
    DBHandlerResponse,
22
    DBHandlerStatus,
23
)
24

25

26
class GithubHandler(DBHandler):
×
27
    def __init__(self, name: str, **kwargs):
×
28
        """
29
        Initialize the handler.
30
        Args:
31
            name (str): name of the DB handler instance
32
            **kwargs: arbitrary keyword arguments for establishing the connection.
33
        """
34
        super().__init__(name)
×
35
        self.owner = kwargs.get("owner", "")
×
36
        self.repo = kwargs.get("repo", "")
×
37
        self.github_token = kwargs.get("github_token", "")
×
38

39
    @property
×
40
    def supported_table(self):
×
41
        def _stargazer_generator():
×
42
            for stargazer in self.connection.get_repo(
×
43
                "{}/{}".format(self.owner, self.repo)
44
            ).get_stargazers():
45
                yield {
×
46
                    property_name: getattr(stargazer, property_name)
47
                    for property_name, _ in STARGAZERS_COLUMNS
48
                }
49

50
        mapping = {
×
51
            "stargazers": {
52
                "columns": STARGAZERS_COLUMNS,
53
                "generator": _stargazer_generator(),
54
            },
55
        }
56
        return mapping
×
57

58
    def connect(self):
×
59
        """
60
        Set up the connection required by the handler.
61
        Returns:
62
            DBHandlerStatus
63
        """
64
        try:
×
65
            if self.github_token:
×
66
                self.connection = github.Github(self.github_token)
×
67
            else:
68
                self.connection = github.Github()
×
69
            return DBHandlerStatus(status=True)
×
70
        except Exception as e:
71
            return DBHandlerStatus(status=False, error=str(e))
72

73
    def disconnect(self):
×
74
        """
75
        Close any existing connections.
76
        """
77
        pass
×
78

79
    def check_connection(self) -> DBHandlerStatus:
×
80
        """
81
        Check connection to the handler.
82
        Returns:
83
            DBHandlerStatus
84
        """
85
        if self.connection:
×
86
            return DBHandlerStatus(status=True)
×
87
        else:
88
            return DBHandlerStatus(status=False, error="Not connected to the database.")
×
89

90
    def get_tables(self) -> DBHandlerResponse:
×
91
        """
92
        Return the list of tables in the database.
93
        Returns:
94
            DBHandlerResponse
95
        """
96
        if not self.connection:
×
97
            return DBHandlerResponse(data=None, error="Not connected to the database.")
×
98

99
        try:
×
100
            tables_df = pd.DataFrame(
×
101
                list(self.supported_table.keys()), columns=["table_name"]
102
            )
103
            return DBHandlerResponse(data=tables_df)
×
104
        except Exception as e:
105
            return DBHandlerResponse(data=None, error=str(e))
106

107
    def get_columns(self, table_name: str) -> DBHandlerResponse:
×
108
        """
109
        Returns the list of columns for the given table.
110
        Args:
111
            table_name (str): name of the table whose columns are to be retrieved.
112
        Returns:
113
            DBHandlerResponse
114
        """
115
        if not self.connection:
×
116
            return DBHandlerResponse(data=None, error="Not connected to the database.")
×
117
        try:
×
118
            columns_df = pd.DataFrame(
×
119
                self.supported_table[table_name]["columns"], columns=["name", "dtype"]
120
            )
121
            return DBHandlerResponse(data=columns_df)
×
122
        except Exception as e:
123
            return DBHandlerResponse(data=None, error=str(e))
124

125
    def select(self, table_name: str) -> DBHandlerResponse:
×
126
        """
127
        Returns a generator that yields the data from the given table.
128
        Args:
129
            table_name (str): name of the table whose data is to be retrieved.
130
        Returns:
131
            DBHandlerResponse
132
        """
133
        if not self.connection:
×
134
            return DBHandlerResponse(data=None, error="Not connected to the database.")
×
135
        try:
×
136
            if table_name not in self.supported_table:
×
137
                return DBHandlerResponse(
×
138
                    data=None,
139
                    error="{} is not supported or does not exist.".format(table_name),
140
                )
141
            # TODO: Projection column trimming optimization opportunity
142
            return DBHandlerResponse(
×
143
                data=None,
144
                data_generator=self.supported_table[table_name]["generator"],
145
            )
146
        except Exception as e:
147
            return DBHandlerResponse(data=None, error=str(e))
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc