summaryrefslogtreecommitdiff
path: root/wizards/source/scriptforge/python/scriptforge.py
blob: 53a724d9b8f421ba94ef39862c0af94186c0390c (plain)
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
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
# -*- coding: utf-8 -*-

#     Copyright 2020-2022 Jean-Pierre LEDURE, Rafael LIMA, Alain ROMEDENNE

# =====================================================================================================================
# ===           The ScriptForge library and its associated libraries are part of the LibreOffice project.           ===
# ===                   Full documentation is available on https://help.libreoffice.org/                            ===
# =====================================================================================================================

# ScriptForge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

# ScriptForge is free software; you can redistribute it and/or modify it under the terms of either (at your option):

# 1) The Mozilla Public License, v. 2.0. If a copy of the MPL was not
# distributed with this file, you can obtain one at http://mozilla.org/MPL/2.0/ .

# 2) The GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. If a copy of the LGPL was not
# distributed with this file, see http://www.gnu.org/licenses/ .

"""
    ScriptForge libraries are an extensible and robust collection of macro scripting resources for LibreOffice
    to be invoked from user Basic or Python macros. Users familiar with other BASIC macro variants often face hard
    times to dig into the extensive LibreOffice Application Programming Interface even for the simplest operations.
    By collecting most-demanded document operations in a set of easy to use, easy to read routines, users can now
    program document macros with much less hassle and get quicker results.

    ScriptForge abundant methods are organized in reusable modules that cleanly isolate Basic/Python programming
    language constructs from ODF document content accesses and user interface(UI) features.

    The scriptforge.py module
        - implements a protocol between Python (user) scripts and the ScriptForge Basic library
        - contains the interfaces (classes and attributes) to be used in Python user scripts
          to run the services implemented in the standard libraries shipped with LibreOffice

    Usage:

        When Python and LibreOffice run in the same process (usual case): either
            from scriptforge import *   # or, better ...
            from scriptforge import CreateScriptService

        When Python and LibreOffice are started in separate processes,
        LibreOffice being started from console ... (example for Linux with port = 2021)
            ./soffice --accept='socket,host=localhost,port=2021;urp;'
        then use next statement:
            from scriptforge import *   # or, better ...
            from scriptforge import CreateScriptService, ScriptForge
            ScriptForge(hostname = 'localhost', port = 2021)

    Specific documentation about the use of ScriptForge from Python scripts:
        https://help.libreoffice.org/7.3/en-US/text/sbasic/shared/03/sf_intro.html
    """

import uno

import datetime
import time
import os


class _Singleton(type):
    """
        A Singleton metaclass design pattern
        Credits: « Python in a Nutshell » by Alex Martelli, O'Reilly
        """
    instances = {}

    def __call__(cls, *args, **kwargs):
        if cls not in cls.instances:
            cls.instances[cls] = super(_Singleton, cls).__call__(*args, **kwargs)
        return cls.instances[cls]


# #####################################################################################################################
#                           ScriptForge CLASS                                                                       ###
# #####################################################################################################################

class ScriptForge(object, metaclass = _Singleton):
    """
        The ScriptForge (singleton) class encapsulates the core of the ScriptForge run-time
            - Bridge with the LibreOffice process
            - Implementation of the inter-language protocol with the Basic libraries
            - Identification of the available services interfaces
            - Dispatching of services
            - Coexistence with UNO

        It embeds the Service class that manages the protocol with Basic
        """

    # #########################################################################
    # Class attributes
    # #########################################################################
    hostname = ''
    port = 0
    componentcontext = None
    scriptprovider = None
    SCRIPTFORGEINITDONE = False

    # #########################################################################
    # Class constants
    # #########################################################################
    library = 'ScriptForge'
    Version = '7.3'  # Actual version number
    #
    # Basic dispatcher for Python scripts
    basicdispatcher = '@application#ScriptForge.SF_PythonHelper._PythonDispatcher'
    # Python helper functions module
    pythonhelpermodule = 'ScriptForgeHelper.py'
    #
    # VarType() constants
    V_EMPTY, V_NULL, V_INTEGER, V_LONG, V_SINGLE, V_DOUBLE = 0, 1, 2, 3, 4, 5
    V_CURRENCY, V_DATE, V_STRING, V_OBJECT, V_BOOLEAN = 6, 7, 8, 9, 11
    V_VARIANT, V_ARRAY, V_ERROR, V_UNO = 12, 8192, -1, 16
    # Object types
    objMODULE, objCLASS, objUNO = 1, 2, 3
    # Special argument symbols
    cstSymEmpty, cstSymNull, cstSymMissing = '+++EMPTY+++', '+++NULL+++', '+++MISSING+++'
    # Predefined references for services implemented as standard Basic modules
    servicesmodules = dict([('ScriptForge.Array', 0),
                            ('ScriptForge.Exception', 1),
                            ('ScriptForge.FileSystem', 2),
                            ('ScriptForge.Platform', 3),
                            ('ScriptForge.Services', 4),
                            ('ScriptForge.Session', 5),
                            ('ScriptForge.String', 6),
                            ('ScriptForge.UI', 7)])

    def __init__(self, hostname = '', port = 0):
        """
            Because singleton, constructor is executed only once while Python active
            Arguments are mandatory when Python and LibreOffice run in separate processes
            :param hostname: probably 'localhost'
            :param port: port number
            """
        ScriptForge.hostname = hostname
        ScriptForge.port = port
        # Determine main pyuno entry points
        ScriptForge.componentcontext = self.ConnectToLOProcess(hostname, port)  # com.sun.star.uno.XComponentContext
        ScriptForge.scriptprovider = self.ScriptProvider(self.componentcontext)  # ...script.provider.XScriptProvider
        #
        # Establish a list of the available services as a dictionary (servicename, serviceclass)
        ScriptForge.serviceslist = dict((cls.servicename, cls) for cls in SFServices.__subclasses__())
        ScriptForge.servicesdispatcher = None
        #
        # All properties and methods of the ScriptForge API are ProperCased
        # Compute their synonyms as lowercased and camelCased names
        ScriptForge.SetAttributeSynonyms()
        #
        ScriptForge.SCRIPTFORGEINITDONE = True

    @classmethod
    def ConnectToLOProcess(cls, hostname = '', port = 0):
        """
            Called by the ScriptForge class constructor to establish the connection with
            the requested LibreOffice instance
            The default arguments are for the usual interactive mode

            :param hostname: probably 'localhost' or ''
            :param port: port number or 0
            :return: the derived component context
            """
        if len(hostname) > 0 and port > 0:  # Explicit connection request via socket
            ctx = uno.getComponentContext()  # com.sun.star.uno.XComponentContext
            resolver = ctx.ServiceManager.createInstanceWithContext(
                'com.sun.star.bridge.UnoUrlResolver', ctx)  # com.sun.star.comp.bridge.UnoUrlResolver
            try:
                conn = 'socket,host=%s,port=%d' % (hostname, port)
                url = 'uno:%s;urp;StarOffice.ComponentContext' % conn
                ctx = resolver.resolve(url)
            except Exception:  # thrown when LibreOffice specified instance isn't started
                raise SystemExit(
                    'Connection to LibreOffice failed (host = ' + hostname + ', port = ' + str(port) + ')')
            return ctx
        elif len(hostname) == 0 and port == 0:  # Usual interactive mode
            return uno.getComponentContext()
        else:
            raise SystemExit('The creation of the ScriptForge() instance got invalid arguments: '
                             + '(host = ' + hostname + ', port = ' + str(port) + ')')

    @classmethod
    def ScriptProvider(cls, context = None):
        """
            Returns the general script provider
            """
        servicemanager = context.ServiceManager  # com.sun.star.lang.XMultiComponentFactory
        masterscript = servicemanager.createInstanceWithContext(
            'com.sun.star.script.provider.MasterScriptProviderFactory', context)
        return masterscript.createScriptProvider("")

    @classmethod
    def InvokeSimpleScript(cls, script, *args):
        """
            Create a UNO object corresponding with the given Python or Basic script
            The execution is done with the invoke() method applied on the created object
            Implicit scope: Either
                "application"            a shared library                               (BASIC)
                "share"                  a library of LibreOffice Macros                (PYTHON)
            :param script: Either
                    [@][scope#][library.]module.method - Must not be a class module or method
                        [@] means that the targeted method accepts ParamArray arguments (Basic only)
                    [scope#][directory/]module.py$method - Must be a method defined at module level
            :return: the value returned by the invoked script, or an error if the script was not found
            """

        # The frequently called PythonDispatcher in the ScriptForge Basic library is cached to privilege performance
        if cls.servicesdispatcher is not None and script == ScriptForge.basicdispatcher:
            xscript = cls.servicesdispatcher
            fullscript = script
            paramarray = True
        #    Build the URI specification described in
        #    https://wiki.openoffice.org/wiki/Documentation/DevGuide/Scripting/Scripting_Framework_URI_Specification
        elif len(script) > 0:
            # Check ParamArray arguments
            paramarray = False
            if script[0] == '@':
                script = script[1:]
                paramarray = True
            scope = ''
            if '#' in script:
                scope, script = script.split('#')
            if '.py$' in script.lower():  # Python
                if len(scope) == 0:
                    scope = 'share'     # Default for Python
                # Provide an alternate helper script depending on test context
                if script.startswith(cls.pythonhelpermodule) and hasattr(cls, 'pythonhelpermodule2'):
                    script = cls.pythonhelpermodule2 + script[len(cls.pythonhelpermodule):]
                    if '#' in script:
                        scope, script = script.split('#')
                uri = 'vnd.sun.star.script:{0}?language=Python&location={1}'.format(script, scope)
            else:  # Basic
                if len(scope) == 0:
                    scope = 'application'     # Default for Basic
                lib = ''
                if len(script.split('.')) < 3:
                    lib = cls.library + '.'     # Default library = ScriptForge
                uri = 'vnd.sun.star.script:{0}{1}?language=Basic&location={2}'.format(lib, script, scope)
            # Get the script object
            fullscript = ('@' if paramarray else '') + scope + ':' + script
            try:
                xscript = cls.scriptprovider.getScript(uri)
            except Exception:
                raise RuntimeError(
                    'The script \'{0}\' could not be located in your LibreOffice installation'.format(script))
        else:  # Should not happen
            return None

        # At 1st execution of the common Basic dispatcher, buffer xscript
        if fullscript == ScriptForge.basicdispatcher and cls.servicesdispatcher is None:
            cls.servicesdispatcher = xscript

        # Execute the script with the given arguments
        # Packaging for script provider depends on presence of ParamArray arguments in the called Basic script
        if paramarray:
            scriptreturn = xscript.invoke(args[0], (), ())
        else:
            scriptreturn = xscript.invoke(args, (), ())

        #
        return scriptreturn[0]  # Updatable arguments passed by reference are ignored

    @classmethod
    def InvokeBasicService(cls, basicobject, flags, method, *args):
        """
            Execute a given Basic script and interpret its result
            This method has as counterpart the ScriptForge.SF_PythonHelper._PythonDispatcher() Basic method
            :param basicobject: a Service subclass
            :param flags: see the vb* and flg* constants in the SFServices class
            :param method: the name of the method or property to invoke, as a string
            :param args: the arguments of the method. Symbolic cst* constants may be necessary
            :return: The invoked Basic counterpart script (with InvokeSimpleScript()) will return a tuple
                [0]     The returned value - scalar, object reference or a tuple
                [1]     The Basic VarType() of the returned value
                        Null, Empty and Nothing have different vartypes but return all None to Python
                Additionally, when [0] is a tuple:
                [2]     Number of dimensions in Basic
                Additionally, when [0] is a UNO or Basic object:
                [2]     Module (1), Class instance (2) or UNO (3)
                [3]     The object's ObjectType
                [4]     The object's ServiceName
                [5]     The object's name
                When an error occurs Python receives None as a scalar. This determines the occurrence of a failure
                The method returns either
                    - the 0th element of the tuple when scalar, tuple or UNO object
                    - a new Service() object or one of its subclasses otherwise
            """
        # Constants
        script = ScriptForge.basicdispatcher
        cstNoArgs = '+++NOARGS+++'
        cstValue, cstVarType, cstDims, cstClass, cstType, cstService, cstName = 0, 1, 2, 2, 3, 4, 5

        #
        # Run the basic script
        # The targeted script has a ParamArray argument. Do not change next 4 lines except if you know what you do !
        if len(args) == 0:
            args = (basicobject,) + (flags,) + (method,) + (cstNoArgs,)
        else:
            args = (basicobject,) + (flags,) + (method,) + args
        returntuple = cls.InvokeSimpleScript(script, args)
        #
        # Interpret the result
        # Did an error occur in the Basic world ?
        if not isinstance(returntuple, (tuple, list)):
            raise RuntimeError("The execution of the method '" + method + "' failed. Execution stops.")
        #
        # Analyze the returned tuple
        if returntuple[cstVarType] == ScriptForge.V_OBJECT and len(returntuple) > cstClass:  # Avoid Nothing
            if returntuple[cstClass] == ScriptForge.objUNO:
                pass
            else:
                # Create the new class instance of the right subclass of SFServices()
                servname = returntuple[cstService]
                if servname not in cls.serviceslist:
                    # When service not found
                    raise RuntimeError("The service '" + servname + "' is not available in Python. Execution stops.")
                subcls = cls.serviceslist[servname]
                if subcls is not None:
                    return subcls(returntuple[cstValue], returntuple[cstType], returntuple[cstClass],
                                  returntuple[cstName])
        elif returntuple[cstVarType] >= ScriptForge.V_ARRAY:
            # Intercept empty array
            if isinstance(returntuple[cstValue], uno.ByteSequence):
                return ()
        elif returntuple[cstVarType] == ScriptForge.V_DATE:
            dat = SFScriptForge.SF_Basic.CDateFromUnoDateTime(returntuple[cstValue])
            return dat
        else:         # All other scalar values
            pass
        return returntuple[cstValue]

    @staticmethod
    def SetAttributeSynonyms():
        """
            A synonym of an attribute is either the lowercase or the camelCase form of its original ProperCase name.
            In every subclass of SFServices:
            1) Fill the propertysynonyms dictionary with the synonyms of the properties listed in serviceproperties
                Example:
                     serviceproperties = dict(ConfigFolder = False, InstallFolder = False)
                     propertysynonyms = dict(configfolder = 'ConfigFolder', installfolder = 'InstallFolder',
                                             configFolder = 'ConfigFolder', installFolder = 'InstallFolder')
            2) Define new method attributes synonyms of the original methods
                Example:
                    def CopyFile(...):
                        # etc ...
                    copyFile, copyfile = CopyFile, CopyFile
            """
        def camelCase(key):
            return key[0].lower() + key[1:]

        for cls in SFServices.__subclasses__():
            # Synonyms of properties
            if hasattr(cls, 'serviceproperties'):
                dico = cls.serviceproperties
                dicosyn = dict(zip(map(str.lower, dico.keys()), dico.keys()))   # lower case
                cc = dict(zip(map(camelCase, dico.keys()), dico.keys()))        # camel Case
                dicosyn.update(cc)
                setattr(cls, 'propertysynonyms', dicosyn)
            # Synonyms of methods. A method is a public callable attribute
            methods = [method for method in dir(cls) if not method.startswith('_')]
            for method in methods:
                func = getattr(cls, method)
                if callable(func):
                    # Assign to each synonym a reference to the original method
                    lc = method.lower()
                    setattr(cls, lc, func)
                    cc = camelCase(method)
                    if cc != lc:
                        setattr(cls, cc, func)
        return

    @staticmethod
    def unpack_args(kwargs):
        """
            Convert a dictionary passed as argument to a list alternating keys and values
            Example:
                dict(A = 'a', B = 2) => 'A', 'a', 'B', 2
            """
        return [v for p in zip(list(kwargs.keys()), list(kwargs.values())) for v in p]


# #####################################################################################################################
#                           SFServices CLASS    (ScriptForge services superclass)                                   ###
# #####################################################################################################################

class SFServices(object):
    """
        Generic implementation of a parent Service class
        Every service must subclass this class to be recognized as a valid service
        A service instance is created by the CreateScriptService method
        It can have a mirror in the Basic world or be totally defined in Python

        Every subclass must initialize 3 class properties:
            servicename (e.g. 'ScriptForge.FileSystem', 'ScriptForge.Basic')
            servicesynonyms (e.g. 'FileSystem', 'Basic')
            serviceimplementation: either 'python' or 'basic'
        This is sufficient to register the service in the Python world

        The communication with Basic is managed by 2 ScriptForge() methods:
            InvokeSimpleScript(): low level invocation of a Basic script. This script must be located
                in a usual Basic module. The result is passed as-is
            InvokeBasicService(): the result comes back encapsulated with additional info
                The result is interpreted in the method
                The invoked script can be a property or a method of a Basic class or usual module
        It is up to every service method to determine which method to use

        For Basic services only:
            Each instance is identified by its
                - object reference: the real Basic object embedded as a UNO wrapper object
                - object type ('SF_String', 'DICTIONARY', ...)
                - class module: 1 for usual modules, 2 for class modules
                - name (form, control, ... name) - may be blank

            The role of the SFServices() superclass is mainly to propose a generic properties management
            Properties are got and set following next strategy:
                1. Property names are controlled strictly ('Value' or 'value', not 'VALUE')
                2. Getting a property value for the first time is always done via a Basic call
                3. Next occurrences are fetched from the Python dictionary of the instance if the property
                   is read-only, otherwise via a Basic call
                4. Read-only properties may be modified or deleted exceptionally by the class
                   when self.internal == True. The latter must immediately be reset after use

            Each subclass must define its interface with the user scripts:
            1.  The properties
                Property names are proper-cased
                Conventionally, camel-cased and lower-cased synonyms are supported where relevant
                    a dictionary named 'serviceproperties' with keys = (proper-cased) property names and value = boolean
                        True = editable, False = read-only
                    a list named 'localProperties' reserved to properties for internal use
                        e.g. oDlg.Controls() is a method that uses '_Controls' to hold the list of available controls
                When
                    forceGetProperty = False    # Standard behaviour
                read-only serviceproperties are buffered in Python after their 1st get request to Basic
                Otherwise set it to True to force a recomputation at each property getter invocation
                If there is a need to handle a specific property in a specific manner:
                    @property
                    def myProperty(self):
                        return self.GetProperty('myProperty')
            2   The methods
                a usual def: statement
                    def myMethod(self, arg1, arg2 = ''):
                        return self.Execute(self.vbMethod, 'myMethod', arg1, arg2)
                Method names are proper-cased, arguments are lower-cased
                Conventionally, camel-cased and lower-cased homonyms are supported where relevant
                All arguments must be present and initialized before the call to Basic, if any
        """
    # Python-Basic protocol constants and flags
    vbGet, vbLet, vbMethod, vbSet = 2, 4, 1, 8  # CallByName constants
    flgPost = 32    # The method or the property implies a hardcoded post-processing
    flgDateArg = 64  # Invoked service method may contain a date argument
    flgDateRet = 128  # Invoked service method can return a date
    flgArrayArg = 512  # 1st argument can be a 2D array
    flgArrayRet = 1024  # Invoked service method can return a 2D array (standard modules) or any array (class modules)
    flgUno = 256  # Invoked service method/property can return a UNO object
    flgObject = 2048  # 1st argument may be a Basic object
    # Basic class type
    moduleClass, moduleStandard = 2, 1
    #
    # Define the default behaviour for read-only properties: buffer their values in Python
    forceGetProperty = False
    # Empty dictionary for lower/camelcased homonyms or properties
    propertysynonyms = {}
    # To operate dynamic property getting/setting it is necessary to
    # enumerate all types of properties and adapt __getattr__() and __setattr__() according to their type
    internal_attributes = ('objectreference', 'objecttype', 'name', 'internal', 'servicename',
                           'serviceimplementation', 'classmodule', 'EXEC', 'SIMPLEEXEC')
    # Shortcuts to script provider interfaces
    SIMPLEEXEC = ScriptForge.InvokeSimpleScript
    EXEC = ScriptForge.InvokeBasicService

    def __init__(self, reference = -1, objtype = None, classmodule = 0, name = ''):
        """
            Trivial initialization of internal properties
            If the subclass has its own __init()__ method, a call to this one should be its first statement.
            Afterwards localProperties should be filled with the list of its own properties
            """
        self.objectreference = reference  # the index in the Python storage where the Basic object is stored
        self.objecttype = objtype  # ('SF_String', 'DICTIONARY', ...)
        self.classmodule = classmodule  # Module (1), Class instance (2)
        self.name = name  # '' when no name
        self.internal = False  # True to exceptionally allow assigning a new value to a read-only property
        self.localProperties = []  # the properties reserved for internal use (often empty)

    def __getattr__(self, name):
        """
            Executed for EVERY property reference if name not yet in the instance dict
            At the 1st get, the property value is always got from Basic
            Due to the use of lower/camelcase synonyms, it is called for each variant of the same property
            The method manages itself the buffering in __dict__ based on the official ProperCase property name
            """
        if name in self.propertysynonyms:  # Reset real name if argument provided in lower or camel case
            name = self.propertysynonyms[name]
        if self.serviceimplementation == 'basic':
            if name in ('serviceproperties', 'localProperties', 'internal_attributes', 'propertysynonyms',
                        'forceGetProperty'):
                pass
            elif name in self.serviceproperties:
                if self.forceGetProperty is False and self.serviceproperties[name] is False:  # False = read-only
                    if name in self.__dict__:
                        return self.__dict__[name]
                    else:
                        # Get Property from Basic and store it
                        prop = self.GetProperty(name)
                        self.__dict__[name] = prop
                        return prop
                else:   # Get Property from Basic and do not store it
                    return self.GetProperty(name)
        # Execute the usual attributes getter
        return super(SFServices, self).__getattribute__(name)

    def __setattr__(self, name, value):
        """
            Executed for EVERY property assignment, including in __init__() !!
            Setting a property requires for serviceproperties() to be executed in Basic
            Management of __dict__ is automatically done in the final usual object.__setattr__ method
            """
        if self.serviceimplementation == 'basic':
            if name in ('serviceproperties', 'localProperties', 'internal_attributes', 'propertysynonyms',
                        'forceGetProperty'):
                pass
            elif name[0:2] == '__' or name in self.internal_attributes or name in self.localProperties:
                pass
            elif name in self.serviceproperties or name in self.propertysynonyms:
                if name in self.propertysynonyms:  # Reset real name if argument provided in lower or camel case
                    name = self.propertysynonyms[name]
                if self.internal:  # internal = True forces property local setting even if property is read-only
                    pass
                elif self.serviceproperties[name] is True:  # True == Editable
                    self.SetProperty(name, value)
                    return
                else:
                    raise AttributeError(
                        "type object '" + self.objecttype + "' has no editable property '" + name + "'")
            else:
                raise AttributeError("type object '" + self.objecttype + "' has no property '" + name + "'")
        object.__setattr__(self, name, value)
        return

    def __repr__(self):
        return self.serviceimplementation + '/' + self.servicename + '/' + str(self.objectreference) + '/' + \
               super(SFServices, self).__repr__()

    def Dispose(self):
        if self.serviceimplementation == 'basic':
            if self.objectreference >= len(ScriptForge.servicesmodules):    # Do not dispose predefined module objects
                self.ExecMethod(self.vbMethod, 'Dispose')
                self.objectreference = -1

    def ExecMethod(self, flags = 0, methodname = '', *args):
        if flags == 0:
            flags = self.vbMethod
        if len(methodname) > 0:
            return self.EXEC(self.objectreference, flags, methodname, *args)

    def GetProperty(self, propertyname, arg = None):
        """
            Get the given property from the Basic world
            """
        if self.serviceimplementation == 'basic':
            # Conventionally properties starting with X (and only them) may return a UNO object
            calltype = self.vbGet + (self.flgUno if propertyname[0] == 'X' else 0)
            if arg is None:
                return self.EXEC(self.objectreference, calltype, propertyname)
            else:   # There are a few cases (Calc ...) where GetProperty accepts an argument
                return self.EXEC(self.objectreference, calltype, propertyname, arg)
        return None

    def Properties(self):
        return list(self.serviceproperties)

    def basicmethods(self):
        if self.serviceimplementation == 'basic':
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'Methods')
        else:
            return []

    def basicproperties(self):
        if self.serviceimplementation == 'basic':
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'Properties')
        else:
            return []

    def SetProperty(self, propertyname, value):
        """
            Set the given property to a new value in the Basic world
            """
        if self.serviceimplementation == 'basic':
            flag = self.vbLet
            if isinstance(value, datetime.datetime):
                value = SFScriptForge.SF_Basic.CDateToUnoDateTime(value)
                flag += self.flgDateArg
            if repr(type(value)) == "<class 'pyuno'>":
                flag += self.flgUno
            return self.EXEC(self.objectreference, flag, propertyname, value)


