Skip to content

Commit

Permalink
remove nanosaur_home from config file
Browse files Browse the repository at this point in the history
  • Loading branch information
rbonghi committed Jan 22, 2025
1 parent a4678bc commit 7a4abad
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 38 deletions.
11 changes: 4 additions & 7 deletions src/nanosaur/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@


NANOSAUR_INSTALL_OPTIONS = ['Simple', 'Developer', 'Maintainer']
NANOSAUR_CONFIG_FILE_NAME = 'nanosaur.yaml'
NANOSAUR_HOME_NAME = 'nanosaur'

# Define default parameters
DEFAULT_PARAMS = {
'nanosaur_branch': 'nanosaur2',
'nanosaur_home': NANOSAUR_HOME_NAME,
'nanosaur_raw_github_repo': 'https://raw.githubusercontent.com/rnanosaur/nanosaur',
'ws_developer_name': 'ros_ws',
'ws_perception_name': 'perception_ws',
Expand Down Expand Up @@ -99,8 +96,8 @@ def info(platform, params: Params, args):
print(TerminalFormatter.color_text("\nVersion Information:", bold=True))
print(f" {TerminalFormatter.color_text('Nanosaur package:', bold=True)} {__version__}")
print(f" {TerminalFormatter.color_text('Nanosaur version (branch):', bold=True)} {params['nanosaur_branch']}")
print(f" {TerminalFormatter.color_text('Nanosaur home:', bold=True)} {TerminalFormatter.clickable_path(get_nanosaur_home(params['nanosaur_home']))}")
config_file_path = Params.get_params_file(params['nanosaur_home'], NANOSAUR_CONFIG_FILE_NAME)
print(f" {TerminalFormatter.color_text('Nanosaur home:', bold=True)} {TerminalFormatter.clickable_path(get_nanosaur_home())}")
config_file_path = Params.get_params_file()
print(f" {TerminalFormatter.color_text('Nanosaur config file:', bold=True)} {TerminalFormatter.clickable_path(config_file_path)}")


Expand Down Expand Up @@ -159,7 +156,7 @@ def robot_control(params, subparsers):

def main():
# Load the parameters
params = Params.load(DEFAULT_PARAMS, home_folder=NANOSAUR_HOME_NAME, params_file_name=NANOSAUR_CONFIG_FILE_NAME)
params = Params.load(DEFAULT_PARAMS)

# Extract device information with jtop
try:
Expand Down Expand Up @@ -201,7 +198,7 @@ def main():
parser_workspace = workspace.parser_workspace_menu(subparsers)

# Subcommand: simulation (with a sub-menu for simulation types)
if device_type == 'desktop':
if device_type == 'desktop' and 'mode' in params:
# Add simulation subcommand
parser_simulation = parser_simulation_menu(subparsers, params)

Expand Down
4 changes: 2 additions & 2 deletions src/nanosaur/ros.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def run_dev_script(params, host_workspace_path, workspace_path):
os.chdir(isaac_ros_common_path)
print(f"Changed directory to: {isaac_ros_common_path}")

nanosaur_home_path = get_nanosaur_home(params['nanosaur_home'])
nanosaur_home_path = get_nanosaur_home()

# Path to the script you want to run
command = "./scripts/run_dev.sh"
Expand Down Expand Up @@ -266,7 +266,7 @@ def deploy_docker_perception(params: Params, perception_ws_path: str) -> bool:
nanosaur_perception_path = os.path.join(perception_ws_path, 'src', 'nanosaur_perception')

src_folders = [
os.path.join(get_nanosaur_home(params['nanosaur_home']), 'shared_src'),
os.path.join(get_nanosaur_home(), 'shared_src'),
os.path.join(perception_ws_path, 'src')
]

Expand Down
35 changes: 18 additions & 17 deletions src/nanosaur/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@
ENGINES_CHOICES = ['vslam', 'nvblox', 'apriltag']


NANOSAUR_CONFIG_FILE_NAME = 'nanosaur.yaml'
NANOSAUR_HOME_NAME = 'nanosaur'


class Robot:

@classmethod
Expand Down Expand Up @@ -219,22 +223,20 @@ def print_all_robots(self, robot_idx=None):
class Params:

@classmethod
def load(cls, default_params, home_folder, params_file_name):
params_file = Params.get_params_file(home_folder, params_file_name)
def load(cls, default_params):
params_file = Params.get_params_file()
# Load parameters from YAML file if it exists
if os.path.exists(params_file):
with open(params_file, 'r') as file:
params_dict = yaml.safe_load(file)
else:
params_dict = default_params

return cls(params_dict, home_folder, params_file)
return cls(params_dict)

def __init__(self, params_dict, home_folder, params_file_name):
def __init__(self, params_dict):
self._params_dict = params_dict
self._default_params = copy.deepcopy(params_dict)
self.home_folder = home_folder
self.params_file_name = params_file_name
for key, value in params_dict.items():
setattr(self, key, value)

Expand All @@ -260,19 +262,19 @@ def __repr__(self):
return str(self._params_dict)

def save(self):
params_file = Params.get_params_file(self.home_folder, self.params_file_name)
params_file = Params.get_params_file()
# Save the parameters to the file if they are different from the default
if params_file and self._params_dict != self._default_params:
# Get the current nanosaur's home directory
create_nanosaur_home(self.home_folder)
create_nanosaur_home()
# Save the parameters to the file
print(TerminalFormatter.color_text(f"Saving parameters to {self.params_file_name}", color='yellow'))
print(TerminalFormatter.color_text(f"Saving parameters to {params_file}", color='yellow'))
with open(params_file, 'w') as file:
yaml.dump(self._params_dict, file)

@staticmethod
def get_params_file(home_folder, params_file_name) -> str:
return os.path.join(get_nanosaur_home(home_folder), params_file_name)
def get_params_file() -> str:
return os.path.join(get_nanosaur_home(), NANOSAUR_CONFIG_FILE_NAME)

def get(self, key, default=None):
return getattr(self, key, default)
Expand All @@ -287,20 +289,19 @@ def items(self):
return self._params_dict.items()


def create_nanosaur_home(nanosaur_home) -> str:
# Get the current user's home directory
user_home_dir = os.path.expanduser("~")
# Create the full path for the workspace folder in the user's home directory
nanosaur_home_path = os.path.join(user_home_dir, nanosaur_home)
def create_nanosaur_home() -> str:
# Get the current nanosaur's home directory
nanosaur_home_path = get_nanosaur_home()
# Check if folder exists, if not, create it
if not os.path.exists(nanosaur_home_path):
os.makedirs(nanosaur_home_path)
print(TerminalFormatter.color_text(f"Folder '{nanosaur_home_path}' created.", color='green'))
return nanosaur_home_path


def get_nanosaur_home(nanosaur_home) -> str:
def get_nanosaur_home() -> str:
# Get the current user's home directory
nanosaur_home = os.getenv('NANOSAUR_HOME', NANOSAUR_HOME_NAME)
user_home_dir = os.path.expanduser("~")
return os.path.join(user_home_dir, nanosaur_home)

Expand Down
21 changes: 9 additions & 12 deletions src/nanosaur/workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def get_selected_workspace(params, workspace_actions, args):

def clean(platform, params: Params, args):
""" Clean the workspace """
nanosaur_home_path = get_nanosaur_home(params['nanosaur_home'])
nanosaur_home_path = get_nanosaur_home()
workspace_actions = {
'developer': lambda: clean_workspace(nanosaur_home_path, params['ws_developer_name']),
'robot': lambda: clean_workspace(nanosaur_home_path, params['ws_robot_name']),
Expand All @@ -114,8 +114,8 @@ def clean(platform, params: Params, args):
def update(platform, params: Params, args):
""" Update the workspace """
# Update shared workspace
def update_shared_workspace(params):
nanosaur_home_path = get_nanosaur_home(params['nanosaur_home'])
def update_shared_workspace():
nanosaur_home_path = get_nanosaur_home()
shared_src_path = os.path.join(nanosaur_home_path, "shared_src")
rosinstall_path = os.path.join(shared_src_path, "shared.rosinstall")
if os.path.exists(rosinstall_path):
Expand Down Expand Up @@ -156,7 +156,7 @@ def update_workspace(params, workspace_type, workspace_name_key, force, skip_ros
}
if args.all:
print(TerminalFormatter.color_text("Updating all workspaces", bold=True))
update_shared_workspace(params)
update_shared_workspace()
results = [action() for action in workspace_actions.values()]
return all(results)
# Get the workspace
Expand All @@ -166,7 +166,7 @@ def update_workspace(params, workspace_type, workspace_name_key, force, skip_ros
# Update the workspace
print(TerminalFormatter.color_text(f"Updating {workspace}", bold=True))
if action := workspace_actions.get(workspace):
update_shared_workspace(params)
update_shared_workspace()
return action()
print(TerminalFormatter.color_text(f"I cannot update this {workspace}", color='red'))
return False
Expand Down Expand Up @@ -251,7 +251,7 @@ def deploy(platform, params: Params, args):


def get_workspaces_path(params: Params) -> dict:
nanosaur_home_path = get_nanosaur_home(params['nanosaur_home'])
nanosaur_home_path = get_nanosaur_home()
# Add all workspaces that exist in the Nanosaur home folder
workspaces = {
'ws_developer_name': params['ws_developer_name'],
Expand All @@ -268,7 +268,7 @@ def get_workspaces_path(params: Params) -> dict:

def get_workspace_path(params: Params, ws_name) -> str:
# Create the Nanosaur home folder
nanosaur_home_path = create_nanosaur_home(params['nanosaur_home'])
nanosaur_home_path = create_nanosaur_home()
# Create the full path for the workspace folder in the user's home directory
workspace_path = os.path.join(nanosaur_home_path, ws_name)

Expand Down Expand Up @@ -359,10 +359,8 @@ def build_workspace(nanosaur_raw_github_repo, branch, workspace_path, rosinstall


def create_developer_workspace(platform, params: Params, args, password=None) -> bool:
# Get the Nanosaur home folder and branch
nanosaur_home = params['nanosaur_home']
# Create the Nanosaur home folder
nanosaur_home_path = create_nanosaur_home(nanosaur_home)
nanosaur_home_path = create_nanosaur_home()
# Create developer workspace
create_workspace(nanosaur_home_path, params['ws_developer_name'], skip_create_colcon_setting=True)
return True
Expand All @@ -373,11 +371,10 @@ def create_maintainer_workspace(platform, params: Params, args, password=None):
# determine the device type
device_type = "robot" if platform['Machine'] == 'jetson' else "desktop"
# Get the Nanosaur home folder and branch
nanosaur_home = params['nanosaur_home']
nanosaur_raw_github_repo = params['nanosaur_raw_github_repo']
branch = params['nanosaur_branch']
# Create the Nanosaur home folder
nanosaur_home_path = create_nanosaur_home(nanosaur_home)
nanosaur_home_path = create_nanosaur_home()

# Create the shared source folder
nanosaur_shared_src = os.path.join(nanosaur_home_path, "shared_src")
Expand Down

0 comments on commit 7a4abad

Please sign in to comment.