-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
364950b
commit af01e30
Showing
10 changed files
with
139 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,82 @@ | ||
import os | ||
import sys | ||
|
||
import toml | ||
|
||
from neetbox.config import default as default_config | ||
from neetbox.config import get_module_level_config | ||
from neetbox.config._config import update_with | ||
from neetbox.daemon import _try_attach_daemon | ||
from neetbox.config._config import update_workspace_config_with | ||
from neetbox.logging.formatting import LogStyle | ||
from neetbox.logging.logger import Logger | ||
from neetbox.utils.framing import get_frame_module_traceback | ||
|
||
module = get_frame_module_traceback(1).__name__ # type: ignore | ||
config_file_name = f"{module}.toml" | ||
logger = Logger("NEETBOX", style=LogStyle(with_datetime=False, skip_writers=["ws"])) | ||
|
||
MODULE_NAME = get_frame_module_traceback(1).__name__ # type: ignore | ||
CONFIG_FILE_NAME = f"{MODULE_NAME}.toml" | ||
|
||
def init(path=None, load=False, **kwargs) -> bool: | ||
|
||
def _init_workspace(path=None, **kwargs) -> bool: | ||
if path: | ||
os.chdir(path=path) | ||
current_path = os.getcwd() | ||
config_file_path = os.path.join(current_path, config_file_name) | ||
logger = Logger("NEETBOX", style=LogStyle(with_datetime=False, skip_writers=["ws"])) | ||
if not load: | ||
if not os.path.isfile(config_file_path): # config file not exist | ||
try: # creating config file using default config | ||
with open(config_file_path, "w+") as config_file: | ||
_config = dict(default_config) | ||
if "name" in kwargs and kwargs["name"]: # using given name | ||
_config["name"] = kwargs["name"] | ||
else: # using the folder name | ||
_config["name"] = os.path.basename(os.path.normpath(os.getcwd())) | ||
toml.dump(_config, config_file) | ||
logger.ok(f"Workspace config created as {config_file_path}.") | ||
return True | ||
except Exception as e: | ||
logger.err(f"Failed to create {config_file_path}: {e}") | ||
return False | ||
else: # config file exist: | ||
logger.err((f"Config file already exists.")) # noqa | ||
return False | ||
else: # if load only | ||
if not os.path.isfile(config_file_path): # but config file not exist | ||
logger.err(f"Config file {config_file_path} not exists.") | ||
config_file_path = os.path.join(current_path, CONFIG_FILE_NAME) | ||
if not os.path.isfile(config_file_path): # config file not exist | ||
try: # creating config file using default config | ||
with open(config_file_path, "w+") as config_file: | ||
_config = dict(default_config) | ||
if "name" in kwargs and kwargs["name"]: # using given name | ||
_config["name"] = kwargs["name"] | ||
else: # using the folder name | ||
_config["name"] = os.path.basename(os.path.normpath(os.getcwd())) | ||
toml.dump(_config, config_file) | ||
logger.ok(f"Workspace config created as {config_file_path}.") | ||
except Exception as e: | ||
logger.err(f"Failed to create {config_file_path}: {e}") | ||
return False | ||
else: # config file exist: | ||
logger.err((f"Config file already exists.")) # noqa | ||
return False | ||
|
||
try: # do load only | ||
|
||
def _load_workspace(path=None) -> bool: | ||
if path: | ||
os.chdir(path=path) | ||
current_path = os.getcwd() | ||
config_file_path = os.path.join(current_path, CONFIG_FILE_NAME) | ||
if not os.path.isfile(config_file_path): # but config file not exist | ||
logger.err(f"Config file {config_file_path} not exists.") | ||
return False | ||
try: # do load | ||
load_config = toml.load(config_file_path) | ||
update_with(load_config) | ||
logger.ok(f"found workspace config from {config_file_path}.") | ||
_try_attach_daemon() # try attach daemon | ||
logger.debug(f"running post init...") | ||
update_workspace_config_with(load_config) | ||
logger.ok(f"workspace config loaded from {config_file_path}.") | ||
return True | ||
except Exception as e: | ||
logger.err(f"failed to load config from {config_file_path}: {e}") | ||
raise e | ||
|
||
|
||
def post_init(): | ||
import setproctitle | ||
|
||
project_name = get_module_level_config()["name"] | ||
setproctitle.setproctitle(project_name) | ||
logger.err(f"failed to load workspace config from {config_file_path}: {e}") | ||
return False | ||
|
||
|
||
is_in_daemon_process = ( | ||
"NEETBOX_DAEMON_PROCESS" in os.environ and os.environ["NEETBOX_DAEMON_PROCESS"] == "1" | ||
) | ||
# print('prevent_daemon_loading =', is_in_daemon_process) | ||
if os.path.isfile(config_file_name) and not is_in_daemon_process: # if in a workspace | ||
# todo check if running in cli mode | ||
success = init(load=True) # init from config file | ||
if not success: | ||
|
||
|
||
def _load_workspace_and_attach_daemon(): | ||
success = _load_workspace() # init from config file | ||
if not success: # failed to load workspace config, exiting | ||
os._exit(255) | ||
# run post init | ||
post_init() | ||
# try attach daemon | ||
from neetbox.daemon import _try_attach_daemon | ||
|
||
_try_attach_daemon() | ||
|
||
|
||
def _is_in_workspace(): | ||
return os.path.isfile(CONFIG_FILE_NAME) | ||
|
||
|
||
if len(sys.argv) > 0 and sys.argv[0].endswith("neet") or is_in_daemon_process: | ||
# running in cli or daemon process, do not load workspace | ||
pass | ||
elif _is_in_workspace(): # if a config file exist and not running in cli | ||
_load_workspace_and_attach_daemon() |
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
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
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
Oops, something went wrong.