This file is indexed.

/usr/lib/python2.7/dist-packages/PyTango/databaseds/database.py is in python-pytango 8.1.8-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""This class manage the TANGO database."""

import sys
import time
import logging
import functools

try:
    import argparse
except ImportError:
    argparse = None
    from optparse import OptionParser

import PyTango
from PyTango import AttrWriteType, GreenMode
from PyTango.server import Device, DeviceMeta
from PyTango.server import attribute, command
from PyTango.server import class_property
from PyTango.server import device_property
from PyTango.server import run

from PyTango.globals import get_class, get_class_by_class, \
    get_constructed_class_by_class


READ_ONLY = AttrWriteType.READ
WRITE_ONLY = AttrWriteType.WRITE
READ_WRITE = AttrWriteType.READ_WRITE
READ_WITH_WRITE = AttrWriteType.READ_WITH_WRITE

#Argument Options
global options
global WILDCARD_REPLACEMENT
WILDCARD_REPLACEMENT = True
class DbInter(PyTango.Interceptors):

    def create_thread(self):
        pass

    def delete_thread(self):
        pass

DB_NAME = None

def set_db_name(db_name):
    global DB_NAME
    DB_NAME = db_name

def get_db_name():
    return DB_NAME


import db_access

th_exc = PyTango.Except.throw_exception

from db_errors import *

def check_device_name(dev_name):
    if '*' in dev_name:
        return False, None, None

    if dev_name.startswith("tango:"):
        dev_name = dev_name[6:]
    elif dev_name.startswith("taco:"):
        dev_name = dev_name[5:]
    if dev_name.startswith("//"):
        dev_name = dev_name[2:]
        if not '/' in dev_name or dev_name.startswith("/"):
            return False, None, None
    dfm = dev_name.split("/")
    if len(dfm) != 3:
        return False, None, None
    # check that each element has at least one character
    if not all(map(len, dfm)):
        return False, None, None
    return True, dev_name, dfm

def replace_wildcard(text):
    if not WILDCARD_REPLACEMENT:
        return text
    # escape '%' with '\'
    text = text.replace("%", "\\%")
    # escape '_' with '\'
    text = text.replace("_", "\\_")
    # escape '"' with '\'
    text = text.replace('"', '\\"')
    # escape ''' with '\'
    text = text.replace("'", "\\'")
    # replace '*' with '%'
    text = text.replace("*", "%")
    return text

class TimeStructure:
    def __init__(self):
        self.average = 0
        self.minimum = 0
        self.maximum = 0
        self.maximum = 0
        self.total_elapsed = 0
        self.calls = 0
        self.index = ''

def stats(f):
    fname = f.__name__
    @functools.wraps(f)
    def wrapper(self, *args, **kwargs):
        start = time.time()
        try:
            return f(self, *args, **kwargs)
        finally:
            end = time.time()
            update_timing_stats(self, start, end, fname)
    return wrapper

def update_timing_stats(dev, time_before, time_after, cmd_name):
    tmp_time = dev.timing_maps[cmd_name]
    time_elapsed = (time_after - time_before) * 1000.
    tmp_time.total_elapsed = tmp_time.total_elapsed + time_elapsed
    if time_elapsed > tmp_time.maximum:
        tmp_time.maximum = time_elapsed
    if time_elapsed < tmp_time.minimum or tmp_time.minimum == 0:
        tmp_time.minimum = time_elapsed
    tmp_time.calls = tmp_time.calls + 1
    tmp_time.average = tmp_time.total_elapsed/tmp_time.calls


class DataBase(Device):
    """
    DataBase TANGO device class
    """
    __metaclass__ = DeviceMeta

    # --- attributes ---------------------------------------

    Timing_maximum = attribute(dtype=('float64',),max_dim_x=128, access=READ_ONLY)

    Timing_average = attribute(dtype=('float64',),max_dim_x=128, access=READ_ONLY)

    Timing_index = attribute(dtype=('str',),max_dim_x=128, access=READ_ONLY)

    Timing_calls = attribute(dtype=('float64',),max_dim_x=128, access=READ_ONLY)

    Timing_info = attribute(dtype=('str',),max_dim_x=128, access=READ_ONLY)

    StoredProcedureRelease = attribute(dtype='str', access=READ_ONLY)

    Timing_minimum = attribute(dtype=('float64',),max_dim_x=128, access=READ_ONLY)

    def init_device(self):
        self._log = log = logging.getLogger(self.get_name())
        self._log.debug("In init_device()")
        self.attr_StoredProcedureRelease_read = ''
        self.init_timing_stats()
        m = __import__('%s.%s' % (db_access.__package__,options.db_access),None,None,
                       '%s.%s' % (db_access.__package__,options.db_access))
        self.db = m.get_db(personal_name = options.argv[1])
        try:
            global WILDCARD_REPLACEMENT
            WILDCARD_REPLACEMENT = m.get_wildcard_replacement()
        except AttributeError:
            pass
        self.set_state(PyTango.DevState.ON)

    def init_timing_stats(self):
        self.timing_maps = {}
        for cmd in dir(self):
            if cmd.startswith('Db'):
                self.timing_maps[cmd] = TimeStructure()
                self.timing_maps[cmd].index = cmd

    # --- attribute methods --------------------------------

    def read_Timing_maximum(self):
        self._log.debug("In read_Timing_maximum()")
        return [x.maximum for x in self.timing_maps.values()]

    def read_Timing_average(self):
        self._log.debug("In read_Timing_average()")
        
        return [x.average for x in self.timing_maps.values()]

    def read_Timing_index(self):
        self._log.debug("In read_Timing_index()")
        return [x.index for x in self.timing_maps.values()]

    def read_Timing_calls(self):
        self._log.debug("In read_Timing_calls()")
        return [x.calls for x in self.timing_maps.values()]

    def read_Timing_info(self):
        self._log.debug("In read_Timing_info()")
        util = PyTango.Util.instance()
        attr_Timing_info_read = []
        attr_Timing_info_read.append("TANGO Database Timing info on host " + util.get_host_name())
        attr_Timing_info_read.append(" ")
        attr_Timing_info_read.append("command	average	minimum	maximum	calls")
        attr_Timing_info_read.append(" ")
        for tmp_name in sorted(self.timing_maps.keys()):
            tmp_info = "%41s\t%6.3f\t%6.3f\t%6.3f\t%.0f"%(tmp_name, self.timing_maps[tmp_name].average, self.timing_maps[tmp_name].minimum, self.timing_maps[tmp_name].maximum, self.timing_maps[tmp_name].calls)
            attr_Timing_info_read.append(tmp_info)
        return attr_Timing_info_read

    def read_StoredProcedureRelease(self):
        self._log.debug("In read_StoredProcedureRelease()")
        self.attr_StoredProcedureRelease_read = self.db.get_stored_procedure_release()
        return self.attr_StoredProcedureRelease_read

    def read_Timing_minimum(self):
        self._log.debug("In read_Timing_minimum()")
        return [x.minimum for x in self.timing_maps.values()]

    # --- commands -----------------------------------------

    @stats
    @command(dtype_in='str', doc_in='The wildcard', dtype_out=('str',), doc_out='Device name domain list')
    def DbGetDeviceDomainList(self, argin):
        """ Get list of device domain name matching the specified

        :param argin: The wildcard
        :type: PyTango.DevString
        :return: Device name domain list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceDomainList()")
        return self.db.get_device_domain_list(replace_wildcard(argin))

    @stats
    @command(dtype_in='str', doc_in='Device server name (executable/instance)', doc_out='none')
    def DbUnExportServer(self, argin):
        """ Mark all devices belonging to a specified device server
        process as non exported

        :param argin: Device server name (executable/instance)
        :type: PyTango.DevString
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbUnExportServer()")
        self.db.unexport_server(argin)

    @command(dtype_in=('str',), doc_in='str[0] = device name\nStr[1]...str[n] = attribute name(s)', doc_out='none')
    def DbDeleteAllDeviceAttributeProperty(self, argin):
        """ Delete all attribute properties for the specified device attribute(s)

        :param argin: str[0] = device name
        Str[1]...str[n] = attribute name(s)
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteAllDeviceAttributeProperty()")

        if len(argin) < 2:
            self.warn_stream("DataBase::DbDeleteAllDeviceAttributeProperty(): insufficient number of arguments ")
            th_exc(DB_IncorrectArguments,
                   "insufficient number of arguments to delete all device attribute(s) property",
                   "DataBase::DbDeleteAllDeviceAttributeProperty()")

        dev_name = argin[0]

        ret, d_name, dfm = check_device_name(dev_name)

        if not ret:
            th_exc(DB_IncorrectDeviceName,
                  "device name (" + argin + ") syntax error (should be [tango:][//instance/]domain/family/member)",
                  "DataBase::DbDeleteAllDeviceAttributeProperty()")

        self.db.delete_all_device_attribute_property(dev_name, argin[1:])

    @command(dtype_in='str', doc_in='Attriibute alias name.', doc_out='none')
    def DbDeleteAttributeAlias(self, argin):
        """ Delete an attribute alias.

        :param argin: Attriibute alias name.
        :type: PyTango.DevString
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteAttributeAlias()")
        self.db.delete_attribute_alias(argin)

    @command(dtype_in=('str',), doc_in='Str[0] = Tango class\nStr[1] = Attribute name\nStr[2] = Property name', dtype_out=('str',), doc_out='Str[0] = Attribute name\nStr[1] = Property name\nStr[2] = date\nStr[3] = Property value number (array case)\nStr[4] = Property value 1\nStr[n] = Property value n')
    def DbGetClassAttributePropertyHist(self, argin):
        """ Retrieve Tango class attribute property history

        :param argin: Str[0] = Tango class
        Str[1] = Attribute name
        Str[2] = Property name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Attribute name
        Str[1] = Property name
        Str[2] = date
        Str[3] = Property value number (array case)
        Str[4] = Property value 1
        Str[n] = Property value n
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetClassAttributePropertyHist()")
        class_name = argin[0]
        attribute = replace_wildcard(argin[1])
        prop_name = replace_wildcard(argin[2])
        return self.db.get_class_attribute_property_hist(class_name, attribute, prop_name)

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[1] = Attribute number\nStr[2] = Attribute name\nStr[3] = Property number\nStr[4] = Property name\nStr[5] = Property value number (array case)\nStr[5] = Property value 1\nStr[n] = Property value n (array case)\n.....', doc_out='none')
    def DbPutDeviceAttributeProperty2(self, argin):
        """ Put device attribute property. This command adds the possibility to have attribute property
        which are arrays. Not possible with the old DbPutDeviceAttributeProperty command.
        This old command is not deleted for compatibility reasons.

        :param argin: Str[0] = Device name
        Str[1] = Attribute number
        Str[2] = Attribute name
        Str[3] = Property number
        Str[4] = Property name
        Str[5] = Property value number (array case)
        Str[5] = Property value 1
        Str[n] = Property value n (array case)
        .....
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbPutDeviceAttributeProperty2()")
        device_name = argin[0]
        nb_attributes = int(argin[1])
        self.db.put_device_attribute_property2(device_name, nb_attributes, argin[2:])

    @command(dtype_in='str', doc_in='attribute alias filter string (eg: att*)', dtype_out=('str',), doc_out='attribute aliases')
    def DbGetAttributeAliasList(self, argin):
        """ Get attribute alias list for a specified filter

        :param argin: attribute alias filter string (eg: att*)
        :type: PyTango.DevString
        :return: attribute aliases
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetAttributeAliasList()")
        if not argin:
            argin = "%"
        else:
            argin = replace_wildcard(argin)
        return self.db.get_attribute_alias_list(argin)

    @command(dtype_in='str', doc_in='Class name', dtype_out=('str',), doc_out='Device exported list')
    def DbGetExportdDeviceListForClass(self, argin):
        """ Query the database for device exported for the specified class.

        :param argin: Class name
        :type: PyTango.DevString
        :return: Device exported list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetExportdDeviceListForClass()")
        argin = replace_wildcard(argin)
        return self.db.get_exported_device_list_for_class(argin)

    @command(dtype_in=('str',), doc_in='Str[0] = attribute name\nStr[1] = attribute alias', doc_out='none')
    def DbPutAttributeAlias(self, argin):
        """ Define an alias for an attribute

        :param argin: Str[0] = attribute name
        Str[1] = attribute alias
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbPutAttributeAlias()")

        if len(argin) < 2:
            self.warn_stream("DataBase::DbPutAttributeAlias(): insufficient number of arguments ")
            th_exc(DB_IncorrectArguments,
                   "insufficient number of arguments to put attribute alias",
                   "DataBase::DbPutAttributeAlias()")

        attribute_name = argin[0]
        attribute_alias = argin[1]
        self.db.put_attribute_alias(attribute_name, attribute_alias)

    @stats
    @command(dtype_in='str', doc_in='The filter', dtype_out=('str',), doc_out='Device server process name list')
    def DbGetServerList(self, argin):
        """ Get list of device server process defined in database
        with name matching the specified filter

        :param argin: The filter
        :type: PyTango.DevString
        :return: Device server process name list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetServerList()")
        argin = replace_wildcard(argin)
        return self.db.get_server_list(argin)

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[1] = CORBA IOR\nStr[2] = Device server process host name\nStr[3] = Device server process PID or string ``null``\nStr[4] = Device server process version', doc_out='none')
    def DbExportDevice(self, argin):
        """ Export a device to the database

        :param argin: Str[0] = Device name
        Str[1] = CORBA IOR
        Str[2] = Device server process host name
        Str[3] = Device server process PID or string ``null``
        Str[4] = Device server process version
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        DbExportDevice(self, argin)

    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[1] = Attribute name\nStr[2] = Property name\nStr[n] = Property name', doc_out='none')
    def DbDeleteDeviceAttributeProperty(self, argin):
        """ delete a device attribute property from the database

        :param argin: Str[0] = Device name
        Str[1] = Attribute name
        Str[2] = Property name
        Str[n] = Property name
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteDeviceAttributeProperty()")

        if len(argin) < 3:
            self.warn_stream("DataBase::db_delete_device_attribute_property(): insufficient number of arguments ")
            th_exc(DB_IncorrectArguments,
                   "insufficient number of arguments to delete device attribute property",
                   "DataBase::DeleteDeviceAttributeProperty()")

        dev_name, attr_name = argin[:2]

        ret, dev_name, dfm = check_device_name(argin)
        if not ret:
            self.warn_stream("DataBase::db_delete_device_attribute_property(): device name " + argin + " incorrect ")
            th_exc(DB_IncorrectDeviceName,
                   "failed to delete device attribute property, device name incorrect",
                   "DataBase::DeleteDeviceAttributeProperty()")

        for prop_name in argin[2:]:
            self.db.delete_device_attribute_property(dev_name, attr_name, prop_name)

    @stats
    @command(dtype_in='str', doc_in='The wildcard', dtype_out=('str',), doc_out='Family list')
    def DbGetDeviceFamilyList(self, argin):
        """ Get a list of device name families for device name matching the
        specified wildcard

        :param argin: The wildcard
        :type: PyTango.DevString
        :return: Family list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceFamilyList()")
        argin = replace_wildcard(argin)
        return self.db.get_device_family_list(argin)

    @command(dtype_in='str', doc_in='filter', dtype_out=('str',), doc_out='list of exported devices')
    def DbGetDeviceWideList(self, argin):
        """ Get a list of devices whose names satisfy the filter.

        :param argin: filter
        :type: PyTango.DevString
        :return: list of exported devices
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceWideList()")
        argin = replace_wildcard(argin)
        return self.db.get_device_wide_list(argin)

    @command(dtype_in=('str',), doc_in='Str[0] = Object name\nStr[1] = Property number\nStr[2] = Property name\nStr[3] = Property value number\nStr[4] = Property value 1\nStr[n] = Property value n\n....', doc_out='none')
    def DbPutProperty(self, argin):
        """ Create / Update free object property(ies)

        :param argin: Str[0] = Object name
        Str[1] = Property number
        Str[2] = Property name
        Str[3] = Property value number
        Str[4] = Property value 1
        Str[n] = Property value n
        ....
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbPutProperty()")
        object_name = argin[0]
        nb_properties = int(argin[1])
        self.db.put_property(object_name, properties, argin[2:])

    @command(dtype_in=('str',), doc_in='Str[0]  = Object name\nStr[1] = Property name\nStr[n] = Property name', doc_out='none')
    def DbDeleteProperty(self, argin):
        """ Delete free property from database

        :param argin: Str[0]  = Object name
        Str[1] = Property name
        Str[n] = Property name
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteProperty()")
        obj_name = argin[0]
        for prop_name in argin[1:]:
            self.db.delete_property(obj_name, prop_name)

    @command(dtype_in=('str',), doc_in='Str[0] = Tango class name\nStr[1] = Attribute name\nStr[n] = Attribute name', dtype_out=('str',), doc_out='Str[0] = Tango class name\nStr[1] = Attribute property  number\nStr[2] = Attribute property 1 name\nStr[3] = Attribute property 1 value number (array case)\nStr[4] = Attribute property 1 value\nStr[n] = Attribute property 1 value (array case)\nStr[n + 1] = Attribute property 2 name\nStr[n + 2] = Attribute property 2 value number (array case)\nStr[n + 3] = Attribute property 2 value\nStr[n + m] = Attribute property 2 value (array case)')
    def DbGetClassAttributeProperty2(self, argin):
        """ This command supports array property compared to the old command called
        DbGetClassAttributeProperty. The old command has not been deleted from the
        server for compatibility reasons.

        :param argin: Str[0] = Tango class name
        Str[1] = Attribute name
        Str[n] = Attribute name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Tango class name
        Str[1] = Attribute property  number
        Str[2] = Attribute property 1 name
        Str[3] = Attribute property 1 value number (array case)
        Str[4] = Attribute property 1 value
        Str[n] = Attribute property 1 value (array case)
        Str[n + 1] = Attribute property 2 name
        Str[n + 2] = Attribute property 2 value number (array case)
        Str[n + 3] = Attribute property 2 value
        Str[n + m] = Attribute property 2 value (array case)
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetClassAttributeProperty2()")
        class_name = argin[0]
        return self.db.get_class_attribute_property2(class_name, argin[1:])

    @stats
    @command(dtype_in='str', doc_in='filter', dtype_out=('str',), doc_out='list of exported devices')
    def DbGetDeviceExportedList(self, argin):
        """ Get a list of exported devices whose names satisfy the filter (wildcard is

        :param argin: filter
        :type: PyTango.DevString
        :return: list of exported devices
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceExportedList()")
        argin = replace_wildcard(argin)
        return self.db.get_device_exported_list(argin)

    @command(dtype_in='str', doc_in='The device name', dtype_out='str', doc_out='The alias found')
    def DbGetDeviceAlias(self, argin):
        """ Return alias for device name if found.

        :param argin: The device name
        :type: PyTango.DevString
        :return: The alias found
        :rtype: PyTango.DevString """
        self._log.debug("In DbGetDeviceAlias()")
        ret, dev_name, dfm = check_device_name(argin)
        if not ret:
            th_exc(DB_IncorrectDeviceName,
                  "device name (" + argin + ") syntax error (should be [tango:][//instance/]domain/family/member)",
                  "DataBase::DbGetDeviceAlias()")

        return self.db.get_device_alias(dev_name)

    @command(dtype_in=('str',), doc_in='Str[0] = Tango class name\nStr[1] = Attribute number\nStr[2] = Attribute name\nStr[3] = Property number\nStr[4] = Property name\nStr[5] = Property value\n.....', doc_out='none')
    def DbPutClassAttributeProperty(self, argin):
        """ Create/Update class attribute property(ies) in database

        :param argin: Str[0] = Tango class name
        Str[1] = Attribute number
        Str[2] = Attribute name
        Str[3] = Property number
        Str[4] = Property name
        Str[5] = Property value
        .....
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbPutClassAttributeProperty()")
        class_name = argin[0]
        nb_attributes = int(argin[1])
        self.db.put_class_attribute_property(class_name, nb_attributes, argin[2:])

    @stats
    @command(dtype_in='str', doc_in='The filter', dtype_out=('str',), doc_out='Property name list')
    def DbGetClassPropertyList(self, argin):
        """ Get property list for a given Tango class with a specified filter

        :param argin: The filter
        :type: PyTango.DevString
        :return: Property name list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetClassPropertyList()")
        if not argin:
            argin = "%"
        else:
            argin = replace_wildcard(argin)
        return self.db.get_class_property_list(argin)

    @command(dtype_in='str', doc_in='The filter', dtype_out=('str',), doc_out='Device alias list')
    def DbGetDeviceAliasList(self, argin):
        """ Get device alias name with a specific filter

        :param argin: The filter
        :type: PyTango.DevString
        :return: Device alias list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceAliasList()")
        if not argin:
            argin = "%"
        else:
            argin = replace_wildcard(argin)

        return self.db.get_device_alias_list(argin)

    @command(dtype_in=('str',), doc_in='Str[0] = Tango class name\nStr[1] = Attribute name', doc_out='none')
    def DbDeleteClassAttribute(self, argin):
        """ delete a class attribute and all its properties from database

        :param argin: Str[0] = Tango class name
        Str[1] = Attribute name
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteClassAttribute()")

        if len(argin) < 2:
            self.warn_stream("DataBase::db_delete_class_attribute(): insufficient number of arguments ")
            th_exc(DB_IncorrectArguments,
                   "insufficient number of arguments to delete class attribute",
                   "DataBase::DeleteClassAttribute()")

        klass_name, attr_name = argin[:2]

        self.db.delete_class_attribute(klass_name, attr_name)

    @command(dtype_in=('str',), doc_in='Str[0] = Tango class\nStr[1] = Property name', dtype_out=('str',), doc_out='Str[0] = Property name\nStr[1] = date\nStr[2] = Property value number (array case)\nStr[3] = Property value 1\nStr[n] = Property value n')
    def DbGetClassPropertyHist(self, argin):
        """ Retrieve Tango class property history

        :param argin: Str[0] = Tango class
        Str[1] = Property name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Property name
        Str[1] = date
        Str[2] = Property value number (array case)
        Str[3] = Property value 1
        Str[n] = Property value n
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetClassPropertyHist()")
        class_name = argin[0]
        prop_name = argin[1]
        return self.db.get_class_property_hist(class_name, prop_name)

    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[1] = Attribute name', doc_out='none')
    def DbDeleteDeviceAttribute(self, argin):
        """ Delete  device attribute properties from database

        :param argin: Str[0] = Device name
        Str[1] = Attribute name
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteDeviceAttribute()")

        if len(argin) < 2:
            self.warn_stream("DataBase::db_delete_device_attribute(): insufficient number of arguments ")
            th_exc(DB_IncorrectArguments,
                   "insufficient number of arguments to delete device attribute",
                   "DataBase::DeleteDeviceAttribute()")

        dev_name, attr_name = argin[:2]

        ret, dev_name, dfm = check_device_name(argin)
        if not ret:
            self.warn_stream("DataBase::db_delete_device_attribute(): device name " + argin + " incorrect ")
            th_exc(DB_IncorrectDeviceName,
                   "failed to delete device attribute, device name incorrect",
                   "DataBase::DeleteDeviceAttribute()")

        self.db.delete_device_attribute(dev_name, attr_name)

    @stats
    @command(dtype_in='str', doc_in='MySql Select command', dtype_out='DevVarLongStringArray', doc_out='MySql Select command result\n - svalues : select results\n - lvalue[n] : =0 if svalue[n] is null else =1\n (last lvalue -1) is number of rows, (last lvalue) is number of fields')
    def DbMySqlSelect(self, argin):
        """ This is a very low level command.
        It executes the specified  SELECT command on TANGO database and returns its result without filter.

        :param argin: MySql Select command
        :type: PyTango.DevString
        :return: MySql Select command result
         - svalues : select results
         - lvalue[n] : =0 if svalue[n] is null else =1
         (last lvalue -1) is number of rows, (last lvalue) is number of fields
        :rtype: PyTango.DevVarLongStringArray """
        self._log.debug("In DbMySqlSelect()")
        tmp_argin = argin.lower()

        #  Check if SELECT key is alread inside command

        cmd = argin
        tmp_argin = argin.lower()
        pos = tmp_argin.find('select')
        if pos == -1:
            cmd = "SELECT " + cmd

        pos = tmp_argin.find(';')
        if pos != -1 and len(tmp_argin) > (pos + 1):
            th_exc(DB_IncorrectArguments,
                   "SQL command not valid: " + argin,
                   "DataBase::ExportDevice()")
        return self.db.my_sql_select(cmd)

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[1] = Attribute number\nStr[2] = Attribute name\nStr[3] = Property number\nStr[4] = Property name\nStr[5] = Property value\n.....', doc_out='none')
    def DbPutDeviceAttributeProperty(self, argin):
        """ Create/Update device attribute property(ies) in database

        :param argin: Str[0] = Device name
        Str[1] = Attribute number
        Str[2] = Attribute name
        Str[3] = Property number
        Str[4] = Property name
        Str[5] = Property value
        .....
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbPutDeviceAttributeProperty()")
        device_name = argin[0]
        nb_attributes = int(argin[1])
        self.db.put_device_attribute_property(device_name, nb_attributes, argin[2:])

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[1] = Attribute name\nStr[n] = Attribute name', dtype_out=('str',), doc_out='Str[0] = Device name\nStr[1] = Attribute property  number\nStr[2] = Attribute property 1 name\nStr[3] = Attribute property 1 value\nStr[n + 1] = Attribute property 2 name\nStr[n + 2] = Attribute property 2 value')
    def DbGetDeviceAttributeProperty(self, argin):
        """ Get device attribute property(ies) value

        :param argin: Str[0] = Device name
        Str[1] = Attribute name
        Str[n] = Attribute name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Device name
        Str[1] = Attribute property  number
        Str[2] = Attribute property 1 name
        Str[3] = Attribute property 1 value
        Str[n + 1] = Attribute property 2 name
        Str[n + 2] = Attribute property 2 value
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceAttributeProperty()")
        dev_name = argin[0]
        return self.db.get_device_attribute_property(dev_name, argin[1:])

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = Object name\nStr[1] = filter', dtype_out=('str',), doc_out='Property name list')
    def DbGetPropertyList(self, argin):
        """ Get list of property defined for a free object and matching the
        specified filter

        :param argin: Str[0] = Object name
        Str[1] = filter
        :type: PyTango.DevVarStringArray
        :return: Property name list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetPropertyList()")
        object_name = argin[0]
        wildcard = replace_wildcard(argin[1])
        return self.db.get_property_list(object_name, wildcard)

    @stats
    @command(dtype_in='str', doc_in='Device server process name', dtype_out=('str',), doc_out='Str[0] = Device name\nStr[1] = Tango class\nStr[n] = Device name\nStr[n + 1] = Tango class')
    def DbGetDeviceClassList(self, argin):
        """ Get Tango classes/device list embedded in a specific device server

        :param argin: Device server process name
        :type: PyTango.DevString
        :return: Str[0] = Device name
        Str[1] = Tango class
        Str[n] = Device name
        Str[n + 1] = Tango class
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceClassList()")
        return self.db.get_device_class_list(argin)

    @command(dtype_in='str', doc_in='Device name', doc_out='none')
    def DbUnExportDevice(self, argin):
        """ Mark a device as non exported in database

        :param argin: Device name
        :type: PyTango.DevString
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbUnExportDevice()")
        dev_name = argin[0].lower()
        self.db.unexport_device(dev_name)

    @command(dtype_in='str', doc_in='Alias name', dtype_out='str', doc_out='Device name')
    def DbGetAliasDevice(self, argin):
        """ Get device name from its alias.

        :param argin: Alias name
        :type: PyTango.DevString
        :return: Device name
        :rtype: PyTango.DevString """
        self._log.debug("In DbGetAliasDevice()")
        if not argin:
            argin = "%"
        else:
            argin = replace_wildcard(argin)
        return self.db.get_alias_device(argin)

    @command(dtype_in='str', doc_in='device name', doc_out='none')
    def DbDeleteDevice(self, argin):
        """ Delete a devcie from database

        :param argin: device name
        :type: PyTango.DevString
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteDevice()")

        ret, dev_name, dfm = check_device_name(argin)
        if not ret:
            self.warn_stream("DataBase::db_delete_device(): device name " + argin + " incorrect ")
            th_exc(DB_IncorrectDeviceName,
                   "failed to delete device, device name incorrect",
                   "DataBase::DeleteDevice()")
        self.db.delete_device(dev_name)

    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[1] = Wildcard', dtype_out=('str',), doc_out='attribute name list')
    def DbGetDeviceAttributeList(self, argin):
        """ Return list of attributes matching the wildcard
         for the specified device

        :param argin: Str[0] = Device name
        Str[1] = Wildcard
        :type: PyTango.DevVarStringArray
        :return: attribute name list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceAttributeList()")
        dev_name = argin[0]
        wildcard = argin[1]
        if not wildcard:
            wildcard = "%"
        else:
            wildcard = replace_wildcard(wildcard)
        return self.db.get_device_attribute_list(dev_name, wildcard)

    @command(dtype_in='str', doc_in='Host name', dtype_out=('str',), doc_out='Server info for all servers running on specified host')
    def DbGetHostServersInfo(self, argin):
        """ Get info about all servers running on specified host, name, mode and level

        :param argin: Host name
        :type: PyTango.DevString
        :return: Server info for all servers running on specified host
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetHostServersInfo()")
        argin = replace_wildcard(argin)
        return self.db.get_host_servers_info(argin)

    @command(dtype_in=('str',), doc_in='s[0] = old device server name (exec/instance)\ns[1] = new device server name (exec/instance)')
    def DbRenameServer(self, argin):
        """ Rename a device server process

        :param argin: str[0] = old device server name (exec/instance)
        str[1] =  new device server name (exec/instance)
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbRenameServer()")

        if len(argin) < 2:
            self.warn_stream("DataBase::DbRenameServer(): insufficient number of arguments ")
            th_exc(DB_IncorrectArguments,
                   "insufficient number of arguments (two required: old name and new name",
                   "DataBase::DbRenameServer")

        old_name = argin[0]
        new_name = argin[1]

        if ('/' not in argin[0]) or ('/' not in argin[1]):
            self.warn_stream("DataBase::DbRenameServer(): wrong syntax in command args ")
            th_exc(DB_IncorrectArguments,
                   "Wrong syntax in command args (ds_exec_name/inst_name)",
                   "DataBase::DbRenameServer")

        self.db.rename_server(old_name, new_name)

    @stats
    @command(dtype_in='str', doc_in='The filter', dtype_out=('str',), doc_out='Host name list')
    def DbGetHostList(self, argin):
        """ Get host list with name matching the specified filter

        :param argin: The filter
        :type: PyTango.DevString
        :return: Host name list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetHostList()")
        argin = replace_wildcard(argin)
        return self.db.get_host_list(argin)

    @command(dtype_in='str', doc_in='Device name', dtype_out=('str',), doc_out='Classes off the specified device.\n[0] - is the class of the device.\n[1] - is the class from the device class is inherited.\n........and so on')
    def DbGetClassInheritanceForDevice(self, argin):
        """ Get class inheritance for the specified device.

        :param argin: Device name
        :type: PyTango.DevString
        :return: Classes off the specified device.
        [0] - is the class of the device.
        [1] - is the class from the device class is inherited.
        ........and so on
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetClassInheritanceForDevice()")
        return self.db.get_class_inheritance_for_device(argin)

    @stats
    @command(dtype_in='str', doc_in='Device server name', doc_out='none')
    def DbDeleteServer(self, argin):
        """ Delete server from the database but dont delete device properties

        :param argin: Device server name
        :type: PyTango.DevString
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteServer()")

        if '*' in argin or '%' in argin or not '/' in argin:
            self.warn_stream("DataBase::db_delete_server(): server name " + argin + " incorrect ")
            th_exc(DB_IncorrectServerName,
                   "failed to delete server, server name incorrect",
                   "DataBase::DeleteServer()")

        self.db.delete_server(argin)

    @command(dtype_in='str', doc_in='The attribute alias name', dtype_out='str', doc_out='The attribute name (device/attribute)')
    def DbGetAttributeAlias(self, argin):
        """ Get the attribute name for the given alias.
        If alias not found in database, returns an empty string.

        :param argin: The attribute alias name
        :type: PyTango.DevString
        :return: The attribute name (device/attribute)
        :rtype: PyTango.DevString """
        self._log.debug("In DbGetAttributeAlias()")
        return self.db.get_attribute_alias(argin)

    @command(dtype_in=('str',), doc_in='Elt[0] = DS name (exec_name/inst_name), Elt[1] = Host name', dtype_out=('str',), doc_out='All the data needed by the device server during its startup sequence. Precise list depend on the device server')
    def DbGetDataForServerCache(self, argin):
        """ This command returns all the data needed by a device server process during its
        startup sequence. The aim of this command is to minimize database access during
        device server startup sequence.

        :param argin: Elt[0] = DS name (exec_name/inst_name), Elt[1] = Host name
        :type: PyTango.DevVarStringArray
        :return: All the data needed by the device server during its startup sequence. Precise list depend on the device server
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDataForServerCache()")
        ##  TODO
        return ['']

    @command(dtype_in=('str',), doc_in='Str[0] = Object name\nStr[1] = Property name\nStr[n] = Property name', dtype_out=('str',), doc_out='Str[0] = Object name\nStr[1] = Property number\nStr[2] = Property name\nStr[3] = Property value number (array case)\nStr[4] = Property value 1\nStr[n] = Property value n (array case)\nStr[n + 1] = Property name\nStr[n + 2] = Property value number (array case)\nStr[n + 3] = Property value 1\nStr[n + m] = Property value m')
    def DbGetProperty(self, argin):
        """ Get free object property

        :param argin: Str[0] = Object name
        Str[1] = Property name
        Str[n] = Property name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Object name
        Str[1] = Property number
        Str[2] = Property name
        Str[3] = Property value number (array case)
        Str[4] = Property value 1
        Str[n] = Property value n (array case)
        Str[n + 1] = Property name
        Str[n + 2] = Property value number (array case)
        Str[n + 3] = Property value 1
        Str[n + m] = Property value m
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetProperty()")
        object_name = argin[0]
        return self.db.get_property(object_name, argin[1:])

    @command(dtype_in='str', doc_in='device server process name', dtype_out=('str',), doc_out='list of classes for this device server')
    def DbGetDeviceServerClassList(self, argin):
        """ Get list of Tango classes for a device server

        :param argin: device server process name
        :type: PyTango.DevString
        :return: list of classes for this device server
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceServerClassList()")
        argin = replace_wildcard(argin)
        return self.db.get_server_class_list(argin)

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = Tango device name\nStr[1] = Property number\nStr[2] = Property name\nStr[3] = Property value number\nStr[4] = Property value 1\nStr[n] = Property value n\n....', doc_out='none')
    def DbPutDeviceProperty(self, argin):
        """ Create / Update device property(ies)

        :param argin: Str[0] = Tango device name
        Str[1] = Property number
        Str[2] = Property name
        Str[3] = Property value number
        Str[4] = Property value 1
        Str[n] = Property value n
        ....
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbPutDeviceProperty()")
        device_name = argin[0]
        nb_properties = int(argin[1])
        self.db.put_device_property(device_name, nb_properties, argin[2:])

    @command(doc_in='none', doc_out='none')
    def ResetTimingValues(self):
        """ Reset the timing attribute values.

        :param :
        :type: PyTango.DevVoid
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In ResetTimingValues()")
        for tmp_timing in self.timing_maps.itervalues():
            tmp_timing.average = 0.
            tmp_timing.minimum = 0.
            tmp_timing.maximum = 0.
            tmp_timing.total_elapsed = 0.
            tmp_timing.calls = 0.

    @command(doc_in='none', dtype_out=('str',), doc_out='List of host:port with one element for each database server')
    def DbGetCSDbServerList(self):
        """ Get a list of host:port for all database server defined in the control system

        :param :
        :type: PyTango.DevVoid
        :return: List of host:port with one element for each database server
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetCSDbServerList()")
        return self.db.get_csdb_server_list()

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = Tango class name\nStr[1] = Property number\nStr[2] = Property name\nStr[3] = Property value number\nStr[4] = Property value 1\nStr[n] = Property value n\n....', doc_out='none')
    def DbPutClassProperty(self, argin):
        """ Create / Update class property(ies)

        :param argin: Str[0] = Tango class name
        Str[1] = Property number
        Str[2] = Property name
        Str[3] = Property value number
        Str[4] = Property value 1
        Str[n] = Property value n
        ....
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbPutClassProperty()")
        class_name = argin[0]
        nb_properties = int(argin[1])
        self.db.put_class_property(class_name, properties, argin[2:])

    @stats
    @command(dtype_in='str', doc_in='Device name (or alias)', dtype_out='DevVarLongStringArray', doc_out='Str[0] = device name\nStr[1] = CORBA IOR\nStr[2] = device version\nStr[3] = device server process name\nStr[4] = host name\nStr[5] = Tango class name\n\nLg[0] = Exported flag\nLg[1] = Device server process PID')
    def DbImportDevice(self, argin):
        """ Import a device from the database

        :param argin: Device name (or alias)
        :type: PyTango.DevString
        :return: Str[0] = device name
        Str[1] = CORBA IOR
        Str[2] = device version
        Str[3] = device server process name
        Str[4] = host name
        Str[5] = Tango class name

        Lg[0] = Exported flag
        Lg[1] = Device server process PID
        :rtype: PyTango.DevVarLongStringArray """
        self._log.debug("In DbImportDevice()")
        return self.db.import_device(argin.lower())

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[1] = Property name\nStr[n] = Property name', doc_out='none')
    def DbDeleteDeviceProperty(self, argin):
        """ Delete device property(ies)

        :param argin: Str[0] = Device name
        Str[1] = Property name
        Str[n] = Property name
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteDeviceProperty()")
        dev_name = argin[0]
        for prop_name in argin[1:]:
            self.db.delete_device_property(dev_name, prop_name)

    @command(dtype_in='str', doc_in='Device name', dtype_out='str', doc_out='Device Tango class')
    def DbGetClassForDevice(self, argin):
        """ Get Tango class for the specified device.

        :param argin: Device name
        :type: PyTango.DevString
        :return: Device Tango class
        :rtype: PyTango.DevString """
        self._log.debug("In DbGetClassForDevice()")
        return self.db.get_class_for_device(argin)

    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[1] = Attribute name\nStr[2] = Property name', dtype_out=('str',), doc_out='Str[0] = Attribute name\nStr[1] = Property name\nStr[2] = date\nStr[3] = Property value number (array case)\nStr[4] = Property value 1\nStr[n] = Property value n')
    def DbGetDeviceAttributePropertyHist(self, argin):
        """ Retrieve device attribute property history

        :param argin: Str[0] = Device name
        Str[1] = Attribute name
        Str[2] = Property name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Attribute name
        Str[1] = Property name
        Str[2] = date
        Str[3] = Property value number (array case)
        Str[4] = Property value 1
        Str[n] = Property value n
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceAttributePropertyHist()")
        dev_name = argin[0]
        attribute = replace_wildcard(argin[1])
        prop_name = replace_wildcard(argin[2])
        return self.db.get_device_attribute_property_hist(dev_name, attribute, prop_name)

    @command(dtype_in='str', doc_in='server name', dtype_out=('str',), doc_out='server info')
    def DbGetServerInfo(self, argin):
        """ Get info about host, mode and level for specified server

        :param argin: server name
        :type: PyTango.DevString
        :return: server info
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetServerInfo()")
        return self.db.get_server_info(argin)

    @command(dtype_in=('str',), doc_in='Str[0] = device name\nStr[1] = alias name', doc_out='none')
    def DbPutDeviceAlias(self, argin):
        """ Define alias for  a given device name

        :param argin: Str[0] = device name
        Str[1] = alias name
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbPutDeviceAlias()")

        if len(argin) < 2:
            self.warn_stream("DataBase::DbPutDeviceAlias(): insufficient number of arguments ")
            th_exc(DB_IncorrectArguments,
                   "insufficient number of arguments to put device alias",
                   "DataBase::DbPutDeviceAlias()")

        device_name = argin[0]
        device_alias = argin[1]
        self.db.put_device_alias(device_name, device_alias)

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = device name\nStr[1] = Filter', dtype_out=('str',), doc_out='Property name list')
    def DbGetDevicePropertyList(self, argin):
        """ Get property list belonging to the specified device and with
        name matching the specified filter

        :param argin: Str[0] = device name
        Str[1] = Filter
        :type: PyTango.DevVarStringArray
        :return: Property name list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDevicePropertyList()")
        device_name = argin[0]
        prop_filter = argin[1]
        prop_filter = replace_wildcard(prop_filter)
        return self.db.get_device_property_list(device_name, prop_filter)

    @stats
    @command(dtype_in='str', doc_in='The filter', dtype_out=('str',), doc_out='Device server process name list')
    def DbGetHostServerList(self, argin):
        """ Get list of device server process name running on host with name matching
        the specified filter

        :param argin: The filter
        :type: PyTango.DevString
        :return: Device server process name list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetHostServerList()")
        argin = replace_wildcard(argin)
        return self.db.get_host_server_list(argin)

    @command(dtype_in=('str',), doc_in='Str[0] = Tango class\nStr[1] = Property name\nStr[2] = Property name', dtype_out=('str',), doc_out='Str[0] = Tango class\nStr[1] = Property number\nStr[2] = Property name\nStr[3] = Property value number (array case)\nStr[4] = Property value\nStr[n] = Propery value (array case)\n....')
    def DbGetClassProperty(self, argin):
        """

        :param argin: Str[0] = Tango class
        Str[1] = Property name
        Str[2] = Property name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Tango class
        Str[1] = Property number
        Str[2] = Property name
        Str[3] = Property value number (array case)
        Str[4] = Property value
        Str[n] = Propery value (array case)
        ....
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetClassProperty()")
        class_name = argin[0]
        return self.db.get_class_property(class_name,argin[1:])

    @command(dtype_in='str', doc_in='The filter', dtype_out=('str',), doc_out='Object name list')
    def DbGetObjectList(self, argin):
        """ Get list of free object defined in database with name
        matching the specified filter

        :param argin: The filter
        :type: PyTango.DevString
        :return: Object name list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetObjectList()")
        argin = replace_wildcard(argin)
        return self.db.get_object_list(argin)

    @command(dtype_in=('str',), doc_in='Str[0] = Tango class name\nStr[1] = Attribute name\nStr[2] = Property name\nStr[n] = Property name', doc_out='none')
    def DbDeleteClassAttributeProperty(self, argin):
        """ delete class attribute properties from database

        :param argin: Str[0] = Tango class name
        Str[1] = Attribute name
        Str[2] = Property name
        Str[n] = Property name
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteClassAttributeProperty()")

        if len(argin) < 3:
            self.warn_stream("DataBase::db_delete_class_attribute_property(): insufficient number of arguments ")
            th_exc(DB_IncorrectArguments,
                   "insufficient number of arguments to delete class attribute property",
                   "DataBase::DeleteClassAttributeProperty()")

        klass_name, attr_name = argin[:2]

        for prop_name in argin[2:]:
            self.db.delete_class_attribute_property(klass_name, attr_name, prop_name)

    @command(dtype_in='str', doc_in='Server name', dtype_out=('str',), doc_out='The instance names found for specified server.')
    def DbGetInstanceNameList(self, argin):
        """ Returns the instance names found for specified server.

        :param argin: Server name
        :type: PyTango.DevString
        :return: The instance names found for specified server.
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetInstanceNameList()")
        return self.db.get_instance_name_list(argin)

    @command(dtype_in='str', doc_in='The attribute name (dev_name/att_name)', dtype_out='str', doc_out='The attribute alias name (or empty string)')
    def DbGetAttributeAlias2(self, argin):
        """ Get the attribute alias from the attribute name.
        Returns one empty string if nothing found in database

        :param argin: The attribute name (dev_name/att_name)
        :type: PyTango.DevString
        :return: The attribute alias name (or empty string)
        :rtype: PyTango.DevString """
        self._log.debug("In DbGetAttributeAlias2()")
        attr_name = argin[0]
        return self.db.get_attribute_alias2(attr_name)

    @command(dtype_in=('str',), doc_in='Str[0] = Full device server name\nStr[1] = Device(s) name\nStr[2] = Tango class name\nStr[n] = Device name\nStr[n + 1] = Tango class name', doc_out='none')
    def DbAddServer(self, argin):
        """ Create a device server process entry in database

        :param argin: Str[0] = Full device server name
        Str[1] = Device(s) name
        Str[2] = Tango class name
        Str[n] = Device name
        Str[n + 1] = Tango class name
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbAddServer()")

        if len(argin) < 3 or not len(argin) % 2:
            self.warn_stream("DataBase::AddServer(): incorrect number of input arguments ")
            th_exc(DB_IncorrectArguments,
                   "incorrect no. of input arguments, needs at least 3 (server,device,class)",
                   "DataBase::AddServer()")
        server_name = argin[0]

        for i in range((len(argin) - 1) / 2):
            d_name, klass_name = argin[i * 2 + 1], argin[i * 2 + 2]
            ret, dev_name, dfm = check_device_name(d_name)
            if not ret:
                th_exc(DB_IncorrectDeviceName,
                      "device name (" + d_name + ") syntax error (should be [tango:][//instance/]domain/family/member)",
                      "DataBase::AddServer()")
            self.db.add_device(server_name, (dev_name, dfm) , klass_name)

    @stats
    @command(dtype_in='str', doc_in='name of event channel or factory', dtype_out='DevVarLongStringArray', doc_out='export information e.g. IOR')
    def DbImportEvent(self, argin):
        """ Get event channel info from database

        :param argin: name of event channel or factory
        :type: PyTango.DevString
        :return: export information e.g. IOR
        :rtype: PyTango.DevVarLongStringArray """
        self._log.debug("In DbImportEvent()")
        argin = replace_wildcard(argin.lower())
        return self.db.import_event(argin)

    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[2] = Property name', dtype_out=('str',), doc_out='Str[0] = Property name\nStr[1] = date\nStr[2] = Property value number (array case)\nStr[3] = Property value 1\nStr[n] = Property value n')
    def DbGetDevicePropertyHist(self, argin):
        """ Retrieve device  property history

        :param argin: Str[0] = Device name
        Str[1] = Property name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Property name
        Str[1] = date
        Str[2] = Property value number (array case)
        Str[3] = Property value 1
        Str[n] = Property value n
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDevicePropertyHist()")
        device_name = argin[0]
        prop_name = argin[1]
        return self.db.get_device_property_hist(device_name, prop_name)

    @command(dtype_in='str', doc_in='wildcard for server names.', dtype_out=('str',), doc_out='server names found.')
    def DbGetServerNameList(self, argin):
        """ Returns the list of server names found for the wildcard specified.
        It returns only the server executable name without instance name as DbGetServerList.

        :param argin: wildcard for server names.
        :type: PyTango.DevString
        :return: server names found.
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetServerNameList()")
        argin = replace_wildcard(argin)
        return self.db.get_server_name_list(argin)

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[1] = Attribute name\nStr[n] = Attribute name', dtype_out=('str',), doc_out='Str[0] = Device name\nStr[1] = Attribute property  number\nStr[2] = Attribute property 1 name\nStr[3] = Attribute property 1 value number (array case)\nStr[4] = Attribute property 1 value\nStr[n] = Attribute property 1 value (array case)\nStr[n + 1] = Attribute property 2 name\nStr[n + 2] = Attribute property 2 value number (array case)\nStr[n + 3] = Attribute property 2 value\nStr[n + m] = Attribute property 2 value (array case)')
    def DbGetDeviceAttributeProperty2(self, argin):
        """ Retrieve device attribute properties. This command has the possibility to retrieve
        device attribute properties which are arrays. It is not possible with the old
        DbGetDeviceAttributeProperty command. Nevertheless, the old command has not been
        deleted for compatibility reason

        :param argin: Str[0] = Device name
        Str[1] = Attribute name
        Str[n] = Attribute name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Device name
        Str[1] = Attribute property  number
        Str[2] = Attribute property 1 name
        Str[3] = Attribute property 1 value number (array case)
        Str[4] = Attribute property 1 value
        Str[n] = Attribute property 1 value (array case)
        Str[n + 1] = Attribute property 2 name
        Str[n + 2] = Attribute property 2 value number (array case)
        Str[n + 3] = Attribute property 2 value
        Str[n + m] = Attribute property 2 value (array case)
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceAttributeProperty2()")
        dev_name = argin[0]
        return self.db.get_device_attribute_property2(dev_name, argin[1:])

    @command(dtype_in=('str',), doc_in='Str[0] = Tango class name\nStr[1] = Property name\nStr[n] = Property name', doc_out='none')
    def DbDeleteClassProperty(self, argin):
        """ Delete class properties from database

        :param argin: Str[0] = Tango class name
        Str[1] = Property name
        Str[n] = Property name
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteClassProperty()")
        klass_name = argin[0]
        for prop_name in argin[1:]:
            self.db.delete_class_property(prop_name)

    @command(dtype_in='str', doc_in='name of event channel or factory to unexport', doc_out='none')
    def DbUnExportEvent(self, argin):
        """ Mark one event channel as non exported in database

        :param argin: name of event channel or factory to unexport
        :type: PyTango.DevString
        :return: none
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbUnExportEvent()")
        event_name = argin[0].lower()
        self.db.unexport_event(event_name)

    @stats
    @command(doc_in='none', dtype_out=('str',), doc_out='Miscellaneous info like:\n- Device defined in database\n- Device marked as exported in database\n- Device server process defined in database\n- Device server process marked as exported in database\n- Device properties defined in database\n- Class properties defined in database\n- Device attribute properties defined in database\n- Class attribute properties defined in database\n- Object properties defined in database')
    def DbInfo(self):
        """ Get miscellaneous numbers on information
        stored in database

        :param :
        :type: PyTango.DevVoid
        :return: Miscellaneous info like:
        - Device defined in database
        - Device marked as exported in database
        - Device server process defined in database
        - Device server process marked as exported in database
        - Device properties defined in database
        - Class properties defined in database
        - Device attribute properties defined in database
        - Class attribute properties defined in database
        - Object properties defined in database
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbInfo()")
        return self.db.info()

    @command(dtype_in=('str',), doc_in='Str[0] = Tango class name\nStr[1] = Attribute name\nStr[n] = Attribute name', dtype_out=('str',), doc_out='Str[0] = Tango class name\nStr[1] = Attribute property  number\nStr[2] = Attribute property 1 name\nStr[3] = Attribute property 1 value\nStr[n + 1] = Attribute property 2 name\nStr[n + 2] = Attribute property 2 value')
    def DbGetClassAttributeProperty(self, argin):
        """ Get Tango class property(ies) value

        :param argin: Str[0] = Tango class name
        Str[1] = Attribute name
        Str[n] = Attribute name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Tango class name
        Str[1] = Attribute property  number
        Str[2] = Attribute property 1 name
        Str[3] = Attribute property 1 value
        Str[n + 1] = Attribute property 2 name
        Str[n + 2] = Attribute property 2 value
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetClassAttributeProperty()")
        class_name = argin[0]
        return self.db.get_class_attribute_property(class_name, argin[1:])

    @command(dtype_in=('str',), doc_in='Str[0] = Tango class name\nStr[1] = Attribute number\nStr[2] = Attribute name\nStr[3] = Property number\nStr[4] = Property name\nStr[5] = Property value number (array case)\nStr[5] = Property value 1\nStr[n] = Property value n (array case)\n.....', doc_out='none')
    def DbPutClassAttributeProperty2(self, argin):
        """ This command adds support for array properties compared to the previous one
        called DbPutClassAttributeProperty. The old comman is still there for compatibility reason

        :param argin: Str[0] = Tango class name
        Str[1] = Attribute number
        Str[2] = Attribute name
        Str[3] = Property number
        Str[4] = Property name
        Str[5] = Property value number (array case)
        Str[5] = Property value 1
        Str[n] = Property value n (array case)
        .....
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbPutClassAttributeProperty2()")
        class_name = argin[0]
        nb_attributes = int(argin[1])
        self.db.put_class_attribute_property2(class_name, nb_attributes, argin[2:])

    @command(dtype_in=('str',), doc_in='server info', doc_out='none')
    def DbPutServerInfo(self, argin):
        """ Update server info including host, mode and level

        :param argin: server info
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbPutServerInfo()")

        if len(argin) < 4:
            self.warn_stream("DataBase::DbPutServerInfo(): insufficient number of arguments ")
            th_exc(DB_IncorrectArguments,
                   "insufficient server info",
                   "DataBase::DbPutServerInfo()")

        tmp_server = argin[0].lower()
        tmp_host = argin[1]
        tmp_mode = argin[2]
        tmp_level = argin[3]
        tmp_extra = []
        if len(argin) > 4:
            tmp_extra = argin[4:]

        tmp_len = len(argin) - 1
        self.db.put_server_info(tmp_server, tmp_host, tmp_mode, tmp_level, tmp_extra)

    @command(dtype_in='str', doc_in='device alias name', doc_out='none')
    def DbDeleteDeviceAlias(self, argin):
        """ Delete a device alias.

        :param argin: device alias name
        :type: PyTango.DevString
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteDeviceAlias()")
        self.db.delete_device_alias(argin)

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = event channel name (or factory name)\nStr[1] = CORBA IOR\nStr[2] = Notifd host name\nStr[3] = Notifd pid\nStr[4] = Notifd version', doc_out='none')
    def DbExportEvent(self, argin):
        """ Export Event channel to database

        :param argin: Str[0] = event channel name (or factory name)
        Str[1] = CORBA IOR
        Str[2] = Notifd host name
        Str[3] = Notifd pid
        Str[4] = Notifd version
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbExportEvent()")

        if len(argin) < 5:
            self.warn_stream("DataBase::db_export_event(): insufficient export info for event ")
            th_exc(DB_IncorrectArguments,
                   "insufficient export info for event",
                   "DataBase::ExportEvent()")

        event, IOR, host, pid, version = argin[:5]
        event = replace_wildcard(event.lower())
        self.db.export_event(event, IOR, host, pid, version)

    @stats
    @command(dtype_in=('str',), doc_in='Str[0] = Device name\nStr[1] = Property name\nStr[n] = Property name', dtype_out=('str',), doc_out='Str[0] = Device name\nStr[1] = Property number\nStr[2] = Property name\nStr[3] = Property value number (array case)\nStr[4] = Property value 1\nStr[n] = Property value n (array case)\nStr[n + 1] = Property name\nStr[n + 2] = Property value number (array case)\nStr[n + 3] = Property value 1\nStr[n + m] = Property value m')
    def DbGetDeviceProperty(self, argin):
        """

        :param argin: Str[0] = Device name
        Str[1] = Property name
        Str[n] = Property name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Device name
        Str[1] = Property number
        Str[2] = Property name
        Str[3] = Property value number (array case)
        Str[4] = Property value 1
        Str[n] = Property value n (array case)
        Str[n + 1] = Property name
        Str[n + 2] = Property value number (array case)
        Str[n + 3] = Property value 1
        Str[n + m] = Property value m
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceProperty()")
        device_name = argin[0]
        return self.db.get_device_property(device_name, argin[1:])

    @command(dtype_in='str', doc_in='Device name', dtype_out='DevVarLongStringArray', doc_out='Str[0] = Device name\nStr[1] = CORBA IOR\nStr[2] = Device version\nStr[3] = Device Server name\nStr[4] = Device Server process host name\nStr[5] = Started date (or ? if not set)\nStr[6] = Stopped date (or ? if not set)\nStr[7] = Device class\n\nLg[0] = Device exported flag\nLg[1] = Device Server process PID (or -1 if not set)')
    def DbGetDeviceInfo(self, argin):
        """ Returns info from DbImportDevice and started/stopped dates.

        :param argin: Device name
        :type: PyTango.DevString
        :return: Str[0] = Device name
        Str[1] = CORBA IOR
        Str[2] = Device version
        Str[3] = Device Server name
        Str[4] = Device Server process host name
        Str[5] = Started date (or ? if not set)
        Str[6] = Stopped date (or ? if not set)
        Str[7] = Device class

        Lg[0] = Device exported flag
        Lg[1] = Device Server process PID (or -1 if not set)
        :rtype: PyTango.DevVarLongStringArray """
        self._log.debug("In DbGetDeviceInfo()")
        ret, dev_name, dfm = check_device_name(argin)
        if not ret:
            th_exc(DB_IncorrectDeviceName,
                  "device name (" + argin + ") syntax error (should be [tango:][//instance/]domain/family/member)",
                  "DataBase::DbGetDeviceAlias()")

        return self.db.get_device_info(dev_name)

    @command(dtype_in=('str',), doc_in='Str[0] = Object name\nStr[2] = Property name', dtype_out=('str',), doc_out='Str[0] = Property name\nStr[1] = date\nStr[2] = Property value number (array case)\nStr[3] = Property value 1\nStr[n] = Property value n')
    def DbGetPropertyHist(self, argin):
        """ Retrieve object  property history

        :param argin: Str[0] = Object name
        Str[2] = Property name
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Property name
        Str[1] = date
        Str[2] = Property value number (array case)
        Str[3] = Property value 1
        Str[n] = Property value n
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetPropertyHist()")
        object_name = argin[0]
        prop_name = argin[1]
        return self.db.get_property_hist(object_name, prop_name)

    @stats
    @command(dtype_in='str', doc_in='The filter', dtype_out=('str',), doc_out='Device names member list')
    def DbGetDeviceMemberList(self, argin):
        """ Get a list of device name members for device name matching the
        specified filter

        :param argin: The filter
        :type: PyTango.DevString
        :return: Device names member list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceMemberList()")
        argin = replace_wildcard(argin)
        return self.db.get_device_member_list(argin)

    @command(dtype_in='str', doc_in='Filter', dtype_out=('str',), doc_out='Class list')
    def DbGetClassList(self, argin):
        """ Get Tango class list with a specified filter

        :param argin: Filter
        :type: PyTango.DevString
        :return: Class list
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetClassList()")
        server = replace_wildcard(argin)
        return self.db.get_class_list(server)

    @command(dtype_in='str', doc_in='The attribute alias', dtype_out='str', doc_out='The attribute name (dev_name/att_name)')
    def DbGetAliasAttribute(self, argin):
        """ Get the attribute name from the given alias.
        If the given alias is not found in database, returns an empty string

        :param argin: The attribute alias
        :type: PyTango.DevString
        :return: The attribute name (dev_name/att_name)
        :rtype: PyTango.DevString """
        self._log.debug("In DbGetAliasAttribute()")
        alias_name = argin[0]
        return self.db.get_alias_attribute(alias_name)

    @command(dtype_in='str', doc_in='Device server name', doc_out='none')
    def DbDeleteServerInfo(self, argin):
        """ delete info related to a Tango devvice server process

        :param argin: Device server name
        :type: PyTango.DevString
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbDeleteServerInfo()")
        self.db.delete_server_info(argin)

    @command(dtype_in=('str',), doc_in='Str[0] = Tango class name\nStr[1] = Attribute name filter (eg: att*)', dtype_out=('str',), doc_out='Str[0] = Class attribute name\nStr[n] = Class attribute name')
    def DbGetClassAttributeList(self, argin):
        """ Get attrilute list for a given Tango class with a specified filter

        :param argin: Str[0] = Tango class name
        Str[1] = Attribute name filter (eg: att*)
        :type: PyTango.DevVarStringArray
        :return: Str[0] = Class attribute name
        Str[n] = Class attribute name
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetClassAttributeList()")
        class_name = argin[0]
        wildcard = replace_wildcard(argin[1])
        return self.db.get_class_attribute_list(class_name, wildcard)

    @command(dtype_in=('str',), doc_in='Str[0] = Full device server process name\nStr[1] = Device name\nStr[2] = Tango class name', doc_out='none')
    def DbAddDevice(self, argin):
        """ Add a Tango class device to a specific device server

        :param argin: Str[0] = Full device server process name
        Str[1] = Device name
        Str[2] = Tango class name
        :type: PyTango.DevVarStringArray
        :return:
        :rtype: PyTango.DevVoid """
        self._log.debug("In DbAddDevice()")

        if len(argin) < 3:
            self.warn_stream("DataBase::AddDevice(): incorrect number of input arguments ")
            th_exc(DB_IncorrectArguments,
                   "incorrect no. of input arguments, needs at least 3 (server,device,class)",
                   "DataBase::AddDevice()")

        self.info_stream("DataBase::AddDevice(): insert %s server with device %s",argin[0],argin[1])
        server_name, d_name, klass_name = argin[:3]
        if len(argin) > 3:
            alias = argin[3]
        else:
            alias = None

        ret, dev_name, dfm = check_device_name(d_name)
        if not ret:
            th_exc(DB_IncorrectDeviceName,
                  "device name (" + d_name + ") syntax error (should be [tango:][//instance/]domain/family/member)",
                  "DataBase::AddDevice()")
        # Lock table
        self.db.add_device(server_name, (dev_name, dfm) , klass_name, alias=alias)

    @command(dtype_in=('str',), doc_in='argin[0] : server name\nargin[1] : class name', dtype_out=('str',), doc_out='The list of devices for specified server and class.')
    def DbGetDeviceList(self, argin):
        """ Get a list of devices for specified server and class.

        :param argin: argin[0] : server name
        argin[1] : class name
        :type: PyTango.DevVarStringArray
        :return: The list of devices for specified server and class.
        :rtype: PyTango.DevVarStringArray """
        self._log.debug("In DbGetDeviceList()")
        server_name = replace_wildcard(argin[0])
        class_name = replace_wildcard(argin[1])
        return self.db.get_device_list(server_name, class_name)


