File: device_class.py

package info (click to toggle)
pytango 10.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,216 kB
  • sloc: python: 28,206; cpp: 16,380; sql: 255; sh: 82; makefile: 43
file content (1085 lines) | stat: -rw-r--r-- 35,601 bytes parent folder | download | duplicates (3)
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
# SPDX-FileCopyrightText: All Contributors to the PyTango project
# SPDX-License-Identifier: LGPL-3.0-or-later

"""
This is an internal PyTango module.
"""


__all__ = ("DeviceClass", "device_class_init")

__docformat__ = "restructuredtext"

import collections.abc

from tango._tango import (
    Except,
    DevFailed,
    DeviceClass,
    CmdArgType,
    DispLevel,
    UserDefaultAttrProp,
)
from tango.pyutil import Util

from tango.utils import is_pure_str, is_non_str_seq, seqStr_2_obj, obj_2_str, is_array
from tango.utils import document_method as __document_method

from tango.globals import get_class, get_class_by_class, get_constructed_class_by_class
from tango.attr_data import AttrData
from tango.pipe_data import PipeData


class PropUtil:
    """An internal Property util class"""

    scalar_int_types = (
        CmdArgType.DevShort,
        CmdArgType.DevUShort,
        CmdArgType.DevLong,
        CmdArgType.DevULong,
    )

    scalar_long_types = (CmdArgType.DevLong64, CmdArgType.DevULong64)

    scalar_float_types = (
        CmdArgType.DevFloat,
        CmdArgType.DevDouble,
    )

    scalar_numerical_types = scalar_int_types + scalar_long_types + scalar_float_types

    scalar_str_types = (
        CmdArgType.DevString,
        CmdArgType.ConstDevString,
    )

    scalar_types = (
        scalar_numerical_types
        + scalar_str_types
        + (
            CmdArgType.DevBoolean,
            CmdArgType.DevEncoded,
            CmdArgType.DevUChar,
            CmdArgType.DevVoid,
        )
    )

    def __init__(self):
        self.db = None
        if Util._UseDb:
            self.db = Util.instance().get_database()

    def set_default_property_values(self, dev_class, class_prop, dev_prop):
        """
        set_default_property_values(self, dev_class, class_prop, dev_prop) -> None

            Sets the default property values

        Parameters :
            - dev_class : (DeviceClass) device class object
            - class_prop : (dict<str,>) class properties
            - dev_prop : (dict<str,>) device properties

        Return     : None
        """
        for name in class_prop:
            type = self.get_property_type(name, class_prop)
            val = self.get_property_values(name, class_prop)
            val = self.values2string(val, type)
            desc = self.get_property_description(name, class_prop)
            dev_class.add_wiz_class_prop(name, desc, val)

        for name in dev_prop:
            type = self.get_property_type(name, dev_prop)
            val = self.get_property_values(name, dev_prop)
            val = self.values2string(val, type)
            desc = self.get_property_description(name, dev_prop)
            dev_class.add_wiz_dev_prop(name, desc, val)

    def get_class_properties(self, dev_class, class_prop):
        """
        get_class_properties(self, dev_class, class_prop) -> None

                Returns the class properties

            Parameters :
                - dev_class : (DeviceClass) the DeviceClass object
                - class_prop : [in, out] (dict<str, None>) the property names. Will be filled
                               with property values

            Return     : None"""
        # initialize default values
        if class_prop == {} or not Util._UseDb:
            return

        # call database to get properties
        props = self.db.get_class_property(
            dev_class.get_name(), list(class_prop.keys())
        )

        # if value defined in database, store it
        for name in class_prop:
            if props[name]:
                type = self.get_property_type(name, class_prop)
                values = self.stringArray2values(props[name], type)
                self.set_property_values(name, class_prop, values)
            else:
                print(name + " property NOT found in database")

    def get_device_properties(self, dev, class_prop, dev_prop):
        """
        get_device_properties(self, dev, class_prop, dev_prop) -> None

                Returns the device properties

            Parameters :
                - dev : (DeviceImpl) the device object
                - class_prop : (dict<str, obj>) the class properties
                - dev_prop : [in,out] (dict<str, None>) the device property names

            Return     : None"""
        #    initialize default properties
        if dev_prop == {} or not Util._UseDb:
            return

        # Call database to get properties
        props = self.db.get_device_property(dev.get_name(), list(dev_prop.keys()))
        #    if value defined in database, store it
        for name in dev_prop:
            prop_value = props[name]
            if len(prop_value):
                data_type = self.get_property_type(name, dev_prop)
                values = self.stringArray2values(prop_value, data_type)
                if not self.is_empty_seq(values):
                    self.set_property_values(name, dev_prop, values)
                else:
                    #    Try to get it from class property
                    values = self.get_property_values(name, class_prop)
                    if not self.is_empty_seq(values):
                        if not self.is_seq(values):
                            values = [values]
                        data_type = self.get_property_type(name, class_prop)
                        values = self.stringArray2values(values, data_type)
                        if not self.is_empty_seq(values):
                            self.set_property_values(name, dev_prop, values)
            else:
                #    Try to get it from class property
                values = self.get_property_values(name, class_prop)
                if not self.is_empty_seq(values):
                    if not self.is_seq(values):
                        values = [values]
                    data_type = self.get_property_type(name, class_prop)
                    values = self.stringArray2values(values, data_type)
                    if not self.is_empty_seq(values):
                        self.set_property_values(name, dev_prop, values)

    def is_seq(self, v):
        """
        is_seq(self, v) -> bool

                Helper method. Determines if the object is a sequence

            Parameters :
                - v : (object) the object to be analysed

            Return     : (bool) True if the object is a sequence or False otherwise"""
        return isinstance(v, collections.abc.Sequence)

    def is_empty_seq(self, v):
        """
        is_empty_seq(self, v) -> bool

                Helper method. Determines if the object is an empty sequence

            Parameters :
                - v : (object) the object to be analysed

            Return     : (bool) True if the object is a sequence which is empty or False otherwise
        """
        return self.is_seq(v) and not len(v)

    def get_property_type(self, prop_name, properties):
        """
        get_property_type(self, prop_name, properties) -> CmdArgType

                Gets the property type for the given property name using the
                information given in properties

            Parameters :
                - prop_name : (str) property name
                - properties : (dict<str,data>) property data

            Return     : (CmdArgType) the tango type for the given property"""
        try:
            tg_type = properties[prop_name][0]
        except Exception:
            tg_type = CmdArgType.DevVoid
        return tg_type

    def set_property_values(self, prop_name, properties, values):
        """
        set_property_values(self, prop_name, properties, values) -> None

                Sets the property value in the properties

            Parameters :
                - prop_name : (str) property name
                - properties : (dict<str,obj>) [in,out] dict which will contain the value
                - values : (seq) the new property value

            Return     : None"""

        properties[prop_name][2] = values

    def get_property_values(self, prop_name, properties):
        """
        get_property_values(self, prop_name, properties) -> obj

                Gets the property value

            Parameters :
                - prop_name : (str) property name
                - properties : (dict<str,obj>) properties
            Return     : (obj) the value for the given property name"""
        try:
            tg_type = self.get_property_type(prop_name, properties)
            val = properties[prop_name][2]
        except Exception:
            val = []

        if is_array(tg_type) or (
            isinstance(val, collections.abc.Sequence) and not len(val)
        ):
            return val
        else:
            if is_non_str_seq(val):
                return val[0]
            else:
                return val

    def get_property_description(self, prop_name, properties):
        """
        get_property_description(self, prop_name, properties) -> obj

                Gets the property description

            Parameters :
                - prop_name : (str) property name
                - properties : (dict<str,obj>) properties
            Return     : (str) the description for the given property name"""
        return properties[prop_name][1]

    def stringArray2values(self, argin, argout_type):
        """internal helper method"""
        return seqStr_2_obj(argin, argout_type)

    def values2string(self, argin, argout_type):
        """internal helper method"""
        return obj_2_str(argin, argout_type)


