Skip to content

models

ArkProfileLoader

Source code in ark_sdk_python/models/ark_profile.py
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
class ArkProfileLoader:
    @staticmethod
    def profiles_folder() -> str:
        """
        Retrieves the profiles folder pathname, from the environment variable when set; otherwise, from the default location.

        Returns:
            str: _description_
        """
        return os.getenv('ARK_PROFILES_FOLDER', os.path.join(Path.home(), '.ark_profiles'))

    @staticmethod
    def default_profile_name() -> str:
        """
        Getter for the default profile name.

        Returns:
            str: _description_
        """
        return 'ark'

    @staticmethod
    def deduce_profile_name(profile_name: Optional[str] = None) -> str:
        """
        Deduces the profile name from the env.

        Args:
            profile_name (Optional[str], optional): Defaults to `None`

        Returns:
            str: _description_
        """
        if profile_name and profile_name != ArkProfileLoader.default_profile_name():
            return profile_name
        if 'ARK_PROFILE' in os.environ:
            return os.environ['ARK_PROFILE']
        if profile_name:
            return profile_name
        return ArkProfileLoader.default_profile_name()

    @staticmethod
    def load_default_profile() -> ArkProfile:
        """
        Loads the default profile, either from the OS or creates a new one.

        Returns:
            ArkProfile: _description_
        """
        folder = ArkProfileLoader.profiles_folder()
        profile_name = ArkProfileLoader.deduce_profile_name()
        if os.path.exists(os.path.join(folder, profile_name)):
            profile: ArkProfile = ArkProfile.parse_file(os.path.join(folder, profile_name))
            return profile
        return ArkProfile()

    @staticmethod
    def load_profile(profile_name: str) -> Optional[ArkProfile]:
        """
        Loads the specified profile from the OS.
        Returns `None` when a profile is not found with the specified name.

        Args:
            profile_name (str): _description_

        Returns:
            Optional[ArkProfile]: _description_
        """
        folder = ArkProfileLoader.profiles_folder()
        if os.path.exists(os.path.join(folder, profile_name)):
            profile: ArkProfile = ArkProfile.parse_file(os.path.join(folder, profile_name))
            return profile
        return None

    @staticmethod
    def save_profile(profile: ArkProfile) -> None:
        """
        Saves the profile to the profiles folder on the filesystem.

        Args:
            profile (ArkProfile): _description_
        """
        folder = ArkProfileLoader.profiles_folder()
        if not os.path.exists(folder):
            os.makedirs(folder)
        with open(os.path.join(folder, profile.profile_name), 'w', encoding='utf-8') as f:
            f.write(profile.json(indent=4, by_alias=False))

    @staticmethod
    def load_all_profiles() -> Optional[List[ArkProfile]]:
        """
        Loads all the profiles that exist on the machine.

        Returns:
            Optional[List[ArkProfile]]: _description_
        """
        logger = get_logger('load_all_profiles')
        folder = ArkProfileLoader.profiles_folder()
        if not os.path.exists(folder):
            return None
        profiles: List[ArkProfile] = []
        for profile_name in os.listdir(folder):
            try:
                profiles.append(ArkProfile.parse_file(os.path.join(folder, profile_name)))
            except Exception as ex:
                logger.warning(f'Profile {profile_name} failed to be loaded successfully [{str(ex)}]')
                continue
        return profiles

    @staticmethod
    def delete_profile(profile_name: str) -> None:
        """
        Deletes the specified profile.

        Args:
            profile_name (str): The name of the profile to delete
        """
        folder = ArkProfileLoader.profiles_folder()
        if not os.path.exists(folder):
            return None
        if os.path.exists(os.path.join(folder, profile_name)):
            os.unlink(os.path.join(folder, profile_name))

    @staticmethod
    def clear_all_profiles() -> None:
        """
        Clears all profiles.
        """
        folder = ArkProfileLoader.profiles_folder()
        if not os.path.exists(folder):
            return None
        for profile_name in os.listdir(folder):
            os.unlink(os.path.join(folder, profile_name))

    @staticmethod
    def profile_exists(profile_name: str) -> bool:
        """
        Checks if the specified profile exists.

        Args:
            profile_name (str): _description_

        Returns:
            bool: _description_
        """
        folder = ArkProfileLoader.profiles_folder()
        if not os.path.exists(folder):
            return False
        return os.path.exists(os.path.join(folder, profile_name))

clear_all_profiles() staticmethod

Clears all profiles.

Source code in ark_sdk_python/models/ark_profile.py
156
157
158
159
160
161
162
163
164
165
@staticmethod
def clear_all_profiles() -> None:
    """
    Clears all profiles.
    """
    folder = ArkProfileLoader.profiles_folder()
    if not os.path.exists(folder):
        return None
    for profile_name in os.listdir(folder):
        os.unlink(os.path.join(folder, profile_name))

deduce_profile_name(profile_name=None) staticmethod

Deduces the profile name from the env.

Parameters:

Name Type Description Default
profile_name Optional[str]

Defaults to None

None

Returns:

Name Type Description
str str

description

Source code in ark_sdk_python/models/ark_profile.py
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
@staticmethod
def deduce_profile_name(profile_name: Optional[str] = None) -> str:
    """
    Deduces the profile name from the env.

    Args:
        profile_name (Optional[str], optional): Defaults to `None`

    Returns:
        str: _description_
    """
    if profile_name and profile_name != ArkProfileLoader.default_profile_name():
        return profile_name
    if 'ARK_PROFILE' in os.environ:
        return os.environ['ARK_PROFILE']
    if profile_name:
        return profile_name
    return ArkProfileLoader.default_profile_name()

