• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

deadc0de6 / catcli / 4399176145

pending completion
4399176145

Pull #30

github

GitHub
Merge ee2cf80d9 into 7590ad02c
Pull Request #30: Fuse

474 of 474 new or added lines in 13 files covered. (100.0%)

1304 of 1691 relevant lines covered (77.11%)

3.86 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.69
/catcli/catalog.py
1
"""
2
author: deadc0de6 (https://github.com/deadc0de6)
3
Copyright (c) 2017, deadc0de6
4

5
Class that represents the catcli catalog
6
"""
7

8
import os
5✔
9
from typing import Optional
5✔
10
from anytree.exporter import JsonExporter  # type: ignore
5✔
11
from anytree.importer import JsonImporter  # type: ignore
5✔
12

13
# local imports
14
from catcli import nodes
5✔
15
from catcli.nodes import NodeMeta, NodeTop
5✔
16
from catcli.utils import ask
5✔
17
from catcli.logger import Logger
5✔
18

19

20
class Catalog:
5✔
21
    """the catalog"""
22

23
    def __init__(self, path: str,
5✔
24
                 debug: bool = False,
25
                 force: bool = False) -> None:
26
        """
27
        @path: catalog path
28
        @usepickle: use pickle
29
        @debug: debug mode
30
        @force: force overwrite if exists
31
        """
32
        self.path = path
5✔
33
        self.debug = debug
5✔
34
        self.force = force
5✔
35
        self.metanode: Optional[NodeMeta] = None
5✔
36

37
    def set_metanode(self, metanode: NodeMeta) -> None:
5✔
38
        """remove the metanode until tree is re-written"""
39
        self.metanode = metanode
5✔
40
        if self.metanode:
5✔
41
            self.metanode.parent = None
5✔
42

43
    def exists(self) -> bool:
5✔
44
        """does catalog exist"""
45
        if not self.path:
5✔
46
            return False
×
47
        if os.path.exists(self.path):
5✔
48
            return True
5✔
49
        return False
×
50

51
    def restore(self) -> Optional[NodeTop]:
5✔
52
        """restore the catalog"""
53
        if not self.path:
5✔
54
            return None
×
55
        if not os.path.exists(self.path):
5✔
56
            return None
5✔
57
        with open(self.path, 'r', encoding='UTF-8') as file:
5✔
58
            content = file.read()
5✔
59
        return self._restore_json(content)
5✔
60

61
    def save(self, node: NodeTop) -> bool:
5✔
62
        """save the catalog"""
63
        if not self.path:
5✔
64
            Logger.err('Path not defined')
×
65
            return False
×
66
        directory = os.path.dirname(self.path)
5✔
67
        if directory and not os.path.exists(directory):
5✔
68
            os.makedirs(directory)
×
69
        elif os.path.exists(self.path) and not self.force:
5✔
70
            if not ask(f'Update catalog \"{self.path}\"'):
×
71
                Logger.info('Catalog not saved')
×
72
                return False
×
73
        if directory and not os.path.exists(directory):
5✔
74
            Logger.err(f'Cannot write to \"{directory}\"')
×
75
            return False
×
76
        if self.metanode:
5✔
77
            self.metanode.parent = node
5✔
78
        return self._save_json(node)
5✔
79

80
    def _debug(self, text: str) -> None:
5✔
81
        if not self.debug:
5✔
82
            return
5✔
83
        Logger.debug(text)
×
84

85
    def _save_json(self, top: NodeTop) -> bool:
5✔
86
        """export the catalog in json"""
87
        self._debug(f'saving {top} to json...')
5✔
88
        exp = JsonExporter(indent=2, sort_keys=True)
5✔
89
        with open(self.path, 'w', encoding='UTF-8') as file:
5✔
90
            exp.write(top, file)
5✔
91
        self._debug(f'Catalog saved to json \"{self.path}\"')
5✔
92
        return True
5✔
93

94
    def _restore_json(self, string: str) -> Optional[NodeTop]:
5✔
95
        """restore the tree from json"""
96
        imp = JsonImporter()
5✔
97
        self._debug(f'import from string: {string}')
5✔
98
        root = imp.import_(string)
5✔
99
        self._debug(f'Catalog imported from json \"{self.path}\"')
5✔
100
        self._debug(f'root imported: {root}')
5✔
101
        if root.type != nodes.TYPE_TOP:
5✔
102
            return None
×
103
        top = NodeTop(root.name, children=root.children)
5✔
104
        self._debug(f'top imported: {top}')
5✔
105
        return top
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc