forked from blender/blender-addons
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiles.py
212 lines (154 loc) · 5.77 KB
/
profiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
206
207
208
209
210
211
212
# SPDX-License-Identifier: GPL-2.0-or-later
import os
import bpy
from . import communication
# Set/created upon register.
profiles_path = ''
profiles_file = ''
class _BIPMeta(type):
"""Metaclass for BlenderIdProfile."""
def __str__(self):
# noinspection PyUnresolvedReferences
return '%s(user_id=%r)' % (self.__qualname__, self.user_id)
class BlenderIdProfile(metaclass=_BIPMeta):
"""Current Blender ID profile.
This is always stored at class level, as there is only one current
profile anyway.
"""
user_id = ''
username = ''
token = ''
expires = ''
subclients = {}
@classmethod
def reset(cls):
cls.user_id = ''
cls.username = ''
cls.token = ''
cls.expires = ''
cls.subclients = {}
@classmethod
def read_json(cls):
"""Updates the active profile information from the JSON file."""
cls.reset()
active_profile = get_active_profile()
if not active_profile:
return
for key, value in active_profile.items():
if hasattr(cls, key):
setattr(cls, key, value)
else:
print('Skipping key %r from profile JSON' % key)
@classmethod
def save_json(cls, make_active_profile=False):
"""Updates the JSON file with the active profile information."""
jsonfile = get_profiles_data()
jsonfile['profiles'][cls.user_id] = {
'username': cls.username,
'token': cls.token,
'expires': cls.expires,
'subclients': cls.subclients,
}
if make_active_profile:
jsonfile['active_profile'] = cls.user_id
save_profiles_data(jsonfile)
def register():
global profiles_path, profiles_file
profiles_path = bpy.utils.user_resource('CONFIG', path='blender_id', create=True)
profiles_file = os.path.join(profiles_path, 'profiles.json')
def _create_default_file():
"""Creates the default profile file, returning its contents."""
import json
profiles_default_data = {
'active_profile': None,
'profiles': {}
}
os.makedirs(profiles_path, exist_ok=True)
# Populate the file, ensuring that its permissions are restrictive enough.
old_umask = os.umask(0o077)
try:
with open(profiles_file, 'w', encoding='utf8') as outfile:
json.dump(profiles_default_data, outfile)
finally:
os.umask(old_umask)
return profiles_default_data
def get_profiles_data():
"""Returns the profiles.json content from a blender_id folder in the
Blender config directory. If the file does not exist we create one with the
basic data structure.
"""
import json
# if the file does not exist
if not os.path.exists(profiles_file):
return _create_default_file()
# try parsing the file
with open(profiles_file, 'r', encoding='utf8') as f:
try:
file_data = json.load(f)
file_data['active_profile']
file_data['profiles']
return file_data
except (ValueError, # malformed json data
KeyError): # it doesn't have the expected content
print('(%s) '
'Warning: profiles.json is either empty or malformed. '
'The file will be reset.' % __name__)
# overwrite the file
return _create_default_file()
def get_active_user_id():
"""Get the id of the currently active profile. If there is no
active profile on the file, this function will return None.
"""
return get_profiles_data()['active_profile']
def get_active_profile():
"""Pick the active profile from profiles.json. If there is no
active profile on the file, this function will return None.
@returns: dict like {'user_id': 1234, 'username': '[email protected]'}
"""
file_content = get_profiles_data()
user_id = file_content['active_profile']
if not user_id or user_id not in file_content['profiles']:
return None
profile = file_content['profiles'][user_id]
profile['user_id'] = user_id
return profile
def get_profile(user_id):
"""Loads the profile data for a given user_id if existing
else it returns None.
"""
file_content = get_profiles_data()
if not user_id or user_id not in file_content['profiles']:
return None
profile = file_content['profiles'][user_id]
return dict(
username=profile['username'],
token=profile['token']
)
def save_profiles_data(all_profiles: dict):
"""Saves the profiles data to JSON."""
import json
with open(profiles_file, 'w', encoding='utf8') as outfile:
json.dump(all_profiles, outfile, sort_keys=True)
def save_as_active_profile(auth_result: communication.AuthResult, username, subclients):
"""Saves the given info as the active profile."""
BlenderIdProfile.user_id = auth_result.user_id
BlenderIdProfile.token = auth_result.token
BlenderIdProfile.expires = auth_result.expires
BlenderIdProfile.username = username
BlenderIdProfile.subclients = subclients
BlenderIdProfile.save_json(make_active_profile=True)
def logout(user_id):
"""Invalidates the token and state of active for this user.
This is different from switching the active profile, where the active
profile is changed but there isn't an explicit logout.
"""
import json
file_content = get_profiles_data()
# Remove user from 'active profile'
if file_content['active_profile'] == user_id:
file_content['active_profile'] = ""
# Remove both user and token from profiles list
if user_id in file_content['profiles']:
del file_content['profiles'][user_id]
with open(profiles_file, 'w', encoding='utf8') as outfile:
json.dump(file_content, outfile)