Skip to content

Commit

Permalink
Merge pull request #300 from MelbourneHighSchoolRobotics/readonly-pac…
Browse files Browse the repository at this point in the history
…kage-1

 Add platform folders support to search paths
  • Loading branch information
angustrau authored Aug 16, 2021
2 parents fe89247 + 22f514d commit 3a194bc
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 52 deletions.
20 changes: 15 additions & 5 deletions ev3sim/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from ev3sim import __version__
from ev3sim.file_helper import WorkspaceError, find_abs, find_abs_directory, make_relative
from ev3sim.search_locations import bot_locations, config_locations, preset_locations
from ev3sim.search_locations import bot_locations, config_locations, preset_locations, workspace_locations
from ev3sim.simulation.loader import StateHandler
from ev3sim.updates import handle_updates
from ev3sim.utils import canUpdate
Expand Down Expand Up @@ -199,7 +199,7 @@ def main(passed_args=None):
args.__dict__.update(passed_args)

# Useful for a few things.
ev3sim_folder = dirname(abspath(__file__))
ev3sim_folder = find_abs_directory(config_locations())

# Step 1: Handling helper commands

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

try:
# This should always exist.
conf_file = find_abs("user_config.yaml", allowed_areas=config_locations())
try:
conf_file = find_abs("user_config.yaml", allowed_areas=config_locations())
except ValueError:
# Config file can't be found
# Can happen when this is a fresh install
import shutil

conf_dir = find_abs_directory(config_locations())
default_conf_file = find_abs("default_config.yaml", allowed_areas=["package/presets/"])
conf_file = join(conf_dir, "user_config.yaml")
shutil.copyfile(default_conf_file, conf_file)

with open(conf_file, "r") as f:
conf = yaml.safe_load(f)

Expand All @@ -232,7 +242,7 @@ def main(passed_args=None):
handler.setConfig(
**{
"app": {
"workspace_folder": find_abs_directory("package/workspace", create=True),
"workspace_folder": find_abs_directory(workspace_locations(), create=True),
}
}
)
Expand Down
35 changes: 34 additions & 1 deletion ev3sim/file_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import os
import platform
from pathlib import Path

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

Expand All @@ -7,6 +9,22 @@ class WorkspaceError(Exception):
pass


def find_platform_location(dir_type):
"""
Attempt to find and create operating system data folders
"""
if platform.system() == "Linux":
home_dir = os.path.expanduser("~")
if dir_type == "config":
xdg_dir = os.environ.get("XDG_CONFIG_HOME") or os.path.join(home_dir, ".config")
elif dir_type == "data":
xdg_dir = os.environ.get("XDG_DATA_HOME") or os.path.join(home_dir, ".local/share")
ev3sim_dir = os.path.join(xdg_dir, "ev3sim")
Path(ev3sim_dir).mkdir(parents=True, exist_ok=True)
return ev3sim_dir
return None


def find_abs(filepath, allowed_areas=None):
"""
Attempt to find a reference file, from this list of appropriate places specified.
Expand Down Expand Up @@ -51,6 +69,17 @@ def find_abs(filepath, allowed_areas=None):
if not WORKSPACE:
continue
path = os.path.join(WORKSPACE, area[10:], filepath)
elif area.startswith("platform"):
if area.startswith("platform/config"):
pl = find_platform_location("config")
if pl is None:
continue
path = os.path.join(pl, area[16:], filepath)
elif area.startswith("platform/data"):
pl = find_platform_location("data")
if pl is None:
continue
path = os.path.join(pl, area[14:], filepath)
elif area == "local":
path = filepath
elif area.startswith("local"):
Expand Down Expand Up @@ -91,12 +120,16 @@ def fix():

def find_abs_directory(dirpath, create=True):
try:
return find_abs("", allowed_areas=[dirpath])
if not isinstance(dirpath, list):
dirpath = [dirpath]
return find_abs("", allowed_areas=dirpath)
except ValueError as e:
if not create:
raise e
else:
# Remove one part of the directory, then try again.
if isinstance(dirpath, list):
dirpath = dirpath[0]
rest, single = os.path.split(dirpath.rstrip("/"))
if rest == dirpath:
raise ValueError(f"Find abs dir failed with input {dirpath}")
Expand Down
18 changes: 2 additions & 16 deletions ev3sim/search_locations.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
preset_locations = lambda: ["workspace/presets/", "workspace", "package/presets/"]
config_locations = lambda: ["workspace", "platform/config", "package"]
device_locations = lambda: ["workspace/devices/", "package/devices/"]
theme_locations = lambda: ["workspace/assets/", "workspace", "package/assets"]
asset_locations = lambda: ["workspace/assets/", "workspace", "package/assets/"]


def config_locations():
import os
import platform
from pathlib import Path

if platform.system() == "Linux":
home_dir = os.path.expanduser("~")
xdg_config_dir = os.environ.get("XDG_CONFIG_HOME") or os.path.join(home_dir, ".config")
config_dir = os.path.join(xdg_config_dir, "ev3sim")
# Ensure config dir exists
Path(config_dir).mkdir(parents=True, exist_ok=True)
return ["workspace", "local|" + config_dir]

return ["workspace", "package"]
workspace_locations = lambda: ["platform/data/workspace/", "package/workspace/"]


def code_locations(bot_path):
Expand Down
27 changes: 5 additions & 22 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 5 additions & 7 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
{
description = "A simulator for soccer robots programmed with ev3dev.";
description = "A simulator for soccer robots programmed with ev3dev";

inputs.nixpkgs.url = "github:nixos/nixpkgs/21.05";
# On unstable to use some new python packages. Eventually will be merged into Nix 21.11
inputs.unstable.url = "github:nixos/nixpkgs/nixpkgs-unstable";
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable";
inputs.flake-utils.url = "github:numtide/flake-utils";
inputs.mindpile.url = "github:MelbourneHighSchoolRobotics/mindpile";
inputs.mindpile.inputs.nixpkgs.follows = "nixpkgs";
inputs.mindpile.inputs.flake-utils.follows = "flake-utils";

outputs = { nixpkgs, unstable, flake-utils, mindpile, ... }:
outputs = { nixpkgs, flake-utils, mindpile, ... }:
flake-utils.lib.eachSystem [ "x86_64-linux" ] (system:
let
pkgs = nixpkgs.legacyPackages.${system};
unstablePkgs = unstable.legacyPackages.${system};
mp = mindpile.legacyPackages.${system};
in rec {
packages = {
linux = pkgs.callPackage ./nix/linux.nix {
inherit (unstablePkgs.python39Packages) ev3dev2 opensimplex pymunk pygame pygame-gui;
mindpile = mindpile.legacyPackages.${system}.mindpile;
inherit (mp) mindpile;
};

windows = pkgs.callPackage ./nix/windows-installer.nix { };
Expand Down
2 changes: 1 addition & 1 deletion nix/linux.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ pkgs, pymunk, ev3dev2, opensimplex, pygame, pygame-gui, mindpile, ... }:
{ pkgs, mindpile, ... }:
pkgs.python39Packages.buildPythonPackage
(let version = pkgs.callPackage ./version.nix { };
in {
Expand Down

0 comments on commit 3a194bc

Please sign in to comment.