Ejemplo n.º 1
0
    def read_settings_for_path(self, project_path) -> (None, ErrorMsg):
        """
        Update self._current_settings according to local settings for 'project_path' (if any)
        and global settings.
        :param project_path: path to the directory for which settings are updated; normally
                     it's a current working directory
        :return: error message as the second element of the tuple
        """
        if self._current_settings_modified:
            return None, f"modified settings not saved"
        if not file_utils.dir_exists(project_path):
            # return None, f"Path not exists{project_path}"
            project_path = file_utils.get_user_home_dir()
        self._current_project_path = project_path
        # Load global settings

        # print(f"Before adding settings from {self.global_config_file}")
        # self._add_settings_from_file(self.global_config_file)
        self._current_settings, error = Settings.read_settings_from_file(
            self.cfg_parser, self.global_config_file)
        if error:
            return None, error

        if project_path == file_utils.get_user_home_dir():
            return None, ""

        current_templgen_dir = os.path.join(project_path,
                                            Settings.TEMPLGEN_DIR_NAME)
        if not file_utils.dir_exists(current_templgen_dir):
            return None, ""

        # Check if path-local settings exist
        local_config_file = os.path.join(current_templgen_dir,
                                         Settings.TEMPLGEN_CONFIG_FILE_NAME)
        # print(f"Before adding settings from {config_file}")
        if file_utils.file_exists(local_config_file):
            # Load local settings and override global
            # self._add_settings_from_file(config_file)
            local_settings, error = Settings.read_settings_from_file(
                self.cfg_parser, local_config_file)
            if error:
                return None, error
            Settings.merge_settings(self._current_settings, local_settings)
        else:
            print(
                f"-- Settings: local config file not found: '{local_config_file}'"
            )
        return None, ""
Ejemplo n.º 2
0
 def initlocal(self, path) -> (None, ErrorMsg):
     local_config_dir = os.path.join(path, Settings.TEMPLGEN_DIR_NAME)
     if file_utils.dir_exists(local_config_dir):
         return None, f"local config directory already exists: '{local_config_dir}'"
     # Create a copy of global settings for current user only
     file_utils.copy_dir_noexcept(self.global_templgen_dir,
                                  local_config_dir)
     return None, ""
Ejemplo n.º 3
0
 def check_home_dir_integrity(cls) -> bool:
     """
     :return: True if success, False if problems found
     """
     if not file_utils.dir_exists(cls.HOME_DIR):
         cls._init_home_dir()
         return True
     if not file_utils.file_exists(cls.SETTINGS_FILE):
         cls.create_default_settings(silent=False)
Ejemplo n.º 4
0
 def user_exists_globally(user_name) -> bool:
     """
     Check if user exists globally
     """
     user_dir = os.path.join(file_utils.get_user_home_dir(),
                             Settings.TEMPLGEN_DIR_NAME,
                             Settings.TEMPLGEN_USERS_DIR_NAME, user_name)
     # print(f"Debug: user_dir: {user_dir}")
     if file_utils.dir_exists(user_dir):
         return True
     return False
Ejemplo n.º 5
0
 def user_exists_locally(user_name, project_path) -> bool:
     """
     Check if user exists for the project (both
     :param user_name:
     :param project_path:
     :return:
     """
     user_dir = os.path.join(project_path, Settings.TEMPLGEN_DIR_NAME,
                             Settings.TEMPLGEN_USERS_DIR_NAME, user_name)
     if file_utils.dir_exists(user_dir):
         return True
     return False
Ejemplo n.º 6
0
    def _init_home_dir(cls, delete_old: bool = False) -> None:
        """
        Called on new installation or if directory for some reason does not exist
        """

        # No logging here: logger may have not been initialized

        if file_utils.dir_exists(cls.HOME_DIR):
            if not delete_old:
                return
            else:
                file_utils.remove_dir_noexcept(cls.HOME_DIR)
        if file_utils.create_path_noexcept(cls.HOME_DIR)["error"]:
            print(
                f"Critical error: can't create home directory {cls.HOME_DIR}")
            sys.exit(-1)

        cls.create_default_settings()
Ejemplo n.º 7
0
    def list_users(project_path: StringOrNone) -> list:
        """
        Lists users for the specified path
        :param project_path: project for which users will be listed;
                             if none, global users will be listed;
        :return: list of user names
        """
        if not project_path:
            project_path = file_utils.get_user_home_dir()

        users_dir = os.path.join(project_path, Settings.TEMPLGEN_DIR_NAME,
                                 Settings.TEMPLGEN_USERS_DIR_NAME)
        # print(f"debug userlist users_dir: {users_dir}")
        if not file_utils.dir_exists(users_dir):
            return []
        error = file_utils.get_subdirs(users_dir)["error"]
        if error:
            return []

        return file_utils.get_subdirs(users_dir)["subdirs"]
Ejemplo n.º 8
0
    def init(self, path=None) -> (None, ErrorMsg):
        """ Create global or local '.templgen' directory and its contents;
            If `path` is `None`, global settings in the user home dir will be initialized
        """
        if not path:
            path = file_utils.get_user_home_dir()
        path_to_templgen = os.path.join(path, Settings.TEMPLGEN_DIR_NAME)
        if file_utils.dir_exists(path_to_templgen):
            # remove old directory
            file_utils.remove_dir_noexcept(path_to_templgen)

        # Create main dir and 'users' and 'templates' dirs inside it
        Settings._create_dir_or_die(
            os.path.join(path_to_templgen, Settings.TEMPLGEN_USERS_DIR_NAME))
        Settings._create_dir_or_die(
            os.path.join(path_to_templgen, Settings.TEMPLGEN_TEMPL_DIR_NAME))
        # Write default config file
        config_file = os.path.join(path_to_templgen,
                                   Settings.TEMPLGEN_CONFIG_FILE_NAME)
        self._create_default_config_file(config_file)

        return None, ""
Ejemplo n.º 9
0
    def ensure_integrity(self, path=None) -> (None, ErrorMsg):
        """

        :param path: path to dir that must contain templgen settings;
                     if `None`, path is set to user home dir and global
                     settings integrity is checked
        :return: Error message as the second element in tuple
        """
        if not path:
            path = file_utils.get_user_home_dir()

        # Ensure .templgen directory exists
        path_to_templgen = os.path.join(path, Settings.TEMPLGEN_DIR_NAME)
        if not file_utils.dir_exists(path_to_templgen):
            # if not exists, create a new one
            print(f" ** Initializing directory '{path_to_templgen}' ...")
            return self.init(path)
        # Check config file
        config_file = os.path.join(path_to_templgen,
                                   Settings.TEMPLGEN_CONFIG_FILE_NAME)
        if not file_utils.file_exists(config_file):
            self._create_default_config_file(config_file)
        # TODO: additional check of file structure
        return None, ""
Ejemplo n.º 10
0
 def has_local_settings(path) -> bool:
     return file_utils.dir_exists(
         os.path.join(path, Settings.TEMPLGEN_DIR_NAME))