Skip to content

Commit 12d6694

Browse files
committed
Add a config loader and add output option
1 parent ce8aba8 commit 12d6694

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

SQLGitHub.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
#!/usr/bin/env python
22

3+
import config_loader
34
from components import top_level
45

5-
try:
6-
import config
7-
except ImportError:
8-
token = None
9-
else:
10-
token = config.token
11-
126

137
if __name__ == "__main__":
14-
token = token or raw_input("Please enter your GitHub token (which can be obtained from https://github.com/settings/tokens):")
15-
sqlserv = top_level.SQLGitHub(token)
8+
token, output = config_loader.Load("config")
9+
sqlserv = top_level.SQLGitHub(token, output)
1610
sqlserv.Start()

components/top_level.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ class SQLGitHub:
2020

2121
_PROMPT_STR = u"SQLGitHub> "
2222

23-
def __init__(self, token):
23+
def __init__(self, token, output="str"):
2424
self._github = Github(token)
25+
self._output = output
2526
self._parser = parser.SgParser(self._github)
2627
self._completer = WordCompleter(definition.ALL_TOKENS,
2728
ignore_case=True)
@@ -45,7 +46,10 @@ def Execute(self, sql, measure_time=True):
4546
except AttributeError:
4647
sys.stderr.write("One or more of the specified fields doesn't exist.\n")
4748
else:
48-
print(result)
49+
if self._output == "str":
50+
print(result)
51+
elif self._output == "csv":
52+
print(result.InCsv())
4953
print("-")
5054
print("Total rows: %d" % (len(result)))
5155
if measure_time:

config_loader.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""Config loader for SQLGitHub."""
2+
3+
import importlib
4+
5+
6+
def Load(module):
7+
try:
8+
mod = importlib.import_module(module)
9+
except ImportError:
10+
token = None
11+
output = "str"
12+
else:
13+
token = mod.token if hasattr(mod, "token") else None
14+
output = mod.output if hasattr(mod, "output") else "str"
15+
16+
token = token or raw_input("Please enter your GitHub token (which can be obtained from https://github.com/settings/tokens): ")
17+
18+
return (token,
19+
output)

0 commit comments

Comments
 (0)