o |nh! @sdZddlZddlZddlZddlZddlmZddlmZddlm Z m Z m Z ddl m Z d"de ed efd d Zd ed e ee ffd dZde d efddZ    d#dededededed ef ddZGdddeZd$deded efd d!ZdS)%zMisc utilities.N) TextWrapper)random)AnyOptionalDict) deprecatesaltreturncCs,|dur tt}t|ddS)a Generate a random hash for whatever purpose. Useful for testing or any other time that something random is required. Args: salt (str): Optional ``salt``, if ``None`` then ``random.random()`` is used. Returns: str: Random hash Example: .. code-block:: python from cement.utils.misc import rando rando('dAhn49amvnah3m') N )strrhashlibsha256encode hexdigest)r rI/usr/local/bin/dhwp/env/lib/python3.10/site-packages/cement/utils/misc.pyrando s rsectionscGst}|D]}t||<q|S)a Returns a standard dictionary object to use for application defaults. If sections are given, it will create a nested dict for each section name. This is sometimes more useful, or cleaner than creating an entire dict set (often used in testing). Args: sections: Section keys to create nested dictionaries for. Returns: dict: Dictionary of nested dictionaries (sections) Example: .. code-block:: python from cement import App, init_defaults defaults = init_defaults('myapp', 'section2', 'section3') defaults['myapp']['debug'] = False defaults['section2']['foo'] = 'bar defaults['section3']['foo2'] = 'bar2' app = App('myapp', config_defaults=defaults) )dict)rdefaultssectionrrr init_defaults+s ritemcCsRgd}t|tr||vrdSt|tr|durdSt|tr'|dkr'dSdS)aP Given a value, determine if it is one of ``[True, 'true', 'yes', 'y', 'on', '1', 1,]`` (note: strings are converted to lowercase before comparison). Args: item: The item to convert to a boolean. Returns: bool: ``True`` if ``item`` equates to a true-ish value, ``False`` otherwise )trueyesyon1TF) isinstancer lowerboolint)rtstringsrrris_trueLsr%MFtextwidthindent long_wordshyphenscCs4tg}t||vr tdt||||d}||S)a Wrap text for cleaner output (this is a simple wrapper around ``textwrap.TextWrapper`` in the standard library). Args: text (str): The text to wrap Keyword Arguments: width (int): The max width of a line before breaking indent (str): String to prefix subsequent lines after breaking long_words (bool): Whether or not to break on long words hyphens (bool): Whether or not to break on hyphens Returns: str: The wrapped string z.Argument `text` must be one of [str, unicode].)subsequent_indentr)break_long_wordsbreak_on_hyphens)r type TypeErrorrfill)r(r)r*r+r,typeswrapperrrrwrapes  r5c @seZdZdededededdf ddZdeededeeeffd d Z e defd d Z dd edeededdfddZ dd edeededdfddZ dd edeededdfddZ dd edeededdfddZ dd edeededdfddZdS) MinimalLogger namespacedebugargskwr NcOs||_t||_||_td}t}|||tj |jtj d}t t j dds6|dur9d}ndtjvrEtdd}nt t j ddrTtd d}|dure|tj|jtj|j|dS) Nz7%(asctime)s (%(levelname)s) %(namespace)s : %(message)sF CEMENT_LOGrT--debug3.0.8-2CEMENT_FRAMEWORK_LOGGING3.0.8-1)r7logging getLoggerbackend_debug Formatter StreamHandler setFormattersetLevelINFOr%osenvirongetsysargvrDEBUG addHandler)selfr7r8r9r: formatterconsole_enabledrrr__init__s.     zMinimalLogger.__init__cKsj|s|j}d|vrd|dvr |Sd|vr,d|dvr,||dd<|St|d|d<|S)Nextrar7)r7)r7keysr)rPr7r:rrr_get_logging_kwargss z!MinimalLogger._get_logging_kwargscCsvd}dtjvs |jrtdtjdd}t|rd}|Sttjddr*d}|Sttjddr9td d}|S) NFr<r="CEMENT_LOG_DEPRECATED_DEBUG_OPTIONrTr;r>r?)rLrMrCrrIrJrKr%)rPenabledvalrrrlogging_is_enabledsz MinimalLogger.logging_is_enabledmsgcK4|jr|j|fi|}|jj|fi|dSdSN)r[rWrBinforPr\r7r:kwargsrrrr_zMinimalLogger.infocKr]r^)r[rWrBwarningr`rrrrcrbzMinimalLogger.warningcKr]r^)r[rWrBerrorr`rrrrdrbzMinimalLogger.errorcKr]r^)r[rWrBfatalr`rrrrerbzMinimalLogger.fatalcKr]r^)r[rWrBr8r`rrrr8rbzMinimalLogger.debugr^)__name__ __module__ __qualname__r r"rrTrrrWpropertyr[r_rcrdrer8rrrrr6s !          r6r7r8cCs t||S)a Setup just enough for cement to be able to do debug logging. This is the logger used by the Cement framework, which is setup and accessed before the application is functional (and more importantly before the applications log handler is usable). Args: namespace (str): The logging namespace. This is generally ``__name__`` or anything you want. Keyword Args: debug (bool): Toggle debug output. Returns: object: A Logger object Example: .. code-block:: python from cement.utils.misc import minimal_logger LOG = minimal_logger('cement') LOG.debug('This is a debug message') )r6)r7r8rrrminimal_loggers rjr^)r&r'FF)F)__doc__rIrLr@r textwraprrtypingrrrcore.deprecationsrr rrr"r%r#r5objectr6rjrrrrs:   ! !k