Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement lazy imports #2584

Closed
wants to merge 42 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
1ca4be1
add lazy imports to toga core
KRRT7 May 20, 2024
2ea3248
add lazy_imports to toga_cocoa
KRRT7 May 20, 2024
9bd86ed
Create 2547.feature.rst
KRRT7 May 20, 2024
92f75ef
add changes rst
KRRT7 May 20, 2024
3840644
factory / toga init __all__ changes
KRRT7 May 20, 2024
4027c2c
remove unneeded blank lines
KRRT7 May 20, 2024
5d75865
optimization path for the lazy loader
KRRT7 May 20, 2024
176e854
__all__ must be generated at the top level of the module
KRRT7 May 20, 2024
58cacb4
fixup
KRRT7 May 21, 2024
ed7f885
sort the dictionary
KRRT7 May 21, 2024
fc88269
it's a misc, not feat
KRRT7 May 21, 2024
4af3866
ghosts and stuff
KRRT7 May 21, 2024
211a7aa
update gtk slider
KRRT7 May 29, 2024
e8e169f
update android slider
KRRT7 May 29, 2024
411307d
misspell
KRRT7 May 29, 2024
0d46e14
update from toga to toga_cocoa
KRRT7 May 29, 2024
66e292b
Correct wording of release note.
freakboy3742 May 29, 2024
a1298b8
Clean up slider imports.
freakboy3742 May 29, 2024
284f8d5
various loaders
KRRT7 Jun 1, 2024
cf264c6
add lazy imports to toga core
KRRT7 May 20, 2024
17f60de
add lazy_imports to toga_cocoa
KRRT7 May 20, 2024
63f30c4
Create 2547.feature.rst
KRRT7 May 20, 2024
09d8b4d
add changes rst
KRRT7 May 20, 2024
a2abda5
factory / toga init __all__ changes
KRRT7 May 20, 2024
3cd4f0a
remove unneeded blank lines
KRRT7 May 20, 2024
2e8728f
optimization path for the lazy loader
KRRT7 May 20, 2024
95d9344
__all__ must be generated at the top level of the module
KRRT7 May 20, 2024
b4ba652
fixup
KRRT7 May 21, 2024
63ed7cf
sort the dictionary
KRRT7 May 21, 2024
ff8e114
it's a misc, not feat
KRRT7 May 21, 2024
b2d9acc
ghosts and stuff
KRRT7 May 21, 2024
cc17bf4
update gtk slider
KRRT7 May 29, 2024
55e6f5e
update android slider
KRRT7 May 29, 2024
ed10200
misspell
KRRT7 May 29, 2024
ba6f591
update from toga to toga_cocoa
KRRT7 May 29, 2024
e607f23
Correct wording of release note.
freakboy3742 May 29, 2024
d774aea
Clean up slider imports.
freakboy3742 May 29, 2024
cbcdf26
various loaders
KRRT7 Jun 1, 2024
ea60f28
Merge branch 'lazy_imports' of https://github.com/KRRT7/toga into laz…
KRRT7 Jun 2, 2024
4a2b9d9
MainWindow
KRRT7 Jun 2, 2024
4c83435
textual, web APIs
KRRT7 Jun 3, 2024
9950c4f
Update testbed/tests/test_factory.py
KRRT7 Jun 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2547.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
implements lazy loading for toga core and toga cocoa
KRRT7 marked this conversation as resolved.
Show resolved Hide resolved
132 changes: 48 additions & 84 deletions cocoa/src/toga_cocoa/factory.py
Original file line number Diff line number Diff line change
@@ -1,90 +1,54 @@
from toga import NotImplementedWarning
import importlib

from . import dialogs
from .app import App, DocumentApp, MainWindow
from .command import Command
from .documents import Document
from .fonts import Font
from .hardware.camera import Camera
from .hardware.location import Location
from .icons import Icon
from .images import Image
from .paths import Paths
from toga import NotImplementedWarning