# #####################################################################################################################
#                       SFScriptForge CLASS    (alias of ScriptForge Basic library)                                 ###
# #####################################################################################################################
class SFScriptForge:
    pass

    # #########################################################################
    # SF_Array CLASS
    # #########################################################################
    class SF_Array(SFServices, metaclass = _Singleton):
        """
            Provides a collection of methods for manipulating and transforming arrays of one dimension (vectors)
            and arrays of two dimensions (matrices). This includes set operations, sorting,
            importing to and exporting from text files.
            The Python version of the service provides a single method: ImportFromCSVFile
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'ScriptForge.Array'
        servicesynonyms = ('array', 'scriptforge.array')
        serviceproperties = dict()

        def ImportFromCSVFile(self, filename, delimiter = ',', dateformat = ''):
            """
                Difference with the Basic version: dates are returned in their iso format,
                not as any of the datetime objects.
                """
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'ImportFromCSVFile',
                                   filename, delimiter, dateformat)

    # #########################################################################
    # SF_Basic CLASS
    # #########################################################################
    class SF_Basic(SFServices, metaclass = _Singleton):
        """
            This service proposes a collection of Basic methods to be executed in a Python context
            simulating the exact syntax and behaviour of the identical Basic builtin method.
            Typical example:
                SF_Basic.MsgBox('This has to be displayed in a message box')

            The signatures of Basic builtin functions are derived from
                core/basic/source/runtime/stdobj.cxx

            Detailed user documentation:
                https://help.libreoffice.org/latest/en-US/text/sbasic/shared/03/sf_basic.html?DbPAR=BASIC
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'python'
        servicename = 'ScriptForge.Basic'
        servicesynonyms = ('basic', 'scriptforge.basic')
        # Basic helper functions invocation
        module = 'SF_PythonHelper'
        # Message box constants
        MB_ABORTRETRYIGNORE, MB_DEFBUTTON1, MB_DEFBUTTON2, MB_DEFBUTTON3 = 2, 128, 256, 512
        MB_ICONEXCLAMATION, MB_ICONINFORMATION, MB_ICONQUESTION, MB_ICONSTOP = 48, 64, 32, 16
        MB_OK, MB_OKCANCEL, MB_RETRYCANCEL, MB_YESNO, MB_YESNOCANCEL = 0, 1, 5, 4, 3
        IDABORT, IDCANCEL, IDIGNORE, IDNO, IDOK, IDRETRY, IDYES = 3, 2, 5, 7, 1, 4, 6

        @classmethod
        def CDate(cls, datevalue):
            cdate = cls.SIMPLEEXEC(cls.module + '.PyCDate', datevalue)
            return cls.CDateFromUnoDateTime(cdate)

        @staticmethod
        def CDateFromUnoDateTime(unodate):
            """
                Converts a UNO date/time representation to a datetime.datetime Python native object
                :param unodate: com.sun.star.util.DateTime, com.sun.star.util.Date or com.sun.star.util.Time
                :return: the equivalent datetime.datetime
                """
            date = datetime.datetime(1899, 12, 30, 0, 0, 0, 0)      # Idem as Basic builtin TimeSeria() function
            datetype = repr(type(unodate))
            if 'com.sun.star.util.DateTime' in datetype:
                if 1900 <= unodate.Year <= datetime.MAXYEAR:
                    date = datetime.datetime(unodate.Year, unodate.Month, unodate.Day, unodate.Hours,
                                             unodate.Minutes, unodate.Seconds, int(unodate.NanoSeconds / 1000))
            elif 'com.sun.star.util.Date' in datetype:
                if 1900 <= unodate.Year <= datetime.MAXYEAR:
                    date = datetime.datetime(unodate.Year, unodate.Month, unodate.Day)
            elif 'com.sun.star.util.Time' in datetype:
                date = datetime.datetime(unodate.Hours, unodate.Minutes, unodate.Seconds,
                                         int(unodate.NanoSeconds / 1000))
            else:
                return unodate  # Not recognized as a UNO date structure
            return date

        @staticmethod
        def CDateToUnoDateTime(date):
            """
                Converts a date representation into the ccom.sun.star.util.DateTime date format
                Acceptable boundaries: year >= 1900 and <= 32767
                :param date: datetime.datetime, datetime.date, datetime.time, float (time.time) or time.struct_time
                :return: a com.sun.star.util.DateTime
                """
            unodate = uno.createUnoStruct('com.sun.star.util.DateTime')
            unodate.Year, unodate.Month, unodate.Day, unodate.Hours, unodate.Minutes, unodate.Seconds, \
                unodate.NanoSeconds, unodate.IsUTC = \
                1899, 12, 30, 0, 0, 0, 0, False    # Identical to Basic TimeSerial() function

            if isinstance(date, float):
                date = time.localtime(date)
            if isinstance(date, time.struct_time):
                if 1900 <= date[0] <= 32767:
                    unodate.Year, unodate.Month, unodate.Day, unodate.Hours, unodate.Minutes, unodate.Seconds =\
                        date[0:6]
                else:   # Copy only the time related part
                    unodate.Hours, unodate.Minutes, unodate.Seconds = date[3:3]
            elif isinstance(date, (datetime.datetime, datetime.date, datetime.time)):
                if isinstance(date, (datetime.datetime, datetime.date)):
                    if 1900 <= date.year <= 32767:
                        unodate.Year, unodate.Month, unodate.Day = date.year, date.month, date.day
                if isinstance(date, (datetime.datetime, datetime.time)):
                    unodate.Hours, unodate.Minutes, unodate.Seconds, unodate.NanoSeconds = \
                        date.hour, date.minute, date.second, date.microsecond * 1000
            else:
                return date     # Not recognized as a date
            return unodate

        @classmethod
        def ConvertFromUrl(cls, url):
            return cls.SIMPLEEXEC(cls.module + '.PyConvertFromUrl', url)

        @classmethod
        def ConvertToUrl(cls, systempath):
            return cls.SIMPLEEXEC(cls.module + '.PyConvertToUrl', systempath)

        @classmethod
        def CreateUnoService(cls, servicename):
            return cls.SIMPLEEXEC(cls.module + '.PyCreateUnoService', servicename)

        @classmethod
        def DateAdd(cls, interval, number, date):
            if isinstance(date, datetime.datetime):
                date = cls.CDateToUnoDateTime(date)
            dateadd = cls.SIMPLEEXEC(cls.module + '.PyDateAdd', interval, number, date)
            return cls.CDateFromUnoDateTime(dateadd)

        @classmethod
        def DateDiff(cls, interval, date1, date2, firstdayofweek = 1, firstweekofyear = 1):
            if isinstance(date1, datetime.datetime):
                date1 = cls.CDateToUnoDateTime(date1)
            if isinstance(date2, datetime.datetime):
                date2 = cls.CDateToUnoDateTime(date2)
            return cls.SIMPLEEXEC(cls.module + '.PyDateDiff', interval, date1, date2, firstdayofweek, firstweekofyear)

        @classmethod
        def DatePart(cls, interval, date, firstdayofweek = 1, firstweekofyear = 1):
            if isinstance(date, datetime.datetime):
                date = cls.CDateToUnoDateTime(date)
            return cls.SIMPLEEXEC(cls.module + '.PyDatePart', interval, date, firstdayofweek, firstweekofyear)

        @classmethod
        def DateValue(cls, string):
            if isinstance(string, datetime.datetime):
                string = string.isoformat()
            datevalue = cls.SIMPLEEXEC(cls.module + '.PyDateValue', string)
            return cls.CDateFromUnoDateTime(datevalue)

        @classmethod
        def Format(cls, expression, format = ''):
            if isinstance(expression, datetime.datetime):
                expression = cls.CDateToUnoDateTime(expression)
            return cls.SIMPLEEXEC(cls.module + '.PyFormat', expression, format)

        @classmethod
        def GetDefaultContext(cls):
            return ScriptForge.componentcontext

        @classmethod
        def GetGuiType(cls):
            return cls.SIMPLEEXEC(cls.module + '.PyGetGuiType')

        @classmethod
        def GetPathSeparator(cls):
            return os.sep

        @classmethod
        def GetSystemTicks(cls):
            return cls.SIMPLEEXEC(cls.module + '.PyGetSystemTicks')

        class GlobalScope(object, metaclass = _Singleton):
            @classmethod  # Mandatory because the GlobalScope class is normally not instantiated
            def BasicLibraries(cls):
                return ScriptForge.InvokeSimpleScript(SFScriptForge.SF_Basic.module + '.PyGlobalScope', 'Basic')

            @classmethod
            def DialogLibraries(cls):
                return ScriptForge.InvokeSimpleScript(SFScriptForge.SF_Basic.module + '.PyGlobalScope', 'Dialog')

        @classmethod
        def InputBox(cls, prompt, title = '', default = '', xpostwips = -1, ypostwips = -1):
            if xpostwips < 0 or ypostwips < 0:
                return cls.SIMPLEEXEC(cls.module + '.PyInputBox', prompt, title, default)
            return cls.SIMPLEEXEC(cls.module + '.PyInputBox', prompt, title, default, xpostwips, ypostwips)

        @classmethod
        def MsgBox(cls, prompt, buttons = 0, title = ''):
            return cls.SIMPLEEXEC(cls.module + '.PyMsgBox', prompt, buttons, title)

        @classmethod
        def Now(cls):
            return datetime.datetime.now()

        @classmethod
        def RGB(cls, red, green, blue):
            return int('%02x%02x%02x' % (red, green, blue), 16)

        @property
        def StarDesktop(self):
            ctx = ScriptForge.componentcontext
            if ctx is None:
                return None
            smgr = ctx.getServiceManager()  # com.sun.star.lang.XMultiComponentFactory
            DESK = 'com.sun.star.frame.Desktop'
            desktop = smgr.createInstanceWithContext(DESK, ctx)
            return desktop
        starDesktop, stardesktop = StarDesktop, StarDesktop

        @property
        def ThisComponent(self):
            """
                When the current component is the Basic IDE, the ThisComponent object returns
                in Basic the component owning the currently run user script.
                Above behaviour cannot be reproduced in Python.
                :return: the current component or None when not a document
                """
            comp = self.StarDesktop.getCurrentComponent()
            if comp is None:
                return None
            impl = comp.ImplementationName
            if impl in ('com.sun.star.comp.basic.BasicIDE', 'com.sun.star.comp.sfx2.BackingComp'):
                return None     # None when Basic IDE or welcome screen
            return comp
        thisComponent, thiscomponent = ThisComponent, ThisComponent

        @property
        def ThisDatabaseDocument(self):
            """
                When the current component is the Basic IDE, the ThisDatabaseDocument object returns
                in Basic the database owning the currently run user script.
                Above behaviour cannot be reproduced in Python.
                :return: the current Base (main) component or None when not a Base document or one of its subcomponents
            """
            comp = self.ThisComponent   # Get the current component
            if comp is None:
                return None
            #
            sess = CreateScriptService('Session')
            impl, ident = '', ''
            if sess.HasUnoProperty(comp, 'ImplementationName'):
                impl = comp.ImplementationName
            if sess.HasUnoProperty(comp, 'Identifier'):
                ident = comp.Identifier
            #
            targetimpl = 'com.sun.star.comp.dba.ODatabaseDocument'
            if impl == targetimpl:  # The current component is the main Base window
                return comp
            # Identify resp. form, table/query, table/query in edit mode, report, relations diagram
            if impl == 'SwXTextDocument' and ident == 'com.sun.star.sdb.FormDesign' \
                    or impl == 'org.openoffice.comp.dbu.ODatasourceBrowser' \
                    or impl in ('org.openoffice.comp.dbu.OTableDesign', 'org.openoffice.comp.dbu.OQuertDesign') \
                    or impl == 'SwXTextDocument' and ident == 'com.sun.star.sdb.TextReportDesign' \
                    or impl == 'org.openoffice.comp.dbu.ORelationDesign':
                db = comp.ScriptContainer
                if sess.HasUnoProperty(db, 'ImplementationName'):
                    if db.ImplementationName == targetimpl:
                        return db
            return None
        thisDatabaseDocument, thisdatabasedocument = ThisDatabaseDocument, ThisDatabaseDocument

        @classmethod
        def Xray(cls, unoobject = None):
            return cls.SIMPLEEXEC('XrayTool._main.xray', unoobject)

    # #########################################################################
    # SF_Dictionary CLASS
    # #########################################################################
    class SF_Dictionary(SFServices, dict):
        """
            The service adds to a Python dict instance the interfaces for conversion to and from
            a list of UNO PropertyValues

            Usage:
                dico = dict(A = 1, B = 2, C = 3)
                myDict = CreateScriptService('Dictionary', dico)    # Initialize myDict with the content of dico
                myDict['D'] = 4
                print(myDict)   # {'A': 1, 'B': 2, 'C': 3, 'D': 4}
                propval = myDict.ConvertToPropertyValues()
            or
                dico = dict(A = 1, B = 2, C = 3)
                myDict = CreateScriptService('Dictionary')          # Initialize myDict as an empty dict object
                myDict.update(dico) # Load the values of dico into myDict
                myDict['D'] = 4
                print(myDict)   # {'A': 1, 'B': 2, 'C': 3, 'D': 4}
                propval = myDict.ConvertToPropertyValues()
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'python'
        servicename = 'ScriptForge.Dictionary'
        servicesynonyms = ('dictionary', 'scriptforge.dictionary')

        def __init__(self, dic = None):
            SFServices.__init__(self)
            dict.__init__(self)
            if dic is not None:
                self.update(dic)

        def ConvertToPropertyValues(self):
            """
                Store the content of the dictionary in an array of PropertyValues.
                Each entry in the array is a com.sun.star.beans.PropertyValue.
                he key is stored in Name, the value is stored in Value.

                If one of the items has a type datetime, it is converted to a com.sun.star.util.DateTime structure.
                If one of the items is an empty list, it is converted to None.

                The resulting array is empty when the dictionary is empty.
                """
            result = []
            for key in iter(self):
                value = self[key]
                item = value
                if isinstance(value, dict):  # check that first level is not itself a (sub)dict
                    item = None
                elif isinstance(value, (tuple, list)):  # check every member of the list is not a (sub)dict
                    if len(value) == 0:  # Property values do not like empty lists
                        value = None
                    else:
                        for i in range(len(value)):
                            if isinstance(value[i], dict):
                                value[i] = None
                    item = value
                elif isinstance(value, (datetime.datetime, datetime.date, datetime.time)):
                    item = SFScriptForge.SF_Basic.CDateToUnoDateTime(value)
                pv = uno.createUnoStruct('com.sun.star.beans.PropertyValue')
                pv.Name = key
                pv.Value = item
                result.append(pv)
            return result

        def ImportFromPropertyValues(self, propertyvalues, overwrite = False):
            """
                Inserts the contents of an array of PropertyValue objects into the current dictionary.
                PropertyValue Names are used as keys in the dictionary, whereas Values contain the corresponding values.
                Date-type values are converted to datetime.datetime instances.
                :param propertyvalues: a list.tuple containing com.sun.star.beans.PropertyValue objects
                :param overwrite: When True, entries with same name may exist in the dictionary and their values
                    are overwritten. When False (default), repeated keys are not overwritten.
                :return: True when successful
                """
            result = []
            for pv in iter(propertyvalues):
                key = pv.Name
                if overwrite is True or key not in self:
                    item = pv.Value
                    if 'com.sun.star.util.DateTime' in repr(type(item)):
                        item = datetime.datetime(item.Year, item.Month, item.Day,
                                                 item.Hours, item.Minutes, item.Seconds, int(item.NanoSeconds / 1000))
                    elif 'com.sun.star.util.Date' in repr(type(item)):
                        item = datetime.datetime(item.Year, item.Month, item.Day)
                    elif 'com.sun.star.util.Time' in repr(type(item)):
                        item = datetime.datetime(item.Hours, item.Minutes, item.Seconds, int(item.NanoSeconds / 1000))
                    result.append((key, item))
            self.update(result)
            return True

    # #########################################################################
    # SF_Exception CLASS
    # #########################################################################
    class SF_Exception(SFServices, metaclass = _Singleton):
        """
            The Exception service is a collection of methods for code debugging and error handling.

            The Exception service console stores events, variable values and information about errors.
            Use the console when the Python shell is not available, for example in Calc user defined functions (UDF)
            or during events processing.
            Use DebugPrint() method to aggregate additional user data of any type.

            Console entries can be dumped to a text file or visualized in a dialogue.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'ScriptForge.Exception'
        servicesynonyms = ('exception', 'scriptforge.exception')
        serviceproperties = dict()

        def Console(self, modal = True):
            # From Python, the current XComponentContext must be added as last argument
            return self.ExecMethod(self.vbMethod, 'Console', modal, ScriptForge.componentcontext)

        def ConsoleClear(self, keep = 0):
            return self.ExecMethod(self.vbMethod, 'ConsoleClear', keep)

        def ConsoleToFile(self, filename):
            return self.ExecMethod(self.vbMethod, 'ConsoleToFile', filename)

        def DebugDisplay(self, *args):
            # Arguments are concatenated in a single string similar to what the Python print() function would produce
            self.DebugPrint(*args)
            param = '\n'.join(list(map(lambda a: a.strip("'") if isinstance(a, str) else repr(a), args)))
            bas = CreateScriptService('ScriptForge.Basic')
            return bas.MsgBox(param, bas.MB_OK + bas.MB_ICONINFORMATION, 'DebugDisplay')

        def DebugPrint(self, *args):
            # Arguments are concatenated in a single string similar to what the Python print() function would produce
            # Avoid using repr() on strings to not have backslashes * 4
            param = '\t'.join(list(map(lambda a: a.strip("'") if isinstance(a, str) else repr(a),
                                       args))).expandtabs(tabsize = 4)
            return self.ExecMethod(self.vbMethod, 'DebugPrint', param)

        @classmethod
        def PythonShell(cls, variables = None):
            """
                Open an APSO python shell window - Thanks to its authors Hanya/Tsutomu Uchino/Hubert Lambert
                :param variables: Typical use
                                        PythonShell.({**globals(), **locals()})
                                  to push the global and local dictionaries to the shell window
                """
            if variables is None:
                variables = locals()
            # Is APSO installed ?
            ctx = ScriptForge.componentcontext
            ext = ctx.getByName('/singletons/com.sun.star.deployment.PackageInformationProvider')
            apso = 'apso.python.script.organizer'
            if len(ext.getPackageLocation(apso)) > 0:
                # Directly derived from apso.oxt|python|scripts|tools.py$console
                # we need to load apso before import statement
                ctx.ServiceManager.createInstance('apso.python.script.organizer.impl')
                # now we can use apso_utils library
                from apso_utils import console
                kwargs = {'loc': variables}
                kwargs['loc'].setdefault('XSCRIPTCONTEXT', uno)
                console(**kwargs)
                # An interprocess call is necessary to allow a redirection of STDOUT and STDERR by APSO
                #   Choice is a minimalist call to a Basic routine: no arguments, a few lines of code
                SFScriptForge.SF_Basic.GetGuiType()
            else:
                # The APSO extension could not be located in your LibreOffice installation
                cls._RaiseFatal('SF_Exception.PythonShell', 'variables=None', 'PYTHONSHELLERROR')

        @classmethod
        def RaiseFatal(cls, errorcode, *args):
            """
                Generate a run-time error caused by an anomaly in a user script detected by ScriptForge
                The message is logged in the console. The execution is STOPPED
                For INTERNAL USE only
                """
            # Direct call because RaiseFatal forces an execution stop in Basic
            if len(args) == 0:
                args = (None,)
            return cls.SIMPLEEXEC('@SF_Exception.RaiseFatal', (errorcode, *args))   # With ParamArray

        @classmethod
        def _RaiseFatal(cls, sub, subargs, errorcode, *args):
            """
                Wrapper of RaiseFatal(). Includes method and syntax of the failed Python routine
                to simulate the exact behaviour of the Basic RaiseFatal() method
                For INTERNAL USE only
                """
            ScriptForge.InvokeSimpleScript('ScriptForge.SF_Utils._EnterFunction', sub, subargs)
            cls.RaiseFatal(errorcode, *args)
            raise RuntimeError("The execution of the method '" + sub.split('.')[-1] + "' failed. Execution stops.")

    # #########################################################################
    # SF_FileSystem CLASS
    # #########################################################################
    class SF_FileSystem(SFServices, metaclass = _Singleton):
        """
            The "FileSystem" service includes common file and folder handling routines.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'ScriptForge.FileSystem'
        servicesynonyms = ('filesystem', 'scriptforge.filesystem')
        serviceproperties = dict(FileNaming = True, ConfigFolder = False, ExtensionsFolder = False, HomeFolder = False,
                                 InstallFolder = False, TemplatesFolder = False, TemporaryFolder = False,
                                 UserTemplatesFolder = False)
        # Force for each property to get its value from Basic - due to FileNaming updatability
        forceGetProperty = True
        # Open TextStream constants
        ForReading, ForWriting, ForAppending = 1, 2, 8

        def BuildPath(self, foldername, name):
            return self.ExecMethod(self.vbMethod, 'BuildPath', foldername, name)

        def CompareFiles(self, filename1, filename2, comparecontents = False):
            py = ScriptForge.pythonhelpermodule + '$' + '_SF_FileSystem__CompareFiles'
            if self.FileExists(filename1) and self.FileExists(filename2):
                file1 = self._ConvertFromUrl(filename1)
                file2 = self._ConvertFromUrl(filename2)
                return self.SIMPLEEXEC(py, file1, file2, comparecontents)
            else:
                return False

        def CopyFile(self, source, destination, overwrite = True):
            return self.ExecMethod(self.vbMethod, 'CopyFile', source, destination, overwrite)

        def CopyFolder(self, source, destination, overwrite = True):
            return self.ExecMethod(self.vbMethod, 'CopyFolder', source, destination, overwrite)

        def CreateFolder(self, foldername):
            return self.ExecMethod(self.vbMethod, 'CreateFolder', foldername)

        def CreateTextFile(self, filename, overwrite = True, encoding = 'UTF-8'):
            return self.ExecMethod(self.vbMethod, 'CreateTextFile', filename, overwrite, encoding)

        def DeleteFile(self, filename):
            return self.ExecMethod(self.vbMethod, 'DeleteFile', filename)

        def DeleteFolder(self, foldername):
            return self.ExecMethod(self.vbMethod, 'DeleteFolder', foldername)

        def ExtensionFolder(self, extension):
            return self.ExecMethod(self.vbMethod, 'ExtensionFolder', extension)

        def FileExists(self, filename):
            return self.ExecMethod(self.vbMethod, 'FileExists', filename)

        def Files(self, foldername, filter = ''):
            return self.ExecMethod(self.vbMethod, 'Files', foldername, filter)

        def FolderExists(self, foldername):
            return self.ExecMethod(self.vbMethod, 'FolderExists', foldername)

        def GetBaseName(self, filename):
            return self.ExecMethod(self.vbMethod, 'GetBaseName', filename)

        def GetExtension(self, filename):
            return self.ExecMethod(self.vbMethod, 'GetExtension', filename)

        def GetFileLen(self, filename):
            py = ScriptForge.pythonhelpermodule + '$' + '_SF_FileSystem__GetFilelen'
            if self.FileExists(filename):
                file = self._ConvertFromUrl(filename)
                return int(self.SIMPLEEXEC(py, file))
            else:
                return 0

        def GetFileModified(self, filename):
            return self.ExecMethod(self.vbMethod + self.flgDateRet, 'GetFileModified', filename)

        def GetName(self, filename):
            return self.ExecMethod(self.vbMethod, 'GetName', filename)

        def GetParentFolderName(self, filename):
            return self.ExecMethod(self.vbMethod, 'GetParentFolderName', filename)

        def GetTempName(self):
            return self.ExecMethod(self.vbMethod, 'GetTempName')

        def HashFile(self, filename, algorithm):
            py = ScriptForge.pythonhelpermodule + '$' + '_SF_FileSystem__HashFile'
            if self.FileExists(filename):
                file = self._ConvertFromUrl(filename)
                return self.SIMPLEEXEC(py, file, algorithm.lower())
            else:
                return ''

        def MoveFile(self, source, destination):
            return self.ExecMethod(self.vbMethod, 'MoveFile', source, destination)

        def MoveFolder(self, source, destination):
            return self.ExecMethod(self.vbMethod, 'MoveFolder', source, destination)

        def OpenTextFile(self, filename, iomode = 1, create = False, encoding = 'UTF-8'):
            return self.ExecMethod(self.vbMethod, 'OpenTextFile', filename, iomode, create, encoding)

        def PickFile(self, defaultfile = ScriptForge.cstSymEmpty, mode = 'OPEN', filter = ''):
            return self.ExecMethod(self.vbMethod, 'PickFile', defaultfile, mode, filter)

        def PickFolder(self, defaultfolder = ScriptForge.cstSymEmpty, freetext = ''):
            return self.ExecMethod(self.vbMethod, 'PickFolder', defaultfolder, freetext)

        def SubFolders(self, foldername, filter = ''):
            return self.ExecMethod(self.vbMethod, 'SubFolders', foldername, filter)

        @classmethod
        def _ConvertFromUrl(cls, filename):
            # Alias for same function in FileSystem Basic module
            return cls.SIMPLEEXEC('ScriptForge.SF_FileSystem._ConvertFromUrl', filename)

    # #########################################################################
    # SF_L10N CLASS
    # #########################################################################
    class SF_L10N(SFServices):
        """
            This service provides a number of methods related to the translation of strings
            with minimal impact on the program's source code.
            The methods provided by the L10N service can be used mainly to:
                Create POT files that can be used as templates for translation of all strings in the program.
                Get translated strings at runtime for the language defined in the Locale property.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'ScriptForge.L10N'
        servicesynonyms = ('l10n', 'scriptforge.l10n')
        serviceproperties = dict(Folder = False, Languages = False, Locale = False)

        @classmethod
        def ReviewServiceArgs(cls, foldername = '', locale = '', encoding = 'UTF-8'):
            """
                Transform positional and keyword arguments into positional only
                """
            return foldername, locale, encoding

        def AddText(self, context = '', msgid = '', comment = ''):
            return self.ExecMethod(self.vbMethod, 'AddText', context, msgid, comment)

        def AddTextsFromDialog(self, dialog):
            dialogobj = dialog.objectreference if isinstance(dialog, SFDialogs.SF_Dialog) else dialog
            return self.ExecMethod(self.vbMethod + self.flgObject, 'AddTextsFromDialog', dialogobj)

        def ExportToPOTFile(self, filename, header = '', encoding= 'UTF-8'):
            return self.ExecMethod(self.vbMethod, 'ExportToPOTFile', filename, header, encoding)

        def GetText(self, msgid, *args):
            return self.ExecMethod(self.vbMethod, 'GetText', msgid, *args)
        _ = GetText

    # #########################################################################
    # SF_Platform CLASS
    # #########################################################################
    class SF_Platform(SFServices, metaclass = _Singleton):
        """
            The 'Platform' service implements a collection of properties about the actual execution environment
            and context :
                the hardware platform
                the operating system
                the LibreOffice version
                the current user
            All those properties are read-only.
            The implementation is mainly based on the 'platform' module of the Python standard library
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'ScriptForge.Platform'
        servicesynonyms = ('platform', 'scriptforge.platform')
        serviceproperties = dict(Architecture = False, ComputerName = False, CPUCount = False, CurrentUser = False,
                                 Extensions = False, Fonts = False, FormatLocale = False, Locale = False,
                                 Machine = False, OfficeLocale = False, OfficeVersion = False, OSName = False,
                                 OSPlatform = False, OSRelease = False, OSVersion = False, Printers = False,
                                 Processor = False, PythonVersion = False, SystemLocale = False)
        # Python helper functions
        py = ScriptForge.pythonhelpermodule + '$' + '_SF_Platform'

        @property
        def Architecture(self):
            return self.SIMPLEEXEC(self.py, 'Architecture')

        @property
        def ComputerName(self):
            return self.SIMPLEEXEC(self.py, 'ComputerName')

        @property
        def CPUCount(self):
            return self.SIMPLEEXEC(self.py, 'CPUCount')

        @property
        def CurrentUser(self):
            return self.SIMPLEEXEC(self.py, 'CurrentUser')

        @property
        def Machine(self):
            return self.SIMPLEEXEC(self.py, 'Machine')

        @property
        def OSName(self):
            return self.SIMPLEEXEC(self.py, 'OSName')

        @property
        def OSPlatform(self):
            return self.SIMPLEEXEC(self.py, 'OSPlatform')

        @property
        def OSRelease(self):
            return self.SIMPLEEXEC(self.py, 'OSRelease')

        @property
        def OSVersion(self):
            return self.SIMPLEEXEC(self.py, 'OSVersion')

        @property
        def Processor(self):
            return self.SIMPLEEXEC(self.py, 'Processor')

        @property
        def PythonVersion(self):
            return self.SIMPLEEXEC(self.py, 'PythonVersion')

    # #########################################################################
    # SF_Session CLASS
    # #########################################################################
    class SF_Session(SFServices, metaclass = _Singleton):
        """
            The Session service gathers various general-purpose methods about:
            - UNO introspection
            - the invocation of external scripts or programs
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'ScriptForge.Session'
        servicesynonyms = ('session', 'scriptforge.session')
        serviceproperties = dict()

        # Class constants                       Where to find an invoked library ?
        SCRIPTISEMBEDDED = 'document'           # in the document
        SCRIPTISAPPLICATION = 'application'     # in any shared library (Basic)
        SCRIPTISPERSONAL = 'user'               # in My Macros (Python)
        SCRIPTISPERSOXT = 'user:uno_packages'   # in an extension installed for the current user (Python)
        SCRIPTISSHARED = 'share'                # in LibreOffice macros (Python)
        SCRIPTISSHAROXT = 'share:uno_packages'  # in an extension installed for all users (Python)
        SCRIPTISOXT = 'uno_packages'            # in an extension but the installation parameters are unknown (Python)

        @classmethod
        def ExecuteBasicScript(cls, scope = '', script = '', *args):
            if scope is None or scope == '':
                scope = cls.SCRIPTISAPPLICATION
            if len(args) == 0:
                args = (scope,) + (script,) + (None,)
            else:
                args = (scope,) + (script,) + args
            # ExecuteBasicScript method has a ParamArray parameter in Basic
            return cls.SIMPLEEXEC('@SF_Session.ExecuteBasicScript', args)

        @classmethod
        def ExecuteCalcFunction(cls, calcfunction, *args):
            if len(args) == 0:
                # Arguments of Calc functions are strings or numbers. None == Empty is a good alias for no argument
                args = (calcfunction,) + (None,)
            else:
                args = (calcfunction,) + args
            # ExecuteCalcFunction method has a ParamArray parameter in Basic
            return cls.SIMPLEEXEC('@SF_Session.ExecuteCalcFunction', args)

        @classmethod
        def ExecutePythonScript(cls, scope = '', script = '', *args):
            return cls.SIMPLEEXEC(scope + '#' + script, *args)

        def HasUnoMethod(self, unoobject, methodname):
            return self.ExecMethod(self.vbMethod, 'HasUnoMethod', unoobject, methodname)

        def HasUnoProperty(self, unoobject, propertyname):
            return self.ExecMethod(self.vbMethod, 'HasUnoProperty', unoobject, propertyname)

        @classmethod
        def OpenURLInBrowser(cls, url):
            py = ScriptForge.pythonhelpermodule + '$' + '_SF_Session__OpenURLInBrowser'
            return cls.SIMPLEEXEC(py, url)

        def RunApplication(self, command, parameters):
            return self.ExecMethod(self.vbMethod, 'RunApplication', command, parameters)

        def SendMail(self, recipient, cc = '', bcc = '', subject = '', body = '', filenames = '', editmessage = True):
            return self.ExecMethod(self.vbMethod, 'SendMail', recipient, cc, bcc, subject, body, filenames, editmessage)

        def UnoObjectType(self, unoobject):
            return self.ExecMethod(self.vbMethod, 'UnoObjectType', unoobject)

        def UnoMethods(self, unoobject):
            return self.ExecMethod(self.vbMethod, 'UnoMethods', unoobject)

        def UnoProperties(self, unoobject):
            return self.ExecMethod(self.vbMethod, 'UnoProperties', unoobject)

        def WebService(self, uri):
            return self.ExecMethod(self.vbMethod, 'WebService', uri)

    # #########################################################################
    # SF_String CLASS
    # #########################################################################
    class SF_String(SFServices, metaclass = _Singleton):
        """
            Focus on string manipulation, regular expressions, encodings and hashing algorithms.
            The methods implemented in Basic that are redundant with Python builtin functions
            are not duplicated
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'ScriptForge.String'
        servicesynonyms = ('string', 'scriptforge.string')
        serviceproperties = dict()

        @classmethod
        def HashStr(cls, inputstr, algorithm):
            py = ScriptForge.pythonhelpermodule + '$' + '_SF_String__HashStr'
            return cls.SIMPLEEXEC(py, inputstr, algorithm.lower())

        def IsADate(self, inputstr, dateformat = 'YYYY-MM-DD'):
            return self.ExecMethod(self.vbMethod, 'IsADate', inputstr, dateformat)

        def IsEmail(self, inputstr):
            return self.ExecMethod(self.vbMethod, 'IsEmail', inputstr)

        def IsFileName(self, inputstr, osname = ScriptForge.cstSymEmpty):
            return self.ExecMethod(self.vbMethod, 'IsFileName', inputstr, osname)

        def IsIBAN(self, inputstr):
            return self.ExecMethod(self.vbMethod, 'IsIBAN', inputstr)

        def IsIPv4(self, inputstr):
            return self.ExecMethod(self.vbMethod, 'IsIPv4', inputstr)

        def IsLike(self, inputstr, pattern, casesensitive = False):
            return self.ExecMethod(self.vbMethod, 'IsLike', inputstr, pattern, casesensitive)

        def IsSheetName(self, inputstr):
            return self.ExecMethod(self.vbMethod, 'IsSheetName', inputstr)

        def IsUrl(self, inputstr):
            return self.ExecMethod(self.vbMethod, 'IsUrl', inputstr)

        def SplitNotQuoted(self, inputstr, delimiter = ' ', occurrences = 0, quotechar = '"'):
            return self.ExecMethod(self.vbMethod, 'SplitNotQuoted', inputstr, delimiter, occurrences, quotechar)

        def Wrap(self, inputstr, width = 70, tabsize = 8):
            return self.ExecMethod(self.vbMethod, 'Wrap', inputstr, width, tabsize)

    # #########################################################################
    # SF_TextStream CLASS
    # #########################################################################
    class SF_TextStream(SFServices):
        """
            The TextStream service is used to sequentially read from and write to files opened or created
            using the ScriptForge.FileSystem service..
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'ScriptForge.TextStream'
        servicesynonyms = ()
        serviceproperties = dict(AtEndOfStream = False, Encoding = False, FileName = False, IOMode = False,
                                 Line = False, NewLine = True)

        @property
        def AtEndOfStream(self):
            return self.GetProperty('AtEndOfStream')
        atEndOfStream, atendofstream = AtEndOfStream, AtEndOfStream

        @property
        def Line(self):
            return self.GetProperty('Line')
        line = Line

        def CloseFile(self):
            return self.ExecMethod(self.vbMethod, 'CloseFile')

        def ReadAll(self):
            return self.ExecMethod(self.vbMethod, 'ReadAll')

        def ReadLine(self):
            return self.ExecMethod(self.vbMethod, 'ReadLine')

        def SkipLine(self):
            return self.ExecMethod(self.vbMethod, 'SkipLine')

        def WriteBlankLines(self, lines):
            return self.ExecMethod(self.vbMethod, 'WriteBlankLines', lines)

        def WriteLine(self, line):
            return self.ExecMethod(self.vbMethod, 'WriteLine', line)

    # #########################################################################
    # SF_Timer CLASS
    # #########################################################################
    class SF_Timer(SFServices):
        """
            The "Timer" service measures the amount of time it takes to run user scripts.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'ScriptForge.Timer'
        servicesynonyms = ('timer', 'scriptforge.timer')
        serviceproperties = dict(Duration = False, IsStarted = False, IsSuspended = False,
                                 SuspendDuration = False, TotalDuration = False)
        # Force for each property to get its value from Basic
        forceGetProperty = True

        @classmethod
        def ReviewServiceArgs(cls, start = False):
            """
                Transform positional and keyword arguments into positional only
                """
            return (start,)

        def Continue(self):
            return self.ExecMethod(self.vbMethod, 'Continue')

        def Restart(self):
            return self.ExecMethod(self.vbMethod, 'Restart')

        def Start(self):
            return self.ExecMethod(self.vbMethod, 'Start')

        def Suspend(self):
            return self.ExecMethod(self.vbMethod, 'Suspend')

        def Terminate(self):
            return self.ExecMethod(self.vbMethod, 'Terminate')

    # #########################################################################
    # SF_UI CLASS
    # #########################################################################
    class SF_UI(SFServices, metaclass = _Singleton):
        """
            Singleton class for the identification and the manipulation of the
            different windows composing the whole LibreOffice application:
                - Windows selection
                - Windows moving and resizing
                - Statusbar settings
                - Creation of new windows
                - Access to the underlying "documents"
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'ScriptForge.UI'
        servicesynonyms = ('ui', 'scriptforge.ui')
        serviceproperties = dict(ActiveWindow = False)

        # Class constants
        MACROEXECALWAYS, MACROEXECNEVER, MACROEXECNORMAL = 2, 1, 0
        BASEDOCUMENT, CALCDOCUMENT, DRAWDOCUMENT, IMPRESSDOCUMENT, MATHDOCUMENT, WRITERDOCUMENT = \
            'Base', 'Calc', 'Draw', 'Impress', 'Math', 'Writer'

        @property
        def ActiveWindow(self):
            return self.ExecMethod(self.vbMethod, 'ActiveWindow')
        activeWindow, activewindow = ActiveWindow, ActiveWindow

        def Activate(self, windowname = ''):
            return self.ExecMethod(self.vbMethod, 'Activate', windowname)

        def CreateBaseDocument(self, filename, embeddeddatabase = 'HSQLDB', registrationname = '', calcfilename = ''):
            return self.ExecMethod(self.vbMethod, 'CreateBaseDocument', filename, embeddeddatabase, registrationname,
                                   calcfilename)

        def CreateDocument(self, documenttype = '', templatefile = '', hidden = False):
            return self.ExecMethod(self.vbMethod, 'CreateDocument', documenttype, templatefile, hidden)

        def Documents(self):
            return self.ExecMethod(self.vbMethod, 'Documents')

        def GetDocument(self, windowname = ''):
            return self.ExecMethod(self.vbMethod, 'GetDocument', windowname)

        def Maximize(self, windowname = ''):
            return self.ExecMethod(self.vbMethod, 'Maximize', windowname)

        def Minimize(self, windowname = ''):
            return self.ExecMethod(self.vbMethod, 'Minimize', windowname)

        def OpenBaseDocument(self, filename = '', registrationname = '', macroexecution = MACROEXECNORMAL):
            return self.ExecMethod(self.vbMethod, 'OpenBaseDocument', filename, registrationname, macroexecution)

        def OpenDocument(self, filename, password = '', readonly = False, hidden = False,
                         macroexecution = MACROEXECNORMAL, filtername = '', filteroptions = ''):
            return self.ExecMethod(self.vbMethod, 'OpenDocument', filename, password, readonly, hidden,
                                   macroexecution, filtername, filteroptions)

        def Resize(self, left = -1, top = -1, width = -1, height = -1):
            return self.ExecMethod(self.vbMethod, 'Resize', left, top, width, height)

        def RunCommand(self, command, *args, **kwargs):
            params = tuple(list(args) + ScriptForge.unpack_args(kwargs))
            if len(params) == 0:
                params = (command,) + (None,)
            else:
                params = (command,) + params
            return self.SIMPLEEXEC('@SF_UI.RunCommand', params)

        def SetStatusbar(self, text = '', percentage = -1):
            return self.ExecMethod(self.vbMethod, 'SetStatusbar', text, percentage)

        def ShowProgressBar(self, title = '', text = '', percentage = -1):
            # From Python, the current XComponentContext must be added as last argument
            return self.ExecMethod(self.vbMethod, 'ShowProgressBar', title, text, percentage,
                                   ScriptForge.componentcontext)

        def WindowExists(self, windowname):
            return self.ExecMethod(self.vbMethod, 'WindowExists', windowname)


