~ubuntu-branches/debian/experimental/nuitka/experimental

« back to all changes in this revision

Viewing changes to nuitka/tree/ReformulationNamespacePackages.py

  • Committer: Package Import Robot
  • Author(s): Kay Hayen
  • Date: 2015-04-06 17:20:44 UTC
  • mfrom: (1.1.37)
  • Revision ID: package-import@ubuntu.com-20150406172044-grhy9sk7g0whu2ag
Tags: 0.5.12+ds-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
"""
22
22
 
 
23
from nuitka import Options
23
24
from nuitka.nodes.AssignNodes import StatementAssignmentVariable
 
25
from nuitka.nodes.AttributeNodes import ExpressionAttributeLookup
24
26
from nuitka.nodes.CallNodes import ExpressionCallNoKeywords
25
27
from nuitka.nodes.ConstantRefNodes import ExpressionConstantRef
 
28
from nuitka.nodes.ContainerMakingNodes import (
 
29
    ExpressionMakeList,
 
30
    ExpressionMakeTuple
 
31
)
26
32
from nuitka.nodes.FutureSpecs import FutureSpec
27
33
from nuitka.nodes.ImportNodes import (
28
34
    ExpressionImportModule,
 
35
    ExpressionImportModuleHard,
29
36
    ExpressionImportName
30
37
)
31
 
from nuitka.nodes.ModuleNodes import PythonPackage
 
38
from nuitka.nodes.ModuleNodes import (
 
39
    ExpressionModuleFileAttributeRef,
 
40
    PythonPackage
 
41
)
32
42
from nuitka.nodes.VariableRefNodes import ExpressionTargetVariableRef
33
43
from nuitka.SourceCodeReferences import SourceCodeReference
 
44
from nuitka.utils import Utils
34
45
 
35
46
from .Helpers import makeStatementsSequenceFromStatement
36
47
from .VariableClosure import completeVariableClosures
37
48
 
38
49
 
 
50
def createPathAssignment(source_ref):
 
51
    if Options.getFileReferenceMode() == "original":
 
52
        path_value = ExpressionConstantRef(
 
53
            constant      = [
 
54
                Utils.dirname(source_ref.getFilename())
 
55
            ],
 
56
            source_ref    = source_ref,
 
57
            user_provided = True
 
58
        )
 
59
    else:
 
60
        path_value = ExpressionMakeList(
 
61
            elements   = (
 
62
                ExpressionCallNoKeywords(
 
63
                    called     = ExpressionAttributeLookup(
 
64
                        source         = ExpressionImportModuleHard(
 
65
                            module_name = "os",
 
66
                            import_name = "path",
 
67
                            source_ref  = source_ref
 
68
                        ),
 
69
                        attribute_name = "dirname",
 
70
                        source_ref     = source_ref
 
71
                    ),
 
72
                    args       = ExpressionMakeTuple(
 
73
                        elements   = (
 
74
                            ExpressionModuleFileAttributeRef(
 
75
                                source_ref = source_ref,
 
76
                            ),
 
77
                        ),
 
78
                        source_ref = source_ref,
 
79
                    ),
 
80
                    source_ref = source_ref,
 
81
                ),
 
82
            ),
 
83
            source_ref = source_ref
 
84
        )
 
85
 
 
86
    return  StatementAssignmentVariable(
 
87
        variable_ref = ExpressionTargetVariableRef(
 
88
            variable_name = "__path__",
 
89
            source_ref    = source_ref
 
90
        ),
 
91
        source       = path_value,
 
92
        source_ref   = source_ref
 
93
    )
 
94
 
 
95
def createPython3NamespacePath(package_name, module_relpath, source_ref):
 
96
    return StatementAssignmentVariable(
 
97
        variable_ref = ExpressionTargetVariableRef(
 
98
            variable_name = "__path__",
 
99
            source_ref    = source_ref
 
100
        ),
 
101
        source       = ExpressionCallNoKeywords(
 
102
            called     = ExpressionImportName(
 
103
                module      = ExpressionImportModule(
 
104
                    module_name = "_frozen_importlib",
 
105
                    import_list = (),
 
106
                    level       = 0,
 
107
                    source_ref  = source_ref
 
108
                ),
 
109
                import_name = "_NamespacePath",
 
110
                source_ref  = source_ref
 
111
            ),
 
112
            args       = ExpressionConstantRef(
 
113
                constant   = (
 
114
                    package_name,
 
115
                    [module_relpath],
 
116
                    None
 
117
                ),
 
118
                source_ref =  source_ref
 
119
            ),
 
120
            source_ref =  source_ref
 
121
        ),
 
122
        source_ref   = source_ref
 
123
    )
 
124
 
 
125
 
39
126
def createNamespacePackage(package_name, module_relpath):
40
127
    parts = package_name.split('.')
41
128
 
53
140
        source_ref   = source_ref,
54
141
    )
55
142
 
 
143
    if Utils.python_version >= 300:
 
144
        statement = createPython3NamespacePath(
 
145
            package_name   = package_name,
 
146
            module_relpath = module_relpath,
 
147
            source_ref     = source_ref
 
148
        )
 
149
    else:
 
150
        statement = createPathAssignment(source_ref)
 
151
 
56
152
    package.setBody(
57
153
        makeStatementsSequenceFromStatement(
58
 
            statement = (
59
 
                StatementAssignmentVariable(
60
 
                    variable_ref = ExpressionTargetVariableRef(
61
 
                        variable_name = "__path__",
62
 
                        source_ref    = source_ref
63
 
                    ),
64
 
                    source       = ExpressionCallNoKeywords(
65
 
                        called     = ExpressionImportName(
66
 
                            module      = ExpressionImportModule(
67
 
                                module_name = "_frozen_importlib",
68
 
                                import_list = (),
69
 
                                level       = 0,
70
 
                                source_ref  = source_ref
71
 
                            ),
72
 
                            import_name = "_NamespacePath",
73
 
                            source_ref  = source_ref
74
 
                        ),
75
 
                        args       = ExpressionConstantRef(
76
 
                            constant   = (
77
 
                                package_name,
78
 
                                [module_relpath],
79
 
                                None
80
 
                            ),
81
 
                            source_ref =  source_ref
82
 
                        ),
83
 
                        source_ref =  source_ref
84
 
                    ),
85
 
                    source_ref   = source_ref
86
 
                )
87
 
            )
 
154
            statement = statement
88
155
        )
89
156
    )
90
157