# DbExportDevice is executed in the post_init_cb function below.
# It needs to be separated from the actual device to prevent the device in
# gevent mode to queue the request to the gevent thread and waitting for it.
def DbExportDevice(self, argin):
    """ Export a device to the database

    :param argin: Str[0] = Device name
    Str[1] = CORBA IOR
    Str[2] = Device server process host name
    Str[3] = Device server process PID or string ``null``
    Str[4] = Device server process version
    :type: PyTango.DevVarStringArray
    :return:
    :rtype: PyTango.DevVoid """
    self._log.debug("In DbExportDevice()")
    if len(argin) < 5:
        self.warn_stream("DataBase::DbExportDevice(): insufficient export info for device ")
        th_exc(DB_IncorrectArguments,
               "insufficient export info for device",
               "DataBase::ExportDevice()")

    dev_name, IOR, host, pid, version = argin[:5]
    dev_name = dev_name.lower()
    if pid.lower() == 'null':
        pid = "-1"
    self.db.export_device(dev_name, IOR, host, pid, version)


def main(argv = None):
    #Parameters management
    global options
    if argparse:
        parser = argparse.ArgumentParser()
        parser.add_argument("--db_access",dest="db_access",default="sqlite3",
                            help="database type")
        parser.add_argument("-e", "--embedded",dest="embedded",default=False,
                            action="store_true")
        parser.add_argument("--logging_level","-l",dest="logging_level",type=int,
                            default=0,help="logging_level 0:WARNING,1:INFO,2:DEBUG")
        parser.add_argument("--port",dest="port",default=None, type=int,
                            help="database port")
        parser.add_argument('argv',nargs=argparse.REMAINDER)
        options = parser.parse_args(argv)
        options.argv = ["DataBaseds"] + options.argv
    else:
        parser = OptionParser()
        parser.add_option("--db_access",dest="db_access",default="sqlite3",
                          help="database type")
        parser.add_option("-l", "--logging_level",dest="logging_level",default=0,
                          help="logging_level 0:WARNING,1:INFO,2:DEBUG")
        parser.add_option("-e","--embedded",dest="embedded",default=False,
                          action="store_true")
        parser.add_option("--port",dest="port",default=10000, type=int,
                          help="database port")
        (options,args) = parser.parse_args(argv)
        options.argv = ["DataBaseds"] + args

    port = options.port
    if port is None:
        try:
            _, port = PyTango.ApiUtil.get_env_var("TANGO_HOST").split(":")
        except:
            port = 10000

    options.argv += ["-ORBendPoint", "giop:tcp::{0}".format(port)]

    log_fmt = '%(threadName)-14s %(levelname)-8s %(asctime)s %(name)s: %(message)s'
    if options.logging_level == 1:
        logging_level = logging.INFO
    elif options.logging_level == 2:
        logging_level = logging.DEBUG
    else:
        logging_level = logging.WARNING
    logging.basicConfig(format=log_fmt, stream=sys.stdout,level=logging_level)
    try:
        db_name = "sys/database/" + options.argv[1]
        set_db_name(db_name)
        if options.embedded:
            __run_embedded(db_name, options.argv)
        else:
            __run(db_name, options.argv)
    except Exception as e:
        import traceback
        traceback.print_exc()


