This file is indexed.

/usr/lib/python3/dist-packages/cement/core/handler.py is in python3-cement 2.10.0-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
"""
Cement core handler module.

"""

import re
from ..core import exc, meta
from ..core import backend
from ..utils.misc import minimal_logger

LOG = minimal_logger(__name__)


class HandlerManager(object):
    """
    Manages the handler system to define, get, resolve, etc handlers with
    the Cement Framework.

    :param use_backend_globals: Whether to use backend globals (backward
        compatibility and deprecated).
    """

    def __init__(self, use_backend_globals=False):
        if use_backend_globals is True:
            self.__handlers__ = backend.__handlers__
        else:
            self.__handlers__ = {}

    def get(self, handler_type, handler_label, *args):
        """
        Get a handler object.

        :param handler_type: The type of handler (i.e. ``output``)
        :type handler_type: ``str``
        :param handler_label: The label of the handler (i.e. ``json``)
        :type handler_label: ``str``
        :param fallback:  A fallback value to return if handler_label doesn't
            exist.
        :returns: An uninstantiated handler object
        :raises: :class:`cement.core.exc.FrameworkError`

        Usage:

        .. code-block:: python

            output = app.handler.get('output', 'json')
            output.render(dict(foo='bar'))

        """
        if handler_type not in self.__handlers__:
            raise exc.FrameworkError("handler type '%s' does not exist!" %
                                     handler_type)

        if handler_label in self.__handlers__[handler_type]:
            return self.__handlers__[handler_type][handler_label]
        elif len(args) > 0:
            return args[0]
        else:
            raise exc.FrameworkError("handlers['%s']['%s'] does not exist!" %
                                     (handler_type, handler_label))

    def list(self, handler_type):
        """
        Return a list of handlers for a given ``handler_type``.

        :param handler_type: The type of handler (i.e. ``output``)
        :returns: List of handlers that match ``hander_type``.
        :rtype: ``list``
        :raises: :class:`cement.core.exc.FrameworkError`

        Usage:

        .. code-block:: python

            app.handler.list('log')

        """
        if handler_type not in self.__handlers__:
            raise exc.FrameworkError("handler type '%s' does not exist!" %
                                     handler_type)

        res = []
        for label in self.__handlers__[handler_type]:
            if label == '__interface__':
                continue
            res.append(self.__handlers__[handler_type][label])
        return res

    def list_types(self):
        """
        Return a list of handler types (interface labels).

        :returns: List of handlers types (interface labels).
        :rtype: ``list``
        :raises: :class:`cement.core.exc.FrameworkError`

        Usage:

        .. code-block:: python

            app.handler.list_types()

        """
        return self.__handlers__.keys()

    def define(self, interface):
        """
        Define a handler based on the provided interface.  Defines a handler
        type based on ``<interface>.IMeta.label``.

        :param interface: The interface class that defines the interface to be
            implemented by handlers.
        :raises: :class:`cement.core.exc.InterfaceError`
        :raises: :class:`cement.core.exc.FrameworkError`

        Usage:

        .. code-block:: python

            app.handler.define(IDatabaseHandler)

        """
        if not hasattr(interface, 'IMeta'):
            raise exc.InterfaceError("Invalid %s, " % interface +
                                     "missing 'IMeta' class.")
        if not hasattr(interface.IMeta, 'label'):
            raise exc.InterfaceError("Invalid %s, " % interface +
                                     "missing 'IMeta.label' class.")

        LOG.debug("defining handler type '%s' (%s)" %
                  (interface.IMeta.label, interface.__name__))

        if interface.IMeta.label in self.__handlers__:
            raise exc.FrameworkError("Handler type '%s' already defined!" %
                                     interface.IMeta.label)
        self.__handlers__[interface.IMeta.label] = {
            '__interface__': interface
        }

    def defined(self, handler_type):
        """
        Test whether ``handler_type`` is defined.

        :param handler_type: The name or ``handler_type`` of the handler (I.e.
            ``log``, ``config``, ``output``, etc).
        :returns: True if the handler type is defined, False otherwise.
        :rtype: ``boolean``

        Usage:

        .. code-block:: python

            app.handler.defined('log')

        """
        if handler_type in self.__handlers__:
            return True
        else:
            return False

    def register(self, handler_obj, force=False):
        """
        Register a handler object to a handler.  If the same object is already
        registered then no exception is raised, however if a different object
        attempts to be registered to the same name a ``FrameworkError`` is
        raised.

        :param handler_obj: The uninstantiated handler object to register.
        :param force: Whether to allow replacement if an existing
         handler of the same ``label`` is already registered.
        :raises: :class:`cement.core.exc.InterfaceError`
        :raises: :class:`cement.core.exc.FrameworkError`

        Usage:

        .. code-block:: python

            class MyDatabaseHandler(object):
                class Meta:
                    interface = IDatabase
                    label = 'mysql'

                def connect(self):
                    # ...

            app.handler.register(MyDatabaseHandler)

        """

        orig_obj = handler_obj

        # for checks
        obj = orig_obj()

        if not hasattr(obj._meta, 'label') or not obj._meta.label:
            raise exc.InterfaceError("Invalid handler %s, " % orig_obj +
                                     "missing '_meta.label'.")
        if not hasattr(obj._meta, 'interface') or not obj._meta.interface:
            raise exc.InterfaceError("Invalid handler %s, " % orig_obj +
                                     "missing '_meta.interface'.")

        # translate dashes to underscores
        orig_obj.Meta.label = re.sub('-', '_', obj._meta.label)
        obj._meta.label = re.sub('-', '_', obj._meta.label)

        handler_type = obj._meta.interface.IMeta.label
        LOG.debug("registering handler '%s' into handlers['%s']['%s']" %
                  (orig_obj, handler_type, obj._meta.label))

        if handler_type not in self.__handlers__:
            raise exc.FrameworkError("Handler type '%s' doesn't exist." %
                                     handler_type)
        if obj._meta.label in self.__handlers__[handler_type] and \
                self.__handlers__[handler_type][obj._meta.label] != orig_obj:

            if force is True:
                LOG.debug(
                    "handlers['%s']['%s'] already exists" %
                    (handler_type, obj._meta.label) +
                    ", but `force==True`"
                )
            else:
                raise exc.FrameworkError(
                    "handlers['%s']['%s'] already exists" %
                    (handler_type, obj._meta.label)
                )

        interface = self.__handlers__[handler_type]['__interface__']
        if hasattr(interface.IMeta, 'validator'):
            interface.IMeta().validator(obj)
        else:
            LOG.debug("Interface '%s' does not have a validator() function!" %
                      interface)

        self.__handlers__[handler_type][obj._meta.label] = orig_obj

    def registered(self, handler_type, handler_label):
        """
        Check if a handler is registered.

        :param handler_type: The type of handler (interface label)
        :param handler_label: The label of the handler
        :returns: True if the handler is registered, False otherwise
        :rtype: ``boolean``

        Usage:

        .. code-block:: python

            app.handler.registered('log', 'colorlog')

        """
        if handler_type in self.__handlers__ and \
           handler_label in self.__handlers__[handler_type]:
            return True

        return False

    def resolve(self, handler_type, handler_def, **kwargs):
        """
        Resolves the actual handler, as it can be either a string identifying
        the handler to load from self.__handlers__, or it can be an
        instantiated or non-instantiated handler class.

        :param handler_type: The type of handler (aka the interface label)
        :param handler_def: The handler as defined in CementApp.Meta.
        :type handler_def: str, uninstantiated object, or instantiated object
        :keyword raise_error: Whether or not to raise an exception if unable
            to resolve the handler.
        :type raise_error: boolean
        :keywork meta_defaults: Optional meta-data dictionary used as
         defaults to pass when instantiating uninstantiated handlers.  See
         ``CementApp.Meta.meta_defaults``.
        :returns: The instantiated handler object.

        Usage:

        .. code-block:: python

            # via label (str)
            log = app.handler.resolve('log', 'colorlog')

            # via uninstantiated handler class
            log = app.handler.resolve('log', ColorLogHanddler)

            # via instantiated handler instance
            log = app.handler.resolve('log', ColorLogHandler())

        """
        raise_error = kwargs.get('raise_error', True)
        meta_defaults = kwargs.get('meta_defaults', {})
        han = None

        if type(handler_def) == str:
            han = self.get(handler_type, handler_def)(**meta_defaults)
        elif hasattr(handler_def, '_meta'):
            if not self.registered(handler_type, handler_def._meta.label):
                self.register(handler_def.__class__)
            han = handler_def
        elif hasattr(handler_def, 'Meta'):
            han = handler_def(**meta_defaults)
            if not self.registered(handler_type, han._meta.label):
                self.register(handler_def)

        msg = "Unable to resolve handler '%s' of type '%s'" % \
              (handler_def, handler_type)
        if han is not None:
            return han
        elif han is None and raise_error:
            raise exc.FrameworkError(msg)
        elif han is None:
            LOG.debug(msg)
            return None


