Skip to content

Commit

Permalink
mypy strict
Browse files Browse the repository at this point in the history
  • Loading branch information
deadc0de6 committed Jan 17, 2024
1 parent 3639e92 commit d9fabe3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 34 deletions.
41 changes: 26 additions & 15 deletions dotdrop/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

import subprocess
import os
from typing import List
from typing import List, Dict, TypeVar, Optional

# local imports
from dotdrop.dictparser import DictParser
from dotdrop.exceptions import UndefinedException
from dotdrop.templategen import Templategen


class Cmd(DictParser):
Expand All @@ -22,7 +23,7 @@ class Cmd(DictParser):
eq_ignore = ('log',)
descr = 'command'

def __init__(self, key, action):
def __init__(self, key: str, action: str):
"""constructor
@key: action key
@action: action string
Expand All @@ -32,7 +33,7 @@ def __init__(self, key, action):
self.action = action
self.silent = key.startswith('_')

def _get_action(self, templater, debug):
def _get_action(self, templater: Templategen, debug: bool) -> str:
action = ''
try:
action = templater.generate_string(self.action)
Expand All @@ -46,7 +47,7 @@ def _get_action(self, templater, debug):
self.log.dbg(f' - templated \"{action}\"')
return action

def _get_args(self, templater):
def _get_args(self, templater: Templategen) -> List[str]:
args = []
if not self.args:
return args
Expand All @@ -60,7 +61,9 @@ def _get_args(self, templater):
return False
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"""
ret = 1
action = self.action
Expand Down Expand Up @@ -101,21 +104,24 @@ def execute(self, templater=None, debug=False):
return ret == 0

@classmethod
def _adjust_yaml_keys(cls, value):
def _adjust_yaml_keys(cls, value: str) -> Dict[str, str]:
return {'action': value}

def __str__(self):
def __str__(self) -> str:
return f'key:{self.key} -> \"{self.action}\"'


ActionT = TypeVar('ActionT', bound='Action')


class Action(Cmd):
"""An action to execute"""

pre = 'pre'
post = 'post'
descr = 'action'

def __init__(self, key, kind, action):
def __init__(self, key: str, kind: str, action: str):
"""constructor
@key: action key
@kind: type of action (pre or post)
Expand All @@ -125,47 +131,52 @@ def __init__(self, key, kind, action):
self.kind = kind
self.args = []

def copy(self, args):
def copy(self, args: List[str]):
"""return a copy of this object with arguments"""
action = Action(self.key, self.kind, self.action)
action.args = args
return action

@classmethod
def parse(cls, key, value):
def parse(cls, key: str, value: str) -> ActionT:
"""parse key value into object"""
val = {}
val['kind'], val['action'] = value
return cls(key=key, **val)

def __str__(self):
def __str__(self) -> str:
out = f'{self.key}: [{self.kind}] \"{self.action}\"'
return out

def __repr__(self):
def __repr__(self) -> str:
return f'action({self.__str__()})'


TransformT = TypeVar('TransformT', bound='Transform')


class Transform(Cmd):
"""A transformation on a dotfile"""

descr = 'transformation'

def __init__(self, key, action):
def __init__(self, key: str, action: str):
"""constructor
@key: action key
@trans: action string
"""
super().__init__(key, action)
self.args = []

def copy(self, args):
def copy(self, args: List[str]) -> TransformT:
"""return a copy of this object with arguments"""
trans = Transform(self.key, self.action)
trans.args = args
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}
where {0} is the file to transform
Expand Down
33 changes: 20 additions & 13 deletions dotdrop/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ class Logger:
EMPH = '\033[33m'
BOLD = '\033[1m'

def __init__(self, debug=False):
def __init__(self, debug: bool = False):
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"""
cstart = self._color(self.BLUE)
cend = self._color(self.RESET)
Expand All @@ -37,13 +39,14 @@ def log(self, string, end='\n', pre='', bold=False):
fmt = f'{pre}{cstart}{string}{end}{cend}'
sys.stdout.write(fmt)

def sub(self, string, end='\n'):
def sub(self, string: str,
end: str = '\n') -> None:
"""sub log"""
cstart = self._color(self.BLUE)
cend = self._color(self.RESET)
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"""
cstart = self._color(self.EMPH)
cend = self._color(self.RESET)
Expand All @@ -53,46 +56,50 @@ def emph(self, string, stdout=True):
else:
sys.stdout.write(content)

def err(self, string, end='\n'):
def err(self, string: str, end: str = '\n') -> None:
"""error log"""
cstart = self._color(self.RED)
cend = self._color(self.RESET)
msg = f'{string} {end}'
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"""
cstart = self._color(self.YELLOW)
cend = self._color(self.RESET)
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"""
if not force and not self.debug:
return
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]
cstart = self._color(self.MAGENTA)
cend = self._color(self.RESET)
clight = self._color(self.LMAGENTA)
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'
sys.stderr.write(line)

def dry(self, string, end='\n'):
def dry(self, string: str, end: str = '\n') -> None:
"""dry run log"""
cstart = self._color(self.GREEN)
cend = self._color(self.RESET)
sys.stdout.write(f'{cstart}[DRY] {string} {end}{cend}')

@classmethod
def raw(cls, string, end='\n'):
def raw(cls, string: str, end: str = '\n') -> None:
"""raw log"""
sys.stdout.write(f'{string}{end}')

def ask(self, query):
def ask(self, query: str) -> bool:
"""ask user for confirmation"""
cstart = self._color(self.BLUE)
cend = self._color(self.RESET)
Expand All @@ -102,7 +109,7 @@ def ask(self, query):
return resp == 'y'

@classmethod
def _color(cls, col):
def _color(cls, col: str) -> str:
"""is color supported"""
if not sys.stdout.isatty():
return ''
Expand Down
12 changes: 6 additions & 6 deletions scripts/check-syntax.sh
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,20 @@ pylint \
--disable=R0904 \
dotdrop/

# pytype
echo "----------------------------"
echo "checking dotdrop with pytype"
pytype dotdrop/

# mypy
echo "----------------------------"
echo "checking dotdrop with mypy"
# --strict
mypy \
--strict \
--ignore-missing-imports \
--allow-redefinition \
dotdrop/

# pytype
echo "----------------------------"
echo "checking dotdrop with pytype"
pytype dotdrop/

# check shell scripts
# SC2002: Useless cat
# SC2126: Consider using grep -c instead of grep|wc -l
Expand Down

0 comments on commit d9fabe3

Please sign in to comment.