Пример #1
0
def include_resource(resource):
    import sublime
    import os
    file_path = os.path.join(sublime.packages_path(), resource)
    file_contents = sublime.load_resource(resource)
    return process_macros(
        file_contents,
        arguments={"file_path": file_path},
    )
Пример #2
0
def build(source_text, destination_path, error_stream, arguments,
          error_highlighter):
    t0 = time.perf_counter()

    error_stream.print(
        'Building %s... (%s)' %
        (path.basename(arguments['file_path']), arguments['file_path']))

    def done(message):
        error_stream.print('[{message} in {time:.2f} seconds.]\n'.format(
            message=message, time=time.perf_counter() - t0))

    def handle_error(e):
        if isinstance(e, MacroError):
            error_stream.print()
            error_stream.print(e.message)
            error_stream.print(str(e.node.start_mark))

            if e.__cause__:
                handle_error(e.__cause__)

            error_highlighter.highlight(
                e.context.get('file_path'),
                e.node.start_mark.index,
                e.node.end_mark.index,
                e.message,
            )
        else:
            error_stream.print()
            error_stream.print(''.join(
                traceback.format_exception(None, e, e.__traceback__)))

    try:
        result = process_macros(source_text, arguments=arguments)
    except Exception as e:
        handle_error(e)
        done('Failed')
        return

    serializer = get_yaml_instance()

    with open(destination_path, 'w') as output_file:
        serializer.dump(result, stream=output_file)
        error_stream.print('Compiled to %s. (%s)' %
                           (path.basename(destination_path), destination_path))
        done('Succeeded')
Пример #3
0
    def run(self,
            *,
            source_path=None,
            destination_path=None,
            working_dir=None,
            arguments={},
            build_id='YAMLMacros'):
        t0 = time.perf_counter()

        if working_dir:
            os.chdir(working_dir)

        if not source_path:
            source_path = self.window.active_view().file_name()

        with open(source_path, 'r') as source_file:
            source_text = source_file.read()

        if not destination_path:
            destination_path, extension = path.splitext(source_path)
            if extension != '.yaml-macros': raise "Not a .yaml-macros file!"

        arguments['file_path'] = source_path

        panel = OutputPanel(self.window, build_id)
        panel.show()
        panel.print('Building %s... (%s)' %
                    (path.basename(source_path), source_path))

        for v in self.window.views():
            v.erase_phantoms('YAMLMacros')

        def done(message):
            panel.print('[{message} in {time:.2} seconds.]\n'.format(
                message=message, time=time.perf_counter() - t0))

        def handle_error(e):
            if isinstance(e, MacroError):
                panel.print()
                panel.print(e.message)
                panel.print(str(e.node.start_mark))

                if e.__cause__:
                    handle_error(e.__cause__)

                v = self.window.find_open_file(e.context.get('file_path'))
                if v:
                    v.add_phantom(
                        'YAMLMacros',
                        sublime.Region(e.node.start_mark.index,
                                       e.node.end_mark.index),
                        PHANTOM_TEMPLATE.format(e.message),
                        sublime.LAYOUT_BELOW,
                    )
            else:
                panel.print()
                panel.print(''.join(
                    traceback.format_exception(None, e, e.__traceback__)))

        try:
            result = process_macros(source_text, arguments=arguments)
        except Exception as e:
            handle_error(e)
            done('Failed')
            return

        serializer = get_yaml_instance()

        with open(destination_path, 'w') as output_file:
            serializer.dump(result, stream=output_file)
            panel.print('Compiled to %s. (%s)' %
                        (path.basename(destination_path), destination_path))
            done('Succeeded')
Пример #4
0
#!/bin/env python3

import sys
from os import path

sys.path.append(path.join(path.dirname(path.realpath(__file__)), '../..'))

from YAMLMacros.api import process_macros
from YAMLMacros.api import get_yaml_instance

result = process_macros(sys.stdin.read())

serializer = get_yaml_instance()
serializer.dump(result, stream=sys.stdout)
Пример #5
0
def include(path):
    with open(path, 'r') as file:
        return process_macros(
            file.read(),
            arguments={"file_path": path},
        )