~ubuntu-branches/ubuntu/maverick/python3.1/maverick

« back to all changes in this revision

Viewing changes to Lib/importlib/abc.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-03-23 00:01:27 UTC
  • Revision ID: james.westby@ubuntu.com-20090323000127-5fstfxju4ufrhthq
Tags: upstream-3.1~a1+20090322
ImportĀ upstreamĀ versionĀ 3.1~a1+20090322

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Abstract base classes related to import."""
 
2
from . import _bootstrap
 
3
from . import machinery
 
4
import abc
 
5
import types
 
6
 
 
7
 
 
8
class Loader(metaclass=abc.ABCMeta):
 
9
 
 
10
    """Abstract base class for import loaders."""
 
11
 
 
12
    @abc.abstractmethod
 
13
    def load_module(self, fullname:str) -> types.ModuleType:
 
14
        """Abstract method which when implemented should load a module."""
 
15
        raise NotImplementedError
 
16
 
 
17
 
 
18
class Finder(metaclass=abc.ABCMeta):
 
19
 
 
20
    """Abstract base class for import finders."""
 
21
 
 
22
    @abc.abstractmethod
 
23
    def find_module(self, fullname:str, path:[str]=None) -> Loader:
 
24
        """Abstract method which when implemented should find a module."""
 
25
        raise NotImplementedError
 
26
 
 
27
Finder.register(machinery.BuiltinImporter)
 
28
Finder.register(machinery.FrozenImporter)
 
29
Finder.register(machinery.PathFinder)
 
30
 
 
31
 
 
32
class ResourceLoader(Loader):
 
33
 
 
34
    """Abstract base class for loaders which can return data from their
 
35
    back-end storage.
 
36
 
 
37
    This ABC represents one of the optional protocols specified by PEP 302.
 
38
 
 
39
    """
 
40
 
 
41
    @abc.abstractmethod
 
42
    def get_data(self, path:str) -> bytes:
 
43
        """Abstract method which when implemented should return the bytes for
 
44
        the specified path."""
 
45
        raise NotImplementedError
 
46
 
 
47
 
 
48
class InspectLoader(Loader):
 
49
 
 
50
    """Abstract base class for loaders which support inspection about the
 
51
    modules they can load.
 
52
 
 
53
    This ABC represents one of the optional protocols specified by PEP 302.
 
54
 
 
55
    """
 
56
 
 
57
    @abc.abstractmethod
 
58
    def is_package(self, fullname:str) -> bool:
 
59
        """Abstract method which when implemented should return whether the
 
60
        module is a package."""
 
61
        return NotImplementedError
 
62
 
 
63
    @abc.abstractmethod
 
64
    def get_code(self, fullname:str) -> types.CodeType:
 
65
        """Abstract method which when implemented should return the code object
 
66
        for the module"""
 
67
        return NotImplementedError
 
68
 
 
69
    @abc.abstractmethod
 
70
    def get_source(self, fullname:str) -> str:
 
71
        """Abstract method which should return the source code for the
 
72
        module."""
 
73
        return NotImplementedError
 
74
 
 
75
InspectLoader.register(machinery.BuiltinImporter)
 
76
InspectLoader.register(machinery.FrozenImporter)
 
77
 
 
78
 
 
79
class PyLoader(_bootstrap.PyLoader, InspectLoader):
 
80
 
 
81
    """Abstract base class to assist in loading source code by requiring only
 
82
    back-end storage methods to be implemented.
 
83
 
 
84
    The methods get_code, get_source, and load_module are implemented for the
 
85
    user.
 
86
 
 
87
    """
 
88
 
 
89
    @abc.abstractmethod
 
90
    def source_path(self, fullname:str) -> object:
 
91
        """Abstract method which when implemented should return the path to the
 
92
        sourced code for the module."""
 
93
        raise NotImplementedError
 
94
 
 
95
 
 
96
class PyPycLoader(_bootstrap.PyPycLoader, PyLoader):
 
97
 
 
98
    """Abstract base class to assist in loading source and bytecode by
 
99
    requiring only back-end storage methods to be implemented.
 
100
 
 
101
    The methods get_code, get_source, and load_module are implemented for the
 
102
    user.
 
103
 
 
104
    """
 
105
 
 
106
    @abc.abstractmethod
 
107
    def source_mtime(self, fullname:str) -> int:
 
108
        """Abstract method which when implemented should return the
 
109
        modification time for the source of the module."""
 
110
        raise NotImplementedError
 
111
 
 
112
    @abc.abstractmethod
 
113
    def bytecode_path(self, fullname:str) -> object:
 
114
        """Abstract method which when implemented should return the path to the
 
115
        bytecode for the module."""
 
116
        raise NotImplementedError
 
117
 
 
118
    @abc.abstractmethod
 
119
    def write_bytecode(self, fullname:str, bytecode:bytes):
 
120
        """Abstract method which when implemented should attempt to write the
 
121
        bytecode for the module."""
 
122
        raise NotImplementedError