%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/lib/python2.7/site-packages/salt/utils/
Upload File :
Create Path :
Current File : //usr/lib/python2.7/site-packages/salt/utils/win_reg.pyo

�
���^c@@s�dZddlmZmZmZddlZddlZddlmZy:ddl	Z	ddl
Z
ddlZddlZe
ZWnek
r�eZnXddlZddlZddlmZddlmZejddkZeje�ZdZd	�Zd
�Zd�Z de!fd
��YZ"ed�Z#ed�Z$d�Z%ded�Z'dee
d�Z(ded�Z)dddeed�Z*ddd�Z+ed�Z,ded�Z-dS(uP
Manage the Windows registry

-----
Hives
-----
Hives are the main sections of the registry and all begin with the word HKEY.

    - HKEY_LOCAL_MACHINE
    - HKEY_CURRENT_USER
    - HKEY_USER

----
Keys
----
Keys are the folders in the registry. Keys can have many nested subkeys. Keys
can have a value assigned to them under the (Default) value name

-----------------
Values or Entries
-----------------
Values/Entries are name/data pairs. There can be many values in a key. The
(Default) value corresponds to the Key itself, the rest are their own name/value
pairs.

:depends:   - PyWin32
i(tabsolute_importtprint_functiontunicode_literalsN(trange(tCommandExecutionError(tsixiuregcC@s0tjjj�stdfSts,tdfStS(u8
    Only works on Windows systems with the PyWin32
    uPreg execution module failed to load: The module will only run on Windows systemsunreg execution module failed to load: One of the following libraries did not load: win32gui, win32con, win32api(tsalttutilstplatformt
is_windowstFalsetHAS_WINDOWS_MODULESt__virtualname__(((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyt__virtual__<s


cC@stjjj|d�S(ut
    Converts unicode to to current users character encoding. Use this for values
    returned by reg functions
    umbcs(RRtstringutilst
to_unicode(tvdata((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyt_to_mbcsLscC@s&|dkrdStjjj|d�S(u|
    Converts from current users character encoding to unicode. Use this for
    parameters being pass to reg functions
    uutf-8N(tNoneRRRR(R((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyt_to_unicodeTstRegistrycB@s eZdZd�Zd�ZRS(u�
    This was put in a class to delay usage until this module is actually used
    This class contains all the lookup dicts for working with the registry
    cC@s�i
tjd6tjd6tjd6tjd6tjd6tjd6tjd6tjd6tjd	6tjd
6|_itjd6tjd6tj	d
6tj
d6tjd6tjd6|_
idd6dd6|_idtj6dtj6d
tj	6dtj
6dtj6dtj6|_idd6dd6|_idtj6dtj6dtj6dtj6dtj6|_itjtjBt6tjt6|_dS(NuHKEY_CURRENT_CONFIGuHKEY_CLASSES_ROOTuHKEY_CURRENT_USERuHKEY_LOCAL_MACHINEu
HKEY_USERSuHKCCuHKCRuHKCUuHKLMuHKUu
REG_BINARYu	REG_DWORDu
REG_EXPAND_SZuREG_MULTI_SZuREG_SZu	REG_QWORDiuREG_OPTION_NON_VOLATILEiuREG_OPTION_VOLATILE(twin32contHKEY_CURRENT_CONFIGtHKEY_CLASSES_ROOTtHKEY_CURRENT_USERtHKEY_LOCAL_MACHINEt
HKEY_USERSthkeyst
REG_BINARYt	REG_DWORDt
REG_EXPAND_SZtREG_MULTI_SZtREG_SZt	REG_QWORDtvtypetopttypet
vtype_reversetopttype_reversetsubkey_slash_checktKEY_READtKEY_WOW64_32KEYtTrueR
tregistry_32(tself((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyt__init__dsP
























cC@sWy|j|SWnAtk
rRd}dj|j�}t|j||���nXdS(Nu#No hkey named '{0}. Try one of {1}'u, (RtKeyErrortjoinRtformat(R+tktmsgR((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyt__getattr__�s
(t__name__t
__module__t__doc__R,R2(((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyR_s	5c
C@s�t|�}t|�}t�}y|j|}Wn&tk
rZtdj|���nX|j|}d}zSy tj	||d|�}t
SWn,tjk
r�}	|	j
dkr�tS�nXWd|r�tj|�nXdS(u
    Check that the key is found in the registry. This refers to keys and not
    value/data pairs. To check value/data pairs, use ``value_exists``

    Args:

        hive (str): The hive to connect to

        key (str): The key to check

        use_32bit_registry (bool): Look in the 32bit portion of the registry

    Returns:
        bool: True if exists, otherwise False

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.key_exists(hive='HKLM', key='SOFTWARE\Microsoft')
    uInvalid Hive: {0}iiN(RRRR-RR/R*Rtwin32apitRegOpenKeyExR)t
pywintypesterrortwinerrorR
tRegCloseKey(
thivetkeytuse_32bit_registryt
local_hivet	local_keytregistrythkeytaccess_maskthandletexc((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyt
key_exists�s&	

c
C@s4t|�}t|�}t|�}t�}y|j|}Wn&tk
rftdj|���nX|j|}	ytj||d|	�}
Wn,t	j
k
r�}|jdkr�tS�nXz`y tj
|
|�\}}tSWn9t	j
k
r}|jdkr|dkrtStSnXWdtj|
�XdS(u*
    Check that the value/data pair is found in the registry.

    .. version-added:: 2018.3.4

    Args:

        hive (str): The hive to connect to

        key (str): The key to check in

        vname (str): The name of the value/data pair you're checking

        use_32bit_registry (bool): Look in the 32bit portion of the registry

    Returns:
        bool: True if exists, otherwise False

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.key_exists(hive='HKLM', key='SOFTWARE\Microsoft')
    uInvalid Hive: {0}iiN(RRRR-RR/R*R6R7R8R9R:R
tRegQueryValueExR)RR;(
R<R=tvnameR>R?R@tlocal_vnameRARBRCRDREt_((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pytvalue_exists�s0	

cC@s8tjtjtjddtjd�\}}t|�S(uo
    Refresh the windows environment.

    .. note::
        This will only effect new processes and windows. Services will not see
        the change until the system restarts.

    Returns:
        bool: True if successful, otherwise False

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.broadcast_change()
    ii�(twin32guitSendMessageTimeoutRtHWND_BROADCASTtWM_SETTINGCHANGEtSMTO_ABORTIFHUNGtbool(RJtres((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pytbroadcast_change
scC@sNt|�}t|�}t�}y|j|}Wn&tk
rZtdj|���nX|j|}g}d}	z�y{tj	||d|�}	x\t
tj|	�d�D]A}
tj|	|
�}t
r�|jt|��q�|j|�q�WWn=tk
r1tjd||dt�tdj||�fSXWd|	rI|	j�nX|S(u�
    Enumerates the subkeys in a registry key or hive.

    Args:

       hive (str):
            The name of the hive. Can be one of the following:

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USERS or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name. If a key is not
            passed, the keys under the hive will be returned.

        use_32bit_registry (bool):
            Accesses the 32bit portion of the registry on 64 bit installations.
            On 32bit machines this is ignored.

    Returns:
        list: A list of keys/subkeys under the hive or key.

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.list_keys(hive='HKLM', key='SOFTWARE\Microsoft')
    uInvalid Hive: {0}iuCannot find key: %s\%stexc_infouCannot find key: {0}\{1}N(RRRR-RR/R*RR6R7RtRegQueryInfoKeyt
RegEnumKeytPY2tappendRt	ExceptiontlogtdebugR)R
tClose(R<R=R>R?R@RARBRCtsubkeysRDtitsubkey((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyt	list_keys&s0"	

 
cC@s�t|�}t|�}t�}y|j|}Wn&tk
rZtdj|���nX|j|}d}	t�}
z]yt	j
||d|�}	x�tt	j|	�d�D]�}t	j
|	|�\}}
}|s�d}ni|d6|d6t|�d6|j|d6td	6}|tjkrJg|
D]}t|�^q+|d
<n5|tjtjgkrut|
�|d
<n
|
|d
<|
j|�q�WWn?tk
r�}tjd||dt�td
j||�fSXWd|	r�|	j�nX|
S(u
    Enumerates the values in a registry key or hive.

    Args:

        hive (str):
            The name of the hive. Can be one of the following:

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USER or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name. If a key is not
            passed, the values under the hive will be returned.

        use_32bit_registry (bool):
            Accesses the 32bit portion of the registry on 64 bit installations.
            On 32bit machines this is ignored.

        include_default (bool):
            Toggle whether to include the '(Default)' value.

    Returns:
        list: A list of values under the hive or key.

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.list_values(hive='HKLM', key='SYSTEM\CurrentControlSet\Services\Tcpip')
    uInvalid Hive: {0}iiu	(Default)uhiveukeyuvnameuvtypeusuccessuvdatauCannot find key: %s\%sRTuCannot find key: {0}\{1}N(RRRR-RR/R*RtlistR6R7RRUtRegEnumValueRR$R)RRR RRXRYRZR[R
R\(R<R=R>tinclude_defaultR?R@RARBRCRDtvaluesR^RHRR"tvalueRE((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pytlist_valueshsD$	

	 	


&
cC@sgt|�}t|�}t|�}i|d6|d6|d6dd6td6}|s`d|d<nt�}y|j|}	Wn&tk
r�tdj|���nX|j|}
y]t	j
|	|d|
�}y�t	j||�\}}
|s�|dkrv|j|
|d
<|
t
jkr>g|D]}t|�^q|d<q�|
t
jt
jgkrit|�|d<q�||d<n
d|d<Wn�tk
r}|jd
kr�|dkr�d|d<d|d
<qdj|||�}tj|�tj|�||d<t|d<nXWnStk
rb}dj||�}tj|�tj|�||d<t|d<nX|S(u�
    Reads a registry value entry or the default value for a key. To read the
    default value, don't pass ``vname``

    Args:

        hive (str): The name of the hive. Can be one of the following:

            - HKEY_LOCAL_MACHINE or HKLM
            - HKEY_CURRENT_USER or HKCU
            - HKEY_USER or HKU
            - HKEY_CLASSES_ROOT or HKCR
            - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name.

        vname (str):
            The value name. These are the individual name/data pairs under the
            key. If not passed, the key (Default) value will be returned.

        use_32bit_registry (bool):
            Accesses the 32bit portion of the registry on 64bit installations.
            On 32bit machines this is ignored.

    Returns:
        dict: A dictionary containing the passed settings as well as the
            value_data if successful. If unsuccessful, sets success to False.

        bool: Returns False if the key is not found

        If vname is not passed:

            - Returns the first unnamed value (Default) as a string.
            - Returns none if first unnamed value is empty.

    Usage:

        The following will get the value of the ``version`` value name in the
        ``HKEY_LOCAL_MACHINE\\SOFTWARE\\Salt`` key

        .. code-block:: python

            import salt.utils.win_reg
            winreg.read_value(hive='HKLM', key='SOFTWARE\\Salt', vname='version')

    Usage:

        The following will get the default value of the
        ``HKEY_LOCAL_MACHINE\\SOFTWARE\\Salt`` key

        .. code-block:: python

            import salt.utils.win_reg
            winreg.read_value(hive='HKLM', key='SOFTWARE\\Salt')
    uhiveukeyuvnameuvdatausuccessu	(Default)uInvalid Hive: {0}iuuvtypeuEmpty Valueucommentiu(value not set)uREG_SZuCannot find {0} in {1}\{2}uCannot find key: {0}\{1}N(iu(RRR)RRR-RR/R*R6R7RGR$RRRR RRYR:RZttraceR
(R<R=RHR>R?R@RItretRARBRCRDRR"R^RER1((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyt
read_value�sZ=


	

&








uREG_SZcC@s{t|�}t|�}t|�}	t|�}
t�}y|j|}Wn&tk
rrtdj|���nX|j|
}
|j|tj	B}t
d|d|
�}|r�|jd}n
|jd}d	}z�yVt
j|||d|�\}}t
j||	d|
|�t
j|�t�tSWn.t
jtttfk
r[tjd�tSXWd	|rvt
j|�nXd	S(
u

    Sets a value in the registry. If ``vname`` is passed, it will be the value
    for that value name, otherwise it will be the default value for the
    specified key

    Args:

        hive (str):
            The name of the hive. Can be one of the following

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USER or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name.

        vname (str):
            The value name. These are the individual name/data pairs under the
            key. If not passed, the key (Default) value will be set.

        vdata (str, int, list, bytes):
            The value you'd like to set. If a value name (vname) is passed, this
            will be the data for that value name. If not, this will be the
            (Default) value for the key.

            The type of data this parameter expects is determined by the value
            type specified in ``vtype``. The correspondence is as follows:

                - REG_BINARY: Binary data (str in Py2, bytes in Py3)
                - REG_DWORD: int
                - REG_EXPAND_SZ: str
                - REG_MULTI_SZ: list of str
                - REG_QWORD: int
                - REG_SZ: str

                .. note::
                    When setting REG_BINARY, string data will be converted to
                    binary. You can pass base64 encoded using the ``binascii``
                    built-in module. Use ``binascii.b2a_base64('your data')``

            .. note::
                The type for the (Default) value is always REG_SZ and cannot be
                changed.

            .. note::
                This parameter is optional. If not passed, the Key will be
                created with no associated item/value pairs.

        vtype (str):
            The value type. The possible values of the vtype parameter are
            indicated above in the description of the vdata parameter.

        use_32bit_registry (bool):
            Sets the 32bit portion of the registry on 64bit installations. On
            32bit machines this is ignored.

        volatile (bool):
            When this parameter has a value of True, the registry key will be
            made volatile (i.e. it will not persist beyond a system reset or
            shutdown). This parameter only has an effect when a key is being
            created and at no other time.

    Returns:
        bool: True if successful, otherwise False

    Usage:

        This will set the version value to 2015.5.2 in the SOFTWARE\Salt key in
        the HKEY_LOCAL_MACHINE hive

        .. code-block:: python

            import salt.utils.win_reg
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='version', vdata='2015.5.2')

    Usage:

        This function is strict about the type of vdata. For instance this
        example will fail because vtype has a value of REG_SZ and vdata has a
        type of int (as opposed to str as expected).

        .. code-block:: python

            import salt.utils.win_reg
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='str_data', vdata=1.2)

    Usage:

        In this next example vdata is properly quoted and should succeed.

        .. code-block:: python

            import salt.utils.win_reg
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='str_data', vdata='1.2')

    Usage:

        This is an example of using vtype REG_BINARY. Both ``set_value``
        commands will set the same value ``Salty Test``

        .. code-block:: python

            import salt.utils.win_reg
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='bin_data', vdata='Salty Test', vtype='REG_BINARY')

            import binascii
            bin_data = binascii.b2a_base64('Salty Test')
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='bin_data_encoded', vdata=bin_data, vtype='REG_BINARY')

    Usage:

        An example using vtype REG_MULTI_SZ is as follows:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.set_value(hive='HKLM', key='SOFTWARE\Salt', vname='list_data', vdata=['Salt', 'is', 'great'], vtype='REG_MULTI_SZ')
    uInvalid Hive: {0}RR"uREG_OPTION_VOLATILEuREG_OPTION_NON_VOLATILEtOptionsiu(Encountered error setting registry valueN(RRRR-RR/R"R*RtKEY_ALL_ACCESSt