class CementBaseHandler(meta.MetaMixin):

    """Base handler class that all Cement Handlers should subclass from."""

    class Meta:

        """
        Handler meta-data (can also be passed as keyword arguments to the
        parent class).

        """

        label = None
        """The string identifier of this handler."""

        interface = None
        """The interface that this class implements."""

        config_section = None
        """
        A config [section] to merge config_defaults with.

        Note: Though Meta.config_section defaults to None, Cement will
        set this to the value of ``<interface_label>.<handler_label>`` if
        no section is set by the user/developer.
        """

        config_defaults = None
        """
        A config dictionary that is merged into the applications config
        in the ``[<config_section>]`` block.  These are defaults and do not
        override any existing defaults under that section.
        """

        overridable = False
        """
        Whether or not handler can be overridden by
        ``CementApp.Meta.handler_override_options``.  Will be listed as an
        available choice to override the specific handler (i.e.
        ``CementApp.Meta.output_handler``, etc).
        """

    def __init__(self, **kw):
        super(CementBaseHandler, self).__init__(**kw)
        self.app = None

    def _setup(self, app_obj):
        """
        The _setup function is called during application initialization and
        must ``setup`` the handler object making it ready for the framework
        or the application to make further calls to it.

        :param app_obj: The application object.
        :returns: None

        """
        self.app = app_obj

        if self._meta.config_section is None:
            self._meta.config_section = "%s.%s" % \
                (self._meta.interface.IMeta.label, self._meta.label)

        if self._meta.config_defaults is not None:
            LOG.debug("merging config defaults from '%s' " % self +
                      "into section '%s'" % self._meta.config_section)
            dict_obj = dict()
            dict_obj[self._meta.config_section] = self._meta.config_defaults
            self.app.config.merge(dict_obj, override=False)


