Browse Source

typing for commands

Guénaël Muller 7 years ago
parent
commit
996c3e1166
3 changed files with 63 additions and 29 deletions
  1. 13 9
      tracim/command/__init__.py
  2. 14 5
      tracim/command/initializedb.py
  3. 36 15
      tracim/command/user.py

+ 13 - 9
tracim/command/__init__.py View File

@@ -8,13 +8,12 @@ from cliff.command import Command
8 8
 from cliff.commandmanager import CommandManager
9 9
 
10 10
 from pyramid.paster import bootstrap
11
+from pyramid.scripting import AppEnvironment
11 12
 from tracim.exceptions import CommandAbortedError
12 13
 
13 14
 
14
-
15 15
 class TracimCLI(App):
16
-
17
-    def __init__(self):
16
+    def __init__(self) -> None:
18 17
         super(TracimCLI, self).__init__(
19 18
             description='TracimCli',
20 19
             version='0.1',
@@ -22,13 +21,13 @@ class TracimCLI(App):
22 21
             deferred_help=True,
23 22
             )
24 23
 
25
-    def initialize_app(self, argv):
24
+    def initialize_app(self, argv) -> None:
26 25
         self.LOG.debug('initialize_app')
27 26
 
28
-    def prepare_to_run_command(self, cmd):
27
+    def prepare_to_run_command(self, cmd) -> None:
29 28
         self.LOG.debug('prepare_to_run_command %s', cmd.__class__.__name__)
30 29
 
31
-    def clean_up(self, cmd, result, err):
30
+    def clean_up(self, cmd, result, err) -> None:
32 31
         self.LOG.debug('clean_up %s', cmd.__class__.__name__)
33 32
         if err:
34 33
             self.LOG.debug('got an error: %s', err)
