-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathinfo.py
146 lines (119 loc) · 4.72 KB
/
info.py
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
138
139
140
141
142
143
144
145
146
"""This module contains back end functions helping developers use data connector."""
from typing import Any, Dict, List
import pandas as pd
from IPython.display import display
from ..utils import get_styled_schema, is_notebook
from .implicit_database import ImplicitDatabase
from .info_ui import info_ui
from .schema import ConfigDef
from .config_manager import initialize_path
def info(config_path: str, update: bool = False) -> None: # pylint: disable=too-many-locals
"""Show the basic information and provide guidance for users
to issue queries.
Parameters
----------
config_path
The path to the config. It can be hosted, e.g. "yelp", or from
local filesystem, e.g. "./yelp"
update
Force update the config file even if the local version exists.
"""
path = initialize_path(config_path, update)
impdb = ImplicitDatabase(path)
# get info
tbs: Dict[str, Any] = {}
for cur_table in impdb.tables:
table_config_content: ConfigDef = impdb.tables[cur_table].config
joined_auth_params = []
required_params = []
optional_params = []
examples = []
example_query_fields = []
count = False
required_param_count = 1
auth = table_config_content.request.authorization
config_examples = table_config_content.examples
if auth is None:
pass
elif auth.type == "OAuth2":
joined_auth_params.append(
"client_id':'QNf0IeGSeMq3K*********NIU9mfHFMfX3cYe'"
+ " , "
+ "'client_secret':'eeNspLqiRoVfX*********3V2ntIiXKui9A6X'"
)
else:
joined_auth_params.append("access_token':'cCMHU4M4t7rdt*********vp3whGzFjgIKIm0'")
for k, val in table_config_content.request.params.items():
if isinstance(val, bool) and val:
required_params.append(k)
if config_examples:
examples.append(k + "=" + config_examples[k])
required_param_count += 1
elif isinstance(val, bool):
optional_params.append(k)
separator = ", "
if examples:
example_query_fields.append(separator.join(examples))
if table_config_content.request.pagination is not None:
count = True
schema = get_schema(table_config_content.response.schema_)
styled_schema = get_styled_schema(schema)
tbs[cur_table] = {}
tbs[cur_table]["joined_auth_params"] = joined_auth_params
tbs[cur_table]["required_params"] = required_params
tbs[cur_table]["optional_params"] = optional_params
tbs[cur_table]["joined_query_fields"] = example_query_fields
tbs[cur_table]["count"] = count
tbs[cur_table]["schemas"] = styled_schema
# show table info
info_ui(impdb.name, tbs)
def get_schema(schema: Dict[str, Any]) -> pd.DataFrame:
"""This method returns the schema of the table that will be returned,
so that the user knows what information to expect.
Parameters
----------
schema
The schema for the table from the config file.
Returns
-------
pandas.DataFrame
The returned data's schema.
Note
----
The schema is defined in the configuration file.
The user can either use the default one or change it by editing the configuration file.
"""
new_schema_dict: Dict[str, List[Any]] = {}
new_schema_dict["column_name"] = []
new_schema_dict["data_type"] = []
for k in schema.keys():
new_schema_dict["column_name"].append(k)
new_schema_dict["data_type"].append(schema[k].type)
return pd.DataFrame.from_dict(new_schema_dict)
def websites() -> None:
"""Displays names of websites supported by data connector."""
websites = {
"business": ["yelp"],
"finance": ["finnhub"],
"geocoding": ["mapquest"],
"lifestyle": ["spoonacular"],
"music": ["musixmatch", "spotify"],
"news": ["guardian", "times"],
"science": ["dblp"],
"shopping": ["etsy"],
"social": ["twitch", "twitter"],
"video": ["youtube"],
"weather": ["openweathermap"],
}
supported_websites = pd.DataFrame.from_dict(websites, orient="index")
supported_websites = supported_websites.transpose()
supported_websites.columns = supported_websites.columns.str.upper()
# replace "None" values with empty string
mask = supported_websites.applymap(lambda x: x is None)
cols = supported_websites.columns[(mask).any()]
for col in supported_websites[cols]:
supported_websites.loc[mask[col], col] = ""
if is_notebook():
display(supported_websites)
else:
print(supported_websites.to_string(index=False))