22
33import configparser
44import os
5+ import sys
56import typing as T
7+ from typing import TypedDict
68
7- from . import api_v4 , types
9+ if sys .version_info >= (3 , 11 ):
10+ from typing import Required
11+ else :
12+ from typing_extensions import Required
813
14+ from . import api_v4
915
10- _CLIENT_ID = api_v4 .MAPILLARY_CLIENT_TOKEN
11- # Windows is not happy with | so we convert MLY|ID|TOKEN to MLY_ID_TOKEN
12- _CLIENT_ID = _CLIENT_ID .replace ("|" , "_" , 2 )
13-
14- DEFAULT_MAPILLARY_FOLDER = os .path .join (
15- os .path .expanduser ("~" ),
16- ".config" ,
17- "mapillary" ,
18- )
1916
17+ DEFAULT_MAPILLARY_FOLDER = os .path .join (os .path .expanduser ("~" ), ".config" , "mapillary" )
2018MAPILLARY_CONFIG_PATH = os .getenv (
2119 "MAPILLARY_CONFIG_PATH" ,
2220 os .path .join (
2321 DEFAULT_MAPILLARY_FOLDER ,
2422 "configs" ,
25- _CLIENT_ID ,
23+ # Windows is not happy with | so we convert MLY|ID|TOKEN to MLY_ID_TOKEN
24+ api_v4 .MAPILLARY_CLIENT_TOKEN .replace ("|" , "_" ),
2625 ),
2726)
2827
2928
29+ class UserItem (TypedDict , total = False ):
30+ MAPOrganizationKey : int | str
31+ # Username
32+ MAPSettingsUsername : str
33+ # User ID
34+ MAPSettingsUserKey : str
35+ # User access token
36+ user_upload_token : Required [str ]
37+
38+
39+ UserItemSchema = {
40+ "type" : "object" ,
41+ "properties" : {
42+ "MAPOrganizationKey" : {"type" : ["integer" , "string" ]},
43+ # Not in use. Keep here for back-compatibility
44+ "MAPSettingsUsername" : {"type" : "string" },
45+ "MAPSettingsUserKey" : {"type" : "string" },
46+ "user_upload_token" : {"type" : "string" },
47+ },
48+ "required" : ["user_upload_token" ],
49+ "additionalProperties" : True ,
50+ }
51+
52+
3053def _load_config (config_path : str ) -> configparser .ConfigParser :
3154 config = configparser .ConfigParser ()
3255 # Override to not change option names (by default it will lower them)
@@ -36,19 +59,17 @@ def _load_config(config_path: str) -> configparser.ConfigParser:
3659 return config
3760
3861
39- def load_user (
40- profile_name : str , config_path : str | None = None
41- ) -> types .UserItem | None :
62+ def load_user (profile_name : str , config_path : str | None = None ) -> UserItem | None :
4263 if config_path is None :
4364 config_path = MAPILLARY_CONFIG_PATH
4465 config = _load_config (config_path )
4566 if not config .has_section (profile_name ):
4667 return None
4768 user_items = dict (config .items (profile_name ))
48- return T .cast (types . UserItem , user_items )
69+ return T .cast (UserItem , user_items )
4970
5071
51- def list_all_users (config_path : str | None = None ) -> dict [str , types . UserItem ]:
72+ def list_all_users (config_path : str | None = None ) -> dict [str , UserItem ]:
5273 if config_path is None :
5374 config_path = MAPILLARY_CONFIG_PATH
5475 cp = _load_config (config_path )
@@ -60,7 +81,7 @@ def list_all_users(config_path: str | None = None) -> dict[str, types.UserItem]:
6081
6182
6283def update_config (
63- profile_name : str , user_items : types . UserItem , config_path : str | None = None
84+ profile_name : str , user_items : UserItem , config_path : str | None = None
6485) -> None :
6586 if config_path is None :
6687 config_path = MAPILLARY_CONFIG_PATH
0 commit comments