File: stats_reader.py

package info (click to toggle)
trash-cli 0.24.5.26-0.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,972 kB
  • sloc: python: 9,789; sh: 121; makefile: 11
file content (55 lines) | stat: -rw-r--r-- 1,634 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Copyright (C) 2007-2024 Andrea Francia Trivolzio(PV) Italy

import os
from grp import getgrgid
from pwd import getpwuid
from typing import NamedTuple
import re

from trashcli.put.core.either import Either
from trashcli.put.core.either import Left
from trashcli.put.core.either import Right


def gentle_stat_read(path):
    def stats_str(stats):  # type: (Either[Stats, Exception]) -> str
        if isinstance(result, Right):
            value = result.value()
            return "%s %s %s" % (value.octal_mode(), value.user, value.group)
        elif isinstance(result, Left):
            return str(result.error())
        else:
            raise ValueError()

    result = StatReader().read_stats(path)
    return stats_str(result)


class Stats(NamedTuple('Result', [
    ('user', str),
    ('group', str),
    ('mode', int),
])):
    def octal_mode(self):  # () -> str
        return self._remove_octal_prefix(oct(self.mode & 0o777))

    @staticmethod
    def _remove_octal_prefix(mode):  # type: (str) -> str
        remove_new_octal_format = mode.replace('0o', '')
        remove_old_octal_format = re.sub(r"^0", '', remove_new_octal_format)
        return remove_old_octal_format


class StatReader:
    @staticmethod
    def read_stats(path,  # type: str
                   ):  # type: (...) -> Either[Stats, Exception]
        try:
            stats = os.lstat(path)
            user = getpwuid(stats.st_uid).pw_name
            group = getgrgid(stats.st_gid).gr_name
            mode = stats.st_mode

            return Right(Stats(user, group, mode))
        except (IOError, OSError) as e:
            return Left(e)