default_profile_name() staticmethod

Getter for the default profile name.

Returns:

Name Type Description
str str

description

Source code in ark_sdk_python/models/ark_profile.py
45
46
47
48
49
50
51
52
53
@staticmethod
def default_profile_name() -> str:
    """
    Getter for the default profile name.

    Returns:
        str: _description_
    """
    return 'ark'

delete_profile(profile_name) staticmethod

Deletes the specified profile.

Parameters:

Name Type Description Default
profile_name str

The name of the profile to delete

required
Source code in ark_sdk_python/models/ark_profile.py
142
143
144
145
146
147
148
149
150
151
152
153
154
@staticmethod
def delete_profile(profile_name: str) -> None:
    """
    Deletes the specified profile.

    Args:
        profile_name (str): The name of the profile to delete
    """
    folder = ArkProfileLoader.profiles_folder()
    if not os.path.exists(folder):
        return None
    if os.path.exists(os.path.join(folder, profile_name)):
        os.unlink(os.path.join(folder, profile_name))

load_all_profiles() staticmethod

Loads all the profiles that exist on the machine.

Returns:

Type Description
Optional[List[ArkProfile]]

Optional[List[ArkProfile]]: description

Source code in ark_sdk_python/models/ark_profile.py
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
@staticmethod
def load_all_profiles() -> Optional[List[ArkProfile]]:
    """
    Loads all the profiles that exist on the machine.

    Returns:
        Optional[List[ArkProfile]]: _description_
    """
    logger = get_logger('load_all_profiles')
    folder = ArkProfileLoader.profiles_folder()
    if not os.path.exists(folder):
        return None
    profiles: List[ArkProfile] = []
    for profile_name in os.listdir(folder):
        try:
            profiles.append(ArkProfile.parse_file(os.path.join(folder, profile_name)))
        except Exception as ex:
            logger.warning(f'Profile {profile_name} failed to be loaded successfully [{str(ex)}]')
            continue
    return profiles

load_default_profile() staticmethod

Loads the default profile, either from the OS or creates a new one.

Returns:

Name Type Description
ArkProfile ArkProfile

description

Source code in ark_sdk_python/models/ark_profile.py
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@staticmethod
def load_default_profile() -> ArkProfile:
    """
    Loads the default profile, either from the OS or creates a new one.

    Returns:
        ArkProfile: _description_
    """
    folder = ArkProfileLoader.profiles_folder()
    profile_name = ArkProfileLoader.deduce_profile_name()
    if os.path.exists(os.path.join(folder, profile_name)):
        profile: ArkProfile = ArkProfile.parse_file(os.path.join(folder, profile_name))
        return profile
    return ArkProfile()

load_profile(profile_name) staticmethod

Loads the specified profile from the OS. Returns None when a profile is not found with the specified name.

Parameters:

Name Type Description Default
profile_name str

description

required

Returns:

Type Description
Optional[ArkProfile]

Optional[ArkProfile]: description

Source code in ark_sdk_python/models/ark_profile.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
@staticmethod
def load_profile(profile_name: str) -> Optional[ArkProfile]:
    """
    Loads the specified profile from the OS.
    Returns `None` when a profile is not found with the specified name.

    Args:
        profile_name (str): _description_

    Returns:
        Optional[ArkProfile]: _description_
    """
    folder = ArkProfileLoader.profiles_folder()
    if os.path.exists(os.path.join(folder, profile_name)):
        profile: ArkProfile = ArkProfile.parse_file(os.path.join(folder, profile_name))
        return profile
    return None

profile_exists(profile_name) staticmethod

Checks if the specified profile exists.

Parameters:

Name Type Description Default
profile_name str

description

required

Returns:

Name Type Description
bool bool

description

Source code in ark_sdk_python/models/ark_profile.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
@staticmethod
def profile_exists(profile_name: str) -> bool:
    """
    Checks if the specified profile exists.

    Args:
        profile_name (str): _description_

    Returns:
        bool: _description_
    """
    folder = ArkProfileLoader.profiles_folder()
    if not os.path.exists(folder):
        return False
    return os.path.exists(os.path.join(folder, profile_name))

profiles_folder() staticmethod

Retrieves the profiles folder pathname, from the environment variable when set; otherwise, from the default location.

Returns:

Name Type Description
str str

description

Source code in ark_sdk_python/models/ark_profile.py
35
36
37
38
39
40
41
42
43
@staticmethod
def profiles_folder() -> str:
    """
    Retrieves the profiles folder pathname, from the environment variable when set; otherwise, from the default location.

    Returns:
        str: _description_
    """
    return os.getenv('ARK_PROFILES_FOLDER', os.path.join(Path.home(), '.ark_profiles'))

save_profile(profile) staticmethod

Saves the profile to the profiles folder on the filesystem.

Parameters:

Name Type Description Default
profile ArkProfile

description

required
Source code in ark_sdk_python/models/ark_profile.py
107
108
109
110
111
112
113
114
115
116
117
118
119
@staticmethod
def save_profile(profile: ArkProfile) -> None:
    """
    Saves the profile to the profiles folder on the filesystem.

    Args:
        profile (ArkProfile): _description_
    """
    folder = ArkProfileLoader.profiles_folder()
    if not os.path.exists(folder):
        os.makedirs(folder)
    with open(os.path.join(folder, profile.profile_name), 'w', encoding='utf-8') as f:
        f.write(profile.json(indent=4, by_alias=False))