[modules/git] Update to latest API

This commit is contained in:
tobi-wan-kenobi 2020-04-24 16:31:37 +02:00
parent 9a1aedab62
commit b85412fb2d

View file

@ -9,38 +9,31 @@ Requires:
""" """
import os import os
import string import pygit2
import logging
try:
import pygit2
except ImportError:
pass
import bumblebee.input import core.module
import bumblebee.output import core.widget
import bumblebee.engine
import bumblebee.util
class Module(bumblebee.engine.Module): import util.cli
def __init__(self, engine, config):
widgets = [] class Module(core.module.Module):
super(Module, self).__init__(engine, config, widgets) def __init__(self, config):
self._engine = engine super().__init__(config, [])
self._error = False
self.update(self.widgets()) self.__error = False
def hidden(self): def hidden(self):
return self._error return self.__error
def update(self, widgets): def update(self):
state = {} state = {}
new_widgets = [] new_widgets = []
try: try:
directory = bumblebee.util.execute("xcwd").strip() directory = util.cli.execute("xcwd").strip()
directory = self._get_git_root(directory) directory = self.__get_git_root(directory)
repo = pygit2.Repository(directory) repo = pygit2.Repository(directory)
new_widgets.append(bumblebee.output.Widget(name='git.main', full_text=repo.head.shorthand)) new_widgets.append(core.widget.Widget(name='git.main', full_text=repo.head.shorthand))
for filepath, flags in repo.status().items(): for filepath, flags in repo.status().items():
if flags == pygit2.GIT_STATUS_WT_NEW or \ if flags == pygit2.GIT_STATUS_WT_NEW or \
@ -52,26 +45,24 @@ class Module(bumblebee.engine.Module):
if flags == pygit2.GIT_STATUS_WT_MODIFIED or \ if flags == pygit2.GIT_STATUS_WT_MODIFIED or \
flags == pygit2.GIT_STATUS_INDEX_MODIFIED: flags == pygit2.GIT_STATUS_INDEX_MODIFIED:
state['modified'] = True state['modified'] = True
self._error = False self.__error = False
if 'new' in state: if 'new' in state:
new_widgets.append(bumblebee.output.Widget(name='git.new')) new_widgets.append(core.widget.Widget(name='git.new'))
if 'modified' in state: if 'modified' in state:
new_widgets.append(bumblebee.output.Widget(name='git.modified')) new_widgets.append(core.widget.Widget(name='git.modified'))
if 'deleted' in state: if 'deleted' in state:
new_widgets.append(bumblebee.output.Widget(name='git.deleted')) new_widgets.append(core.widget.Widget(name='git.deleted'))
while len(widgets) > 0: self.widgets().clear()
del widgets[0] self.widget(new_widgets)
for widget in new_widgets:
widgets.append(widget)
except Exception as e: except Exception as e:
self._error = True self.__error = True
def state(self, widget): def state(self, widget):
return widget.name.split('.')[1] return widget.name.split('.')[1]
def _get_git_root(self, directory): def __get_git_root(self, directory):
while len(directory) > 1: while len(directory) > 1:
if os.path.exists(os.path.join(directory, ".git")): if os.path.exists(os.path.join(directory, ".git")):
return directory return directory