def get(handler_type, handler_label, *args):
    """
    DEPRECATION WARNING: This function is deprecated as of Cement 2.7.x and
    will be removed in future versions of Cement.
    Use ``CementApp.handler.get()`` instead.

    ---

    Get a handler object.

    Required Arguments:

    :param handler_type: The type of handler (i.e. 'output')
    :type handler_type: str
    :param handler_label: The label of the handler (i.e. 'json')
    :type handler_label: str
    :param fallback:  A fallback value to return if handler_label doesn't
        exist.
    :returns: An uninstantiated handler object
    :raises: cement.core.exc.FrameworkError

    Usage:

        from cement.core import handler
        output = handler.get('output', 'json')
        output.render(dict(foo='bar'))

    """
    # only log debug for now as this won't be removed until Cement 3.x and
    # we don't have access to CementApp.Meta.ignore_deprecation_warnings here
    LOG.debug(
        'Cement Deprecation Warning: `handler.get()` has been deprecated, '
        'and will be removed in future versions of Cement.  You should now '
        'use `CementApp.handler.get()` instead.'
    )

    if handler_type not in backend.__handlers__:
        raise exc.FrameworkError("handler type '%s' does not exist!" %
                                 handler_type)

    if handler_label in backend.__handlers__[handler_type]:
        return backend.__handlers__[handler_type][handler_label]
    elif len(args) > 0:
        return args[0]
    else:
        raise exc.FrameworkError("handlers['%s']['%s'] does not exist!" %
                                 (handler_type, handler_label))


def list(handler_type):
    """
    DEPRECATION WARNING: This function is deprecated as of Cement 2.7.x and
    will be removed in future versions of Cement.
    Use ``CementApp.handler.list()`` instead.

    ---

    Return a list of handlers for a given type.

    :param handler_type: The type of handler (i.e. 'output')
    :returns: List of handlers that match `type`.
    :rtype: ``list``
    :raises: cement.core.exc.FrameworkError

    """
    # only log debug for now as this won't be removed until Cement 3.x and
    # we don't have access to CementApp.Meta.ignore_deprecation_warnings here
    LOG.debug(
        'Cement Deprecation Warning: `handler.list()` has been deprecated, '
        'and will be removed in future versions of Cement.  You should now '
        'use `CementApp.handler.list()` instead.'
    )

    if handler_type not in backend.__handlers__:
        raise exc.FrameworkError("handler type '%s' does not exist!" %
                                 handler_type)

    res = []
    for label in backend.__handlers__[handler_type]:
        if label == '__interface__':
            continue
        res.append(backend.__handlers__[handler_type][label])
    return res


