Skip to content

Commit

Permalink
Config templates (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
mj-xmr authored May 10, 2022
1 parent d5355fa commit 14b27bd
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 23 deletions.
1 change: 0 additions & 1 deletion src/batteries.json

This file was deleted.

1 change: 0 additions & 1 deletion src/computers.json

This file was deleted.

1 change: 0 additions & 1 deletion src/geo-template.json

This file was deleted.

18 changes: 11 additions & 7 deletions src/opti-lib/src/JsonReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,22 @@ EnjoLib::Array<Computer> JsonReader::ReadComputers(bool verbose) const
return ret;
}

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>

EnjoLib::Str JsonReader::GetJson(const EnjoLib::Str & fileName) const
{
Str path = fileName;
if (not FileUtils().FileExists(path))
{
path = "../../../" + fileName; /// TODO: This is an ugly mess.
const char *homedir = nullptr;

if ((homedir = getenv("HOME")) == NULL) {
homedir = getpwuid(getuid())->pw_dir;
}
if (not FileUtils().FileExists(path))
if (homedir == nullptr)
{
path = "../" + fileName;
Assertions::Throw("Not found home dir", "JsonReader::GetJson");
}
Ifstream fcomps(path);
Ifstream fcomps(Str(homedir) + "/.config/solar/" + fileName);
const Tokenizer tok;
const VecStr & lines = tok.GetLines(fcomps);
Str wholeJson;
Expand Down
32 changes: 19 additions & 13 deletions src/sunrise_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,26 @@


HOME = str(Path.home()) + "/" # TODO: move this in config.json too?
DIR_THIS = os.path.basename(os.path.dirname(os.path.realpath(__file__)))

def get_geo():
config_builder = ConfigBuilder()
config = config_builder.parse_config('config.json')

def get_config(name):
os.makedirs(HOME + config.sunrise_lib.DIR_CFG, exist_ok=True)
geo_path = HOME + config.sunrise_lib.PATH_JSON_GEO
if not os.path.isfile(geo_path):
geo_cfg_template = 'geo-template.json'
print("Not found: " + geo_path)
print("Copying " + geo_cfg_template + " to " + geo_path)
shutil.copy(geo_cfg_template, geo_path)
path_local = HOME + config.sunrise_lib.DIR_CFG + "/{}.json".format(name)
if not os.path.isfile(path_local):
cfg_template = 'src/system-cfg/{}-template.json'.format(name)
print("Not found: " + path_local)
print("Copying " + cfg_template + " to " + path_local)
shutil.copy(cfg_template, path_local)

config_geo = config_builder.parse_config(geo_path)
return config_geo
config_local = config_builder.parse_config(path_local)
return config_local

config_builder = ConfigBuilder()
config = config_builder.parse_config('config.json')
config_geo = get_geo()
config_batteries = config_builder.parse_config('batteries.json')
config_geo = get_config('geo')
config_batteries = get_config('batteries')
config_computers = get_config('computers')

PROJECT_NAME = "SolOptXMR"
PROJECT_SUB_NAME = "Solar Optimal mining of XMR"
Expand Down Expand Up @@ -109,5 +112,8 @@ def test_stateful_bool():
def test():
test_stateful_bool()

if __name__ == "__main__":
test()

#LOCAL_TIMEZONE = datetime.datetime.now(datetime.timezone.utc).astimezone().tzname
#print(LOCAL_TIMEZONE)
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 14b27bd

Please sign in to comment.