Skip to content

ark_profiles_action

ArkProfilesAction

Bases: ArkAction

Source code in ark_sdk_python/actions/ark_profiles_action.py
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
class ArkProfilesAction(ArkAction):
    @overrides
    def define_action(self, subparsers: argparse._SubParsersAction) -> None:
        """
        Defines the CLI `profile` action, and adds actions for managing multiple profiles.

        Args:
            subparsers (argparse._SubParsersAction): _description_
        """
        profile_parser: argparse.ArgumentParser = subparsers.add_parser('profiles')
        self._common_actions_configuration(profile_parser)
        profile_cmd_subparsers = profile_parser.add_subparsers(dest="profile_cmd")
        profile_cmd_subparsers.required = True
        list_profiles_parser = profile_cmd_subparsers.add_parser('list', help='List all profiles')
        list_profiles_parser.add_argument('-n', '--name', help='Profile name to filter with by wildcard')
        list_profiles_parser.add_argument('-ap', '--auth-profile', help='Filter profiles by auth types')
        list_profiles_parser.add_argument(
            '-a', '--all', action='store_true', help='Whether to show all profiles data as well and not only their names'
        )
        show_profile_parser = profile_cmd_subparsers.add_parser('show', help='Show a profile')
        show_profile_parser.add_argument(
            '-pn', '--profile-name', required=False, help='Profile name to show, if not given, shows the current one'
        )
        delete_profile_parser = profile_cmd_subparsers.add_parser('delete', help='Delete a specific profile')
        delete_profile_parser.add_argument('-pn', '--profile-name', required=True, help='Profile name to delete')
        delete_profile_parser.add_argument('-y', '--yes', action='store_true', help='Whether to approve deletion non interactively')
        clear_profiles_parser = profile_cmd_subparsers.add_parser('clear', help='Clear all profiles')
        clear_profiles_parser.add_argument('-y', '--yes', action='store_true', help='Whether to approve clear non interactively')
        clone_profile_parser = profile_cmd_subparsers.add_parser('clone', help='Clones a profile')
        clone_profile_parser.add_argument('-pn', '--profile-name', required=True, help='Profile name to clone')
        clone_profile_parser.add_argument(
            '-npn', '--new-profile-name', help='New cloned profile name, if not given, will add _clone as part of the name'
        )
        clone_profile_parser.add_argument('-y', '--yes', action='store_true', help='Whether to override existing profile if exists')
        add_profile_parser = profile_cmd_subparsers.add_parser('add', help='Adds a profile to the profiles folder from a given path')
        add_profile_parser.add_argument('-pp', '--profile-path', required=True, help='Profile file path to be added')
        edit_profile_parser = profile_cmd_subparsers.add_parser('edit', help='Edits a profile interactively')
        edit_profile_parser.add_argument(
            '-pn', '--profile-name', required=False, help='Profile name to edit, if not given, edits the current one'
        )

    def __run_list_action(self, args: argparse.Namespace) -> None:
        # Start by loading all the profiles
        profiles: Optional[List[ArkProfile]] = ArkProfileLoader.load_all_profiles()
        if not profiles:
            ArkArgsFormatter.print_warning(
                'No profiles were found',
            )
            return
        # Filter profiles
        if args.name:
            profiles = [p for p in profiles if fnmatch(p.profile_name, args.name)]
        if args.auth_profile:
            profiles = [p for p in profiles if args.auth_profile in list(p.auth_profiles.keys())]
        # Print them based on request
        if args.all:
            ArkArgsFormatter.print_success(json.dumps([p.dict() for p in profiles], indent=4))
        else:
            ArkArgsFormatter.print_success(json.dumps([p.profile_name for p in profiles], indent=4))

    def __run_show_action(self, args: argparse.Namespace) -> None:
        profile_name = args.profile_name or ArkProfileLoader.deduce_profile_name()
        profile: Optional[ArkProfile] = ArkProfileLoader.load_profile(profile_name)
        if not profile:
            ArkArgsFormatter.print_warning(
                f'No profile was found for the name {profile_name}',
            )
            return
        ArkArgsFormatter.print_success(profile.json(indent=4))

    def __run_delete_action(self, args: argparse.Namespace) -> None:
        profile: Optional[ArkProfile] = ArkProfileLoader.load_profile(args.profile_name)
        if not profile:
            ArkArgsFormatter.print_warning(
                f'No profile was found for the name {args.profile_name}',
            )
            return
        if not args.yes:
            answer = inquirer.prompt(
                [inquirer.Confirm('answer', message=f'Are you sure you want to delete profile {args.profile_name}')],
                render=ArkInquirerRender(),
            )
            if not answer or not answer['answer']:
                return
        ArkProfileLoader.delete_profile(args.profile_name)

    def __run_clear_action(self, args: argparse.Namespace) -> None:
        if not args.yes:
            answer = inquirer.prompt(
                [inquirer.Confirm('answer', message='Are you sure you want to clear all profiles')], render=ArkInquirerRender()
            )
            if not answer or not answer['answer']:
                return
        ArkProfileLoader.clear_all_profiles()

    def __run_clone_action(self, args: argparse.Namespace) -> None:
        profile: Optional[ArkProfile] = ArkProfileLoader.load_profile(args.profile_name)
        if not profile:
            ArkArgsFormatter.print_warning(
                f'No profile was found for the name {args.profile_name}',
            )
            return
        cloned_profile: ArkProfile = profile.copy(update={'profile_name': args.new_profile_name or f'{profile.profile_name}_clone'})
        if ArkProfileLoader.profile_exists(cloned_profile.profile_name):
            if not args.yes:
                answer = inquirer.prompt(
                    [
                        inquirer.Confirm(
                            'answer', message=f'Are you sure you want to override existing profile {cloned_profile.profile_name}'
                        )
                    ],
                    render=ArkInquirerRender(),
                )
                if not answer or not answer['answer']:
                    return
        ArkProfileLoader.save_profile(cloned_profile)

    def __run_add_action(self, args: argparse.Namespace) -> None:
        if not os.path.exists(args.profile_path):
            ArkArgsFormatter.print_warning(
                f'Profile path [{args.profile_path}] does not exist, ignoring',
            )
            return
        try:
            profile: ArkProfile = ArkProfile.parse_file(args.profile_path)
            ArkProfileLoader.save_profile(profile)
        except Exception as ex:
            self._logger.exception(f'Failed to parser profile [{str(ex)}]')
            ArkArgsFormatter.print_failure(
                f'Profile path [{args.profile_path}] failed to be parsed, aborting',
            )
            return

    def __run_edit_action(self, args: argparse.Namespace) -> None:
        profile_name = args.profile_name or ArkProfileLoader.deduce_profile_name()
        profile: Optional[ArkProfile] = ArkProfileLoader.load_profile(profile_name)
        if not profile:
            ArkArgsFormatter.print_warning(
                f'No profile was found for the name {profile_name}',
            )
            return
        answer = inquirer.prompt(
            [inquirer.Editor('profile_edit', message=f'Chosen profile [{profile_name}] is about to be edited')],
            render=ArkInquirerRender(),
            answers={'profile_edit': profile.json(indent=4)},
        )
        edited_profile = ArkProfile.parse_raw(answer['profile_edit'])
        ArkProfileLoader.save_profile(edited_profile)

    @overrides
    def run_action(self, args: argparse.Namespace) -> None:
        """
        Runs the profile action.

        Args:
            args (argparse.Namespace): _description_

        Raises:
            ArkException: _description_
            ArkException: _description_
        """
        if args.profile_cmd == 'list':
            self.__run_list_action(args)
        elif args.profile_cmd == 'show':
            self.__run_show_action(args)
        elif args.profile_cmd == 'delete':
            self.__run_delete_action(args)
        elif args.profile_cmd == 'clear':
            self.__run_clear_action(args)
        elif args.profile_cmd == 'clone':
            self.__run_clone_action(args)
        elif args.profile_cmd == 'add':
            self.__run_add_action(args)
        elif args.profile_cmd == 'edit':
            self.__run_edit_action(args)
        else:
            raise ArkException(f'Invalid command {args.profile_cmd} given')

    @overrides
    def can_run_action(self, action_name: str, args: argparse.Namespace) -> bool:
        """
        Asserts the action is `profile`.

        Args:
            action_name (str): _description_
            args (argparse.Namespace): _description_

        Returns:
            bool: _description_
        """
        return action_name == 'profiles'

