Welcome to mirror list, hosted at ThFree Co, Russian Federation.

helpers.py « bpsrender « BPSRender « scripts « power_sequencer - git.blender.org/blender-addons.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58f797755a5e1594b74cd6815c7a909dcc2717d2 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#
# Copyright (C) 2016-2019 by Razvan Radulescu, Nathan Lovato, and contributors
#
# This file is part of Power Sequencer.
#
# Power Sequencer is free software: you can redistribute it and/or modify it under the terms of the
# GNU General Public License as published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Power Sequencer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with Power Sequencer. If
# not, see <https://www.gnu.org/licenses/>.
#
from collections import deque
from shutil import which


class BSError(Exception):
    """
    Custom Exception raised if Blender is called with a python script argument
    and gives error while trying to execute the script.
    """

    pass


class ToolError(Exception):
    """Raised if external dependencies aren't found on system.
    """

    pass


def checktools(tools):
    tools = [(t, which(t) or "") for t in tools]
    check = {"tools": tools, "test": all(map(lambda x: x[1], tools))}
    if not check["test"]:
        msg = ["BPSRender couldn't find external dependencies:"]
        msg += [
            "[{check}] {tool}: {path}".format(
                check="v" if path != "" else "X", tool=tool, path=path or "NOT FOUND"
            )
            for tool, path in check["tools"]
        ]
        msg += [
            (
                "Check if you have them properly installed and available in the PATH"
                " environemnt variable."
            ),
            "Exiting...",
        ]
        raise ToolError("\n".join(msg))


def checkblender(what, search, cp, s):
    """
    IMPURE
    Check Blender output for python script execution error.

    Parameters
    ----------
    what: str
    A tag used in the exception message.
    search: iter(str)
    One or more string(s) to search for in Blender's output.
    cp: Popen
    Blender subprocess.
    s: PIPE
    Blender's output.

    Returns
    -------
    out: PIPE
    The same pipe `s` is returned so that it can be iterated over on later
    steps.
    """
    if not isinstance(search, list):
        search = [search]
    for search_item in search:
        if search_item in s:
            message = (
                "Script {what} was not properly executed in" " Blender".format(what=what),
                "CMD: {cmd}".format(what=what, cmd=" ".join(cp.args)),
                "DUMP:".format(what=what),
                s,
            )
            raise BSError("\n".join(message))
    return s


def printw(cfg, text, s="\n", e="...", p="", **kwargs):
    p = p or cfg["pre"]["work"]
    print("{s}{p} {}{e}".format(text, s=s, e=e, p=p), **kwargs)


def printd(cfg, text, s="", e=".", p="", **kwargs):
    p = p or cfg["pre"]["done"]
    printw(cfg, text, s=s, e=e, p=p, **kwargs)


def prints(cfg, text, s="", e=".", p="", **kwargs):
    p = p or cfg["pre"]["skip"]
    printw(cfg, text, s=s, e=e, p=p, **kwargs)


def kickstart(it):
    deque(it, maxlen=0)