def __DeviceClass__init__(self, name):
    DeviceClass.__init_orig__(self, name)
    try:
        pu = self.prop_util = PropUtil()
        self.py_dev_list = []
        pu.set_default_property_values(
            self, self.class_property_list, self.device_property_list
        )
        pu.get_class_properties(self, self.class_property_list)
        for prop_name in self.class_property_list:
            if not hasattr(self, prop_name):
                setattr(
                    self,
                    prop_name,
                    pu.get_property_values(prop_name, self.class_property_list),
                )
    except DevFailed as df:
        print(f"PyDS: {name}: A Tango error occurred in the constructor:")
        Except.print_exception(df)
    except Exception as e:
        print(f"PyDS: {name}: An error occurred in the constructor:")
        print(str(e))


def __DeviceClass__str__(self):
    return f"{self.__class__.__name__}({self.get_name()})"


def __DeviceClass__repr__(self):
    return f"{self.__class__.__name__}({self.get_name()})"


def __throw_create_attribute_exception(msg):
    """
    Helper method to throw DevFailed exception when inside
    create_attribute
    """
    Except.throw_exception("PyDs_WrongAttributeDefinition", msg, "create_attribute()")


def __throw_create_command_exception(msg):
    """
    Helper method to throw DevFailed exception when inside
    create_command
    """
    Except.throw_exception("PyDs_WrongCommandDefinition", msg, "create_command()")


