Beispiel #1
0
def test_unknown_data_file():
    test_var = "TEST_ENVIRONMENT_VARIABLE"
    test_value = "am I found"
    os.environ[test_var] = test_value
    context = Context(fs.path.join("tests", "fixtures"))
    data = context.get_data("unknown.data")
    eq_(data[test_var], test_value)
Beispiel #2
0
def test_json_data_overrides_environ_variables():
    test_var = "TEST_ENVIRONMENT_VARIABLE"
    test_value = "am I found"
    os.environ[test_var] = test_value
    context = Context(fs.path.join("tests", "fixtures"))
    data = context.get_data("simple.json")
    eq_(data[test_var], test_value)
Beispiel #3
0
 def __init__(self, template_fs, context_dirs, engine):
     context_dirs = expand_template_directory(context_dirs)
     self.context = Context(context_dirs)
     self.template_fs = template_fs
     self.engine = engine
     self.templated_count = 0
     self.file_count = 0
     self.buffered_writer = BufferedWriter()
     self.engine_action = getattr(
         engine,
         "ACTION_IN_PRESENT_CONTINUOUS_TENSE",
         constants.LABEL_MOBAN_ACTION_IN_PRESENT_CONTINUOUS_TENSE,
     )
     self.engine_actioned = getattr(
         engine,
         "ACTION_IN_PAST_TENSE",
         constants.LABEL_MOBAN_ACTION_IN_PAST_TENSE,
     )
Beispiel #4
0
class MobanEngine(object):
    def __init__(self, template_fs, context_dirs, engine):
        context_dirs = expand_template_directory(context_dirs)
        self.context = Context(context_dirs)
        self.template_fs = template_fs
        self.engine = engine
        self.templated_count = 0
        self.file_count = 0
        self.buffered_writer = BufferedWriter()
        self.engine_action = getattr(
            engine,
            "ACTION_IN_PRESENT_CONTINUOUS_TENSE",
            constants.LABEL_MOBAN_ACTION_IN_PRESENT_CONTINUOUS_TENSE,
        )
        self.engine_actioned = getattr(
            engine,
            "ACTION_IN_PAST_TENSE",
            constants.LABEL_MOBAN_ACTION_IN_PAST_TENSE,
        )

    def report(self):
        if self.templated_count == 0:
            reporter.report_no_action()
        elif self.templated_count == self.file_count:
            reporter.report_full_run(self.engine_actioned, self.file_count)
        else:
            reporter.report_partial_run(self.engine_actioned,
                                        self.templated_count, self.file_count)

    def number_of_templated_files(self):
        return self.templated_count

    def render_to_file(self, template_file, data_file, output_file):
        data = self.context.get_data(data_file)
        template = self.engine.get_template(template_file)
        try:
            template_abs_path = self.template_fs.geturl(template_file,
                                                        purpose="fs")
        except ResourceNotFound:
            template_abs_path = template_file

        flag = self.apply_template(template_abs_path, template, data,
                                   output_file)
        if flag:
            reporter.report_templating(self.engine_action, template_file,
                                       output_file)
            self.templated_count += 1
        self.file_count += 1

        self.buffered_writer.close()

    def render_string_to_file(self, template_in_string, data_file,
                              output_file):
        template = self.engine.get_template_from_string(template_in_string)
        template_abs_path = f"{template_in_string[:10]}..."
        data = self.context.get_data(data_file)
        flag = self.apply_template(template_abs_path, template, data,
                                   output_file)
        if flag:
            reporter.report_templating(self.engine_action, template_abs_path,
                                       output_file)
            self.templated_count += 1
        self.file_count += 1
        self.buffered_writer.close()

    def apply_template(self, template_abs_path, template, data, output_file):
        rendered_content = self.engine.apply_template(template, data,
                                                      output_file)
        if not isinstance(rendered_content, bytes):
            rendered_content = rendered_content.encode("utf-8")

        try:
            flag = HASH_STORE.is_file_changed(output_file, rendered_content,
                                              template_abs_path)
            if flag:
                self.buffered_writer.write_file_out(output_file,
                                                    rendered_content)
                if not file_system.is_zip_alike_url(output_file):
                    file_system.file_permissions_copy(template_abs_path,
                                                      output_file)
            return flag
        except exceptions.FileNotFound:
            # the template is a string from command line
            LOG.info(f"{template_abs_path} is not a file")
            self.buffered_writer.write_file_out(output_file, rendered_content)
            return True

    def render_to_files(self, array_of_template_targets):
        sta = Strategy(array_of_template_targets)

        sta.process()
        choice = sta.what_to_do()
        if choice == Strategy.DATA_FIRST:
            self._render_with_finding_data_first(sta.data_file_index)
        else:
            self._render_with_finding_template_first(sta.template_file_index)
        self.buffered_writer.close()

    def _render_with_finding_template_first(self, template_file_index):
        for (template_file, data_output_pairs) in template_file_index.items():
            template = self.engine.get_template(template_file)
            template_abs_path = self.template_fs.geturl(template_file,
                                                        purpose="fs")
            for (data_file, output) in data_output_pairs:
                data = self.context.get_data(data_file)
                flag = self.apply_template(template_abs_path, template, data,
                                           output)
                if flag:
                    reporter.report_templating(self.engine_action,
                                               template_file, output)
                    self.templated_count += 1
                self.file_count += 1

    def _render_with_finding_data_first(self, data_file_index):
        for (data_file, template_output_pairs) in data_file_index.items():
            data = self.context.get_data(data_file)
            for (template_file, output) in template_output_pairs:
                template = self.engine.get_template(template_file)
                template_abs_path = self.template_fs.geturl(template_file,
                                                            purpose="fs")
                flag = self.apply_template(template_abs_path, template, data,
                                           output)
                if flag:
                    reporter.report_templating(self.engine_action,
                                               template_file, output)
                    self.templated_count += 1
                self.file_count += 1
