-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharcgis_token.py
170 lines (135 loc) · 6.26 KB
/
arcgis_token.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
#! /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import json
import urllib
import time
import ctypes
import PyQt4.QtCore as QtCore
import PyQt4.QtGui as QtGui
class ArcGIS(QtGui.QDialog):
def __init__(self):
QtGui.QDialog.__init__(self)
layout = QtGui.QGridLayout()
self.clipboard = QtGui.QApplication.clipboard()
# Variables
self.main_title = "Token Generator"
self.url = "https://www.arcgis.com/sharing/rest"
self.submitUrl = self.url + "/generateToken"
self.expirations = ['60', '120', '180', 'max']
# Design widgets
self.title_lbl = QtGui.QLabel("ArcGIS Online")
self.username_txt = QtGui.QLineEdit()
self.password_txt = QtGui.QLineEdit()
self.expiration_combo = QtGui.QComboBox()
self.clipboard_chk = QtGui.QCheckBox("Copy token to clipboard ?")
self.get_token_btn = QtGui.QPushButton("Get Token")
self.clear_btn = QtGui.QPushButton("X")
self.message_lbl = QtGui.QLabel()
self.sub_message_lbl = QtGui.QLabel()
self.expiration_lbl = QtGui.QLabel()
self.token_output = QtGui.QTextEdit()
# Configure
self.username_txt.setPlaceholderText("Username")
self.password_txt.setPlaceholderText("Password")
self.password_txt.setEchoMode(QtGui.QLineEdit.Password)
self.get_token_btn.clicked.connect(self.get_token)
self.clear_btn.clicked.connect(self.clear_form)
self.expiration_combo.addItems(self.expirations)
# Add widgets
layout.addWidget(self.title_lbl, 0, 0, 1, 4)
layout.addWidget(self.username_txt, 1, 0, 1, 4)
layout.addWidget(self.password_txt, 2, 0, 1, 4)
layout.addWidget(self.expiration_combo, 3, 0, 1, 1)
layout.addWidget(self.get_token_btn, 3, 1, 1, 2)
layout.addWidget(self.clear_btn, 3, 3, 1, 1)
layout.addWidget(self.clipboard_chk, 4, 0, 1, 4)
layout.addWidget(self.message_lbl, 5, 0, 1, 4)
layout.addWidget(self.sub_message_lbl, 6, 0, 1, 4)
layout.addWidget(self.expiration_lbl, 7, 0, 1, 4)
layout.addWidget(self.token_output, 8, 0, 5, 4)
# Formatting
layout.setAlignment(self.title_lbl, QtCore.Qt.AlignCenter)
layout.setAlignment(self.get_token_btn, QtCore.Qt.AlignCenter)
layout.setAlignment(self.clipboard_chk, QtCore.Qt.AlignCenter)
layout.setAlignment(self.message_lbl, QtCore.Qt.AlignCenter)
layout.setAlignment(self.sub_message_lbl, QtCore.Qt.AlignCenter)
layout.setAlignment(self.token_output, QtCore.Qt.AlignCenter)
self.palette = QtGui.QPalette()
self.sub_expiration_message_font = QtGui.QFont()
self.sub_expiration_message_font.setPointSize(7)
self.sub_message_lbl.setFont(self.sub_expiration_message_font)
self.expiration_font = QtGui.QFont()
self.expiration_font.setBold(True)
self.expiration_lbl.setFont(self.expiration_font)
layout.setAlignment(self.expiration_lbl, QtCore.Qt.AlignCenter)
layout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
# Hide output widgets initially
self.message_lbl.hide()
self.sub_message_lbl.hide()
self.expiration_lbl.hide()
self.token_output.hide()
self.setLayout(layout)
self.setWindowTitle(self.main_title)
self.setFocus() # this is not working to remove focus from username widget
def get_token(self):
# Hide previous messages
self.message_lbl.hide()
self.sub_message_lbl.hide()
self.expiration_lbl.hide()
self.token_output.hide()
# The maximum expiration period is 15 days or 21600 minutes
expire_select = self.expiration_combo.currentText()
data = dict(username=self.username_txt.text(),
password=self.password_txt.text(),
expiration='21600' if expire_select == 'max' else expire_select,
client='referer',
referer='https://www.arcgis.com',
f='json')
submit_response = urllib.urlopen(self.submitUrl, urllib.urlencode(data))
submit_json = json.loads(submit_response.read())
if 'error' in submit_json:
self.palette.setColor(QtGui.QPalette.Foreground, QtCore.Qt.red)
self.message_lbl.setPalette(self.palette)
self.message_lbl.setText(str(submit_json['error']['details'][0]))
# Show messages
self.message_lbl.show()
else:
if self.clipboard_chk.isChecked():
self.clipboard.clear()
self.clipboard.setText(str(submit_json['token']), mode=self.clipboard.Clipboard)
self.palette.setColor(QtGui.QPalette.Foreground, QtCore.Qt.blue)
self.message_lbl.setPalette(self.palette)
self.message_lbl.setText("Token Granted")
self.sub_message_lbl.setText("Your token expires:")
self.expiration_lbl.setText(time.ctime(int(submit_json["expires"]) / 1000))
self.token_output.clear()
self.token_output.insertPlainText(str(submit_json['token']))
# Show messages
self.message_lbl.show()
self.sub_message_lbl.show()
self.expiration_lbl.show()
self.token_output.show()
def resize_dialog(self, size):
self.resize(size)
def clear_form(self):
# Hide previous messages
self.message_lbl.hide()
self.sub_message_lbl.hide()
self.expiration_lbl.hide()
self.token_output.hide()
self.username_txt.clear()
self.password_txt.clear()
self.expiration_combo.setCurrentIndex(0)
self.resize_dialog(self.minimumSizeHint())
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
# http://stackoverflow.com/questions/1551605/how-to-set-applications-taskbar-icon-in-windows-7/1552105#1552105
app_id = u'esricanada.tokengenerator.arcgis.0.1'
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(app_id)
dialog = ArcGIS()
dialog.setWindowIcon(QtGui.QIcon('images/icon-windowed.ico'))
app.setWindowIcon(QtGui.QIcon('images/icon-windowed.ico'))
dialog.setWindowFlags(QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint)
dialog.show()
sys.exit(app.exec_())