def __DeviceClass__create_user_default_attr_prop(self, attr_name, extra_info):
    """for internal usage only"""
    p = UserDefaultAttrProp()
    for k, v in extra_info.items():
        k_lower = k.lower()
        method_name = f"set_{k_lower.replace(' ', '_')}"
        if hasattr(p, method_name):
            method = getattr(p, method_name)
            method(str(v))
        elif k == "delta_time":
            p.set_delta_t(str(v))
        elif k_lower not in ("display level", "polling period", "memorized"):
            name = self.get_name()
            msg = (
                f"Wrong definition of attribute {attr_name} in "
                f"class {name}\nThe object extra information '{k}' "
                f"is not recognized!"
            )
            self.__throw_create_attribute_exception(msg)
    return p


def __DeviceClass__attribute_factory(self, attr_list):
    """for internal usage only"""
    for attr_name, attr_info in self.attr_list.items():
        if isinstance(attr_info, AttrData):
            attr_data = attr_info
        else:
            attr_data = AttrData(attr_name, self.get_name(), attr_info)
        if attr_data.forward:
            self._create_fwd_attribute(
                attr_list, attr_data.attr_name, attr_data.att_prop
            )
        else:
            self._create_attribute(
                attr_list,
                attr_data.attr_name,
                attr_data.attr_type,
                attr_data.attr_format,
                attr_data.attr_write,
                attr_data.dim_x,
                attr_data.dim_y,
                attr_data.display_level,
                attr_data.polling_period,
                attr_data.memorized,
                attr_data.hw_memorized,
                attr_data.read_method_name,
                attr_data.write_method_name,
                attr_data.is_allowed_name,
                attr_data.att_prop,
            )


def __DeviceClass__pipe_factory(self, pipe_list):
    """for internal usage only"""
    for pipe_name, pipe_info in self.pipe_list.items():
        if isinstance(pipe_info, PipeData):
            pipe_data = pipe_info
        else:
            pipe_data = PipeData(pipe_name, self.get_name(), pipe_info)
        self._create_pipe(
            pipe_list,
            pipe_data.pipe_name,
            pipe_data.pipe_write,
            pipe_data.display_level,
            pipe_data.read_method_name,
            pipe_data.write_method_name,
            pipe_data.is_allowed_name,
            pipe_data.pipe_prop,
        )


def __DeviceClass__command_factory(self):
    """for internal usage only"""
    name = self.get_name()
    class_info = get_class(name)
    deviceimpl_class = class_info[1]

    if not hasattr(deviceimpl_class, "init_device"):
        msg = (
            f"Wrong definition of class {name}\n"
            f"The init_device() method does not exist!"
        )
        Except.throw_exception("PyDs_WrongCommandDefinition", msg, "command_factory()")

    for cmd_name, cmd_info in self.cmd_list.items():
        __create_command(self, deviceimpl_class, cmd_name, cmd_info)