can_run_action(action_name, args)

Asserts the action is profile.

Parameters:

Name Type Description Default
action_name str

description

required
args Namespace

description

required

Returns:

Name Type Description
bool bool

description

Source code in ark_sdk_python/actions/ark_profiles_action.py
193
194
195
196
197
198
199
200
201
202
203
204
205
@overrides
def can_run_action(self, action_name: str, args: argparse.Namespace) -> bool:
    """
    Asserts the action is `profile`.

    Args:
        action_name (str): _description_
        args (argparse.Namespace): _description_

    Returns:
        bool: _description_
    """
    return action_name == 'profiles'

define_action(subparsers)

Defines the CLI profile action, and adds actions for managing multiple profiles.

Parameters:

Name Type Description Default
subparsers _SubParsersAction

description

required
Source code in ark_sdk_python/actions/ark_profiles_action.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@overrides
def define_action(self, subparsers: argparse._SubParsersAction) -> None:
    """
    Defines the CLI `profile` action, and adds actions for managing multiple profiles.

    Args:
        subparsers (argparse._SubParsersAction): _description_
    """
    profile_parser: argparse.ArgumentParser = subparsers.add_parser('profiles')
    self._common_actions_configuration(profile_parser)
    profile_cmd_subparsers = profile_parser.add_subparsers(dest="profile_cmd")
    profile_cmd_subparsers.required = True
    list_profiles_parser = profile_cmd_subparsers.add_parser('list', help='List all profiles')
    list_profiles_parser.add_argument('-n', '--name', help='Profile name to filter with by wildcard')
    list_profiles_parser.add_argument('-ap', '--auth-profile', help='Filter profiles by auth types')
    list_profiles_parser.add_argument(
        '-a', '--all', action='store_true', help='Whether to show all profiles data as well and not only their names'
    )
    show_profile_parser = profile_cmd_subparsers.add_parser('show', help='Show a profile')
    show_profile_parser.add_argument(
        '-pn', '--profile-name', required=False, help='Profile name to show, if not given, shows the current one'
    )
    delete_profile_parser = profile_cmd_subparsers.add_parser('delete', help='Delete a specific profile')
    delete_profile_parser.add_argument('-pn', '--profile-name', required=True, help='Profile name to delete')
    delete_profile_parser.add_argument('-y', '--yes', action='store_true', help='Whether to approve deletion non interactively')
    clear_profiles_parser = profile_cmd_subparsers.add_parser('clear', help='Clear all profiles')
    clear_profiles_parser.add_argument('-y', '--yes', action='store_true', help='Whether to approve clear non interactively')
    clone_profile_parser = profile_cmd_subparsers.add_parser('clone', help='Clones a profile')
    clone_profile_parser.add_argument('-pn', '--profile-name', required=True, help='Profile name to clone')
    clone_profile_parser.add_argument(
        '-npn', '--new-profile-name', help='New cloned profile name, if not given, will add _clone as part of the name'
    )
    clone_profile_parser.add_argument('-y', '--yes', action='store_true', help='Whether to override existing profile if exists')
    add_profile_parser = profile_cmd_subparsers.add_parser('add', help='Adds a profile to the profiles folder from a given path')
    add_profile_parser.add_argument('-pp', '--profile-path', required=True, help='Profile file path to be added')
    edit_profile_parser = profile_cmd_subparsers.add_parser('edit', help='Edits a profile interactively')
    edit_profile_parser.add_argument(
        '-pn', '--profile-name', required=False, help='Profile name to edit, if not given, edits the current one'
    )

run_action(args)

Runs the profile action.

Parameters:

Name Type Description Default
args Namespace

description

required

Raises:

Type Description
ArkException

description

ArkException

description

Source code in ark_sdk_python/actions/ark_profiles_action.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
@overrides
def run_action(self, args: argparse.Namespace) -> None:
    """
    Runs the profile action.

    Args:
        args (argparse.Namespace): _description_

    Raises:
        ArkException: _description_
        ArkException: _description_
    """
    if args.profile_cmd == 'list':
        self.__run_list_action(args)
    elif args.profile_cmd == 'show':
        self.__run_show_action(args)
    elif args.profile_cmd == 'delete':
        self.__run_delete_action(args)
    elif args.profile_cmd == 'clear':
        self.__run_clear_action(args)
    elif args.profile_cmd == 'clone':
        self.__run_clone_action(args)
    elif args.profile_cmd == 'add':
        self.__run_add_action(args)
    elif args.profile_cmd == 'edit':
        self.__run_edit_action(args)
    else:
        raise ArkException(f'Invalid command {args.profile_cmd} given')