# Widgets
from .widgets.activityindicator import ActivityIndicator
from .widgets.box import Box
from .widgets.button import Button
from .widgets.canvas import Canvas
from .widgets.detailedlist import DetailedList
from .widgets.divider import Divider
from .widgets.imageview import ImageView
from .widgets.label import Label
from .widgets.mapview import MapView
from .widgets.multilinetextinput import MultilineTextInput
from .widgets.numberinput import NumberInput
from .widgets.optioncontainer import OptionContainer
from .widgets.passwordinput import PasswordInput
from .widgets.progressbar import ProgressBar
from .widgets.scrollcontainer import ScrollContainer
from .widgets.selection import Selection
from .widgets.slider import Slider
from .widgets.splitcontainer import SplitContainer
from .widgets.switch import Switch
from .widgets.table import Table
from .widgets.textinput import TextInput
from .widgets.tree import Tree
from .widgets.webview import WebView
from .window import Window
toga_factory_imports = {
"toga.app": ["App", "DocumentApp", "MainWindow"],
"toga_cocoa": ["dialogs"],
"toga.command": ["Command"],
"toga.documents": ["Document"],
"toga.fonts": ["Font"],
"toga.hardware.camera": ["Camera"],
"toga.hardware.location": ["Location"],
"toga.icons": ["Icon"],
"toga.images": ["Image"],
"toga.paths": ["Paths"],
"toga.widgets.activityindicator": ["ActivityIndicator"],
"toga.widgets.box": ["Box"],
"toga.widgets.button": ["Button"],
"toga.widgets.canvas": ["Canvas"],
"toga.widgets.detailedlist": ["DetailedList"],
"toga.widgets.divider": ["Divider"],
"toga.widgets.imageview": ["ImageView"],
"toga.widgets.label": ["Label"],
"toga.widgets.mapview": ["MapView"],
"toga.widgets.multilinetextinput": ["MultilineTextInput"],
"toga.widgets.numberinput": ["NumberInput"],
"toga.widgets.optioncontainer": ["OptionContainer"],
"toga.widgets.passwordinput": ["PasswordInput"],
"toga.widgets.progressbar": ["ProgressBar"],
"toga.widgets.scrollcontainer": ["ScrollContainer"],
"toga.widgets.selection": ["Selection"],
"toga.widgets.slider": ["Slider"],
"toga.widgets.splitcontainer": ["SplitContainer"],
"toga.widgets.switch": ["Switch"],
"toga.widgets.table": ["Table"],
"toga.widgets.textinput": ["TextInput"],
"toga.widgets.tree": ["Tree"],
"toga.widgets.webview": ["WebView"],
"toga.window": ["Window"],
}


def __getattr__(name):
for module, names in toga_factory_imports.items():
if name in names:
module = importlib.import_module(module)
globals()[name] = getattr(module, name)
return getattr(module, name)
else:
raise NotImplementedError(f"Toga's Cocoa backend doesn't implement {name}")


def not_implemented(feature):
NotImplementedWarning.warn("Cocoa", feature)


__all__ = [
"not_implemented",
"App",
"DocumentApp",
"MainWindow",
"Command",
"Document",
# Resources
"Font",
"Icon",
"Image",
"Paths",
"dialogs",
# Hardware
"Camera",
"Location",
# Widgets
"ActivityIndicator",
"Box",
"Button",
"Canvas",
"DetailedList",
"Divider",
"ImageView",
"Label",
"MapView",
"MultilineTextInput",
"NumberInput",
"OptionContainer",
"PasswordInput",
"ProgressBar",
"ScrollContainer",
"Selection",
"Slider",
"SplitContainer",
"Switch",
"Table",
"TextInput",
"Tree",
"WebView",
"Window",
]


def __getattr__(name): # pragma: no cover
raise NotImplementedError(f"Toga's Cocoa backend doesn't implement {name}")
152 changes: 53 additions & 99 deletions core/src/toga/__init__.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,61 @@
from __future__ import annotations

import importlib
import importlib.util
import warnings