def __create_command(self, deviceimpl_class, cmd_name, cmd_info):
    """for internal usage only"""
    name = self.get_name()

    # check for well defined command info

    # check parameter
    if not isinstance(cmd_info, collections.abc.Sequence):
        msg = (
            f"Wrong data type for value for describing command {cmd_name} in "
            f"class {name}\nMust be a sequence with 2 or 3 elements"
        )
        __throw_create_command_exception(msg)

    if len(cmd_info) < 2 or len(cmd_info) > 3:
        msg = (
            f"Wrong number of argument for describing command {cmd_name} in "
            f"class {name}\nMust be a sequence with 2 or 3 elements"
        )
        __throw_create_command_exception(msg)

    param_info, result_info = cmd_info[0], cmd_info[1]

    if not isinstance(param_info, collections.abc.Sequence):
        msg = (
            f"Wrong data type in command argument for command {cmd_name} in "
            f"class {name}\nCommand parameter (first element) must be a sequence"
        )
        __throw_create_command_exception(msg)

    if len(param_info) < 1 or len(param_info) > 2:
        msg = (
            f"Wrong data type in command argument for command {cmd_name} in "
            f"class {name}\nSequence describing command parameters must contain "
            f"1 or 2 elements"
        )
        __throw_create_command_exception(msg)

    param_type = CmdArgType.DevVoid
    try:
        param_type = CmdArgType(param_info[0])
    except Exception:
        msg = (
            f"Wrong data type in command argument for command {cmd_name} in "
            f"class {name}\nCommand parameter type (first element in first "
            f"sequence) must be a tango.CmdArgType"
        )
        __throw_create_command_exception(msg)

    param_desc = ""
    if len(param_info) > 1:
        param_desc = param_info[1]
        if not is_pure_str(param_desc):
            msg = (
                f"Wrong data type in command parameter for command {cmd_name} in "
                f"class {name}\nCommand parameter description (second element "
                f"in first sequence), when given, must be a string"
            )
            __throw_create_command_exception(msg)

    # Check result
    if not isinstance(result_info, collections.abc.Sequence):
        msg = (
            f"Wrong data type in command result for command {cmd_name} in "
            f"class {name}\nCommand result (second element) must be a sequence"
        )
        __throw_create_command_exception(msg)

    if len(result_info) < 1 or len(result_info) > 2:
        msg = (
            f"Wrong data type in command result for command {cmd_name} in "
            f"class {name}\nSequence describing command result must contain "
            f"1 or 2 elements"
        )
        __throw_create_command_exception(msg)

    result_type = CmdArgType.DevVoid
    try:
        result_type = CmdArgType(result_info[0])
    except Exception:
        msg = (
            f"Wrong data type in command result for command {cmd_name} in "
            f"class {name}\nCommand result type (first element in second "
            f"sequence) must be a tango.CmdArgType"
        )
        __throw_create_command_exception(msg)

    result_desc = ""
    if len(result_info) > 1:
        result_desc = result_info[1]
        if not is_pure_str(result_desc):
            msg = (
                f"Wrong data type in command result for command {cmd_name} in "
                f"class {name}\nCommand parameter description (second element "
                f"in second sequence), when given, must be a string"
            )
            __throw_create_command_exception(msg)

    # If it is defined, get addictional dictionnary used for optional parameters
    display_level, default_command, polling_period, is_allowed = (
        DispLevel.OPERATOR,
        False,
        -1,
        None,
    )

    if len(cmd_info) == 3:
        extra_info = cmd_info[2]
        if not isinstance(extra_info, collections.abc.Mapping):
            msg = (
                f"Wrong data type in command information for command {cmd_name} in "
                f"class {name}\nCommand information (third element in sequence), "
                f"when given, must be a dictionary"
            )
            __throw_create_command_exception(msg)

        if len(extra_info) > 4:
            msg = (
                f"Wrong data type in command information for command {cmd_name} in "
                f"class {name}\nThe optional dictionary can not have more than "
                f"four elements"
            )
            __throw_create_command_exception(msg)

        for info_name, info_value in extra_info.items():
            info_name_lower = info_name.lower()
            if info_name_lower == "display level":
                try:
                    display_level = DispLevel(info_value)
                except Exception:
                    msg = (
                        f"Wrong data type in command information for command {cmd_name} in "
                        f"class {name}\nCommand information for display level is not a "
                        f"tango.DispLevel"
                    )
                    __throw_create_command_exception(msg)
            elif info_name_lower == "default command":
                if not is_pure_str(info_value):
                    msg = (
                        f"Wrong data type in command information for command {cmd_name} in "
                        f"class {name}\nCommand information for default command is not a "
                        f"string"
                    )
                    __throw_create_command_exception(msg)
                v = info_value.lower()
                default_command = v == "true"
            elif info_name_lower == "polling period":
                try:
                    polling_period = int(info_value)
                except Exception:
                    msg = (
                        f"Wrong data type in command information for command {cmd_name} in "
                        f"class {name}\nCommand information for polling period is not an "
                        f"integer"
                    )
                    __throw_create_command_exception(msg)
            elif info_name_lower == "is allowed":
                is_allowed = info_value
                if not is_pure_str(is_allowed):
                    msg = (
                        "Wrong data type in command information for command %s in "
                        "class %s\nCommand information for is allowed function name"
                        "is not an string" % (cmd_name, name)
                    )
                    __throw_create_command_exception(msg)
            elif info_name_lower == "is allowed green_mode":
                pass
            else:
                msg = (
                    f"Wrong data type in command information for command {cmd_name} in "
                    f"class {name}\nCommand information has unknown key "
                    f"{info_name}"
                )
                __throw_create_command_exception(msg)

    # check that the method to be executed exists
    try:
        cmd = getattr(deviceimpl_class, cmd_name)
        if not isinstance(cmd, collections.abc.Callable):
            msg = (
                f"Wrong definition of command {cmd_name} in "
                f"class {name}\nThe object exists in class but is not "
                f"a method!"
            )
            __throw_create_command_exception(msg)
    except AttributeError:
        msg = (
            f"Wrong definition of command {cmd_name} in "
            f"class {name}\nThe command method does not exist!"
        )
        __throw_create_command_exception(msg)

    if is_allowed is None:
        is_allowed_name = f"is_{cmd_name}_allowed"
    else:
        is_allowed_name = is_allowed

    try:
        is_allowed_function = getattr(deviceimpl_class, is_allowed_name)
        if not isinstance(is_allowed_function, collections.abc.Callable):
            msg = (
                f"Wrong definition of command {cmd_name} in "
                f"class {name}\nThe object '{is_allowed_name}' exists in class but is "
                f"not a method!"
            )
            __throw_create_command_exception(msg)
    except Exception:
        is_allowed_name = ""

    self._create_command(
        cmd_name,
        param_type,
        result_type,
        param_desc,
        result_desc,
        display_level,
        default_command,
        polling_period,
        is_allowed_name,
    )


