File: pseudo_modules.py

package info (click to toggle)
pyinfra 0.2.2%2Bgit20161227.ec708ef-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,804 kB
  • ctags: 677
  • sloc: python: 5,944; sh: 71; makefile: 11
file content (53 lines) | stat: -rw-r--r-- 1,311 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
# pyinfra
# File: pyinfra/pseudo_modules.py
# Desc: essentially a hack that provides dynamic imports for the current deploy (CLI only)

import sys
import pyinfra


class PseudoModule(object):
    _module = None

    def __getattr__(self, key):
        return getattr(self._module, key)

    def __setattr__(self, key, value):
        if key == '_module':
            return object.__setattr__(self, key, value)

        setattr(self._module, key, value)

    def __getitem__(self, key):
        return self._module[key]

    def __iter__(self):
        return iter(self._module)

    def __len__(self):
        return len(self._module)

    def set(self, module):
        self._module = module

    def reset(self):
        self._module = None

    def isset(self):
        return self._module is not None


# The current deploy state
sys.modules['pyinfra.pseudo_state'] = sys.modules['pyinfra.state'] = \
    pyinfra.pseudo_state = pyinfra.state = \
    PseudoModule()

# The current deploy inventory
sys.modules['pyinfra.pseudo_inventory'] = sys.modules['pyinfra.inventory'] = \
    pyinfra.pseudo_inventory = pyinfra.inventory = \
    PseudoModule()

# The current target host
sys.modules['pyinfra.pseudo_host'] = sys.modules['pyinfra.host'] = \
    pyinfra.pseudo_host = pyinfra.host = \
    PseudoModule()