Skip to content

Commit

Permalink
Allow customized emoji.
Browse files Browse the repository at this point in the history
Extract fatal to a class method
  • Loading branch information
ye11ow committed Mar 4, 2020
1 parent 908e39f commit 90c4b9c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
56 changes: 37 additions & 19 deletions noti.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,15 @@ class NotiConfig:
# Shared configurations
'global': {
# Max number of MRs that will be shown on the list
'mr_limit': 5
'mr_limit': 5,
},

'bitbar': {
'good_day': '😃',
'approved': '👍',
'running': '🏃',
'failed': '🙃',
'comments': '💬'
}
}

Expand All @@ -160,6 +168,10 @@ def user_config(self):

def get_config(self, vcs):
return {**self._shared_config, **self.user_config.get(vcs)}

@property
def bitbar_config(self):
return {**self.DEFAULT_CONFIG.get('bitbar'), **self.user_config.get('bitbar', {})}

class NotiError(Exception):
def __init__(self, vcs, message, help_link=None):
Expand Down Expand Up @@ -360,10 +372,14 @@ def __init__(self, comment):
)

class BitbarPrinter:
def __init__(self):
self._title = ""

_default_config = 'Configure noti | bash="vi $HOME/.noticonfig.json"'

def __init__(self, conf):
self._conf = conf
self._title = ''
self._items = []
self._configs = ['Configure noti | bash="vi $HOME/.noticonfig.json"']
self._configs = [self._default_config]

def title(self, title):
self._title = title
Expand Down Expand Up @@ -392,15 +408,18 @@ def print(self):
def add_error(self, title):
self._configs.insert(0, f"{title}| color=red")

def fatal(self, message, help_link=None):
self.clear()
@classmethod
def fatal(cls, message, help_link=None):
print('Noti Error | color=red')
print('---')

self.title('Noti Error | color=red')
if help_link is not None:
message += ' | color=red href=' + help_link
self.add(message)
print(message)

print('---')
print(cls._default_config)

self.print()
exit(1)

def generate_mr(self, mr):
Expand All @@ -412,7 +431,7 @@ def generate_mr(self, mr):

title = ''
if mr.approved:
title += ' 👍'
title += ' ' + self._conf.get('approved')
title += f" | href={mr.url}"

sub_text = ''
Expand All @@ -438,7 +457,7 @@ def generate_mr(self, mr):
if len(mr.reviews) == 0:
title = f"{mr.branch} {title}"
else:
title = f"{mr.branch} 💬{len(mr.reviews)} {title}"
title = f"{mr.branch} {self._conf.get('comments')}{len(mr.reviews)} {title}"

self.add(f"{title}\n\n\n{sub_text}")
self.add(f"{mr.title} | alternate=true")
Expand All @@ -451,10 +470,10 @@ def generate_title(self, mrs):
'comments': 0
}
pipeline_icon_map = {
'failed': '🙃',
'running': '🏃',
'comments': '💬',
'approved': '👍'
'failed': self._conf.get('failed'),
'running': self._conf.get('running'),
'comments': self._conf.get('comments'),
'approved': self._conf.get('approved')
}

for key, value in mrs.items():
Expand All @@ -473,7 +492,7 @@ def generate_title(self, mrs):
title += pipeline_icon_map[key] + str(statistics[key])

if len(title) == 0:
title = '😃'
title = self._conf.get('good_day')

self.title(title)

Expand Down Expand Up @@ -502,14 +521,12 @@ def time_diff(self, before):
minutes = int(seconds%3600/60)
return f"{hours_text}{minutes} minutes ago"

bp = BitbarPrinter()

try:
from dateutil import parser
from dateutil.tz import tzlocal
from dateutil.tz import tzutc
except:
bp.fatal('Missing dependencies: You need to install python-dateutil', 'https://dateutil.readthedocs.io/en/stable/#installation')
BitbarPrinter.fatal('Missing dependencies: You need to install python-dateutil', 'https://dateutil.readthedocs.io/en/stable/#installation')

def main(registry, conf, bp):
vcs = []
Expand Down Expand Up @@ -546,6 +563,7 @@ def main(registry, conf, bp):

if __name__ == "__main__":
conf = NotiConfig()
bp = BitbarPrinter(conf.bitbar_config)
registry = [Gitlab, Github]

main(registry, conf, bp)
3 changes: 2 additions & 1 deletion tests/unit/test_bitbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from dateutil import parser

from noti import BitbarPrinter
from noti import NotiConfig

def proxy_print(bp):
saved_stdout = sys.stdout
Expand Down Expand Up @@ -62,7 +63,7 @@ class TestBitbarPrinter:

@pytest.fixture
def bp(self):
return BitbarPrinter()
return BitbarPrinter(NotiConfig.DEFAULT_CONFIG.get('bitbar'))

def test_time_diff(self, bp):
before = datetime.now().astimezone(tzlocal()) - timedelta(minutes=30)
Expand Down

0 comments on commit 90c4b9c

Please sign in to comment.