mirror of
https://github.com/deadc0de6/dotdrop.git
synced 2026-02-10 21:24:15 +00:00
mypy strict
This commit is contained in:
@@ -8,11 +8,12 @@ in dotdrop
|
|||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
from typing import List
|
from typing import List, Dict, TypeVar, Optional
|
||||||
|
|
||||||
# local imports
|
# local imports
|
||||||
from dotdrop.dictparser import DictParser
|
from dotdrop.dictparser import DictParser
|
||||||
from dotdrop.exceptions import UndefinedException
|
from dotdrop.exceptions import UndefinedException
|
||||||
|
from dotdrop.templategen import Templategen
|
||||||
|
|
||||||
|
|
||||||
class Cmd(DictParser):
|
class Cmd(DictParser):
|
||||||
@@ -22,7 +23,7 @@ class Cmd(DictParser):
|
|||||||
eq_ignore = ('log',)
|
eq_ignore = ('log',)
|
||||||
descr = 'command'
|
descr = 'command'
|
||||||
|
|
||||||
def __init__(self, key, action):
|
def __init__(self, key: str, action: str):
|
||||||
"""constructor
|
"""constructor
|
||||||
@key: action key
|
@key: action key
|
||||||
@action: action string
|
@action: action string
|
||||||
@@ -32,7 +33,7 @@ class Cmd(DictParser):
|
|||||||
self.action = action
|
self.action = action
|
||||||
self.silent = key.startswith('_')
|
self.silent = key.startswith('_')
|
||||||
|
|
||||||
def _get_action(self, templater, debug):
|
def _get_action(self, templater: Templategen, debug: bool) -> str:
|
||||||
action = ''
|
action = ''
|
||||||
try:
|
try:
|
||||||
action = templater.generate_string(self.action)
|
action = templater.generate_string(self.action)
|
||||||
@@ -46,7 +47,7 @@ class Cmd(DictParser):
|
|||||||
self.log.dbg(f' - templated \"{action}\"')
|
self.log.dbg(f' - templated \"{action}\"')
|
||||||
return action
|
return action
|
||||||
|
|
||||||
def _get_args(self, templater):
|
def _get_args(self, templater: Templategen) -> List[str]:
|
||||||
args = []
|
args = []
|
||||||
if not self.args:
|
if not self.args:
|
||||||
return args
|
return args
|
||||||
@@ -60,7 +61,9 @@ class Cmd(DictParser):
|
|||||||
return False
|
return False
|
||||||
return args
|
return args
|
||||||
|
|
||||||
def execute(self, templater=None, debug=False):
|
def execute(self,
|
||||||
|
templater: Optional[Templategen] = None,
|
||||||
|
debug: bool = False) -> bool:
|
||||||
"""execute the command in the shell"""
|
"""execute the command in the shell"""
|
||||||
ret = 1
|
ret = 1
|
||||||
action = self.action
|
action = self.action
|
||||||
@@ -101,13 +104,16 @@ class Cmd(DictParser):
|
|||||||
return ret == 0
|
return ret == 0
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _adjust_yaml_keys(cls, value):
|
def _adjust_yaml_keys(cls, value: str) -> Dict[str, str]:
|
||||||
return {'action': value}
|
return {'action': value}
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self) -> str:
|
||||||
return f'key:{self.key} -> \"{self.action}\"'
|
return f'key:{self.key} -> \"{self.action}\"'
|
||||||
|
|
||||||
|
|
||||||
|
ActionT = TypeVar('ActionT', bound='Action')
|
||||||
|
|
||||||
|
|
||||||
class Action(Cmd):
|
class Action(Cmd):
|
||||||
"""An action to execute"""
|
"""An action to execute"""
|
||||||
|
|
||||||
@@ -115,7 +121,7 @@ class Action(Cmd):
|
|||||||
post = 'post'
|
post = 'post'
|
||||||
descr = 'action'
|
descr = 'action'
|
||||||
|
|
||||||
def __init__(self, key, kind, action):
|
def __init__(self, key: str, kind: str, action: str):
|
||||||
"""constructor
|
"""constructor
|
||||||
@key: action key
|
@key: action key
|
||||||
@kind: type of action (pre or post)
|
@kind: type of action (pre or post)
|
||||||
@@ -125,33 +131,36 @@ class Action(Cmd):
|
|||||||
self.kind = kind
|
self.kind = kind
|
||||||
self.args = []
|
self.args = []
|
||||||
|
|
||||||
def copy(self, args):
|
def copy(self, args: List[str]):
|
||||||
"""return a copy of this object with arguments"""
|
"""return a copy of this object with arguments"""
|
||||||
action = Action(self.key, self.kind, self.action)
|
action = Action(self.key, self.kind, self.action)
|
||||||
action.args = args
|
action.args = args
|
||||||
return action
|
return action
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def parse(cls, key, value):
|
def parse(cls, key: str, value: str) -> ActionT:
|
||||||
"""parse key value into object"""
|
"""parse key value into object"""
|
||||||
val = {}
|
val = {}
|
||||||
val['kind'], val['action'] = value
|
val['kind'], val['action'] = value
|
||||||
return cls(key=key, **val)
|
return cls(key=key, **val)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self) -> str:
|
||||||
out = f'{self.key}: [{self.kind}] \"{self.action}\"'
|
out = f'{self.key}: [{self.kind}] \"{self.action}\"'
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self) -> str:
|
||||||
return f'action({self.__str__()})'
|
return f'action({self.__str__()})'
|
||||||
|
|
||||||
|
|
||||||
|
TransformT = TypeVar('TransformT', bound='Transform')
|
||||||
|
|
||||||
|
|
||||||
class Transform(Cmd):
|
class Transform(Cmd):
|
||||||
"""A transformation on a dotfile"""
|
"""A transformation on a dotfile"""
|
||||||
|
|
||||||
descr = 'transformation'
|
descr = 'transformation'
|
||||||
|
|
||||||
def __init__(self, key, action):
|
def __init__(self, key: str, action: str):
|
||||||
"""constructor
|
"""constructor
|
||||||
@key: action key
|
@key: action key
|
||||||
@trans: action string
|
@trans: action string
|
||||||
@@ -159,13 +168,15 @@ class Transform(Cmd):
|
|||||||
super().__init__(key, action)
|
super().__init__(key, action)
|
||||||
self.args = []
|
self.args = []
|
||||||
|
|
||||||
def copy(self, args):
|
def copy(self, args: List[str]) -> TransformT:
|
||||||
"""return a copy of this object with arguments"""
|
"""return a copy of this object with arguments"""
|
||||||
trans = Transform(self.key, self.action)
|
trans = Transform(self.key, self.action)
|
||||||
trans.args = args
|
trans.args = args
|
||||||
return trans
|
return trans
|
||||||
|
|
||||||
def transform(self, arg0, arg1, templater=None, debug=False):
|
def transform(self, arg0: str, arg1: str,
|
||||||
|
templater: Optional[Templategen] = None,
|
||||||
|
debug: bool = False) -> bool:
|
||||||
"""
|
"""
|
||||||
execute transformation with {0} and {1}
|
execute transformation with {0} and {1}
|
||||||
where {0} is the file to transform
|
where {0} is the file to transform
|
||||||
|
|||||||
@@ -22,10 +22,12 @@ class Logger:
|
|||||||
EMPH = '\033[33m'
|
EMPH = '\033[33m'
|
||||||
BOLD = '\033[1m'
|
BOLD = '\033[1m'
|
||||||
|
|
||||||
def __init__(self, debug=False):
|
def __init__(self, debug: bool = False):
|
||||||
self.debug = debug
|
self.debug = debug
|
||||||
|
|
||||||
def log(self, string, end='\n', pre='', bold=False):
|
def log(self, string: str,
|
||||||
|
end: str = '\n', pre: str = '',
|
||||||
|
bold: bool = False) -> None:
|
||||||
"""normal log"""
|
"""normal log"""
|
||||||
cstart = self._color(self.BLUE)
|
cstart = self._color(self.BLUE)
|
||||||
cend = self._color(self.RESET)
|
cend = self._color(self.RESET)
|
||||||
@@ -37,13 +39,14 @@ class Logger:
|
|||||||
fmt = f'{pre}{cstart}{string}{end}{cend}'
|
fmt = f'{pre}{cstart}{string}{end}{cend}'
|
||||||
sys.stdout.write(fmt)
|
sys.stdout.write(fmt)
|
||||||
|
|
||||||
def sub(self, string, end='\n'):
|
def sub(self, string: str,
|
||||||
|
end: str = '\n') -> None:
|
||||||
"""sub log"""
|
"""sub log"""
|
||||||
cstart = self._color(self.BLUE)
|
cstart = self._color(self.BLUE)
|
||||||
cend = self._color(self.RESET)
|
cend = self._color(self.RESET)
|
||||||
sys.stdout.write(f'\t{cstart}->{cend} {string}{end}')
|
sys.stdout.write(f'\t{cstart}->{cend} {string}{end}')
|
||||||
|
|
||||||
def emph(self, string, stdout=True):
|
def emph(self, string: str, stdout: bool = True) -> None:
|
||||||
"""emphasis log"""
|
"""emphasis log"""
|
||||||
cstart = self._color(self.EMPH)
|
cstart = self._color(self.EMPH)
|
||||||
cend = self._color(self.RESET)
|
cend = self._color(self.RESET)
|
||||||
@@ -53,46 +56,50 @@ class Logger:
|
|||||||
else:
|
else:
|
||||||
sys.stdout.write(content)
|
sys.stdout.write(content)
|
||||||
|
|
||||||
def err(self, string, end='\n'):
|
def err(self, string: str, end: str = '\n') -> None:
|
||||||
"""error log"""
|
"""error log"""
|
||||||
cstart = self._color(self.RED)
|
cstart = self._color(self.RED)
|
||||||
cend = self._color(self.RESET)
|
cend = self._color(self.RESET)
|
||||||
msg = f'{string} {end}'
|
msg = f'{string} {end}'
|
||||||
sys.stderr.write(f'{cstart}[ERR] {msg}{cend}')
|
sys.stderr.write(f'{cstart}[ERR] {msg}{cend}')
|
||||||
|
|
||||||
def warn(self, string, end='\n'):
|
def warn(self, string: str, end: str = '\n') -> None:
|
||||||
"""warning log"""
|
"""warning log"""
|
||||||
cstart = self._color(self.YELLOW)
|
cstart = self._color(self.YELLOW)
|
||||||
cend = self._color(self.RESET)
|
cend = self._color(self.RESET)
|
||||||
sys.stderr.write(f'{cstart}[WARN] {string} {end}{cend}')
|
sys.stderr.write(f'{cstart}[WARN] {string} {end}{cend}')
|
||||||
|
|
||||||
def dbg(self, string, force=False):
|
def dbg(self, string: str, force: bool = False) -> None:
|
||||||
"""debug log"""
|
"""debug log"""
|
||||||
if not force and not self.debug:
|
if not force and not self.debug:
|
||||||
return
|
return
|
||||||
frame = inspect.stack()[1]
|
frame = inspect.stack()[1]
|
||||||
mod = inspect.getmodule(frame[0]).__name__
|
|
||||||
|
mod = inspect.getmodule(frame[0])
|
||||||
|
mod_name = 'module?'
|
||||||
|
if mod:
|
||||||
|
mod_name = mod.__name__
|
||||||
func = inspect.stack()[1][3]
|
func = inspect.stack()[1][3]
|
||||||
cstart = self._color(self.MAGENTA)
|
cstart = self._color(self.MAGENTA)
|
||||||
cend = self._color(self.RESET)
|
cend = self._color(self.RESET)
|
||||||
clight = self._color(self.LMAGENTA)
|
clight = self._color(self.LMAGENTA)
|
||||||
bold = self._color(self.BOLD)
|
bold = self._color(self.BOLD)
|
||||||
line = f'{bold}{clight}[DEBUG][{mod}.{func}]'
|
line = f'{bold}{clight}[DEBUG][{mod_name}.{func}]'
|
||||||
line += f'{cend}{cstart} {string}{cend}\n'
|
line += f'{cend}{cstart} {string}{cend}\n'
|
||||||
sys.stderr.write(line)
|
sys.stderr.write(line)
|
||||||
|
|
||||||
def dry(self, string, end='\n'):
|
def dry(self, string: str, end: str = '\n') -> None:
|
||||||
"""dry run log"""
|
"""dry run log"""
|
||||||
cstart = self._color(self.GREEN)
|
cstart = self._color(self.GREEN)
|
||||||
cend = self._color(self.RESET)
|
cend = self._color(self.RESET)
|
||||||
sys.stdout.write(f'{cstart}[DRY] {string} {end}{cend}')
|
sys.stdout.write(f'{cstart}[DRY] {string} {end}{cend}')
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def raw(cls, string, end='\n'):
|
def raw(cls, string: str, end: str = '\n') -> None:
|
||||||
"""raw log"""
|
"""raw log"""
|
||||||
sys.stdout.write(f'{string}{end}')
|
sys.stdout.write(f'{string}{end}')
|
||||||
|
|
||||||
def ask(self, query):
|
def ask(self, query: str) -> bool:
|
||||||
"""ask user for confirmation"""
|
"""ask user for confirmation"""
|
||||||
cstart = self._color(self.BLUE)
|
cstart = self._color(self.BLUE)
|
||||||
cend = self._color(self.RESET)
|
cend = self._color(self.RESET)
|
||||||
@@ -102,7 +109,7 @@ class Logger:
|
|||||||
return resp == 'y'
|
return resp == 'y'
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _color(cls, col):
|
def _color(cls, col: str) -> str:
|
||||||
"""is color supported"""
|
"""is color supported"""
|
||||||
if not sys.stdout.isatty():
|
if not sys.stdout.isatty():
|
||||||
return ''
|
return ''
|
||||||
|
|||||||
18
scripts/check-syntax.sh
vendored
18
scripts/check-syntax.sh
vendored
@@ -104,20 +104,20 @@ pylint \
|
|||||||
--disable=R0904 \
|
--disable=R0904 \
|
||||||
dotdrop/
|
dotdrop/
|
||||||
|
|
||||||
|
# mypy
|
||||||
|
echo "----------------------------"
|
||||||
|
echo "checking dotdrop with mypy"
|
||||||
|
mypy \
|
||||||
|
--strict \
|
||||||
|
--ignore-missing-imports \
|
||||||
|
--allow-redefinition \
|
||||||
|
dotdrop/
|
||||||
|
|
||||||
# pytype
|
# pytype
|
||||||
echo "----------------------------"
|
echo "----------------------------"
|
||||||
echo "checking dotdrop with pytype"
|
echo "checking dotdrop with pytype"
|
||||||
pytype dotdrop/
|
pytype dotdrop/
|
||||||
|
|
||||||
# mypy
|
|
||||||
echo "----------------------------"
|
|
||||||
echo "checking dotdrop with mypy"
|
|
||||||
# --strict
|
|
||||||
mypy \
|
|
||||||
--ignore-missing-imports \
|
|
||||||
--allow-redefinition \
|
|
||||||
dotdrop/
|
|
||||||
|
|
||||||
# check shell scripts
|
# check shell scripts
|
||||||
# SC2002: Useless cat
|
# SC2002: Useless cat
|
||||||
# SC2126: Consider using grep -c instead of grep|wc -l
|
# SC2126: Consider using grep -c instead of grep|wc -l
|
||||||
|
|||||||
Reference in New Issue
Block a user