Skip to content

Commit 3a194bc

Browse files
authored
Merge pull request #300 from MelbourneHighSchoolRobotics/readonly-package-1
Add platform folders support to search paths
2 parents fe89247 + 22f514d commit 3a194bc

6 files changed

Lines changed: 62 additions & 52 deletions

File tree

ev3sim/entry.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
from ev3sim import __version__
99
from ev3sim.file_helper import WorkspaceError, find_abs, find_abs_directory, make_relative
10-
from ev3sim.search_locations import bot_locations, config_locations, preset_locations
10+
from ev3sim.search_locations import bot_locations, config_locations, preset_locations, workspace_locations
1111
from ev3sim.simulation.loader import StateHandler
1212
from ev3sim.updates import handle_updates
1313
from ev3sim.utils import canUpdate
@@ -199,7 +199,7 @@ def main(passed_args=None):
199199
args.__dict__.update(passed_args)
200200

201201
# Useful for a few things.
202-
ev3sim_folder = dirname(abspath(__file__))
202+
ev3sim_folder = find_abs_directory(config_locations())
203203

204204
# Step 1: Handling helper commands
205205

@@ -219,8 +219,18 @@ def main(passed_args=None):
219219
# Step 2: Safely load configs and set up error reporting
220220

221221
try:
222-
# This should always exist.
223-
conf_file = find_abs("user_config.yaml", allowed_areas=config_locations())
222+
try:
223+
conf_file = find_abs("user_config.yaml", allowed_areas=config_locations())
224+
except ValueError:
225+
# Config file can't be found
226+
# Can happen when this is a fresh install
227+
import shutil
228+
229+
conf_dir = find_abs_directory(config_locations())
230+
default_conf_file = find_abs("default_config.yaml", allowed_areas=["package/presets/"])
231+
conf_file = join(conf_dir, "user_config.yaml")
232+
shutil.copyfile(default_conf_file, conf_file)
233+
224234
with open(conf_file, "r") as f:
225235
conf = yaml.safe_load(f)
226236

@@ -232,7 +242,7 @@ def main(passed_args=None):
232242
handler.setConfig(
233243
**{
234244
"app": {
235-
"workspace_folder": find_abs_directory("package/workspace", create=True),
245+
"workspace_folder": find_abs_directory(workspace_locations(), create=True),
236246
}
237247
}
238248
)

ev3sim/file_helper.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import os
2+
import platform
3+
from pathlib import Path
24

35
ROOT = os.path.abspath(os.path.dirname(__file__))
46

@@ -7,6 +9,22 @@ class WorkspaceError(Exception):
79
pass
810

911

