# Path: smc/compat.py
# PY3 = sys.version_info > (3,)
# def min_smc_version(version):
# 
# Path: smc/base/util.py
# def save_to_file(filename, content):
#     """
#     Save content to file. Used by node initial contact but
#     can be used anywhere.
# 
#     :param str filename: name of file to save to
#     :param str content: content to save
#     :return: None
#     :raises IOError: permissions issue saving, invalid directory, etc
#     """
#     import os.path
#     path = os.path.abspath(filename)
#     with open(path, "w") as text_file:
#         text_file.write("{}".format(content))
# 
# Path: smc/api/exceptions.py
# class CertificateImportError(CertificateError):
#     """
#     Failure to import a certificate or private key
#     """
# 
# class CertificateExportError(CertificateError):
#     """
#     Failure to export a certificate
#     """


import re
from smc.compat import unicode
from smc.base.util import save_to_file
from smc.api.exceptions import CertificateImportError, CertificateExportError
"""
TLS Common module provides mixin methods that are common to certificate
handling in SMC.
Importing certificates and private keys can be done by providing a file
where the certificates/keys are stored, or providing in string format.
"""

    

CERT_TYPES = (b'PRIVATE KEY', b'RSA PRIVATE KEY', b'CERTIFICATE')
    
_PEM_RE = re.compile(b"-----BEGIN (" + b"|".join(CERT_TYPES) + \
    b""")-----\r?.+?\r?-----END \\1-----\r?\n?""", re.DOTALL)

    
def pem_as_string(cert):
    """
    Only return False if the certificate is a file path. Otherwise it
    is a file object or raw string and will need to be fed to the
    file open context.
    """
    if hasattr(cert, 'read'): # File object - return as is
        return cert

# Path: smc/compat.py
# PY3 = sys.version_info > (3,)
# def min_smc_version(version):
# 
# Path: smc/base/util.py
# def save_to_file(filename, content):
#     """
#     Save content to file. Used by node initial contact but
#     can be used anywhere.
# 
#     :param str filename: name of file to save to
#     :param str content: content to save
#     :return: None
#     :raises IOError: permissions issue saving, invalid directory, etc
#     """
#     import os.path
#     path = os.path.abspath(filename)
#     with open(path, "w") as text_file:
#         text_file.write("{}".format(content))
# 
# Path: smc/api/exceptions.py
# class CertificateImportError(CertificateError):
#     """
#     Failure to import a certificate or private key
#     """
# 
# class CertificateExportError(CertificateError):
#     """
#     Failure to export a certificate
#     """


# Path: smc/administration/certificates/tls_common.py
import re
from smc.compat import unicode
from smc.base.util import save_to_file
from smc.api.exceptions import CertificateImportError, CertificateExportError

"""
TLS Common module provides mixin methods that are common to certificate
handling in SMC.
Importing certificates and private keys can be done by providing a file
where the certificates/keys are stored, or providing in string format.
"""

    

CERT_TYPES = (b'PRIVATE KEY', b'RSA PRIVATE KEY', b'CERTIFICATE')
    
_PEM_RE = re.compile(b"-----BEGIN (" + b"|".join(CERT_TYPES) + \
    b""")-----\r?.+?\r?-----END \\1-----\r?\n?""", re.DOTALL)

    
def pem_as_string(cert):
    """
    Only return False if the certificate is a file path. Otherwise it
    is a file object or raw string and will need to be fed to the
    file open context.
    """
    if hasattr(cert, 'read'): # File object - return as is
        return cert

    cert = cert.encode('utf-8') if isinstance(cert, unicode) else cert