def define(interface):
    """
    DEPRECATION WARNING: This function is deprecated as of Cement 2.7.x and
    will be removed in future versions of Cement.
    Use ``CementApp.handler.define()`` instead.

    ---

    Define a handler based on the provided interface.  Defines a handler type
    based on <interface>.IMeta.label.

    :param interface: The interface class that defines the interface to be
        implemented by handlers.
    :raises: cement.core.exc.InterfaceError
    :raises: cement.core.exc.FrameworkError

    Usage:

    .. code-block:: python

        from cement.core import handler

        handler.define(IDatabaseHandler)

    """
    # only log debug for now as this won't be removed until Cement 3.x and
    # we don't have access to CementApp.Meta.ignore_deprecation_warnings here
    LOG.debug(
        'Cement Deprecation Warning: `handler.define()` has been deprecated, '
        'and will be removed in future versions of Cement.  You should now '
        'use `CementApp.handler.define()` instead.'
    )

    if not hasattr(interface, 'IMeta'):
        raise exc.InterfaceError("Invalid %s, " % interface +
                                 "missing 'IMeta' class.")
    if not hasattr(interface.IMeta, 'label'):
        raise exc.InterfaceError("Invalid %s, " % interface +
                                 "missing 'IMeta.label' class.")

    LOG.debug("defining handler type '%s' (%s)" %
              (interface.IMeta.label, interface.__name__))

    if interface.IMeta.label in backend.__handlers__:
        raise exc.FrameworkError("Handler type '%s' already defined!" %
                                 interface.IMeta.label)
    backend.__handlers__[interface.IMeta.label] = {'__interface__': interface}


def defined(handler_type):
    """
    DEPRECATION WARNING: This function is deprecated as of Cement 2.7.x and
    will be removed in future versions of Cement.
    Use ``CementApp.handler.defined()`` instead.

    ---

    Test whether a handler type is defined.

    :param handler_type: The name or 'type' of the handler (I.e. 'logging').
    :returns: True if the handler type is defined, False otherwise.
    :rtype: ``boolean``

    """
    # only log debug for now as this won't be removed until Cement 3.x and
    # we don't have access to CementApp.Meta.ignore_deprecation_warnings here
    LOG.debug(
        'Cement Deprecation Warning: `handler.defined()` has been deprecated, '
        'and will be removed in future versions of Cement.  You should now '
        'use `CementApp.handler.defined()` instead.'
    )

    if handler_type in backend.__handlers__:
        return True
    else:
        return False