12+
def find_platform_location(dir_type):
13+
"""
14+
Attempt to find and create operating system data folders
15+
"""
16+
if platform.system() == "Linux":
17+
home_dir = os.path.expanduser("~")
18+
if dir_type == "config":
19+
xdg_dir = os.environ.get("XDG_CONFIG_HOME") or os.path.join(home_dir, ".config")
20+
elif dir_type == "data":
21+
xdg_dir = os.environ.get("XDG_DATA_HOME") or os.path.join(home_dir, ".local/share")
22+
ev3sim_dir = os.path.join(xdg_dir, "ev3sim")
23+
Path(ev3sim_dir).mkdir(parents=True, exist_ok=True)
24+
return ev3sim_dir
25+
return None
26+
27+
1028
def find_abs(filepath, allowed_areas=None):
1129
"""
1230
Attempt to find a reference file, from this list of appropriate places specified.
@@ -51,6 +69,17 @@ def find_abs(filepath, allowed_areas=None):
5169
if not WORKSPACE:
5270
continue
5371
path = os.path.join(WORKSPACE, area[10:], filepath)
72+
elif area.startswith("platform"):
73+
if area.startswith("platform/config"):
74+
pl = find_platform_location("config")
75+
if pl is None:
76+
continue
77+
path = os.path.join(pl, area[16:], filepath)
78+
elif area.startswith("platform/data"):
79+
pl = find_platform_location("data")
80+
if pl is None:
81+
continue
82+
path = os.path.join(pl, area[14:], filepath)
5483
elif area == "local":
5584
path = filepath
5685
elif area.startswith("local"):
@@ -91,12 +120,16 @@ def fix():
91120

92121
def find_abs_directory(dirpath, create=True):
93122
try:
94-
return find_abs("", allowed_areas=[dirpath])
123+
if not isinstance(dirpath, list):
124+
dirpath = [dirpath]
125+
return find_abs("", allowed_areas=dirpath)
95126
except ValueError as e:
96127
if not create:
97128
raise e
98129
else:
99130
# Remove one part of the directory, then try again.
131+
if isinstance(dirpath, list):
132+
dirpath = dirpath[0]
100133
rest, single = os.path.split(dirpath.rstrip("/"))
101134
if rest == dirpath:
102135
raise ValueError(f"Find abs dir failed with input {dirpath}")

ev3sim/search_locations.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,9 @@
11
preset_locations = lambda: ["workspace/presets/", "workspace", "package/presets/"]
2+
config_locations = lambda: ["workspace", "platform/config", "package"]
23
device_locations = lambda: ["workspace/devices/", "package/devices/"]
34
theme_locations = lambda: ["workspace/assets/", "workspace", "package/assets"]
45
asset_locations = lambda: ["workspace/assets/", "workspace", "package/assets/"]
5-
6-
7-
def config_locations():
8-
import os
9-
import platform
10-
from pathlib import Path
11-
12-
if platform.system() == "Linux":
13-
home_dir = os.path.expanduser("~")
14-
xdg_config_dir = os.environ.get("XDG_CONFIG_HOME") or os.path.join(home_dir, ".config")
15-
config_dir = os.path.join(xdg_config_dir, "ev3sim")
16-
# Ensure config dir exists
17-
Path(config_dir).mkdir(parents=True, exist_ok=True)
18-
return ["workspace", "local|" + config_dir]
19-
20-
return ["workspace", "package"]
6+
workspace_locations = lambda: ["platform/data/workspace/", "package/workspace/"]
217

228

239
def code_locations(bot_path):

flake.lock

Lines changed: 5 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
{
2-
description = "A simulator for soccer robots programmed with ev3dev.";
2+
description = "A simulator for soccer robots programmed with ev3dev";
33

4-
inputs.nixpkgs.url = "github:nixos/nixpkgs/21.05";
54
# On unstable to use some new python packages. Eventually will be merged into Nix 21.11
6-
inputs.unstable.url = "github:nixos/nixpkgs/nixpkgs-unstable";
5+
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
76
inputs.flake-utils.url = "github:numtide/flake-utils";
87
inputs.mindpile.url = "github:MelbourneHighSchoolRobotics/mindpile";
98
inputs.mindpile.inputs.nixpkgs.follows = "nixpkgs";
109
inputs.mindpile.inputs.flake-utils.follows = "flake-utils";
1110

12-
outputs = { nixpkgs, unstable, flake-utils, mindpile, ... }:
11+
outputs = { nixpkgs, flake-utils, mindpile, ... }:
1312
flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
1413
let
1514
pkgs = nixpkgs.legacyPackages.${system};
16-
unstablePkgs = unstable.legacyPackages.${system};
15+
mp = mindpile.legacyPackages.${system};
1716
in rec {
1817
packages = {
1918
linux = pkgs.callPackage ./nix/linux.nix {
20-
inherit (unstablePkgs.python39Packages) ev3dev2 opensimplex pymunk pygame pygame-gui;
21-
mindpile = mindpile.legacyPackages.${system}.mindpile;
19+
inherit (mp) mindpile;
2220
};
2321

2422
windows = pkgs.callPackage ./nix/windows-installer.nix { };

nix/linux.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ pkgs, pymunk, ev3dev2, opensimplex, pygame, pygame-gui, mindpile, ... }:
1+
{ pkgs, mindpile, ... }:
22
pkgs.python39Packages.buildPythonPackage
33
(let version = pkgs.callPackage ./version.nix { };
44
in {

0 commit comments

Comments
 (0)