-
Notifications
You must be signed in to change notification settings - Fork 1
/
FILE_FUNCTIONS.py
118 lines (101 loc) · 4.56 KB
/
FILE_FUNCTIONS.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
# imports-----------------------------------------
from PyQt5.QtWidgets import *
import json
from EDITOR import *
from DELETE_USER_MODAL import *
from datetime import datetime, date, timedelta
from PyQt5 import sip
import keyring
import requests
API_PATH = "api/v2/users/find"
BASE_URL= "https://maproulette.org/"
KEY=keyring.get_password("maproulette",'')
from string import ascii_uppercase as alcUP
from string import ascii_lowercase as alcLOW
# import team json file---------------------------
def import_team_json(main):
files = QFileDialog.getOpenFileNames(main, main.filters, main.importDirectory, main.select_filters)[0]
if len(files)>0:
main.team_file=files[0]
with open(main.team_file, 'r') as team_file:
team_obj=team_file.read()
if main.team_obj == False:
main.team_obj = json.loads(team_obj)
main.loaded_team_obj=team_obj
else:
team_obj=json.loads(team_obj)
for i in team_obj['users']:
main.team_obj['users'].append(i)
main.loaded_team_obj=main.team_obj
autosave_team_file(main)
main.team_name=main.team_obj['properties']['team']
main.team_name_field.setText(main.team_name)
parse_editors(main,main.team_obj)
autosave_team_file(main)
if 'accepted_words' in main.team_obj['properties']:
main.accepted_words=main.team_obj['properties']['accepted_words']
for letter in alcUP:
main.accepted_words.append("%s"%(letter))
for letter in alcLOW:
main.accepted_words.append("%s"%(letter))
if 'accepted_hashtags' in main.team_obj['properties']:
main.accepted_hashtags=main.team_obj['properties']['accepted_hashtags']
# get user maproulette id--------------------
def get_single_user_id_from_api(user):
if KEY != None and KEY != '':
response = requests.get(
BASE_URL + API_PATH,
headers={"apikey": KEY},
params={"username": user},
# verify=VERIFY_CERT,
)
if response.json():
print(response.json()[0]["id"])
return response.json()[0]["id"]
# parse editors from json file--------------------
def parse_editors(main,team_obj):
main.team_dict={}
for i in team_obj['users']:
if not 'maproulette_id' in i.keys():
user=i['username']
print('mapillary_id not found for %s'%(user))
maproulette_id = get_single_user_id_from_api(user)
i['maproulette_id']=maproulette_id
editor=EDITOR(i['name'],i['username'],i['user_id'],i['maproulette_id'],i['role'])
editor.construct_list_item(main)
main.team_dict[editor.osm_user_id]=editor
# auto-save team file on close--------------------
def autosave_team_file(main):
if len(main.team_obj['users'])>0:
main.team_obj['properties']['team']=main.team_name
main.team_obj['properties']['version']+=.1
main.team_obj['properties']['last_modified']=str(date.today())
main.team_file=main.team_file.rsplit("/",1)[0]
main.team_file=main.team_file+'/%s.json'%(main.team_name)
with open (main.team_file,'w+')as save_file:
json.dump(main.team_obj, save_file)
# manual save team file----------------------------
def save_team_file(main):
if main.team_obj:
main.team_obj['properties']['team']=main.team_name
main.team_obj['properties']['version']+=1
main.team_obj['properties']['last_modified']=str(date.today())
main.team_file=main.team_file.rsplit("/",1)[0]
main.team_file=main.team_file+'/%s.json'%(main.team_name)
with open (main.team_file,'w+')as save_file:
json.dump(main.team_obj, save_file)
# save changed variables to settings---------------
def save_settings(main):
main.team_obj['properties']['accepted_words']=main.accepted_words
main.team_obj['properties']['accepted_hashtags']=main.accepted_hashtags
# unlock teamname reset button----------------
def unlock_reset_button(main):
main.team_name_reset_button.setDisabled(False)
# save new team name--------------------------
def save_team_name(main):
main.team_name=main.team_name_field.text()
main.team_name_reset_button.setDisabled(True)
# reset team name--------------------------
def reset_team_name(main):
main.team_name_field.setText(main.team_name)
main.team_name_reset_button.setDisabled(True)