def __DeviceClass__new_device(self, klass, dev_class, dev_name):
    return klass(dev_class, dev_name)


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

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

    if info is None:
        raise RuntimeError(f"Device class '{klass_name}' is not registered")

    if klass is None:
        raise RuntimeError(f"Device class '{klass_name}' as not been constructed")

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

    tmp_dev_list = []
    for dev_name in device_list:
        device = self._new_device(deviceImplClass, klass, dev_name)
        self._add_device(device)
        tmp_dev_list.append(device)

    self.dyn_attr(tmp_dev_list)

    for dev in tmp_dev_list:
        if Util._UseDb and not Util._FileDb:
            self.export_device(dev)
        else:
            self.export_device(dev, dev.get_name())
    self.py_dev_list += tmp_dev_list


def __DeviceClass__create_device(self, device_name, alias=None, cb=None):
    """
    create_device(self, device_name, alias=None, cb=None) -> None

        Creates a new device of the given class in the database, creates a new
        DeviceImpl for it and calls init_device (just like it is done for
        existing devices when the DS starts up)

        An optional parameter callback is called AFTER the device is
        registered in the database and BEFORE the init_device for the
        newly created device is called

    Throws tango.DevFailed:
        - the device name exists already or
        - the given class is not registered for this DS.
        - the cb is not a callable

    New in PyTango 7.1.2

    Parameters :
        - device_name : (str) the device name
        - alias : (str) optional alias. Default value is None meaning do not create device alias
        - cb : (callable) a callback that is called AFTER the device is registered
               in the database and BEFORE the init_device for the newly created
               device is called. Typically you may want to put device and/or attribute
               properties in the database here. The callback must receive a parameter:
               device name (str). Default value is None meaning no callback

    Return     : None"""
    util = Util.instance()
    util.create_device(self.get_name(), device_name, alias=alias, cb=cb)


def __DeviceClass__delete_device(self, device_name):
    """
    delete_device(self, klass_name, device_name) -> None

        Deletes an existing device from the database and from this running
        server

        Throws tango.DevFailed:
            - the device name doesn't exist in the database
            - the device name doesn't exist in this DS.

    New in PyTango 7.1.2

    Parameters :
        - klass_name : (str) the device class name
        - device_name : (str) the device name

    Return     : None"""
    util = Util.instance()
    util.delete_device(self.get_name(), device_name)


