summaryrefslogtreecommitdiffstats
path: root/google-appengine/google/appengine/dist/py_imp.py
blob: cb097bc79332a33c940285aa2ad2eec0801d29aa (plain) (blame)
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

"""Stub replacement for Python's imp module."""


import os
import sys


PY_SOURCE, PY_COMPILED, C_EXTENSION = 1, 2, 3
PKG_DIRECTORY, C_BUILTIN, PY_FROZEN = 5, 6, 7


def get_magic():
  """Return the magic string used to recognize byte-compiled code files."""
  return '\xb3\xf2\r\n'


_PY_SOURCE_SUFFIX = ('.py', 'U', PY_SOURCE)
_PKG_DIRECTORY_SUFFIX = ('', '', PKG_DIRECTORY)


def get_suffixes():
  """Return a list that describes the files that find_module() looks for."""
  return [_PY_SOURCE_SUFFIX]


def find_module(name, path=None):
  """Try to find the named module on the given search path or sys.path."""
  if path == None:
    path = sys.path

  for directory in path:
    filename = os.path.join(directory, '%s.py' % name)
    if os.path.exists(filename):
      return open(filename, 'U'), filename, _PY_SOURCE_SUFFIX

    dirname = os.path.join(directory, name)
    filename = os.path.join(dirname, '__init__.py')
    if os.path.exists(filename):
      return None, dirname, _PKG_DIRECTORY_SUFFIX

  raise ImportError('No module named %s' % name)


def load_module(name, file_, pathname, description):
  """Load or reload the specified module.

  Please note that this function has only rudimentary supported on App Engine:
  Only loading packages is supported.
  """
  suffix, mode, type_ = description
  if type_ == PKG_DIRECTORY:
    if name in sys.modules:
      mod = sys.modules[name]
    else:
      mod = new_module(name)
      sys.modules[name] = mod
    filename = os.path.join(pathname, '__init__.py')
    mod.__file__ = filename
    execfile(filename, mod.__dict__, mod.__dict__)
    return mod
  else:
    raise NotImplementedError('Only importing packages is supported on '
                              'App Engine')


def new_module(name):
  """Return a new empty module object."""
  return type(sys)(name)


def lock_held():
  """Return False since threading is not supported."""
  return False


def acquire_lock():
  """Acquiring the lock is a no-op since no threading is supported."""
  pass


def release_lock():
  """There is no lock to release since acquiring is a no-op when there is no
  threading."""
  pass


def init_builtin(name):
  raise NotImplementedError('This function is not supported on App Engine.')


def init_frozen(name):
  raise NotImplementedError('This function is not supported on App Engine.')


def is_builtin(name):
  return name in sys.builtin_module_names


def is_frozen(name):
  return False


def load_compiled(name, pathname, file_=None):
  raise NotImplementedError('This function is not supported on App Engine.')


def load_dynamic(name, pathname, file_=None):
  raise NotImplementedError('This function is not supported on App Engine.')


def load_source(name, pathname, file_=None):
  raise NotImplementedError('This function is not supported on App Engine.')


class NullImporter(object):
  """Null importer object"""

  def __init__(self, path_string):
    if not path_string:
      raise ImportError("empty pathname")
    elif os.path.isdir(path_string):
      raise ImportError("existing directory")

  def find_module(self, fullname):
    return None