-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
__init__.py
780 lines (656 loc) 路 27.1 KB
/
__init__.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# Copyright (C) 2018-2021
# Author: Cesar Roman
# Contact: cesar@thecesrom.dev
"""
Perspective Functions
The following functions offer various ways to interact with a
Perspective session from a Python script.
"""
from __future__ import print_function
__all__ = [
"alterLogging",
"closeDock",
"closePage",
"closePopup",
"closeSession",
"download",
"getProjectInfo",
"getSessionInfo",
"isAuthorized",
"login",
"logout",
"navigate",
"navigateBack",
"navigateForward",
"openDock",
"openPopup",
"print",
"refresh",
"sendMessage",
"setTheme",
"toggleDock",
"togglePopup",
"vibrateDevice",
]
from java.lang import Object
class PyJsonObjectAdapter(Object):
def __init__(self, obj):
from __builtin__ import print
print(self, obj)
def __delitem__(self, key):
pass
def __findattr_ex__(self, name):
pass
def __finditem__(self, key):
pass
def __iter__(self):
pass
def __len__(self):
pass
def __setitem__(self, key, value):
pass
def clear(self):
pass
def get(self, key, default=None):
pass
def has_key(self, key):
pass
def items(self):
pass
def iteritems(self):
pass
def iterkeys(self):
pass
def itervalues(self):
pass
def keys(self):
pass
def pop(self, key):
pass
def popitem(self):
pass
def setdefault(self, key, default):
pass
def update(self, *args, **kwargs):
pass
def values(self):
pass
def alterLogging(
remoteLoggingEnabled=False,
level="info",
remoteLoggingLevel="warn",
sessionId="current_session",
pageId="current_page",
):
"""
Changes Perspective Session logging attributes and levels. All
parameters are optional, with the caveat that at least one of them
needs to be used.
Args:
remoteLoggingEnabled (bool): Will enable remote logging if True.
Remote logging will send log events from the Session to the
Gateway under the perspective.client logger if the meet the
remoteLevel logging level requirement. Optional.
level (str): The desired Session logging level. Possible values
are: all, trace, debug, info, warn, error, fatal, off. The
default is info. Optional.
remoteLoggingLevel (str): The desired remote logging level.
Possible values are: all, trace, debug, info, warn, error,
fatal, off. The default is warn. Optional.
sessionId (str): Identifier of the Session to target. If
omitted, the current Session will be used automatically.
When targeting a different session, then the pageId
parameter must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
"""
from __builtin__ import print
print(remoteLoggingEnabled, level, remoteLoggingLevel, sessionId, pageId)
def closeDock(id, sessionId=None, pageId=None):
"""
Closes a docked view.
Args:
id (str): The unique, preconfigured 'Dock ID' for the docked
View. Is specified when a View is assigned as docked for a
particular Page (in Page Configuration).
sessionId (str): Identifier of the Session to target. If
omitted, the current Session will be used automatically.
When targeting a different session, then the pageId
parameter must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
"""
from __builtin__ import print
print(id, sessionId, pageId)
def closePage(
message=None, sessionId="current_session", pageID="current_page"
):
"""
Closes the page with the given page id or the current page if no
page id is provided. If a message is provided, it is displayed on
the page when the page closes. Otherwise the default message (set in
the Project Properties) is displayed.
Args:
message (str): The message to display when the page closes. If
omitted, the default message (set in the Project Properties)
is shown. Optional.
sessionId (str): Identifier of the Session to target. If
omitted, the current Session will be used automatically.
When targeting a different session, then the pageId
parameter must be included in the call. Optional.
pageID (str): Identifier of the page to be closed. If omitted,
the current pageId is used. Optional.
"""
from __builtin__ import print
print(message, sessionId, pageID)
def closePopup(id, sessionId="current_session", pageId="current_page"):
"""
Closes a popup View.
Args:
id (str): The unique identifier for the the popup, given to the
popup when first opened. If given an empty string, then the
most recently focused popup will be closed.
sessionId (str): Identifier of the Session to target. If
omitted, the current Session will be used automatically.
When targeting a different session, then the pageId
parameter must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
"""
from __builtin__ import print
print(id, sessionId, pageId)
def closeSession(message=None, sessionId="current_session"):
"""
Closes the Perspective Session with the given session ID or the
current session if no ID is provided. If a message is provided, it
is displayed on the page when the session closes. Otherwise the
default message (set in the Project Properties) is displayed.
Args:
message (str): The message to display when the session closes.
If omitted, the default message (set in the Project
Properties) is shown. Optional.
sessionId (str): Identifier of the session to be closed. If
omitted, the current sessionId is used. Optional.
"""
from __builtin__ import print
print(message, sessionId)
def download(
filename,
data,
contentType,
sessionId="current_session",
pageId="current_page",
):
"""
Downloads data from the gateway to a device running a session.
Args:
filename (str): Suggested name for the downloaded file.
data (object): The data to be downloaded. May be a String, a
byte[], or an InputStream. Strings will be written with in
"utf-8" encoding.
contentType (str): Value for the "Content-Type" header. Example:
"text/plain; charset=utf-8". Optional.
sessionId (str): Identifier of the Session to target. If omitted
the current Session will be used automatically. When
targeting a different session, then the pageId parameter
must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
"""
from __builtin__ import print
print(filename, data, contentType, sessionId, pageId)
def getProjectInfo(projectName):
"""
Returns a dictionary of meta data from a Perspective Project.
Args:
projectName (str): The name of the project.
Returns:
dict: A dictionary of project meta data.
"""
from __builtin__ import print
print(projectName)
return {
"name": "Project",
"title": "Project",
"description": "Project's description",
"lastModified": "2021-04-01T13:37:00.000Z",
"lastModifiedBy": "thecesrom",
"views": [{"path": "Page/Home"}],
}
def getSessionInfo(usernameFilter=None, projectFilter=None):
"""
Returns information about one or more Perspective sessions. The
information returned by this function is a combination of
information available on the perspective sessions status page on the
Gateway, and some session props (id and userAgent).
Args:
usernameFilter (str): A filter based on logged in user.
Optional.
projectFilter (str): A filter based on the project name.
Optional.
Returns:
list[PyJsonObjectAdapter]: A list of objects
(PyJsonObjectAdapter).
"""
from __builtin__ import print
print(usernameFilter, projectFilter)
return [PyJsonObjectAdapter(None)]
def isAuthorized(isAllOf, securityLevels, sessionId="current_session"):
"""
Checks if the user in the current session is authorized against a
target collection of security levels.
Args:
isAllOf (bool): True if the current user must have all of the
given security levels to be authorized. False if the current
user must have at least one of the given security levels to
be authorized.
securityLevels (list[str]): An array of string paths to a
security level node in the form of "Path/To/Node". Each
level in the tree is delimited by a forward slash character.
The public node is never a part of the path.
sessionId (str): Identifier of the Session to target. If
omitted, the current Session will be used automatically.
Optional.
Returns:
bool: True if the user in the current session is authorized,
False otherwise.
"""
from __builtin__ import print
print(isAllOf, securityLevels, sessionId)
return True
def login(sessionId="current_session", pageId="current_page", forceAuth=False):
"""
Triggers a login event that will allow the user to login with the
project's configured Identity Provider (IdP). For this function to
work, an Identity Provider must be set in Perspective project
properties. This is particularly useful when you want it to be
possible to start a session without authentication and sign in to
access certain restricted features.
Args:
sessionId (str): Identifier of the Session to target. If omitted
the current Session will be used automatically. When
targeting a different session, then the pageId parameter
must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
forceAuth (bool): Determines if Ignition should ask the Identity
Provider to re-authenticate the user, even if the user is
already signed into the Identity Provider. If set to True,
then the Identity Provider will ask the user to re-enter
their credentials. If set to False, then the Gateway will
request that the Identity Provider use the last provided
credentials for the session, potentially allowing
re-authentication without requiring the user to re-type
their credentials. Note that support for this argument is
determined by the Identity Provider; the IdP may choose to
ignore this request. If this parameter is omitted, then the
function will use the re-authentication setting defined
under Project Properties. Optional.
"""
from __builtin__ import print
print(sessionId, pageId, forceAuth)
def logout(sessionId="current_session", pageId="current_page"):
"""
Triggers a logout event, which will log the user out. For this
function to work, an Identity Provider must be set in the
Perspective project properties.
Args:
sessionId (str): Identifier of the Session to target. If omitted
the current Session will be used automatically. When
targeting a different session, then the pageId parameter
must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
"""
from __builtin__ import print
print(sessionId, pageId)
def navigate(
page,
url=None,
view=None,
params=None,
sessionId="current_session",
pageId="current_page",
newTab=False,
):
"""
Navigate the session to a specified view or mounted page.
The function can be used in three different ways, depending on which
parameter is specified:
page: navigates to a perspective-page
url: navigates to a web address, so the function can be used to
navigate the user to a web portal, search engine, or other
website. This parameter is specified via keyword argument
view: navigates to a view. Note that using this parameter does
not modify the web browser's address bar, so the browser's
history will not contain a listing for the new view. This
parameter is specified via keyword argument
Args:
page (str): The URL of a Perspective page to navigate to.
url (str): The URL of a web address to navigate to. If the page
or view parameters are specified, then this parameter is
ignored.
view (str): If specified, will navigate to a specific view.
Navigating to a view via this parameter does not change the
address in the web browser. Thus the web browser's back
button will not be able to return the user to the previous
view. If the page parameter is specified, then this
parameter is ignored. Optional.
params (dict): Used only in conjunction with the view parameter,
Dictionary of values to pass to any parameters on the view.
Optional.
sessionId (str): Identifier of the Session to target. If omitted
the current Session will be used automatically. When
targeting a different session, then the pageId parameter
must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
newTab (bool): If True, opens the contents in a new tab.
Optional.
"""
from __builtin__ import print
print(page, url, view, params, sessionId, pageId, newTab)
def navigateBack(sessionId=None, pageId=None):
"""
Navigate the session to a specified view or mounted page. This is
similar to a browser's "back" function.
Args:
sessionId (str): Identifier of the Session to target. If
omitted, the current Session will be used automatically.
Optional.
pageId (str): Identifier of the page to target. If omitted, the
current page will be used automatically. Optional.
"""
from __builtin__ import print
print(sessionId, pageId)
def navigateForward(sessionId=None, pageId=None):
"""
Navigate the session to a specified view or mounted page. This is
similar to a browser's "forward" function.
Args:
sessionId (str): Identifier of the Session to target. If
omitted, the current Session will be used automatically.
When targeting a different session, then the pageId
parameter must be included in the call. Optional.
pageId (str): Identifier of the page to target. If omitted, the
current page will be used automatically. Optional.
"""
from __builtin__ import print
print(sessionId, pageId)
def openDock(
id, sessionId="current_session", pageId="current_page", params=None
):
"""
Opens a docked View. Requires the preconfigured dock ID for the
view.
Args:
id (str): The unique, preconfigured 'Dock ID' for the docked
View. Is specified when a View is assigned as docked for a
particular Page (in Page Configuration).
sessionId (str): Identifier of the Session to target. If omitted
the current Session will be used automatically. When
targeting a different session, then the pageId parameter
must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
params (dict): Parameters that can be passed into the docked
view. Must match the docked views View Parameters. Optional.
"""
from __builtin__ import print
print(id, sessionId, pageId, params)
def openPopup(
id,
view,
params=None,
title="",
position=None,
showCloseIcon=True,
draggable=True,
resizable=False,
modal=False,
overlayDismiss=False,
sessionId="current_session",
pageId="current_page",
viewPortBound=False,
):
"""
Open a popup view over the given page.
Args:
id (str): A unique popup string. Will be used to close the popup
from other popup or script actions.
view (str): The path to the View to use in the popup.
params (dict): Dictionary of key-value pairs to us as input
parameters to the View. Optional.
title (str): Text to display in the title bar. If no value or an
empty string are given, the title bar will not be displayed.
Defaults to an empty string. Optional.
position (dict): Dictionary of key-value pairs to use for
position. Possible position keys are: left, top, right,
bottom, width, height. Defaults to the center of the window.
Optional.
showCloseIcon (bool): Will show the close icon if True. Defaults
to True. Optional.
draggable (bool): Will allow the popup to be dragged if True.
Defaults to True. Optional.
resizable (bool): Will allow the popup to be resized if True.
Defaults to False. Optional.
modal (bool): Will make the popup modal if True. A modal popup
is the only view the user can interact with. Defaults to
False. Optional.
overlayDismiss (bool): Will allow the user to dismiss and close
a modal popup by clicking outside of it if True. Defaults to
False. Optional.
sessionId (str): Identifier of the Session to target. If omitted
the current Session will be used automatically. When
targeting a different session, then the pageId parameter
must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
viewPortBound (bool): If True, popups will be "shifted" to open
within the bounds of the viewport. If the popup would be
larger than the viewport, then it will be resized to fit
within the bounds. Default is False. Optional.
"""
from __builtin__ import print
print(
id,
view,
params,
title,
position,
showCloseIcon,
draggable,
resizable,
modal,
overlayDismiss,
sessionId,
pageId,
viewPortBound,
)
def print(
message,
sessionId="current_session",
pageId="current_page",
destination="client",
):
"""
Sends print statements to the scripting console when in the
Designer. When in a Session, sends print statements to the output
console. This function makes scripting diagnostics easier.
Args:
message (str): The print statement that will be displayed on the
console.
sessionId (str): Identifier of the Session to target. If omitted
the current Session will be used automatically. When
targeting a different session, then the pageId parameter
must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
destination (str): Where the message should be printed. If
specified, must be "client", "gateway", or "all". Default is
"client". Optional.
"""
from __builtin__ import print
print(message, sessionId, pageId, destination)
def refresh(sessionId="current_session", pageId="current_page"):
"""
Triggers a refresh of the page.
Args:
sessionId (str): Identifier of the Session to target. If
omitted, the current Session will be used automatically.
When targeting a different session, then the pageId
parameter must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
"""
from __builtin__ import print
print(sessionId, pageId)
def sendMessage(
messageType,
payload,
scope="page",
sessionId="current_session",
pageId="current_page",
):
"""
Send a message to a message handler within the same session.
Args:
messageType (str): The message type that will be invoked.
Message handlers configured within the project are listening
for messages of a specific messageType.
payload (dict): A python dictionary representing any parameters
that will be passed to the message handler.
scope (str): The scope that the message should be delivered to.
Valid values are "session", "page", or "view". If omitted,
"page" will be used. Optional.
sessionId (str): Identifier of the Session to target. If
omitted, the current Session will be used automatically.
When targeting a different session, then the pageId
parameter must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
"""
from __builtin__ import print
print(messageType, payload, scope, sessionId, pageId)
def setTheme(name, sessionId="current_session", pageId="current_page"):
"""
Changes the theme in a page to the specified theme.
Note that this function only changes the theme for a single page,
not the entire session. To change the theme for a session, write
directly to the session.theme property instead.
Args:
name (str): The theme name to switch to. Possible values are
"dark" or "light".
sessionId (str): Identifier of the Session to target. If
omitted the current Session will be used automatically.
When targeting a different session, then the pageId
parameter must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
"""
from __builtin__ import print
print(name, sessionId, pageId)
def toggleDock(
id, sessionId="current_session", pageId="current_page", params=None
):
"""
Toggles a docked View.
Args:
id (str): The unique, preconfigured 'Dock ID' for the docked
View. Is specified when a View is assigned as docked for a
particular Page (in Page Configuration).
sessionId (str): Identifier of the Session to target. If
omitted the current Session will be used automatically.
When targeting a different session, then the pageId
parameter must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
params (dict): Parameters that can be passed into the docked
view. Must match the docked views View Parameters. Optional.
"""
from __builtin__ import print
print(id, sessionId, pageId, params)
def togglePopup(
id,
view,
params,
title="",
position=None,
showCloseIcon=True,
draggable=True,
resizable=False,
modal=False,
overlayDismiss=False,
sessionId="current_session",
pageId="current_page",
viewPortBound=False,
):
"""
Toggles a popup. Will open up the popup if it has not been opened
yet. Otherwise, it will close the currently opened popup.
Args:
id (str): A unique popup string. Will be used to close the popup
from other popup or script actions.
view (str): The path to the View to use in the popup.
params (dict): Dictionary of key-value pairs to us as input
parameters to the View. Optional.
title (str): Text to display in the title bar. If no value or an
empty string are given, the title bar will not be displayed.
Defaults to an empty string. Optional.
position (dict): Dictionary of key-value pairs to use for
position. Possible position keys are: left, top, right,
bottom, width, height. Defaults to the center of the window.
Optional.
showCloseIcon (bool): Will show the close icon if True. Defaults
to True. Optional.
draggable (bool): Will allow the popup to be dragged if True.
Defaults to True. Optional.
resizable (bool): Will allow the popup to be resized if True.
Defaults to False. Optional.
modal (bool): Will make the popup modal if True. A modal popup
is the only view the user can interact with. Defaults to
False. Optional.
overlayDismiss (bool): Will allow the user to dismiss and close
a modal popup by clicking outside of it if True. Defaults to
False. Optional.
sessionId (str): Identifier of the Session to target. If omitted
the current Session will be used automatically. When
targeting a different session, then the pageId parameter
must be included in the call. Optional.
pageId (str): Identifier of the Page to target. If omitted, the
current Page will be used automatically. Optional.
viewPortBound (bool): If True, popups will be "shifted" to open
within the bounds of the viewport. If the popup would be
larger than the viewport, then it will be resized to fit
within the bounds. Default is False. Optional.
"""
from __builtin__ import print
print(
id,
view,
params,
title,
position,
showCloseIcon,
draggable,
resizable,
modal,
overlayDismiss,
sessionId,
pageId,
viewPortBound,
)
def vibrateDevice(duration, sessionId="current_session"):
"""
When called from the Perspective mobile app, will cause the device
to vibrate for the specified number of milliseconds.
Args:
duration (int): The duration in milliseconds to vibrate the
device.
sessionId (str): Identifier of the Session to target. If
omitted the current Session will be used automatically. When
targeting a different session, then the pageId parameter
must be included in the call. Optional.
"""
from __builtin__ import print
print(duration, sessionId)