Esempio n. 1
0
def create_file(resource_path, template_hash, output_file, exception_type):
    """
    Read the template from the resource stream, perform any substitutions,
    and write it to the output file.
    :param resource_path: the resource path of the source template
    :param template_hash: a dictionary of substitution values
    :param output_file: the file to write
    :param exception_type: the type of exception to throw if needed
    """
    _method_name = 'create_file'

    file_writer = open(output_file.getPath(), "w")

    template_stream = FileUtils.getResourceAsStream(resource_path)
    if template_stream is None:
        ex = exception_helper.create_exception(exception_type, 'WLSDPLY-01661',
                                               resource_path)
        __logger.throwing(ex,
                          class_name=__class_name,
                          method_name=_method_name)
        raise ex

    template_reader = BufferedReader(
        InputStreamReader(FileUtils.getResourceAsStream(resource_path)))

    current_block_key = None
    block_lines = []

    more = True
    while more:
        line = template_reader.readLine()
        if line is not None:
            block_start_key = _get_block_start_key(line)
            block_end_key = _get_block_end_key(line)

            # if this is a nested block start, continue and add without substitution
            if (block_start_key is not None) and (current_block_key is None):
                current_block_key = block_start_key
                block_lines = []

            # if this is a nested block end, continue and add without substitution
            elif (block_end_key == current_block_key) and (current_block_key
                                                           is not None):
                _write_block(current_block_key, block_lines, template_hash,
                             file_writer)
                current_block_key = None

            else:
                line = _substitute_line(line, template_hash)

                if current_block_key is not None:
                    block_lines.append(line)
                else:
                    file_writer.write(line + "\n")

        else:
            more = False

    file_writer.close()
 def _load_category_file(self, category_file_path):
     category_input_stream = FileUtils.getResourceAsStream(
         category_file_path)
     self.assertNotEquals(category_input_stream, None)
     json_translator = JsonStreamTranslator(category_file_path,
                                            category_input_stream)
     return json_translator.parse()
def _initialize_password_field_names():
    """
    Initialize the password field names structure.
    :raises: EncryptionException: if an error occurs while loading and parsing the password field names file
    """
    global _password_field_names
    _method_name = '_initialize_password_field_names'

    if _password_field_names is None:
        password_field_names_stream = FileUtils.getResourceAsStream(_password_field_names_file)
        if password_field_names_stream is None:
            ex = exception_helper.create_encryption_exception('WLSDPLY-04100', _password_field_names_file)
            _logger.throwing(ex, class_name=_class_name, method_name=_method_name)
            raise ex

        try:
            password_field_names_dict = \
                JsonStreamTranslator(_password_field_names_file, password_field_names_stream).parse()
        except JsonException, je:
            ex = exception_helper.create_encryption_exception('WLSDPLY-04101', _password_field_names_file,
                                                              je.getLocalizedMessage(), error=je)
            _logger.throwing(ex, class_name=_class_name, method_name=_method_name)
            raise ex

        if password_field_names_dict is not None and 'passwordFieldNames' in password_field_names_dict:
            _password_field_names = password_field_names_dict['passwordFieldNames']
        else:
            ex = exception_helper.create_encryption_exception('WLSDPLY-04102', _password_field_names_file)
            _logger.throwing(ex, class_name=_class_name, method_name=_method_name)
            raise ex
Esempio n. 4
0
def extract_file(from_file_path, logger):
    """

    :param from_file_path:
    :param logger: A PlatformLogger instance that will be used for logging
                   any exceptions that are thrown
    :return:
    """
    _method_name = 'extract_file'

    from_file_name = os.path.basename(from_file_path)

    json_inputstream = FileUtils.getResourceAsStream(from_file_path)
    if json_inputstream is None:
        ex = exception_helper.create_testing_exception('WLSDPLY-09824',
                                                       from_file_path)
        logger.throwing(ex, class_name=_class_name, method_name=_method_name)
        raise ex

    try:
        j_file = FileUtils.writeInputStreamToFile(json_inputstream,
                                                  from_file_name)
    except IOException, ioe:
        ex = exception_helper.create_testing_exception(
            'WLSDPLY-09825',
            from_file_name,
            ioe.getLocalizedMessage(),
            error=ioe)
        logger.throwing(ex, class_name=_class_name, method_name=_method_name)
        raise ex
def get_domain_resource_schema(exception_type=ExceptionType.DEPLOY):
    """
    Read the WKO domain resource schema from its resource path.
    """
    _method_name = 'get_domain_resource_schema'

    template_stream = None
    try:
        template_stream = FileUtils.getResourceAsStream(
            DOMAIN_RESOURCE_SCHEMA_PATH)
        if template_stream is None:
            ex = exception_helper.create_exception(
                exception_type, 'WLSDPLY-10010', DOMAIN_RESOURCE_SCHEMA_PATH)
            _logger.throwing(ex,
                             class_name=_class_name,
                             method_name=_method_name)
            raise ex

        full_schema = JsonStreamToPython(DOMAIN_RESOURCE_SCHEMA_FILE,
                                         template_stream, True).parse()

        # remove the root element, since it has a version-specific name
        schema = full_schema[DOMAIN_RESOURCE_SCHEMA_ROOT]

    finally:
        if template_stream:
            template_stream.close()

    return schema
Esempio n. 6
0
def create_file_from_resource(resource_path, template_hash, output_file, exception_type):
    """
    Read the template from the resource stream, perform any substitutions,
    and write it to the output file.
    :param resource_path: the resource path of the source template
    :param template_hash: a dictionary of substitution values
    :param output_file: the file to write
    :param exception_type: the type of exception to throw if needed
    """
    _method_name = 'create_file_from_resource'

    template_stream = FileUtils.getResourceAsStream(resource_path)
    if template_stream is None:
        ex = exception_helper.create_exception(exception_type, 'WLSDPLY-01661', resource_path)
        __logger.throwing(ex, class_name=__class_name, method_name=_method_name)
        raise ex

    _create_file_from_stream(template_stream, template_hash, output_file)
Esempio n. 7
0
def get_resource_as_stream(file_path):
    return FileUtils.getResourceAsStream(file_path)