forked from georchestra/sdi-consistence-check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentials.py
42 lines (35 loc) · 1.29 KB
/
credentials.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
import os
from urllib.parse import urlparse
class Credentials:
def __init__(self, logger=None):
"""
Loads the credentials file, which consists of a text file
formatted with "hostname username password" and whose default location is set to
~/.sdichecker.
"""
self._credentials = {}
try:
with open(os.getenv("HOME") + "/.sdichecker") as file:
for line in file:
try:
(hostname, user, password) = line.rstrip("\n").split(" ", 3)
self.add(hostname, user, password)
except ValueError:
pass
except FileNotFoundError:
if logger is not None:
logger.info("No ~/.sdichecker file found, skipping credentials definition.")
pass
def add(self, site, username, password):
self._credentials[site] = (username, password)
def addFromUrl(self, url, username, password):
u = urlparse(url)
self.add(u.hostname, username, password)
def get(self, site):
try:
return self._credentials[site]
except KeyError:
return (None, None)
def getFromUrl(self, url):
u = urlparse(url)
return self.get(u.hostname)