from .app import App, DocumentApp, DocumentMainWindow, MainWindow
toga_core_imports = {
"toga.app": ["App", "DocumentApp", "DocumentMainWindow", "MainWindow"],
"toga.colors": ["hsl", "hsla", "rgb", "rgba"],
"toga.command": ["Command", "Group"],
"toga.commands": ["Command", "Group"],
"toga.documents": ["Document"],
"toga.fonts": ["Font"],
"toga.icons": ["Icon"],
"toga.images": ["Image"],
"toga.keys": ["Key"],
"toga.types": ["LatLng"],
"toga.widgets.activityindicator": ["ActivityIndicator"],
"toga.widgets.base": ["Widget"],
"toga.widgets.box": ["Box"],
"toga.widgets.button": ["Button"],
"toga.widgets.canvas": ["Canvas"],
"toga.widgets.dateinput": ["DateInput", "DatePicker"],
"toga.widgets.detailedlist": ["DetailedList"],
"toga.widgets.divider": ["Divider"],
"toga.widgets.imageview": ["ImageView"],
"toga.widgets.label": ["Label"],
"toga.widgets.mapview": ["MapPin", "MapView"],
"toga.widgets.multilinetextinput": ["MultilineTextInput"],
"toga.widgets.numberinput": ["NumberInput"],
"toga.widgets.optioncontainer": ["OptionContainer", "OptionItem"],
"toga.widgets.passwordinput": ["PasswordInput"],
"toga.widgets.progressbar": ["ProgressBar"],
"toga.widgets.scrollcontainer": ["ScrollContainer"],
"toga.widgets.selection": ["Selection"],
"toga.widgets.slider": ["Slider"],
"toga.widgets.splitcontainer": ["SplitContainer"],
"toga.widgets.switch": ["Switch"],
"toga.widgets.table": ["Table"],
"toga.widgets.textinput": ["TextInput"],
"toga.widgets.timeinput": ["TimeInput", "TimePicker"],
"toga.widgets.tree": ["Tree"],
"toga.widgets.webview": ["WebView"],
"toga.window": ["Window"],
}

# Resources
from .colors import hsl, hsla, rgb, rgba
from .command import Command, Group
from .documents import Document
from .fonts import Font
from .icons import Icon
from .images import Image
from .keys import Key
__all__ = []

# Types
from .types import LatLng

# Widgets
from .widgets.activityindicator import ActivityIndicator
from .widgets.base import Widget
from .widgets.box import Box
from .widgets.button import Button
from .widgets.canvas import Canvas
from .widgets.dateinput import DateInput, DatePicker
from .widgets.detailedlist import DetailedList
from .widgets.divider import Divider
from .widgets.imageview import ImageView
from .widgets.label import Label
from .widgets.mapview import MapPin, MapView
from .widgets.multilinetextinput import MultilineTextInput
from .widgets.numberinput import NumberInput
from .widgets.optioncontainer import OptionContainer, OptionItem
from .widgets.passwordinput import PasswordInput
from .widgets.progressbar import ProgressBar
from .widgets.scrollcontainer import ScrollContainer
from .widgets.selection import Selection
from .widgets.slider import Slider
from .widgets.splitcontainer import SplitContainer
from .widgets.switch import Switch
from .widgets.table import Table
from .widgets.textinput import TextInput
from .widgets.timeinput import TimeInput, TimePicker
from .widgets.tree import Tree
from .widgets.webview import WebView
from .window import Window
def __getattr__(name):
for module, names in toga_core_imports.items():
if name in names:
KRRT7 marked this conversation as resolved.
Show resolved Hide resolved
module = importlib.import_module(module)
globals()[name] = getattr(module, name)
__all__.append(name)
KRRT7 marked this conversation as resolved.
Show resolved Hide resolved
return getattr(module, name)
else:
raise AttributeError(f"module {__name__} has no attribute {name}")
KRRT7 marked this conversation as resolved.
Show resolved Hide resolved


class NotImplementedWarning(RuntimeWarning):
Expand All @@ -54,66 +68,6 @@ def warn(self, platform, feature):
warnings.warn(NotImplementedWarning(f"[{platform}] Not implemented: {feature}"))


__all__ = [
"NotImplementedWarning",
# Applications
"App",
"DocumentApp",
"MainWindow",
"DocumentMainWindow",
# Commands
"Command",
"Group",
# Documents
"Document",
# Keys
"Key",
# Resources
"hsl",
"hsla",
"rgb",
"rgba",
"Font",
"Icon",
"Image",
# Types
"LatLng",
# Widgets
"ActivityIndicator",
"Box",
"Button",
"Canvas",
"DateInput",
"DetailedList",
"Divider",
"ImageView",
"Label",
"MapPin",
"MapView",
"MultilineTextInput",
"NumberInput",
"OptionContainer",
"OptionItem",
"PasswordInput",
"ProgressBar",
"ScrollContainer",
"Selection",
"Slider",
"SplitContainer",
"Switch",
"Table",
"TextInput",
"TimeInput",
"Tree",
"WebView",
"Widget",
"Window",
# Deprecated widget names
"DatePicker",
"TimePicker",
]


def _package_version(file, name):
try:
# Read version from SCM metadata
Expand Down
Loading