forked from cbogart/githubscraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
54 lines (45 loc) · 1.77 KB
/
config.py
File metadata and controls
54 lines (45 loc) · 1.77 KB
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
import mysql.connector
import json
import pdb
import os
import sys
from pymongo import MongoClient
class Config:
def __init__(self, config_file):
try:
self.config = json.load(open(config_file,"r"))
self.mongo_cli = None
self.mongo_db = None
self.mongo_db_raw = None
self.mongo_db_proc = None
except Exception, e:
raise(ValueError("Can't read from configuration file " + config_file + ":\n" + str(e)))
def __getitem__(self, key):
return self.config[key]
def ensure_dir(self, d):
try:
os.makedirs(self.config[d])
except OSError as e:
pass
return self.config[d]
def mongo_client(self):
if self.mongo_cli is None: self.mongo_cli = MongoClient(self.config["mongourl"])
return self.mongo_cli
def raw_db(self):
"""Store json records literally as scraped from github here"""
if self.mongo_db_raw is None: self.mongo_db_raw = self.mongo_client()[self.config["mongodb_raw"]]
return self.mongo_db_raw
def proc_db(self):
"""Store researcher-friendly processed records here"""
if self.mongo_db_proc is None: self.mongo_db_proc = self.mongo_client()[self.config["mongodb_proc"]]
return self.mongo_db_proc
def get_ghtorrent_connection(self):
return mysql.connector.connect(
host= self.config["db_host"],
user= self.config["db_user"],
password= self.config["db_password"],
time_zone= self.config["db_time_zone"],
database= self.config["db_ghtorrent_database"]
);
def get_sample_set_project_names(self):
return [r.strip() for r in open(self.config["sample_set"], "r").readlines()]