# #####################################################################################################################
#                       SFDatabases CLASS    (alias of SFDatabases Basic library)                                   ###
# #####################################################################################################################
class SFDatabases:
    """
        The SFDatabases class manages databases embedded in or connected to Base documents
        """
    pass

    # #########################################################################
    # SF_Database CLASS
    # #########################################################################
    class SF_Database(SFServices):
        """
            Each instance of the current class represents a single database, with essentially its tables, queries
            and data
            The exchanges with the database are done in SQL only.
            To make them more readable, use optionally square brackets to surround table/query/field names
            instead of the (RDBMS-dependent) normal surrounding character.
            SQL statements may be run in direct or indirect mode. In direct mode the statement is transferred literally
            without syntax checking nor review to the database engine.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFDatabases.Database'
        servicesynonyms = ('database', 'sfdatabases.database')
        serviceproperties = dict(Queries = False, Tables = False, XConnection = False, XMetaData = False)

        @classmethod
        def ReviewServiceArgs(cls, filename = '', registrationname = '', readonly = True, user = '', password = ''):
            """
                Transform positional and keyword arguments into positional only
                """
            return filename, registrationname, readonly, user, password

        def CloseDatabase(self):
            return self.ExecMethod(self.vbMethod, 'CloseDatabase')

        def DAvg(self, expression, tablename, criteria = ''):
            return self.ExecMethod(self.vbMethod, 'DAvg', expression, tablename, criteria)

        def DCount(self, expression, tablename, criteria = ''):
            return self.ExecMethod(self.vbMethod, 'DCount', expression, tablename, criteria)

        def DLookup(self, expression, tablename, criteria = '', orderclause = ''):
            return self.ExecMethod(self.vbMethod, 'DLookup', expression, tablename, criteria, orderclause)

        def DMax(self, expression, tablename, criteria = ''):
            return self.ExecMethod(self.vbMethod, 'DMax', expression, tablename, criteria)

        def DMin(self, expression, tablename, criteria = ''):
            return self.ExecMethod(self.vbMethod, 'DMin', expression, tablename, criteria)

        def DSum(self, expression, tablename, criteria = ''):
            return self.ExecMethod(self.vbMethod, 'DSum', expression, tablename, criteria)

        def GetRows(self, sqlcommand, directsql = False, header = False, maxrows = 0):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'GetRows', sqlcommand, directsql, header, maxrows)

        def RunSql(self, sqlcommand, directsql = False):
            return self.ExecMethod(self.vbMethod, 'RunSql', sqlcommand, directsql)


# #####################################################################################################################
#                       SFDialogs CLASS    (alias of SFDialogs Basic library)                                       ###
# #####################################################################################################################
class SFDialogs:
    """
        The SFDialogs class manages dialogs defined with the Basic IDE
        """
    pass

    # #########################################################################
    # SF_Dialog CLASS
    # #########################################################################
    class SF_Dialog(SFServices):
        """
            Each instance of the current class represents a single dialog box displayed to the user.
            The dialog box must have been designed and defined with the Basic IDE previously.
            From a Python script, a dialog box can be displayed in modal or in non-modal modes.

            In modal mode, the box is displayed and the execution of the macro process is suspended
            until one of the OK or Cancel buttons is pressed. In the meantime, other user actions
            executed on the box can trigger specific actions.

            In non-modal mode, the floating dialog remains displayed until the dialog is terminated
            by code (Terminate()) or until the LibreOffice application stops.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFDialogs.Dialog'
        servicesynonyms = ('dialog', 'sfdialogs.dialog')
        serviceproperties = dict(Caption = True, Height = True, Modal = False, Name = False,
                                 OnFocusGained = False, OnFocusLost = False, OnKeyPressed = False,
                                 OnKeyReleased = False, OnMouseDragged = False, OnMouseEntered = False,
                                 OnMouseExited = False, OnMouseMoved = False, OnMousePressed = False,
                                 OnMouseReleased = False,
                                 Page = True, Visible = True, Width = True, XDialogModel = False, XDialogView = False)
        # Class constants used together with the Execute() method
        OKBUTTON, CANCELBUTTON = 1, 0

        @classmethod
        def ReviewServiceArgs(cls, container = '', library = 'Standard', dialogname = ''):
            """
                Transform positional and keyword arguments into positional only
                Add the XComponentContext as last argument
                """
            return container, library, dialogname, ScriptForge.componentcontext

        def Activate(self):
            return self.ExecMethod(self.vbMethod, 'Activate')

        def Controls(self, controlname = ''):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'Controls', controlname)

        def EndExecute(self, returnvalue):
            return self.ExecMethod(self.vbMethod, 'EndExecute', returnvalue)

        def Execute(self, modal = True):
            return self.ExecMethod(self.vbMethod, 'Execute', modal)

        def GetTextsFromL10N(self, l10n):
            l10nobj = l10n.objectreference if isinstance(l10n, SFScriptForge.SF_L10N) else l10n
            return self.ExecMethod(self.vbMethod + self.flgObject, 'GetTextsFromL10N', l10nobj)

        def Terminate(self):
            return self.ExecMethod(self.vbMethod, 'Terminate')

    # #########################################################################
    # SF_DialogControl CLASS
    # #########################################################################
    class SF_DialogControl(SFServices):
        """
            Each instance of the current class represents a single control within a dialog box.
            The focus is clearly set on getting and setting the values displayed by the controls of the dialog box,
            not on their formatting.
            A special attention is given to controls with type TreeControl.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFDialogs.DialogControl'
        servicesynonyms = ()
        serviceproperties = dict(Cancel = True, Caption = True, ControlType = False, CurrentNode = True,
                                 Default = True, Enabled = True, Format = True, ListCount = False,
                                 ListIndex = True, Locked = True, MultiSelect = True, Name = False,
                                 OnActionPerformed = False, OnAdjustmentValueChanged = False, OnFocusGained = False,
                                 OnFocusLost = False, OnItemStateChanged = False, OnKeyPressed = False,
                                 OnKeyReleased = False, OnMouseDragged = False, OnMouseEntered = False,
                                 OnMouseExited = False, OnMouseMoved = False, OnMousePressed = False,
                                 OnMouseReleased = False, OnNodeExpanded = True, OnNodeSelected = True,
                                 OnTextChanged = False, Page = True, Parent = False, Picture = True,
                                 RootNode = False, RowSource = True, Text = False, TipText = True,
                                 TripleState = True, Value = True, Visible = True,
                                 XControlModel = False, XControlView = False, XGridColumnModel = False,
                                 XGridDataModel = False, XTreeDataModel = False)

        # Root related properties do not start with X and, nevertheless, return a UNO object
        @property
        def CurrentNode(self):
            return self.EXEC(self.objectreference, self.vbGet + self.flgUno, 'CurrentNode')

        @property
        def RootNode(self):
            return self.EXEC(self.objectreference, self.vbGet + self.flgUno, 'RootNode')

        def AddSubNode(self, parentnode, displayvalue, datavalue = ScriptForge.cstSymEmpty):
            return self.ExecMethod(self.vbMethod + self.flgUno, 'AddSubNode', parentnode, displayvalue, datavalue)

        def AddSubTree(self, parentnode, flattree, withdatavalue = False):
            return self.ExecMethod(self.vbMethod, 'AddSubTree', parentnode, flattree, withdatavalue)

        def CreateRoot(self, displayvalue, datavalue = ScriptForge.cstSymEmpty):
            return self.ExecMethod(self.vbMethod + self.flgUno, 'CreateRoot', displayvalue, datavalue)

        def FindNode(self, displayvalue, datavalue = ScriptForge.cstSymEmpty, casesensitive = False):
            return self.ExecMethod(self.vbMethod + self.flgUno, 'FindNode', displayvalue, datavalue, casesensitive)

        def SetFocus(self):
            return self.ExecMethod(self.vbMethod, 'SetFocus')

        def SetTableData(self, dataarray, widths = (1,), alignments = ''):
            return self.ExecMethod(self.vbMethod + self.flgArrayArg, 'SetTableData', dataarray, widths, alignments)

        def WriteLine(self, line = ''):
            return self.ExecMethod(self.vbMethod, 'WriteLine', line)


# #####################################################################################################################
#                       SFDocuments CLASS    (alias of SFDocuments Basic library)                                   ###
# #####################################################################################################################
class SFDocuments:
    """
        The SFDocuments class gathers a number of classes, methods and properties making easy
        managing and manipulating LibreOffice documents
        """
    pass

    # #########################################################################
    # SF_Document CLASS
    # #########################################################################
    class SF_Document(SFServices):
        """
            The methods and properties are generic for all types of documents: they are combined in the
            current SF_Document class
                - saving, closing documents
                - accessing their standard or custom properties
            Specific properties and methods are implemented in the concerned subclass(es) SF_Calc, SF_Base, ...
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFDocuments.Document'
        servicesynonyms = ('document', 'sfdocuments.document')
        serviceproperties = dict(Description = True, DocumentType = False, IsBase = False, IsCalc = False,
                                 IsDraw = False, IsImpress = False, IsMath = False, IsWriter = False,
                                 Keywords = True, Readonly = False, Subject = True, Title = True,
                                 XComponent = False)
        # Force for each property to get its value from Basic - due to intense interactivity with user
        forceGetProperty = True

        @classmethod
        def ReviewServiceArgs(cls, windowname = ''):
            """
                Transform positional and keyword arguments into positional only
                """
            return windowname,

        def Activate(self):
            return self.ExecMethod(self.vbMethod, 'Activate')

        def CloseDocument(self, saveask = True):
            return self.ExecMethod(self.vbMethod, 'CloseDocument', saveask)

        def CreateMenu(self, menuheader, before = '', submenuchar = '>'):
            return self.ExecMethod(self.vbMethod, 'CreateMenu', menuheader, before, submenuchar)

        def ExportAsPDF(self, filename, overwrite = False, pages = '', password = '', watermark = ''):
            return self.ExecMethod(self.vbMethod, 'ExportAsPDF', filename, overwrite, pages, password, watermark)

        def PrintOut(self, pages = '', copies = 1):
            return self.ExecMethod(self.vbMethod, 'PrintOut', pages, copies)

        def RemoveMenu(self, menuheader):
            return self.ExecMethod(self.vbMethod, 'RemoveMenu', menuheader)

        def RunCommand(self, command, *args, **kwargs):
            params = tuple([command] + list(args) + ScriptForge.unpack_args(kwargs))
            return self.ExecMethod(self.vbMethod, 'RunCommand', *params)

        def Save(self):
            return self.ExecMethod(self.vbMethod, 'Save')

        def SaveAs(self, filename, overwrite = False, password = '', filtername = '', filteroptions = ''):
            return self.ExecMethod(self.vbMethod, 'SaveAs', filename, overwrite, password, filtername, filteroptions)

        def SaveCopyAs(self, filename, overwrite = False, password = '', filtername = '', filteroptions = ''):
            return self.ExecMethod(self.vbMethod, 'SaveCopyAs', filename, overwrite,
                                   password, filtername, filteroptions)

        def SetPrinter(self, printer = '', orientation = '', paperformat = ''):
            return self.ExecMethod(self.vbMethod, 'SetPrinter', printer, orientation, paperformat)

    # #########################################################################
    # SF_Base CLASS
    # #########################################################################
    class SF_Base(SF_Document, SFServices):
        """
            The SF_Base module is provided mainly to block parent properties that are NOT applicable to Base documents
            In addition, it provides methods to identify form documents and access their internal forms
            (read more elsewhere (the "SFDocuments.Form" service) about this subject)
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFDocuments.Base'
        servicesynonyms = ('base', 'scriptforge.base')
        serviceproperties = dict(DocumentType = False, IsBase = False, IsCalc = False,
                                 IsDraw = False, IsImpress = False, IsMath = False, IsWriter = False,
                                 XComponent = False)

        @classmethod
        def ReviewServiceArgs(cls, windowname = ''):
            """
                Transform positional and keyword arguments into positional only
                """
            return windowname,

        def CloseDocument(self, saveask = True):
            return self.ExecMethod(self.vbMethod, 'CloseDocument', saveask)

        def CloseFormDocument(self, formdocument):
            return self.ExecMethod(self.vbMethod, 'CloseFormDocument', formdocument)

        def FormDocuments(self):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'FormDocuments')

        def Forms(self, formdocument, form = ''):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'Forms', formdocument, form)

        def GetDatabase(self, user = '', password = ''):
            return self.ExecMethod(self.vbMethod, 'GetDatabase', user, password)

        def IsLoaded(self, formdocument):
            return self.ExecMethod(self.vbMethod, 'IsLoaded', formdocument)

        def OpenFormDocument(self, formdocument, designmode = False):
            return self.ExecMethod(self.vbMethod, 'OpenFormDocument', formdocument, designmode)

        def PrintOut(self, formdocument, pages = '', copies = 1):
            return self.ExecMethod(self.vbMethod, 'PrintOut', formdocument, pages, copies)

        def SetPrinter(self, formdocument = '', printer = '', orientation = '', paperformat = ''):
            return self.ExecMethod(self.vbMethod, 'SetPrinter', formdocument, printer, orientation, paperformat)

    # #########################################################################
    # SF_Calc CLASS
    # #########################################################################
    class SF_Calc(SF_Document, SFServices):
        """
            The SF_Calc module is focused on :
                - management (copy, insert, move, ...) of sheets within a Calc document
                - exchange of data between Basic data structures and Calc ranges of values
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFDocuments.Calc'
        servicesynonyms = ('calc', 'sfdocuments.calc')
        serviceproperties = dict(CurrentSelection = True, Sheets = False,
                                 Description = True, DocumentType = False, IsBase = False, IsCalc = False,
                                 IsDraw = False, IsImpress = False, IsMath = False, IsWriter = False,
                                 Keywords = True, Readonly = False, Subject = True, Title = True,
                                 XComponent = False)
        # Force for each property to get its value from Basic - due to intense interactivity with user
        forceGetProperty = True

        @classmethod
        def ReviewServiceArgs(cls, windowname = ''):
            """
                Transform positional and keyword arguments into positional only
                """
            return windowname,

        # Next functions are implemented in Basic as read-only properties with 1 argument
        def FirstCell(self, rangename):
            return self.GetProperty('FirstCell', rangename)

        def FirstColumn(self, rangename):
            return self.GetProperty('FirstColumn', rangename)

        def FirstRow(self, rangename):
            return self.GetProperty('FirstRow', rangename)

        def Height(self, rangename):
            return self.GetProperty('Height', rangename)

        def LastCell(self, rangename):
            return self.GetProperty('LastCell', rangename)

        def LastColumn(self, rangename):
            return self.GetProperty('LastColumn', rangename)

        def LastRow(self, rangename):
            return self.GetProperty('LastRow', rangename)

        def Range(self, rangename):
            return self.GetProperty('Range', rangename)

        def Region(self, rangename):
            return self.GetProperty('Region', rangename)

        def Sheet(self, sheetname):
            return self.GetProperty('Sheet', sheetname)

        def SheetName(self, rangename):
            return self.GetProperty('SheetName', rangename)

        def Width(self, rangename):
            return self.GetProperty('Width', rangename)

        def XCellRange(self, rangename):
            return self.ExecMethod(self.vbGet + self.flgUno, 'XCellRange', rangename)

        def XSheetCellCursor(self, rangename):
            return self.ExecMethod(self.vbGet + self.flgUno, 'XSheetCellCursor', rangename)

        def XSpreadsheet(self, sheetname):
            return self.ExecMethod(self.vbGet + self.flgUno, 'XSpreadsheet', sheetname)

        # Usual methods
        def A1Style(self, row1, column1, row2 = 0, column2 = 0, sheetname = '~'):
            return self.ExecMethod(self.vbMethod, 'A1Style', row1, column1, row2, column2, sheetname)

        def Activate(self, sheetname = ''):
            return self.ExecMethod(self.vbMethod, 'Activate', sheetname)

        def Charts(self, sheetname, chartname = ''):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'Charts', sheetname, chartname)

        def ClearAll(self, range):
            return self.ExecMethod(self.vbMethod, 'ClearAll', range)

        def ClearFormats(self, range):
            return self.ExecMethod(self.vbMethod, 'ClearFormats', range)

        def ClearValues(self, range):
            return self.ExecMethod(self.vbMethod, 'ClearValues', range)

        def CopySheet(self, sheetname, newname, beforesheet = 32768):
            sheet = (sheetname.objectreference if isinstance(sheetname, SFDocuments.SF_CalcReference) else sheetname)
            return self.ExecMethod(self.vbMethod + self.flgObject, 'CopySheet', sheet, newname, beforesheet)

        def CopySheetFromFile(self, filename, sheetname, newname, beforesheet = 32768):
            sheet = (sheetname.objectreference if isinstance(sheetname, SFDocuments.SF_CalcReference) else sheetname)
            return self.ExecMethod(self.vbMethod + self.flgObject, 'CopySheetFromFile',
                                   filename, sheet, newname, beforesheet)

        def CopyToCell(self, sourcerange, destinationcell):
            range = (sourcerange.objectreference if isinstance(sourcerange, SFDocuments.SF_CalcReference)
                     else sourcerange)
            return self.ExecMethod(self.vbMethod + self.flgObject, 'CopyToCell', range, destinationcell)

        def CopyToRange(self, sourcerange, destinationrange):
            range = (sourcerange.objectreference if isinstance(sourcerange, SFDocuments.SF_CalcReference)
                     else sourcerange)
            return self.ExecMethod(self.vbMethod + self.flgObject, 'CopyToRange', range, destinationrange)

        def CreateChart(self, chartname, sheetname, range, columnheader = False, rowheader = False):
            return self.ExecMethod(self.vbMethod, 'CreateChart', chartname, sheetname, range, columnheader, rowheader)

        def DAvg(self, range):
            return self.ExecMethod(self.vbMethod, 'DAvg', range)

        def DCount(self, range):
            return self.ExecMethod(self.vbMethod, 'DCount', range)

        def DMax(self, range):
            return self.ExecMethod(self.vbMethod, 'DMax', range)

        def DMin(self, range):
            return self.ExecMethod(self.vbMethod, 'DMin', range)

        def DSum(self, range):
            return self.ExecMethod(self.vbMethod, 'DSum', range)

        def Forms(self, sheetname, form = ''):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'Forms', sheetname, form)

        def GetColumnName(self, columnnumber):
            return self.ExecMethod(self.vbMethod, 'GetColumnName', columnnumber)

        def GetFormula(self, range):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'GetFormula', range)

        def GetValue(self, range):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'GetValue', range)

        def ImportFromCSVFile(self, filename, destinationcell, filteroptions = ScriptForge.cstSymEmpty):
            return self.ExecMethod(self.vbMethod, 'ImportFromCSVFile', filename, destinationcell, filteroptions)

        def ImportFromDatabase(self, filename = '', registrationname = '', destinationcell = '', sqlcommand = '',
                               directsql = False):
            return self.ExecMethod(self.vbMethod, 'ImportFromDatabase', filename, registrationname,
                                   destinationcell, sqlcommand, directsql)

        def InsertSheet(self, sheetname, beforesheet = 32768):
            return self.ExecMethod(self.vbMethod, 'InsertSheet', sheetname, beforesheet)

        def MoveRange(self, source, destination):
            return self.ExecMethod(self.vbMethod, 'MoveRange', source, destination)

        def MoveSheet(self, sheetname, beforesheet = 32768):
            return self.ExecMethod(self.vbMethod, 'MoveSheet', sheetname, beforesheet)

        def Offset(self, range, rows = 0, columns = 0, height = ScriptForge.cstSymEmpty,
                   width = ScriptForge.cstSymEmpty):
            return self.ExecMethod(self.vbMethod, 'Offset', range, rows, columns, height, width)

        def OpenRangeSelector(self, title = '', selection = '~', singlecell = False, closeafterselect = True):
            return self.ExecMethod(self.vbMethod, 'OpenRangeSelector', title, selection, singlecell, closeafterselect)

        def Printf(self, inputstr, range, tokencharacter = '%'):
            return self.ExecMethod(self.vbMethod, 'Printf', inputstr, range, tokencharacter)

        def PrintOut(self, sheetname = '~', pages = '', copies = 1):
            return self.ExecMethod(self.vbMethod, 'PrintOut', sheetname, pages, copies)

        def RemoveSheet(self, sheetname):
            return self.ExecMethod(self.vbMethod, 'RemoveSheet', sheetname)

        def RenameSheet(self, sheetname, newname):
            return self.ExecMethod(self.vbMethod, 'RenameSheet', sheetname, newname)

        def SetArray(self, targetcell, value):
            return self.ExecMethod(self.vbMethod + self.flgArrayArg, 'SetArray', targetcell, value)

        def SetCellStyle(self, targetrange, style):
            return self.ExecMethod(self.vbMethod, 'SetCellStyle', targetrange, style)

        def SetFormula(self, targetrange, formula):
            return self.ExecMethod(self.vbMethod + self.flgArrayArg, 'SetFormula', targetrange, formula)

        def SetValue(self, targetrange, value):
            return self.ExecMethod(self.vbMethod + self.flgArrayArg, 'SetValue', targetrange, value)

        def ShiftDown(self, range, wholerow = False, rows = 0):
            return self.ExecMethod(self.vbMethod, 'ShiftDown', range, wholerow, rows)

        def ShiftLeft(self, range, wholecolumn = False, columns = 0):
            return self.ExecMethod(self.vbMethod, 'ShiftLeft', range, wholecolumn, columns)

        def ShiftRight(self, range, wholecolumn = False, columns = 0):
            return self.ExecMethod(self.vbMethod, 'ShiftRight', range, wholecolumn, columns)

        def ShiftUp(self, range, wholerow = False, rows = 0):
            return self.ExecMethod(self.vbMethod, 'ShiftUp', range, wholerow, rows)

        def SortRange(self, range, sortkeys, sortorder = 'ASC', destinationcell = ScriptForge.cstSymEmpty,
                      containsheader = False, casesensitive = False, sortcolumns = False):
            return self.ExecMethod(self.vbMethod, 'SortRange', range, sortkeys, sortorder, destinationcell,
                                   containsheader, casesensitive, sortcolumns)

    # #########################################################################
    # SF_CalcReference CLASS
    # #########################################################################
    class SF_CalcReference(SFServices):
        """
            The SF_CalcReference class has as unique role to hold sheet and range references.
            They are implemented in Basic as Type ... End Type data structures
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFDocuments.CalcReference'
        servicesynonyms = ()
        serviceproperties = dict()

    # #########################################################################
    # SF_Chart CLASS
    # #########################################################################
    class SF_Chart(SFServices):
        """
            The SF_Chart module is focused on the description of chart documents
            stored in Calc sheets.
            With this service, many chart types and chart characteristics available
            in the user interface can be read or modified.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFDocuments.Chart'
        servicesynonyms = ()
        serviceproperties = dict(ChartType = True, Deep = True, Dim3D = True, Exploded = True, Filled = True,
                                 Legend = True, Percent = True, Stacked = True, Title = True,
                                 XChartObj = False, XDiagram = False, XShape = False, XTableChart = False,
                                 XTitle = True, YTitle = True)

        def Resize(self, xpos = -1, ypos = -1, width = -1, height = -1):
            return self.ExecMethod(self.vbMethod, 'Resize', xpos, ypos, width, height)

        def ExportToFile(self, filename, imagetype = 'png', overwrite = False):
            return self.ExecMethod(self.vbMethod, 'ExportToFile', filename, imagetype, overwrite)

    # #########################################################################
    # SF_Form CLASS
    # #########################################################################
    class SF_Form(SFServices):
        """
            Management of forms defined in LibreOffice documents. Supported types are Base, Calc and Writer documents.
            It includes the management of subforms
            Each instance of the current class represents a single form or a single subform
            A form may optionally be (understand "is often") linked to a data source manageable with
            the SFDatabases.Database service. The current service offers a rapid access to that service.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFDocuments.Form'
        servicesynonyms = ()
        serviceproperties = dict(AllowDeletes = True, AllowInserts = True, AllowUpdates = True, BaseForm = False,
                                 Bookmark = True, CurrentRecord = True, Filter = True, LinkChildFields = False,
                                 LinkParentFields = False, Name = False,
                                 OnApproveCursorMove = True, OnApproveParameter = True, OnApproveReset = True,
                                 OnApproveRowChange = True, OnApproveSubmit = True, OnConfirmDelete = True,
                                 OnCursorMoved = True, OnErrorOccurred = True, OnLoaded = True, OnReloaded = True,
                                 OnReloading = True, OnResetted = True, OnRowChanged = True, OnUnloaded = True,
                                 OnUnloading = True,
                                 OrderBy = True, Parent = False, RecordSource = True, XForm = False)

        def Activate(self):
            return self.ExecMethod(self.vbMethod, 'Activate')

        def CloseFormDocument(self):
            return self.ExecMethod(self.vbMethod, 'CloseFormDocument')

        def Controls(self, controlname = ''):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'Controls', controlname)

        def GetDatabase(self, user = '', password = ''):
            return self.ExecMethod(self.vbMethod, 'GetDatabase', user, password)

        def MoveFirst(self):
            return self.ExecMethod(self.vbMethod, 'MoveFirst')

        def MoveLast(self):
            return self.ExecMethod(self.vbMethod, 'MoveLast')

        def MoveNew(self):
            return self.ExecMethod(self.vbMethod, 'MoveNew')

        def MoveNext(self, offset = 1):
            return self.ExecMethod(self.vbMethod, 'MoveNext', offset)

        def MovePrevious(self, offset = 1):
            return self.ExecMethod(self.vbMethod, 'MovePrevious', offset)

        def Requery(self):
            return self.ExecMethod(self.vbMethod, 'Requery')

        def Subforms(self, subform = ''):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'Subforms', subform)

    # #########################################################################
    # SF_FormControl CLASS
    # #########################################################################
    class SF_FormControl(SFServices):
        """
            Manage the controls belonging to a form or subform stored in a document.
            Each instance of the current class represents a single control within a form, a subform or a tablecontrol.
            A prerequisite is that all controls within the same form, subform or tablecontrol must have
            a unique name.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFDocuments.FormControl'
        servicesynonyms = ()
        serviceproperties = dict(Action = True, Caption = True, ControlSource = False, ControlType = False,
                                 Default = True, DefaultValue = True, Enabled = True, Format = True,
                                 ListCount = False, ListIndex = True, ListSource = True, ListSourceType = True,
                                 Locked = True, MultiSelect = True, Name = False,
                                 OnActionPerformed = True, OnAdjustmentValueChanged = True,
                                 OnApproveAction = True, OnApproveReset = True, OnApproveUpdate = True,
                                 OnChanged = True, OnErrorOccurred = True, OnFocusGained = True, OnFocusLost = True,
                                 OnItemStateChanged = True, OnKeyPressed = True, OnKeyReleased = True,
                                 OnMouseDragged = True, OnMouseEntered = True, OnMouseExited = True,
                                 OnMouseMoved = True, OnMousePressed = True, OnMouseReleased = True, OnResetted = True,
                                 OnTextChanged = True, OnUpdated = True, Parent = False, Picture = True,
                                 Required = True, Text = False, TipText = True, TripleState = True, Value = True,
                                 Visible = True, XControlModel = False, XControlView = False)

        def Controls(self, controlname = ''):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'Controls', controlname)

        def SetFocus(self):
            return self.ExecMethod(self.vbMethod, 'SetFocus')

    # #########################################################################
    # SF_Writer CLASS
    # #########################################################################
    class SF_Writer(SF_Document, SFServices):
        """
            The SF_Writer module is focused on :
                - TBD
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFDocuments.Writer'
        servicesynonyms = ('writer', 'sfdocuments.writer')
        serviceproperties = dict(Description = True, DocumentType = False, IsBase = False, IsCalc = False,
                                 IsDraw = False, IsImpress = False, IsMath = False, IsWriter = False,
                                 Keywords = True, Readonly = False, Subject = True, Title = True,
                                 XComponent = False)
        # Force for each property to get its value from Basic - due to intense interactivity with user
        forceGetProperty = True

        @classmethod
        def ReviewServiceArgs(cls, windowname = ''):
            """
                Transform positional and keyword arguments into positional only
                """
            return windowname,

        def Forms(self, form = ''):
            return self.ExecMethod(self.vbMethod + self.flgArrayRet, 'Forms', form)

        def PrintOut(self, pages = '', copies = 1, printbackground = True, printblankpages = False,
                     printevenpages = True, printoddpages = True, printimages = True):
            return self.ExecMethod(self.vbMethod, 'PrintOut', pages, copies, printbackground, printblankpages,
                                   printevenpages, printoddpages, printimages)


# #####################################################################################################################
#                       SFWidgets CLASS    (alias of SFWidgets Basic library)                                       ###
# #####################################################################################################################
class SFWidgets:
    """
        The SFWidgets class manages toolbars and popup menus
        """
    pass

    # #########################################################################
    # SF_Menu CLASS
    # #########################################################################
    class SF_Menu(SFServices):
        """
            Display a menu in the menubar of a document or a form document.
            After use, the menu will not be saved neither in the application settings, nor in the document.
            The menu will be displayed, as usual, when its header in the menubar is clicked.
            When one of its items is selected, there are 3 alternative options:
            - a UNO command (like ".uno:About") is triggered
            - a user script is run receiving a standard argument defined in this service
            - one of above combined with a toggle of the status of the item
            The menu is described from top to bottom. Each menu item receives a numeric and a string identifier.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFWidgets.Menu'
        servicesynonyms = ('menu', 'sfwidgets.menu')
        serviceproperties = dict(ShortcutCharacter = False, SubmenuCharacter = False)

        def AddCheckBox(self, menuitem, name = '', status = False, icon = '', tooltip = '',
                        command = '', script = ''):
            return self.ExecMethod(self.vbMethod, 'AddCheckBox', menuitem, name, status, icon, tooltip,
                                   command, script)

        def AddItem(self, menuitem, name = '', icon = '', tooltip = '', command = '', script = ''):
            return self.ExecMethod(self.vbMethod, 'AddItem', menuitem, name, icon, tooltip, command, script)

        def AddRadioButton(self, menuitem, name = '', status = False, icon = '', tooltip = '',
                           command = '', script = ''):
            return self.ExecMethod(self.vbMethod, 'AddRadioButton', menuitem, name, status, icon, tooltip,
                                   command, script)

    # #########################################################################
    # SF_PopupMenu CLASS
    # #########################################################################
    class SF_PopupMenu(SFServices):
        """
            Display a popup menu anywhere and any time.
            A popup menu is usually triggered by a mouse action (typically a right-click) on a dialog, a form
            or one of their controls. In this case the menu will be displayed below the clicked area.
            When triggered by other events, including in the normal flow of a user script, the script should
            provide the coordinates of the topleft edge of the menu versus the actual component.
            The menu is described from top to bottom. Each menu item receives a numeric and a string identifier.
            The execute() method returns the item selected by the user.
            """
        # Mandatory class properties for service registration
        serviceimplementation = 'basic'
        servicename = 'SFWidgets.PopupMenu'
        servicesynonyms = ('popupmenu', 'sfwidgets.popupmenu')
        serviceproperties = dict(ShortcutCharacter = False, SubmenuCharacter = False)

        @classmethod
        def ReviewServiceArgs(cls, event = None, x = 0, y = 0, submenuchar = ''):
            """
                Transform positional and keyword arguments into positional only
                """
            return event, x, y, submenuchar

        def AddCheckBox(self, menuitem, name = '', status = False, icon = '', tooltip = ''):
            return self.ExecMethod(self.vbMethod, 'AddCheckBox', menuitem, name, status, icon, tooltip)

        def AddItem(self, menuitem, name = '', icon = '', tooltip = ''):
            return self.ExecMethod(self.vbMethod, 'AddItem', menuitem, name, icon, tooltip)

        def AddRadioButton(self, menuitem, name = '', status = False, icon = '', tooltip = ''):
            return self.ExecMethod(self.vbMethod, 'AddRadioButton', menuitem, name, status, icon, tooltip)

        def Execute(self, returnid = True):
            return self.ExecMethod(self.vbMethod, 'Execute', returnid)