def register(handler_obj, force=False):
    """
    DEPRECATION WARNING: This function is deprecated as of Cement 2.7.x and
    will be removed in future versions of Cement.
    Use ``CementApp.handler.register()`` instead.

    ---

    Register a handler object to a handler.  If the same object is already
    registered then no exception is raised, however if a different object
    attempts to be registered to the same name a FrameworkError is
    raised.

    :param handler_obj: The uninstantiated handler object to register.
    :param force: Whether to allow replacement if an existing
     handler of the same ``label`` is already registered.
    :raises: cement.core.exc.InterfaceError
    :raises: cement.core.exc.FrameworkError

    Usage:

    .. code-block:: python

        from cement.core import handler

        class MyDatabaseHandler(object):
            class Meta:
                interface = IDatabase
                label = 'mysql'

            def connect(self):
            ...

        handler.register(MyDatabaseHandler)

    """
    # only log debug for now as this won't be removed until Cement 3.x and
    # we don't have access to CementApp.Meta.ignore_deprecation_warnings here
    LOG.debug(
        'Cement Deprecation Warning: `handler.register()` has been '
        'deprecated, and will be removed in future versions of Cement.  You '
        'should now use `CementApp.handler.register()` instead.'
    )

    orig_obj = handler_obj

    # for checks
    obj = orig_obj()

    if not hasattr(obj._meta, 'label') or not obj._meta.label:
        raise exc.InterfaceError("Invalid handler %s, " % orig_obj +
                                 "missing '_meta.label'.")
    if not hasattr(obj._meta, 'interface') or not obj._meta.interface:
        raise exc.InterfaceError("Invalid handler %s, " % orig_obj +
                                 "missing '_meta.interface'.")

    # translate dashes to underscores
    orig_obj.Meta.label = re.sub('-', '_', obj._meta.label)
    obj._meta.label = re.sub('-', '_', obj._meta.label)

    handler_type = obj._meta.interface.IMeta.label
    LOG.debug("registering handler '%s' into handlers['%s']['%s']" %
              (orig_obj, handler_type, obj._meta.label))

    if handler_type not in backend.__handlers__:
        raise exc.FrameworkError("Handler type '%s' doesn't exist." %
                                 handler_type)
    if obj._meta.label in backend.__handlers__[handler_type] and \
            backend.__handlers__[handler_type][obj._meta.label] != obj:
        if force is True:
            LOG.debug(
                "handlers['%s']['%s'] already exists" %
                (handler_type, obj._meta.label) +
                ", but `force==True`"
            )
        else:
            raise exc.FrameworkError(
                "handlers['%s']['%s'] already exists" %
                (handler_type, obj._meta.label)
            )

    interface = backend.__handlers__[handler_type]['__interface__']
    if hasattr(interface.IMeta, 'validator'):
        interface.IMeta().validator(obj)
    else:
        LOG.debug("Interface '%s' does not have a validator() function!" %
                  interface)

    backend.__handlers__[handler_type][obj.Meta.label] = orig_obj


def registered(handler_type, handler_label):
    """
    DEPRECATION WARNING: This function is deprecated as of Cement 2.7.x and
    will be removed in future versions of Cement.
    Use ``CementApp.handler.registered()`` instead.

    ---

    Check if a handler is registered.

    :param handler_type: The type of handler (interface label)
    :param handler_label: The label of the handler
    :returns: True if the handler is registered, False otherwise
    :rtype: ``boolean``

    """
    # only log debug for now as this won't be removed until Cement 3.x and
    # we don't have access to CementApp.Meta.ignore_deprecation_warnings here
    LOG.debug(
        'Cement Deprecation Warning: `handler.registered()` has been '
        'deprecated, and will be removed in future versions of Cement.  You '
        'should now use `CementApp.handler.registered()` instead.'
    )

    if handler_type in backend.__handlers__ and \
       handler_label in backend.__handlers__[handler_type]:
        return True

    return False


def resolve(handler_type, handler_def, raise_error=True):
    """
    DEPRECATION WARNING: This function is deprecated as of Cement 2.7.x and
    will be removed in future versions of Cement.
    Use ``CementApp.handler.resolve()`` instead.

    ---

    Resolves the actual handler, as it can be either a string identifying
    the handler to load from backend.__handlers__, or it can be an
    instantiated or non-instantiated handler class.

    :param handler_type: The type of handler (aka the interface label)
    :param hander_def: The handler as defined in CementApp.Meta.
    :type handler_def: str, uninstantiated object, or instantiated object
    :param raise_error: Whether or not to raise an exception if unable
        to resolve the handler.
    :type raise_error: boolean
    :returns: The instantiated handler object.

    """
    # only log debug for now as this won't be removed until Cement 3.x and
    # we don't have access to CementApp.Meta.ignore_deprecation_warnings here
    LOG.debug(
        'Cement Deprecation Warning: `handler.resove()` has been '
        'deprecated, and will be removed in future versions of Cement.  You '
        'should now use `CementApp.handler.resolve()` instead.'
    )

    han = None
    if type(handler_def) == str:
        han = get(handler_type, handler_def)()
    elif hasattr(handler_def, '_meta'):
        if not registered(handler_type, handler_def._meta.label):
            register(handler_def.__class__)
        han = handler_def
    elif hasattr(handler_def, 'Meta'):
        han = handler_def()
        if not registered(handler_type, han._meta.label):
            register(handler_def)

    msg = "Unable to resolve handler '%s' of type '%s'" % \
          (handler_def, handler_type)
    if han is not None:
        return han
    elif han is None and raise_error:
        raise exc.FrameworkError(msg)
    elif han is None:
        LOG.debug(msg)
        return None