# coding: utf-8
"""
General utils
"""

import os
import sys
import grp
import pwd


class TablePrint:
    """
    Print table on the CLI.
    """

    def __init__(self, table):
        """
        Table is [(1,2,3,), (4,5,6,),] etc data.
        """
        self.table = table
        self.widths = []

    def _check(self):
        """
        Check if table is consistent grid.
        Header is a leader here.
        """
        if not self.table:
            raise Exception("Table is empty!")

        header = None
        for row in self.table:
            if header is None:
                header = len(row)
                continue
            if len(row) != header:
                raise Exception("Table has different row widths.")

    def _get_widths(self):
        """
        Find extra-widths by max width of any value.
        """

        self.widths = [0 for _ in self.table[0]]
        for row in self.table:
            for idx, cell in enumerate(row):
                cell_len = len(str(cell))
                if cell_len > self.widths[idx]:
                    self.widths[idx] = cell_len

    def _format(self):
        """
        Format the output.
        """
        out = []
        ftable = []
        for row in self.table:
            frow = []
            for idx, cell in enumerate(row):
                frow.append(str(cell) + (" " * (self.widths[idx] - len(str(cell)))))
            ftable.append(frow)

        for idx, row in enumerate(ftable):
            out.append(' | '.join(row))
            if not idx:
                out.append('-+-'.join(["-" * len(item) for item in row]))

        return '\n'.join(out)

    def __str__(self):
        self._check()
        self._get_widths()
        return self._format()


def create_dirs(path, owner, mode=0o700):
    """
    Create path and change owner of it accordingly.
    Default mode is 0700
    """
    if not os.path.exists(path):
        os.makedirs(path, mode=mode)
        owner = pwd.getpwnam(owner)
        os.chown(path, owner.pw_uid, owner.pw_gid)
        return True

    return False


def get_path_owner(path):
    """
    Returns the owner and group IDs of a directory.
    """
    class Owner:
        """
        Owner class
        """
        def __init__(self):
            self.uid = -1
            self.gid = -1
            self.user = None
            self.group = None

    owner = Owner()
    stat_info = os.stat(path)
    owner.uid = stat_info.st_uid
    owner.gid = stat_info.st_gid
    owner.user = pwd.getpwuid(owner.uid)[0]
    owner.group = grp.getgrgid(owner.gid)[0]

    return owner


# pylint: disable=R1706,W0212,R0911
def unquote(self, elm):
    """
    Unquote an element.
    """
    if elm is None:
        return None

    elm = elm.strip()
    if not elm or len(elm) < 2:
        return elm

    return (elm[0] == elm[-1] and elm[0] in '\'"') and self._dequote(elm[1:][:-1]) or elm
# pylint: enable=R1706,W0212,R0911


def eprint(*args, **kwargs):
    """
    Print to the STDERR.

    :param args: print arguments
    :param kwargs: keywords
    :return: None
    """
    print(*args, file=sys.stderr, **kwargs)

@@ -75,16 +75,30 @@
 ort pwd%0A
+import typing%0A
 %0A%0Aclass 
@@ -178,24 +178,37 @@
 (self, table
+: typing.List
 ):%0A        %22
@@ -313,24 +313,30 @@
  self.widths
+: list
  = %5B%5D%0A%0A    d
@@ -346,24 +346,32 @@
 _check(self)
+ -%3E None
 :%0A        %22%22
@@ -802,24 +802,32 @@
 widths(self)
+ -%3E None
 :%0A        %22%22
@@ -1175,24 +1175,31 @@
 format(self)
+ -%3E str
 :%0A        %22%22
@@ -1829,23 +1829,33 @@
 irs(path
+: str
 , owner
+: str
 , mode=0
@@ -3012,17 +3012,16 @@
 ,R0911%0A%0A
-%0A
 def epri
@@ -3028,27 +3028,59 @@
 nt(*args
-, **kwargs)
+: typing.Any, **kwargs: typing.Any) -%3E None
 :%0A    %22%22

Add typing annotations