Debian Codegraph @

src/pygame_sdl2/__init__.py

raw
1 # Copyright 2014 Tom Rothamel <[email protected]>
2 # Copyright 2014 Patrick Dawson <[email protected]>
3 #
4 # This software is provided 'as-is', without any express or implied
5 # warranty. In no event will the authors be held liable for any damages
6 # arising from the use of this software.
7 #
8 # Permission is granted to anyone to use this software for any purpose,
9 # including commercial applications, and to alter it and redistribute it
10 # freely, subject to the following restrictions:
11 #
12 # 1. The origin of this software must not be misrepresented; you must not
13 # claim that you wrote the original software. If you use this software
14 # in a product, an acknowledgment in the product documentation would be
15 # appreciated but is not required.
16 # 2. Altered source versions must be plainly marked as such, and must not be
17 # misrepresented as being the original software.
18 # 3. This notice may not be removed or altered from any source distribution.
19
20 from __future__ import division, print_function, absolute_import
21
22 import sys
23 import importlib
24
25
26 class MissingModule(object):
27
28 def __init__(self, name, reason):
29 self.__name__ = name
30 self.reason = reason
31
32 def __getattr__(self, attr):
33 raise NotImplementedError(self.reason)
34
35
36 def try_import(name):
37 full_name = "pygame_sdl2." + name
38
39 try:
40 module = importlib.import_module(full_name)
41 except (IOError, ImportError) as e:
42 module = MissingModule(full_name, "Could not import {}: {}".format(full_name, str(e)))
43
44 globals()[name] = module
45 sys.modules[full_name] = module
46
47
48 # Lists of functions that are called on init and quit.
49 init_functions = [ ]
50 quit_functions = [ ]
51
52
53 def register_init(fn):
54 init_functions.append(fn)
55 return fn
56
57
58 def register_quit(fn):
59 quit_functions.append(fn)
60 return fn
61
62
63 def init():
64 numpass = 0
65 numfail = 0
66
67 for i in init_functions:
68 try:
69 i()
70 numpass += 1
71 except:
72 numfail += 1
73
74 return numpass, numfail
75
76
77 def quit(): # @ReservedAssignment
78 for i in quit_functions:
79 try:
80 i()
81 except:
82 pass
83
84
85 # Import core modules.
86 from pygame_sdl2.error import *
87
88 from pygame_sdl2.surface import Surface
89 from pygame_sdl2.rect import Rect
90
91 import pygame_sdl2.color
92 import pygame_sdl2.display
93 import pygame_sdl2.event
94 import pygame_sdl2.key
95 import pygame_sdl2.locals # @ReservedAssignment
96 import pygame_sdl2.time
97 import pygame_sdl2.version
98 import pygame_sdl2.locals as constants
99
100 # Import optional modules.
101 try_import("controller")
102 try_import("draw")
103 try_import("font")
104 try_import("image")
105 try_import("joystick")
106 try_import("mixer")
107 try_import("mouse")
108 try_import("power")
109 try_import("transform")
110 try_import("scrap")
111 try_import("sprite")
112 try_import("sysfont")
113
114 # Optional imports should be included in this function, so they show up
115 # in packaging tools (like py2exe).
116
117
118 def _optional_imports():
119 import pygame_sdl2.compat
120 import pygame_sdl2.controller
121 import pygame_sdl2.rwobject
122 import pygame_sdl2.gfxdraw
123 import pygame_sdl2.draw
124 import pygame_sdl2.font
125 import pygame_sdl2.image
126 import pygame_sdl2.joystick
127 import pygame_sdl2.mixer
128 import pygame_sdl2.mouse
129 import pygame_sdl2.power
130 import pygame_sdl2.transform
131 import pygame_sdl2.scrap
132 import pygame_sdl2.sprite
133 import pygame_sdl2.sysfont
134
135
136 # Fill this module with locals.
137 from pygame_sdl2.locals import *
138
139
140 def import_as_pygame():
141 """
142 Imports pygame_sdl2 as pygame, so that running the 'import pygame.whatever'
143 statement will import pygame_sdl2.whatever instead.
144 """
145
146 import os
147 import warnings
148
149 if "PYGAME_SDL2_USE_PYGAME" in os.environ:
150 return
151
152 if "pygame" in sys.modules:
153 warnings.warn("Pygame has already been imported, import_as_pygame may not work.", stacklevel=2)
154
155 for name, mod in list(sys.modules.items()):
156 name = name.split('.')
157 if name[0] != 'pygame_sdl2':
158 continue
159
160 name[0] = 'pygame'
161 name = ".".join(name)
162
163 sys.modules[name] = mod
164
165 sys.modules['pygame.constants'] = constants
166
167
168 def get_sdl_byteorder():
169 return BYTEORDER
170
171
172 def get_sdl_version():
173 return SDL_VERSION_TUPLE
174
175
176 get_platform = pygame_sdl2.display.get_platform
177
178 # We have new-style buffers, but not the pygame.newbuffer module.
179 HAVE_NEWBUF = False