cast_vdataR#RR6tRegCreateKeyExt
RegSetValueExtRegFlushKeyRSR)R9tSystemErrort
ValueErrort	TypeErrorRZt	exceptionR
R;(R<R=RHRR"R>tvolatileR?R@RItlocal_vtypeRARBtvtype_valueRCtlocal_vdatatcreate_optionsRDRJ((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyt	set_value,s:�	




	cC@s�t�}|j|}|tjtjgkr8t|�S|tjkrjt|tj	�rf|j
d�S|S|tjkr�g|D]}t|�^q�S|tjkr�tj
r�|St|�St|�SdS(u
    Cast the ``vdata` value to the appropriate data type for the registry type
    specified in ``vtype``

    Args:

        vdata (str, int, list, bytes): The data to cast

        vtype (str):
            The type of data to be written to the registry. Must be one of the
            following:

                - REG_BINARY
                - REG_DWORD
                - REG_EXPAND_SZ
                - REG_MULTI_SZ
                - REG_QWORD
                - REG_SZ

    Returns:
        The vdata cast to the appropriate type. Will be unicode string, binary,
        list of unicode strings, or int

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.cast_vdata(vdata='This is the string', vtype='REG_SZ')
    uutf-8N(RR"RR RRRt
isinstanceRt	text_typetencodeRR!tPY3tlongtint(RR"RARvR^((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyRl�s#	


c
@s�t|�}t|�}t�}y|j|}Wn&tk
rZtdj|���nX|}|j|tjB�t	|||�s�t
St|�dkr�|jdd�|j
|kr�tjd||�t
Sd�����fd��g}�|||��}|j|�igd6gd6}	x�|D]�}
yItj||
d	��}tj|d
�|	djdj||
��Wq4tk
r�}tj|dt�|	djd
j||
|��q4Xq4Wt�|	S(uJ
    .. versionadded:: 2015.5.4

    Delete a registry key to include all subkeys and value/data pairs.

    Args:

        hive (str):
            The name of the hive. Can be one of the following

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USER or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

            key (str):
                The key to remove (looks like a path)

            use_32bit_registry (bool):
                Deletes the 32bit portion of the registry on 64bit
                installations. On 32bit machines this is ignored.

    Returns:
        dict: A dictionary listing the keys that deleted successfully as well as
            those that failed to delete.

    Usage:

        The following example will remove ``salt`` and all its subkeys from the
        ``SOFTWARE`` key in ``HKEY_LOCAL_MACHINE``:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.delete_key_recursive(hive='HKLM', key='SOFTWARE\DeleteMe')
    uInvalid Hive: {0}iu\u<Hive:%s Key:%s; key is too close to root, not safe to removecs@sWd}xJtrRy+tj||�}t|�V|d7}Wq	tk
rNPq	Xq	WdS(u 
        Enumerate keys
        iiN(R)R6RVRRY(t_keyR^R_((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyt_subkeysHs	
c@sgtj||d|�}xH�|�D]:}dj||�}�|||��}|j|�q%W|S(uD
        Traverse the registry tree i.e. dive into the tree
        iu{0}\{1}(R6R7R/RX(t_hkeyt_keypatht_rett_access_maskR�t
subkeynamet
subkeypath(R�t_traverse_registry_treeRC(s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyR�UsuDeleteduFailediuu{0}\{1}RTu{0}\{1} {2}(RRRR-RR/R*RRkRFR
tlentcountR&RZR9RXR6R7tRegDeleteKeytWindowsErrorR)RS(
R<R=R>R?R@RARBtkey_pathtkey_listRhtsub_key_patht
key_handleRE((R�R�RCs6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pytdelete_key_recursive
sB'	
1
	




!(cC@sZt|�}t|�}t|�}t�}y|j|}Wn&tk
rftdj|���nX|j|tjB}	d	}
z�y7t
j||d|	�}
t
j|
|�t
�tSWn}tk
r:}|jdkr�d	Stj|dt�tjd|�tjd|�tjd|�tjd|�tSnXWd	|
rUt
j|
�nXd	S(
u
    Delete a registry value entry or the default value for a key.

    Args:

        hive (str):
            The name of the hive. Can be one of the following

                - HKEY_LOCAL_MACHINE or HKLM
                - HKEY_CURRENT_USER or HKCU
                - HKEY_USER or HKU
                - HKEY_CLASSES_ROOT or HKCR
                - HKEY_CURRENT_CONFIG or HKCC

        key (str):
            The key (looks like a path) to the value name.

        vname (str):
            The value name. These are the individual name/data pairs under the
            key. If not passed, the key (Default) value will be deleted.

        use_32bit_registry (bool):
            Deletes the 32bit portion of the registry on 64bit installations. On
            32bit machines this is ignored.

    Return:
        bool: True if successful, otherwise False

    Usage:

        .. code-block:: python

            import salt.utils.win_reg
            winreg.delete_value(hive='HKLM', key='SOFTWARE\SaltTest', vname='version')
    uInvalid Hive: {0}iiRTuHive: %suKey: %su
ValueName: %su
32bit Reg: %sN(RRRR-RR/R*RRkRR6R7tRegDeleteValueRSR)RYR:RZR9R
R;(R<R=RHR>R?R@RIRARBRCRDRE((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pytdelete_valuexs6$	
(.R5t
__future__RRRtsystloggingtsalt.ext.six.movesRRLR6RR8R)RtImportErrorR
tsalt.utils.platformRtsalt.utils.stringutilstsalt.exceptionsRtsalt.extRtversion_infoRWt	getLoggerR3RZRR
RRtobjectRRFRKRSRR`RfRiRyRlR�R�(((s6/usr/lib/python2.7/site-packages/salt/utils/win_reg.pyt<module>sJ


			C.=	BOw�:n

Zerion Mini Shell 1.0