Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1964 from freakboy3742/app-paths
Rework implementation of app paths to factor out common implementations.
- Loading branch information
Showing
67 changed files
with
841 additions
and
1,242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,24 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
import toga | ||
from toga import App | ||
|
||
|
||
class Paths: | ||
# Allow instantiating Path object via the factory | ||
Path = Path | ||
def __init__(self, interface): | ||
self.interface = interface | ||
|
||
@property | ||
def __context(self): | ||
return App.app._impl.native.getApplicationContext() | ||
|
||
def __init__(self): | ||
# On Android, __main__ only exists during app startup, so cache its location now. | ||
self._app = Path(sys.modules["__main__"].__file__).parent | ||
def get_config_path(self): | ||
return Path(self.__context.getFilesDir().getPath()) / "config" | ||
|
||
@property | ||
def app(self): | ||
return self._app | ||
|
||
@property | ||
def data(self): | ||
return Path(self.__context.getFilesDir().getPath()) | ||
def get_data_path(self): | ||
return Path(self.__context.getFilesDir().getPath()) / "data" | ||
|
||
@property | ||
def cache(self): | ||
def get_cache_path(self): | ||
return Path(self.__context.getCacheDir().getPath()) | ||
|
||
@property | ||
def logs(self): | ||
return self.data | ||
|
||
@property | ||
def toga(self): | ||
"""Return a path to a Toga resources.""" | ||
return Path(toga.__file__).parent | ||
|
||
|
||
paths = Paths() | ||
def get_logs_path(self): | ||
return Path(self.__context.getFilesDir().getPath()) / "log" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from pathlib import Path | ||
|
||
from toga_android.libs.activity import MainActivity | ||
|
||
from .probe import BaseProbe | ||
|
||
|
||
class AppProbe(BaseProbe): | ||
def __init__(self, app): | ||
super().__init__() | ||
self.app = app | ||
assert isinstance(self.app._impl.native, MainActivity) | ||
|
||
def get_app_context(self): | ||
return self.app._impl.native.getApplicationContext() | ||
|
||
@property | ||
def config_path(self): | ||
return Path(self.get_app_context().getFilesDir().getPath()) / "config" | ||
|
||
@property | ||
def data_path(self): | ||
return Path(self.get_app_context().getFilesDir().getPath()) / "data" | ||
|
||
@property | ||
def cache_path(self): | ||
return Path(self.get_app_context().getCacheDir().getPath()) | ||
|
||
@property | ||
def logs_path(self): | ||
return Path(self.get_app_context().getFilesDir().getPath()) / "log" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import asyncio | ||
|
||
from toga.fonts import SYSTEM | ||
|
||
|
||
class BaseProbe: | ||
def assert_font_family(self, expected): | ||
actual = self.font.family | ||
if expected == SYSTEM: | ||
assert actual == "sans-serif" | ||
else: | ||
assert actual == expected | ||
|
||
async def redraw(self, message=None): | ||
"""Request a redraw of the app, waiting until that redraw has completed.""" | ||
# If we're running slow, wait for a second | ||
if self.app.run_slow: | ||
print("Waiting for redraw" if message is None else message) | ||
await asyncio.sleep(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The Paths property of apps now has 100% test coverage, and complete API documentation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The app paths now include a ``config`` path for storing config files. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
The location returned by ``toga.App.paths.app`` is now the folder that contains the Python source file that defines the app class used by the app. If you are using a ``toga.App`` instance directly, this may alter the path that is returned. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
On Winforms, if an application doesn't define an author, an author of ``Unknown`` is now used in application data paths, rather than ``Toga``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Winforms now returns ``AppData/Local/<Author Name>/<App Name>/Data`` as the user data file location, rather than ``AppData/Local/<Author Name>/<App Name>``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
On Android, the user data folder is now a ``data`` subdirectory of the location returned by ``context.getFilesDir()``, rather than the bare ``context.getFilesDir()`` location. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
GTK now returns ``~/.local/state/appname/log`` as the log file location, rather than ``~/.cache/appname/log``. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,20 @@ | ||
import sys | ||
from pathlib import Path | ||
|
||
import toga | ||
from toga import App | ||
|
||
|
||
class Paths: | ||
# Allow instantiating Path object via the factory | ||
Path = Path | ||
def __init__(self, interface): | ||
self.interface = interface | ||
|
||
@property | ||
def app(self): | ||
try: | ||
return Path(sys.modules["__main__"].__file__).parent | ||
except KeyError: | ||
# If we're running in test conditions, | ||
# there is no __main__ module. | ||
return Path.cwd() | ||
except AttributeError: | ||
# If we're running at an interactive prompt, | ||
# the __main__ module isn't file-based. | ||
return Path.cwd() | ||
def get_config_path(self): | ||
return Path.home() / "Library" / "Preferences" / App.app.app_id | ||
|
||
@property | ||
def data(self): | ||
def get_data_path(self): | ||
return Path.home() / "Library" / "Application Support" / App.app.app_id | ||
|
||
@property | ||
def cache(self): | ||
def get_cache_path(self): | ||
return Path.home() / "Library" / "Caches" / App.app.app_id | ||
|
||
@property | ||
def logs(self): | ||
def get_logs_path(self): | ||
return Path.home() / "Library" / "Logs" / App.app.app_id | ||
|
||
@property | ||
def toga(self): | ||
"""Return a path to a Toga resources.""" | ||
return Path(toga.__file__).parent | ||
|
||
|
||
paths = Paths() |
Oops, something went wrong.