# ##############################################False##################################################################
#                           CreateScriptService()                                                                   ###
# #####################################################################################################################
def CreateScriptService(service, *args, **kwargs):
    """
        A service being the name of a collection of properties and methods,
        this method returns either
            - the Python object mirror of the Basic object implementing the requested service
            - the Python object implementing the service itself

        A service may be designated by its official name, stored in its class.servicename
        or by one of its synonyms stored in its class.servicesynonyms list
        If the service is not identified, the service creation is delegated to Basic, that might raise an error
        if still not identified there

        :param service: the name of the service as a string 'library.service' - cased exactly
                or one of its synonyms
        :param args: the arguments to pass to the service constructor
        :return: the service as a Python object
        """
    # Init at each CreateScriptService() invocation
    #       CreateScriptService is usually the first statement in user scripts requesting ScriptForge services
    #       ScriptForge() is optional in user scripts when Python process inside LibreOffice process
    if ScriptForge.SCRIPTFORGEINITDONE is False:
        ScriptForge()

    def ResolveSynonyms(servicename):
        """
            Synonyms within service names implemented in Python or predefined are resolved here
            :param servicename: The short name of the service
            :return: The official service name if found, the argument otherwise
            """
        for cls in SFServices.__subclasses__():
            if servicename.lower() in cls.servicesynonyms:
                return cls.servicename
        return servicename

    #
    # Check the list of available services
    scriptservice = ResolveSynonyms(service)
    if scriptservice in ScriptForge.serviceslist:
        serv = ScriptForge.serviceslist[scriptservice]
        # Check if the requested service is within the Python world
        if serv.serviceimplementation == 'python':
            return serv(*args)
        # Check if the service is a predefined standard Basic service
        elif scriptservice in ScriptForge.servicesmodules:
            return serv(ScriptForge.servicesmodules[scriptservice], classmodule = SFServices.moduleStandard)
    else:
        serv = None
    # The requested service is to be found in the Basic world
    # Check if the service must review the arguments
    if serv is not None:
        if hasattr(serv, 'ReviewServiceArgs'):
            # ReviewServiceArgs() must be a class method
            args = serv.ReviewServiceArgs(*args, **kwargs)
    # Get the service object back from Basic
    if len(args) == 0:
        serv = ScriptForge.InvokeBasicService('SF_Services', SFServices.vbMethod, 'CreateScriptService', service)
    else:
        serv = ScriptForge.InvokeBasicService('SF_Services', SFServices.vbMethod, 'CreateScriptService',
                                              service, *args)
    return serv


createScriptService, createscriptservice = CreateScriptService, CreateScriptService


# ######################################################################
# Lists the scripts, that shall be visible inside the Basic/Python IDE
# ######################################################################

g_exportedScripts = ()