Skip to content

Commit

Permalink
Load the VCS module when they are configured
Browse files Browse the repository at this point in the history
  • Loading branch information
ye11ow committed Jan 16, 2020
1 parent dfdaaf3 commit 3e9d00d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
26 changes: 18 additions & 8 deletions noti.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,18 @@
# <bitbar.dependencies>python</bitbar.dependencies>
# <bitbar.abouturl>https://github.com/ye11ow/noti</bitbar.abouturl>

import sys
import json
from pathlib import Path
from datetime import datetime

try:
import gitlab
from github import Github as GH
from requests.exceptions import ConnectionError
from dateutil import parser
from dateutil.tz import tzlocal
except:
print("Missing dependencies")
print("---")
print("You need to install python-gitlab | href=https://python-gitlab.readthedocs.io/en/stable/install.html")
print("You need to install PyGithub | href=https://pygithub.readthedocs.io/en/latest/introduction.html#download-and-install")
print("You need to install python-dateutil | href=https://dateutil.readthedocs.io/en/stable/#installation")
import sys
sys.exit(0)

# Put your personal configuration here
Expand All @@ -52,8 +47,15 @@

class Gitlab:
def __init__(self, config):
try:
import gitlab
except:
print("Missing dependencies")
print("---")
print("You need to install python-gitlab | href=https://python-gitlab.readthedocs.io/en/stable/install.html")
sys.exit(0)

self._config = config.get('gitlab', {})
# TODO: raise error if there is no gitlab object
self._gl = gitlab.Gitlab(self._config.get('host'), private_token=self._config.get('token'))

def get_mrs(self):
Expand Down Expand Up @@ -165,6 +167,14 @@ def url(self):

class Github:
def __init__(self, config):
try:
from github import Github as GH
except:
print("Missing dependencies")
print("---")
print("You need to install PyGithub | href=https://pygithub.readthedocs.io/en/latest/introduction.html#download-and-install")
sys.exit(0)

self._config = config.get('github', {})
self._gh = GH(self._config.get('token'))

Expand Down Expand Up @@ -342,7 +352,7 @@ def print_title(self, mrs):

try:
mrs = gl.get_mrs()
except ConnectionError:
except:
print("failed to connect to gitlab")
print('---\n')
bp.print_config()
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/test_noti.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys

class TestNoti:

def test_import(self):
# This test tries to make sure neither github nor gitlab would be loaded
# by default. We only want to load those modules when they are configured
# by the user.
import noti

assert not 'github' in sys.modules
assert not 'gitlab' in sys.modules
assert 'dateutil' in sys.modules

0 comments on commit 3e9d00d

Please sign in to comment.