def __monkey_patch_database_class():
    DataBaseClass = DataBase.TangoClassClass

    def device_factory(self, device_list):
        """for internal usage only"""

        dev_name = get_db_name()

        klass = self.__class__
        klass_name = klass.__name__
        info = get_class_by_class(klass)
        klass = get_constructed_class_by_class(klass)

        if info is None:
            raise RuntimeError("Device class '%s' is not " \
                               "registered" % klass_name)

        if klass is None:
            raise RuntimeError("Device class '%s' as not been " \
                               "constructed" % klass_name)

        deviceClassClass, deviceImplClass, deviceImplName = info
        deviceImplClass._device_class_instance = klass

        device = self._new_device(deviceImplClass, klass, dev_name)
        self._add_device(device)
        tmp_dev_list = [device]

        self.dyn_attr(tmp_dev_list)

        self.export_device(device, "database")
        self.py_dev_list += tmp_dev_list

    DataBaseClass.device_factory = device_factory


def __monkey_patch_util(util):
    # trick util to execute orb_run instead of the usual server_run
    util._original_server_run = util.server_run
    util.server_run = util.orb_run


def __run(db_name,argv):
    """
    Runs the Database DS as a standalone database. Run it with::

        ./DataBaseds pydb-test -ORBendPoint giop:tcp::11000
    """
    PyTango.Util.set_use_db(False)
    util = PyTango.Util(argv)
    __monkey_patch_util(util)
    __monkey_patch_database_class()

    dbi = DbInter()
    util.set_interceptors(dbi)

    def post_init_cb():
        logging.debug("post_init_cb()")
        util = PyTango.Util.instance()
        dserver = util.get_dserver_device()
        dserver_name = dserver.get_name()
        dserver_ior = util.get_dserver_ior(dserver)
        dbase = util.get_device_by_name(db_name)
        dbase_name = dbase.get_name()
        dbase_ior = util.get_device_ior(dbase)
        host = util.get_host_name()
        pid = util.get_pid_str()
        version = util.get_version_str()
        DbExportDevice(dbase, [dserver_name, dserver_ior, host, pid, version])
        DbExportDevice(dbase, [dbase_name, dbase_ior, host, pid, version])

    run((DataBase,), args=argv, util=util, post_init_callback=post_init_cb,
        green_mode=GreenMode.Gevent, verbose=True)


def __run_embedded(db_name,argv):
    """Runs the Database device server embeded in another TANGO Database
    (just like any other TANGO device server)"""
    __monkey_patch_database_class()

    run((DataBase,), args=argv, util=util, green_mode=GreenMode.Gevent)


if __name__ == '__main__':
    main()