summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-09-16 13:58:52 -0500
committerAdrien Hopkins <adrien.p.hopkins@gmail.com>2023-09-16 13:58:52 -0500
commit370fbe971379d4f833158d41f2c95cb669fb6dbf (patch)
tree5e003fa6ef125c73cb908093fa77a90d82abc119
parentc8ac31e271afe78f6127de737c616c6f6fab05cf (diff)
Allow config file to load custom data files
The parameters "custom_unit_file", "custom_dimension_file" and "custom_exception_file" can now be used to load custom unit, dimension and exception files. Specify them more than once to load multiple files. I haven't yet added this to the GUI, and I probably won't, because you already need to be able to edit text files to create this, so having a GUI won't make it any more intuitive.
-rw-r--r--docs/roadmap.org1
-rw-r--r--src/main/java/sevenUnitsGUI/Presenter.java27
2 files changed, 22 insertions, 6 deletions
diff --git a/docs/roadmap.org b/docs/roadmap.org
index a68dae5..c57fb2a 100644
--- a/docs/roadmap.org
+++ b/docs/roadmap.org
@@ -17,7 +17,6 @@ Feature Requirements:
Data/Configuration Requirements:
- All of the data formats (e.g. unit files) should be standardized.
-- The config file should be able to specify the location of the data files.
Documentation/Testing Requirements:
- 7Units should be fully documented.
diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java
index 3ecfba6..e471b01 100644
--- a/src/main/java/sevenUnitsGUI/Presenter.java
+++ b/src/main/java/sevenUnitsGUI/Presenter.java
@@ -21,7 +21,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
-import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@@ -73,16 +72,16 @@ public final class Presenter {
if (System.getProperty("os.name").startsWith("Windows")) {
final String envFolder = System.getenv("LOCALAPPDATA");
if (envFolder == null || "".equals(envFolder)) {
- configDir = Paths.get(System.getenv("USERPROFILE"), "AppData", "Local", "SevenUnits");
+ configDir = Path.of(System.getenv("USERPROFILE"), "AppData", "Local", "SevenUnits");
} else {
- configDir = Paths.get(envFolder, "SevenUnits");
+ configDir = Path.of(envFolder, "SevenUnits");
}
} else {
final String envFolder = System.getenv("XDG_CONFIG_HOME");
if (envFolder == null || "".equals(envFolder)) {
- configDir = Paths.get(System.getenv("HOME"), ".config", "SevenUnits");
+ configDir = Path.of(System.getenv("HOME"), ".config", "SevenUnits");
} else {
- configDir = Paths.get(envFolder, "SevenUnits");
+ configDir = Path.of(envFolder, "SevenUnits");
}
}
@@ -99,6 +98,13 @@ public final class Presenter {
return Optional.of(configDir.resolve("config.txt"));
}
+ /** Gets a Path from a pathname in the config file. */
+ private static Path pathFromConfig(String pathname) {
+ return CONFIG_FILE.map(configFile ->
+ configFile.getParent().resolve(pathname)
+ ).orElse(Path.of(pathname));
+ }
+
/**
* Adds default units and dimensions to a database.
*
@@ -615,6 +621,17 @@ public final class Presenter {
switch (param) {
// set manually to avoid the unnecessary saving of the non-manual
// methods
+ case "custom_dimension_file":
+ this.database.loadDimensionFile(pathFromConfig(value));
+ break;
+ case "custom_exception_file":
+ Files.lines(pathFromConfig(value))
+ .map(Presenter::withoutComments)
+ .forEach(this.metricExceptions::add);
+ break;
+ case "custom_unit_file":
+ this.database.loadUnitsFile(pathFromConfig(value));
+ break;
case "number_display_rule":
this.numberDisplayRule = StandardDisplayRules
.getStandardRule(value);