def __DeviceClass__dyn_attr(self, device_list):
    """
    dyn_attr(self,device_list) -> None

        Default implementation does not do anything
        Overwrite in order to provide dynamic attributes

    Parameters :
        - device_list : (seq<DeviceImpl>) sequence of devices of this class

    Return     : None"""
    pass


def __DeviceClass__device_destroyer(self, name):
    """for internal usage only"""
    name = name.lower()
    for d in self.py_dev_list:
        dname = d.get_name().lower()
        if dname == name:
            dev_cl = d.get_device_class()
            # the internal C++ device_destroyer isn't case sensitive so we
            # use the internal DeviceImpl name to make sure the DeviceClass
            # finds it
            dev_cl._device_destroyer(d.get_name())
            self.py_dev_list.remove(d)
            return
    err_mess = "Device " + name + " not in Tango class device list!"
    Except.throw_exception(
        "PyAPI_CantDestroyDevice", err_mess, "DeviceClass.device_destroyer"
    )


def __DeviceClass__device_name_factory(self, dev_name_list):
    """
    device_name_factory(self, dev_name_list) ->  None

        Create device(s) name list (for no database device server).
        This method can be re-defined in DeviceClass sub-class for
        device server started without database. Its rule is to
        initialise class device name. The default method does nothing.

    Parameters :
        - dev_name_list : (seq<str>) sequence of devices to be filled

    Return     : None"""
    pass


def __init_DeviceClass():
    DeviceClass.class_property_list = {}
    DeviceClass.device_property_list = {}
    DeviceClass.cmd_list = {}
    DeviceClass.attr_list = {}
    DeviceClass.pipe_list = {}
    DeviceClass.__init_orig__ = DeviceClass.__init__
    DeviceClass.__init__ = __DeviceClass__init__
    DeviceClass.__str__ = __DeviceClass__str__
    DeviceClass.__repr__ = __DeviceClass__repr__
    DeviceClass._create_user_default_attr_prop = (
        __DeviceClass__create_user_default_attr_prop
    )
    DeviceClass._attribute_factory = __DeviceClass__attribute_factory
    DeviceClass._pipe_factory = __DeviceClass__pipe_factory
    DeviceClass._command_factory = __DeviceClass__command_factory
    DeviceClass._new_device = __DeviceClass__new_device

    DeviceClass.device_factory = __DeviceClass__device_factory
    DeviceClass.create_device = __DeviceClass__create_device
    DeviceClass.delete_device = __DeviceClass__delete_device
    DeviceClass.dyn_attr = __DeviceClass__dyn_attr
    DeviceClass.device_destroyer = __DeviceClass__device_destroyer
    DeviceClass.device_name_factory = __DeviceClass__device_name_factory