@@ -49,14 +48,14 @@ class AppContextCommand(Command):
49 48
     """
50 49
     auto_setup_context = True
51 50
 
52
-    def take_action(self, parsed_args):
51
+    def take_action(self, parsed_args: argparse.Namespace) -> None:
53 52
         super(AppContextCommand, self).take_action(parsed_args)
54 53
         if self.auto_setup_context:
55 54
             with bootstrap(parsed_args.config_file) as app_context:
56 55
                 with app_context['request'].tm:
57 56
                     self.take_app_action(parsed_args, app_context)
58 57
 
59
-    def get_parser(self, prog_name):
58
+    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
60 59
         parser = super(AppContextCommand, self).get_parser(prog_name)
61 60
 
62 61
         parser.add_argument(
@@ -73,9 +72,14 @@ class AppContextCommand(Command):
73 72
     #     transaction.commit()
74 73
 
75 74
 
75
+# TODO - G.M - 10-04-2018 - [Cleanup][tempExample] - Drop this
76 76
 class TestTracimCommand(AppContextCommand):
77 77
 
78
-    def take_app_action(self, parser, app_context):
78
+    def take_app_action(
79
+            self,
80
+            parser: argparse.ArgumentParser,
81
+            app_context: AppEnvironment
82
+    ) -> None:
79 83
         print('test')
80 84
 
81 85
 

+ 14 - 5
tracim/command/initializedb.py View File

@@ -1,4 +1,7 @@
1 1
 # -*- coding: utf-8 -*-
2
+import argparse
3
+
4
+import plaster_pastedeploy
2 5
 import transaction
3 6
 from pyramid.paster import (
4 7
     get_appsettings,
@@ -21,14 +24,14 @@ from tracim.models import (
21 24
 class InitializeDBCommand(AppContextCommand):
22 25
     auto_setup_context = False
23 26
 
24
-    def get_description(self):
27
+    def get_description(self) -> str:
25 28
         return "Initialize DB"
26 29
 
27
-    def get_parser(self, prog_name):
30
+    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
28 31
         parser = super().get_parser(prog_name)
29 32
         return parser
30 33
 
31
-    def take_action(self, parsed_args):
34
+    def take_action(self, parsed_args: argparse.Namespace) -> None:
32 35
         super(InitializeDBCommand, self).take_action(parsed_args)
33 36
         config_uri = parsed_args.config_file
34 37
         setup_logging(config_uri)
@@ -37,13 +40,19 @@ class InitializeDBCommand(AppContextCommand):
37 40
         self._populate_database(settings)
38 41
 
39 42
     @classmethod
40
-    def _create_schema(cls, settings):
43
+    def _create_schema(
44
+            cls,
45
+            settings: plaster_pastedeploy.ConfigDict
46
+    ) -> None:
41 47
         print("- Create Schemas of databases -")
42 48
         engine = get_engine(settings)
43 49
         DeclarativeBase.metadata.create_all(engine)
44 50
 
45 51
     @classmethod
46
-    def _populate_database(cls, settings):
52
+    def _populate_database(
53
+            cls,
54
+            settings: plaster_pastedeploy.ConfigDict
55
+    ) -> None:
47 56
         engine = get_engine(settings)
48 57
         session_factory = get_session_factory(engine)
49 58
         app_config = CFG(settings)

+ 36 - 15
tracim/command/user.py View File

@@ -1,4 +1,6 @@
1 1
 # -*- coding: utf-8 -*-
2
+import argparse
3
+from pyramid.scripting import AppEnvironment
2 4
 import transaction
3 5
 from sqlalchemy.exc import IntegrityError
4 6
 
@@ -14,6 +16,7 @@ from tracim.exceptions import CommandAbortedError
14 16
 from tracim.lib.core.group import GroupApi
15 17
 from tracim.lib.core.user import UserApi
16 18
 from tracim.models import User
19
+from tracim.models import Group
17 20
 
18 21
 
19 22
 class UserCommand(AppContextCommand):
@@ -23,10 +26,10 @@ class UserCommand(AppContextCommand):
23 26
 
24 27
     action = NotImplemented
25 28
 
26
-    def get_description(self):
29
+    def get_description(self) -> str:
27 30
         return '''Create or update user.'''
28 31
 
29
-    def get_parser(self, prog_name):
32
+    def get_parser(self, prog_name: str) -> argparse.ArgumentParser:
30 33
         parser = super().get_parser(prog_name)
31 34
 
32 35
         parser.add_argument(
@@ -77,32 +80,42 @@ class UserCommand(AppContextCommand):
77 80
 
78 81
         return parser
79 82
 
80
-    def _user_exist(self, login):
83
+    def _user_exist(self, login: str) -> User:
81 84
         return self._user_api.user_with_email_exists(login)
82 85
 
83
-    def _get_group(self, name):
86
+    def _get_group(self, name: str) -> Group:
84 87
         return self._group_api.get_one_with_name(name)
85 88
 
86
-    def _add_user_to_named_group(self, user, group_name):
89
+    def _add_user_to_named_group(
90
+            self,
91
+            user: str,
92
+            group_name: str
93
+    ) -> None:
87 94
         group = self._get_group(group_name)
88 95
         if user not in group.users:
89 96
             group.users.append(user)
90 97
         self._session.flush()
91 98
 
92
-    def _remove_user_from_named_group(self, user, group_name):
99
+    def _remove_user_from_named_group(
100
+            self,
101
+            user: User,
102
+            group_name: str
103
+    ) -> None:
93 104
         group = self._get_group(group_name)
94 105
         if user in group.users:
95 106
             group.users.remove(user)
96 107
         self._session.flush()
97 108
 
98
-    def _create_user(self, login, password, **kwargs):
109
+    def _create_user(self, login: str, password: str, **kwargs) -> User:
99 110
         if not password:
100 111
             if self._password_required():
101
-                raise CommandAbortedError("You must provide -p/--password parameter")
112
+                raise CommandAbortedError(
113
+                    "You must provide -p/--password parameter"
114
+                )
102 115
             password = ''
103 116
 
104 117
         try:
105
-            user =self._user_api.create_user(email=login)
118
+            user = self._user_api.create_user(email=login)
106 119
             user.password = password
107 120
             self._user_api.save(user)
108 121
             # TODO - G.M - 04-04-2018 - [Caldav] Check this code
@@ -117,13 +130,17 @@ class UserCommand(AppContextCommand):
117 130
 
118 131
         return user
119 132
 
120
-    def _update_password_for_login(self, login, password):
133
+    def _update_password_for_login(self, login: str, password: str) -> None:
121 134
         user = self._user_api.get_one_by_email(login)
122 135
         user.password = password
123 136
         self._session.flush()
124 137
         transaction.commit()
125 138
 
126
-    def take_app_action(self, parsed_args, app_context):
139
+    def take_app_action(
140
+            self,
141
+            parsed_args: argparse.Namespace,
142
+            app_context: AppEnvironment
143
+    ) -> None:
127 144
         # TODO - G.M - 05-04-2018 -Refactor this in order
128 145
         # to not setup object var outside of __init__ .
129 146
         self._session = app_context['request'].dbsession
@@ -142,7 +159,7 @@ class UserCommand(AppContextCommand):
142 159
 
143 160
         print("User created/updated")
144 161
 
145
-    def _proceed_user(self, parsed_args):
162
+    def _proceed_user(self, parsed_args: argparse.Namespace) -> User:
146 163
         self._check_context(parsed_args)
147 164
 
148 165
         if self.action == self.ACTION_CREATE:
@@ -171,7 +188,11 @@ class UserCommand(AppContextCommand):
171 188
 
172 189
         return user
173 190
 
174
-    def _proceed_groups(self, user, parsed_args):
191
+    def _proceed_groups(
192
+            self,
193
+            user: User,
194
+            parsed_args: argparse.Namespace
195
+    ) -> None:
175 196
         # User always in "users" group
176 197
         self._add_user_to_named_group(user, 'users')
177 198
 
@@ -181,13 +202,13 @@ class UserCommand(AppContextCommand):
181 202
         for group_name in parsed_args.remove_from_group:
182 203
             self._remove_user_from_named_group(user, group_name)
183 204
 
184
-    def _password_required(self):
205
+    def _password_required(self) -> bool:
185 206
         # TODO - G.M - 04-04-2018 - [LDAP] Check this code
186 207
         # if config.get('auth_type') == LDAPAuth.name:
187 208
         #     return False
188 209
         return True
189 210
 
190
-    def _check_context(self, parsed_args):
211
+    def _check_context(self, parsed_args: argparse.Namespace) -> None:
191 212
         # TODO - G.M - 04-04-2018 - [LDAP] Check this code
192 213
         # if config.get('auth_type') == LDAPAuth.name:
193 214
         #     auth_instance = config.get('auth_instance')