<gh_stars>0
import sys
import string
import random

from .orm import Engine


class Command:
    def __init__(self):
        self.engine = Engine()

    def _add(self, domain: str, username: str, password: str):
        self.engine.insert(domain, username, password)

    def _del(self, id_: int):
        self.engine.delete(id_)

    def _set(self, id_: int, *args):
        kwargs = dict(zip(args[::2], args[1::2]))
        self.engine.update(id_, **kwargs)
        self._get(id_)

    def _get(self, id_: int):
        queryset = self.engine.get(id_)
        self.show([queryset])

    def _list(self):
        queryset = self.engine.filter()
        self.show(queryset)

    def _search(self, keyword):
        queryset = self.engine.like(domain=keyword, username=keyword, password=keyword)
        self.show(queryset)

    @staticmethod
    def gen_pwd(length: int):
        pwd = ''.join(random.choices(string.printable[:-6], k=int(length)))
        print(pwd)
        return pwd

    def show(self, queryset):
        result = [self.engine.fields] + queryset
        width = [max(map(len, map(str, row))) for row in zip(*result)]

        print(f'+-{"-+-".join(["-" * w for w in width])}-+')
        for row in result:
            print(f'| {" | ".join([str(col).center(width[i]) for i, col in enumerate(row)])} |')
            print(f'+-{"-+-".join(["-" * w for w in width])}-+')

    def execute(self):
        cmd = {
            'add': self._add,
            'del': self._del,
            'set': self._set,
            'get': self._get,
            'list': self._list,
            'search': self._search,
            'gen': self.gen_pwd
        }
        args = sys.argv[1:]
        cmd[args[0]](*args[1:])

import sys
import string
import random

from .orm import Engine


class Command:
    def __init__(self):
        self.engine = Engine()

    def _add(self, domain: str, username: str, password: str):
        self.engine.insert(domain, username, password)

    def _del(self, id_: int):
        self.engine.delete(id_)

    def _set(self, id_: int, *args):
        kwargs = dict(zip(args[::2], args[1::2]))
        self.engine.update(id_, **kwargs)
        self._get(id_)

    def _get(self, id_: int):
        queryset = self.engine.get(id_)
        self.show([queryset])

    def _list(self):
        queryset = self.engine.filter()
        self.show(queryset)

    def _search(self, keyword):
        queryset = self.engine.like(domain=keyword, username=keyword, password=keyword)
        self.show(queryset)

    @staticmethod
    def gen_pwd(length: int):
        pwd = ''.join(random.choices(string.printable[:-6], k=int(length)))
        print(pwd)
        return pwd

    def show(self, queryset):
        result = [self.engine.fields] + queryset
        width = [max(map(len, map(str, row))) for row in zip(*result)]

        print(f'+-{"-+-".join(["-" * w for w in width])}-+')
        for row in result:
            print(f'| {" | ".join([str(col).center(width[i]) for i, col in enumerate(row)])} |')
            print(f'+-{"-+-".join(["-" * w for w in width])}-+')

    def execute(self):
        cmd = {
            'add': self._add,
            'del': self._del,
            'set': self._set,
            'get': self._get,
            'list': self._list,
            'search': self._search,
            'gen': self.gen_pwd
        }
        args = sys.argv[1:]
        cmd[args[0]](*args[1:])