add xonsh addins

This commit is contained in:
riley 2021-09-22 03:52:45 -04:00
parent d8fa4382d6
commit 0c3eab9ed4
4 changed files with 76 additions and 0 deletions

View file

@ -4,6 +4,14 @@ $XONSH_COLOR_STYLE = 'fruity'
xontrib load apt_tabcomplete argcomplete autovox jedi z
# XONSH WEBCONFIG END
# path stuff
import sys
sys.path.insert(0, '')
sys.path.append(Path("~/addins").resolve())
import addins
# debug
def _debug():
if $XONSH_SHOW_TRACEBACK:
@ -31,3 +39,6 @@ aliases['bwc'] = _bwc
# dots
aliases['config'] = '/usr/bin/git --git-dir=$HOME/.cfg/ --work-tree=$HOME'
# ls
aliases['ls'] = 'ls -alhs --color=auto'

4
addins/__init__.py Normal file
View file

@ -0,0 +1,4 @@
from .version import *
from .package import *
__all__ = ['Version', 'Package']

40
addins/package.xsh Normal file
View file

@ -0,0 +1,40 @@
from .version import Version
v = Version()
class Package:
def __init__(self, name, source):
self.name = name
self.source = source
self._installed = None
@property
def installed(self):
self._installed = self._get_installed()
return self._installed
def _get_installed(self) -> bool:
if self.source == 'apt':
if !(dpkg -s @(self.name)).returncode == 0:
return True
else:
return False
if self.source == 'npm':
if !(npm list -g @(self.name)).returncode == 0:
return True
else:
return False
else:
return None
def install(self):
if self.installed:
print(f"package '{self.name}' already installed.")
return
elif self.source == 'apt':
$(sudo apt-get install -qq --yes @(self.name))
elif self.source == 'npm':
$(npm install -g @(self.name))
elif self.source == 'pacman':
$(pacman -Syu @(self.name))
else:
print(f"unsupported package manager '{source}'.")

21
addins/version.xsh Normal file
View file

@ -0,0 +1,21 @@
from pathlib import Path
__all__ = ['Version']
class Version:
def __init__(self):
data = self._load_dict()
for key, value in data.items():
setattr(self, key.lower(), value)
if hasattr(self, "id_like"): # special handling for ID_LIKE
self.id_like = self.id_like.split()
def _load_dict(self):
data = {}
os_files = Path('/etc').glob("*-release")
for i in os_files:
with i.open() as f:
for line in f:
line = line.split("=")
data[line[0]] = line[1].strip().replace('"', '')
return data