def __doc_DeviceClass():
    DeviceClass.__doc__ = """
    Base class for all TANGO device-class class.
    A TANGO device-class class is a class where is stored all
    data/method common to all devices of a TANGO device class
    """

    def document_method(method_name, desc, append=True):
        return __document_method(DeviceClass, method_name, desc, append)

    document_method(
        "export_device",
        """
    export_device(self, dev, corba_dev_name = 'Unused') -> None

            For internal usage only

        Parameters :
            - dev : (DeviceImpl) device object
            - corba_dev_name : (str) CORBA device name. Default value is 'Unused'

        Return     : None
    """,
    )

    document_method(
        "register_signal",
        """
    register_signal(self, signo) -> None
    register_signal(self, signo, own_handler=false) -> None

            Register a signal.
            Register this class as class to be informed when signal signo
            is sent to to the device server process.
            The second version of the method is available only under Linux.

        Throws tango.DevFailed:
            - if the signal number is out of range
            - if the operating system failed to register a signal for the process.

        Parameters :
            - signo : (int) signal identifier
            - own_handler : (bool) true if you want the device signal handler
                            to be executed in its own handler instead of being
                            executed by the signal thread. If this parameter
                            is set to true, care should be taken on how the
                            handler is written. A default false value is provided

        Return     : None
    """,
    )

    document_method(
        "unregister_signal",
        """
    unregister_signal(self, signo) -> None

            Unregister a signal.
            Unregister this class as class to be informed when signal signo
            is sent to to the device server process

        Parameters :
            - signo : (int) signal identifier
        Return     : None
    """,
    )

    document_method(
        "signal_handler",
        """
    signal_handler(self, signo) -> None

            Signal handler.

            The method executed when the signal arrived in the device server process.
            This method is defined as virtual and then, can be redefined following
            device class needs.

        Parameters :
            - signo : (int) signal identifier
        Return     : None
    """,
    )

    document_method(
        "get_name",
        """
    get_name(self) -> str

            Get the TANGO device class name.

        Parameters : None
        Return     : (str) the TANGO device class name.
    """,
    )

    document_method(
        "get_type",
        """
    get_type(self) -> str

            Gets the TANGO device type name.

        Parameters : None
        Return     : (str) the TANGO device type name
    """,
    )

    document_method(
        "get_doc_url",
        """
    get_doc_url(self) -> str

            Get the TANGO device class documentation URL.

        Parameters : None
        Return     : (str) the TANGO device type name
    """,
    )

    document_method(
        "set_type",
        """
    set_type(self, dev_type) -> None

            Set the TANGO device type name.

        Parameters :
            - dev_type : (str) the new TANGO device type name
        Return     : None
    """,
    )

    document_method(
        "get_cvs_tag",
        """
    get_cvs_tag(self) -> str

            Gets the cvs tag

        Parameters : None
        Return     : (str) cvs tag
    """,
    )

    document_method(
        "get_cvs_location",
        """
    get_cvs_location(self) -> None

            Gets the cvs localtion

        Parameters : None
        Return     : (str) cvs location
    """,
    )

    document_method(
        "get_device_list",
        """
    get_device_list(self) -> sequence<tango.DeviceImpl>

            Gets the list of tango.DeviceImpl objects for this class

        Parameters : None
        Return     : (sequence<tango.DeviceImpl>) list of tango.DeviceImpl objects for this class
    """,
    )

    document_method(
        "get_command_list",
        """
    get_command_list(self) -> sequence<tango.Command>

            Gets the list of tango.Command objects for this class

        Parameters : None
        Return     : (sequence<tango.Command>) list of tango.Command objects for this class

        New in PyTango 8.0.0
    """,
    )

    document_method(
        "get_cmd_by_name",
        """
    get_cmd_by_name(self, (str)cmd_name) -> tango.Command

            Get a reference to a command object.

        Parameters :
            - cmd_name : (str) command name
        Return     : (tango.Command) tango.Command object

        New in PyTango 8.0.0
    """,
    )

    document_method(
        "add_wiz_dev_prop",
        """
    add_wiz_dev_prop(self, name, desc) -> None
    add_wiz_dev_prop(self, name, desc, def) -> None

            For internal usage only

        :param str name: device property name
        :param str desc: device property description
        :param str def: device property default value

        :return: None
    """,
    )

    document_method(
        "add_wiz_class_prop",
        """
    add_wiz_class_prop(self, name, desc) -> None
    add_wiz_class_prop(self, name, desc, def) -> None

            For internal usage only

        :param str name: class property name
        :param str desc: class property description
        :param str def: class property default value

        :return: None
    """,
    )

    document_method(
        "get_class_attr",
        """
    get_class_attr(self) -> None

        Returns the instance of the :class:`tango.MultiClassAttribute` for the class

        :param: None

        :returns: the instance of the :class:`tango.MultiClassAttribute` for the class
        :rtype: :class:`tango.MultiClassAttribute`
    """,
    )

    document_method(
        "get_pipe_list",
        """
    get_pipe_list(self, dev_name) -> None

        Returns the list of pipes for the specified device

        :param atr dev_name: name of the device

        :returns: list of :class:`tango.server.pipe` objects for device
        :rtype: :class:`tango.server.pipe`
    """,
    )

    document_method(
        "get_pipe_by_name",
        """
    get_pipe_by_name(self, pipe_name, dev_name) -> None

        Returns the :class:`Pipe` instance with name <pipe_name> for the specified device

        :param str pipe_name: name of the pipe

        :param str dev_name: name of the device

        :returns: :class:`tango.server.pipe` object
        :rtype: :class:`tango.server.pipe`
    """,
    )


def device_class_init(doc=True):
    __init_DeviceClass()
    if doc:
        __doc_DeviceClass()