Beispiel #5
0
class MobanEngine(object):
    def __init__(self, template_fs, context_dirs, engine):
        context_dirs = expand_template_directory(context_dirs)
        self.context = Context(context_dirs)
        self.template_fs = template_fs
        self.engine = engine
        self.templated_count = 0
        self.file_count = 0
        self.buffered_writer = BufferedWriter()
        self.engine_action = getattr(
            engine,
            "ACTION_IN_PRESENT_CONTINUOUS_TENSE",
            constants.LABEL_MOBAN_ACTION_IN_PRESENT_CONTINUOUS_TENSE,
        )
        self.engine_actioned = getattr(
            engine,
            "ACTION_IN_PAST_TENSE",
            constants.LABEL_MOBAN_ACTION_IN_PAST_TENSE,
        )
        self.fall_out_targets = []

    def report(self):
        if self.templated_count == 0:
            reporter.report_no_action()
        elif self.templated_count == self.file_count:
            reporter.report_full_run(self.engine_actioned, self.file_count)
        else:
            reporter.report_partial_run(self.engine_actioned,
                                        self.templated_count, self.file_count)

    def number_of_templated_files(self):
        return self.templated_count

    def render_to_file(self, template_file, data_file, output_file):
        data = self.context.get_data(data_file)
        template = self.engine.get_template(template_file)
        try:
            template_abs_path = self.template_fs.geturl(template_file,
                                                        purpose="fs")
        except ResourceNotFound:
            template_abs_path = template_file

        flag = self.apply_template(template_abs_path, template, data,
                                   output_file)
        if flag:
            reporter.report_templating(self.engine_action, template_file,
                                       output_file)
            self.templated_count += 1
        self.file_count += 1

        self.buffered_writer.close()

    def render_string_to_file(self, template_in_string, data_file,
                              output_file):
        template = self.engine.get_template_from_string(template_in_string)
        template_abs_path = f"{template_in_string[:10]}..."
        data = self.context.get_data(data_file)
        flag = self.apply_template(template_abs_path, template, data,
                                   output_file)
        if flag:
            reporter.report_templating(self.engine_action, template_abs_path,
                                       output_file)
            self.templated_count += 1
        self.file_count += 1
        self.buffered_writer.close()

    def apply_template(self, template_abs_path, template, data, output_file):

        # render the content
        rendered_content = self.engine.apply_template(template, data,
                                                      output_file)

        # convert to utf8 if not already
        if not isinstance(rendered_content, bytes):
            rendered_content = rendered_content.encode("utf-8")

        # attempt to output to the file and printing to stdout instead
        # if not found
        try:

            # check if any of the files have changed
            flag = HASH_STORE.is_file_changed(output_file, rendered_content,
                                              template_abs_path)

            # if they have re-render things
            if flag:

                # write the content to the output file
                self.buffered_writer.write_file_out(output_file,
                                                    rendered_content)

                # attempt to copy the file permissions of the template
                # file to the output file

                # if it isn't an archive proceed or stdout
                if (not file_system.is_zip_alike_url(output_file)
                        and output_file != "-"):

                    try:
                        file_system.file_permissions_copy(
                            template_abs_path, output_file)
                    except exceptions.NoPermissionsNeeded:
                        # HttpFs does not have getsyspath
                        # zip, tar have no permission
                        # win32 does not work
                        pass
            return flag
        except exceptions.FileNotFound:
            # the template is a string from command line
            LOG.info(f"{template_abs_path} is not a file")
            self.buffered_writer.write_file_out(output_file, rendered_content)
            return True

    def render_to_files(self, array_of_template_targets):
        sta = Strategy(array_of_template_targets)

        sta.process()
        choice = sta.what_to_do()
        if choice == Strategy.DATA_FIRST:
            self._render_with_finding_data_first(sta.data_file_index)
        else:
            self._render_with_finding_template_first(sta.template_file_index)
        self.buffered_writer.close()

    def _render_with_finding_template_first(self, template_file_index):
        for (template_file, data_output_pairs) in template_file_index.items():
            try:
                template = self.engine.get_template(template_file)
                template_abs_path = self.template_fs.geturl(template_file,
                                                            purpose="fs")
                for (data_file, output) in data_output_pairs:
                    data = self.context.get_data(data_file)
                    flag = self.apply_template(template_abs_path, template,
                                               data, output)
                    if flag:
                        reporter.report_templating(self.engine_action,
                                                   template_file, output)
                        self.templated_count += 1
                    self.file_count += 1
            except exceptions.PassOn as e:
                LOG.info(e)
                for (data_file, output) in data_output_pairs:
                    self.fall_out_targets.append(
                        TemplateTarget(
                            template_file,
                            data_file,
                            output,
                            template_type=constants.TEMPLATE_COPY,
                        ))
                    reporter.report_info_message(
                        f"{self.engine_action} is switched to copy:" +
                        f" {template_file} to {output}")
                continue

    def _render_with_finding_data_first(self, data_file_index):
        for (data_file, template_output_pairs) in data_file_index.items():
            data = self.context.get_data(data_file)
            for (template_file, output) in template_output_pairs:
                try:
                    template = self.engine.get_template(template_file)

                    if isinstance(template, bool):
                        if template:
                            reporter.report_templating(self.engine_action,
                                                       template_file, None)
                            self.templated_count += 1
                    else:
                        template_abs_path = self.template_fs.geturl(
                            template_file, purpose="fs")
                        flag = self.apply_template(template_abs_path, template,
                                                   data, output)
                        if flag:
                            reporter.report_templating(self.engine_action,
                                                       template_file, output)
                            self.templated_count += 1
                    self.file_count += 1
                except exceptions.PassOn:
                    self.fall_out_targets.append(
                        TemplateTarget(
                            template_file,
                            data_file,
                            output,
                            template_type=constants.TEMPLATE_COPY,
                        ))

                    reporter.report_info_message(
                        f"{self.engine_action} is switched to copy:" +
                        f" {template_file} to {output}")
                    continue
Beispiel #6
0
def test_context():
    context = Context(fs.path.join("tests", "fixtures"))
    data = context.get_data("simple.yaml")
    eq_(data["simple"], "yaml")
Beispiel #7
0
def test_non_existent_ctx_directries():
    Context(["abc"])