def convert_to_macro(keycodes, title):
    """Convert keycodes to DF macro syntax (complete macro file contents)."""
    keybinds = parse_interface_txt(
        os.path.join(exetest.get_main_dir(), 'config/interface.txt'))

    if not title:  # make up a macro title if one is not provided to us
        title = '~qf' + str(random.randrange(0, 999999999))

    output = [title]  # first line of macro is macro title

    for key in keycodes:
        if key == '':
            continue  # empty keycode, output nothing
        elif keybinds.get(key) is None:
            raise KeystrokerError(
                "Key '%s' not bound in config/interface.txt" % key)
        if key == '^':
            output.append('\t\tLEAVESCREEN')  # escape menu key
        else:
            output.extend(keybinds[key])
        output.append('\tEnd of group')
    output.append('End of macro')
    return output
def convert_to_macro(keycodes, title):
    """Convert keycodes to DF macro syntax (complete macro file contents)."""
    keybinds = parse_interface_txt(
        os.path.join(exetest.get_main_dir(), 'config/interface.txt'))

    if not title:  # make up a macro title if one is not provided to us
        title = '~qf' + str(random.randrange(0, 999999999))

    output = [title]  # first line of macro is macro title

    for key in keycodes:
        if key == '':
            continue  # empty keycode, output nothing
        elif keybinds.get(key) is None:
            raise KeystrokerError(
                "Key '%s' not bound in config/interface.txt" % key)
        if key == '^':
            output.append('\t\tLEAVESCREEN')  # escape menu key
        else:
            output.extend(keybinds[key])
        output.append('\tEnd of group')
    output.append('End of macro')
    return output
Exemple #3
0
def convert_blueprint(layers, details, startpos, transform_str, output_mode,
                      output_title, visualize):
    """
    Transforms the provided layers if required by transform_str, then renders
    keystrokes/macros required to plot or visualize the blueprint specified
    by layers and details and pursuant to args.
    """

    # apply aliases.txt to blueprint contents
    # TODO abstract this better
    alii = aliases.load_aliases(
        os.path.join(exetest.get_main_dir(), 'config/aliases.txt'))

    layers = aliases.apply_aliases(layers, alii)

    # transform the blueprint
    ztransforms = []
    if transform_str:
        logmsg('transform', 'Transforming with: %s' % transform_str)

        newphase, transforms, ztransforms = \
            transformer.parse_transform_str(transform_str)

        if newphase is not None:
            details['build_type'] = buildconfig.get_full_build_type_name(
                newphase)

        tran = Transformer(layers, details['start'])
        tran.transform(transforms)  # do the x/y transformations
        details['start'] = tran.start
        layers = tran.layers

        logmsg('file', 'Results of transform:')
        loglines('file', lambda: FileLayer.str_layers(layers))

    layers = FileLayers_to_GridLayers(layers)

    if not layers:  # empty blueprint handling
        raise BlueprintError("Blueprint appears to be empty.")

    # override starting position if startpos command line option was given
    if startpos is not None:
        details['start'] = parse_startpos(startpos, layers[0].grid.width,
                                          layers[0].grid.height)

    # convert layers and other data to Blueprint
    bp = Blueprint('', layers, details)

    # get keys/macrocode to outline or plot the blueprint
    keys = []
    if output_mode == 'csv':
        bp.analyze()
        # perform any awaiting z-transforms
        layers = bp.repeat_ztransforms(ztransforms, bp.layers,
                                       Blueprint.repeater_layers)
        bp.layers = layers
        output = str(bp)
    else:
        if visualize:
            keys = bp.trace_outline()
        else:
            bp.analyze()
            keys = bp.plot(ztransforms)
        output = keystroker.convert_keys(keys, output_mode, output_title)

    loglines('summary', lambda: str_summary(bp, keys))

    return output
"""Handles conversion from QF keycode lists to keystrokes or DF macros."""

from copy import copy
from math import sqrt
import os
import re
import random

from filereader import load_json
from geometry import Area, Direction, add_points, scale_point, midpoint
import exetest
import util

# load global KEY_LIST which is used liberally below and would be inefficient to constantly reload
KEY_LIST = load_json(os.path.join(exetest.get_main_dir(), "config/keys.json"))


class KeystrokerError(Exception):
    """Base class for keystroker errors."""


class Keystroker:
    """
    Computes keycodes needed to go through route and transforms those keycodes
    into keystrokes or DF macro commands.
    Returns list keystrokes or DF macro lines.
    """

    def __init__(self, grid, buildconfig):
        self.grid = grid
        self.buildconfig = buildconfig
import os
import re

import exetest
from filereader import load_json

# load global BUILD_TYPE_CFG which is used liberally below
# and would be inefficient to constantly reload
BUILD_TYPE_CFG = load_json(
    os.path.join(exetest.get_main_dir(), "config/buildconfig.json"))


class BuildConfig:
    """Represents a build config dictionary and provides .get() for access."""

    def __init__(self, build_type):
        self.build_type = build_type
        self.config = BUILD_TYPE_CFG[build_type]

    def get(self, label, forkey=None):
        """
        Returns build configuration value for given label in the build_type
        section associated with this instance.

        When forkey is specified, the key from the build type section's
        "custom" subsection will be used if forkey matches said custom
        key when treating said custom key as a regex pattern.
        """
        if forkey:
            custom = self.config.get('custom') or {}
            for k in custom:
def convert_blueprint(layers, details, startpos, transform_str,
    output_mode, output_title, visualize):
    """
    Transforms the provided layers if required by transform_str, then renders
    keystrokes/macros required to plot or visualize the blueprint specified
    by layers and details and pursuant to args.
    """

    # apply aliases.txt to blueprint contents
    # TODO abstract this better
    alii = aliases.load_aliases(
        os.path.join(exetest.get_main_dir(), 'config/aliases.txt'))

    layers = aliases.apply_aliases(layers, alii)

    # transform the blueprint
    ztransforms = []
    if transform_str:
        logmsg('transform', 'Transforming with: %s' % transform_str)

        newphase, transforms, ztransforms = \
            transformer.parse_transform_str(transform_str)

        if newphase is not None:
            details['build_type'] = buildconfig.get_full_build_type_name(newphase)

        tran = Transformer(layers, details['start'])
        tran.transform(transforms)  # do the x/y transformations
        details['start'] = tran.start
        layers = tran.layers

        logmsg('file', 'Results of transform:')
        loglines('file', lambda: FileLayer.str_layers(layers))

    layers = FileLayers_to_GridLayers(layers)

    if not layers:  # empty blueprint handling
        raise BlueprintError("Blueprint appears to be empty.")

    # override starting position if startpos command line option was given
    if startpos is not None:
        details['start'] = parse_startpos(startpos,
            layers[0].grid.width,
            layers[0].grid.height)

    # convert layers and other data to Blueprint
    bp = Blueprint('', layers, details)

    # get keys/macrocode to outline or plot the blueprint
    keys = []
    if output_mode == 'csv':
        bp.analyze()
        # perform any awaiting z-transforms
        layers = bp.repeat_ztransforms(ztransforms, bp.layers,
            Blueprint.repeater_layers)
        bp.layers = layers
        output = str(bp)
    else:
        if visualize:
            keys = bp.trace_outline()
        else:
            bp.analyze()
            keys = bp.plot(ztransforms)
        output = keystroker.convert_keys(keys, output_mode, output_title)

    loglines('summary', lambda: str_summary(bp, keys))

    return output
"""Handles conversion from QF keycode lists to keystrokes or DF macros."""

from copy import copy
from math import sqrt
import os
import re
import random

from filereader import load_json
from geometry import Area, Direction, add_points, scale_point, midpoint
import exetest
import util

# load global KEY_LIST which is used liberally below and would be inefficient to constantly reload
KEY_LIST = load_json(os.path.join(exetest.get_main_dir(), "config/keys.json"))


class KeystrokerError(Exception):
    """Base class for keystroker errors."""


class Keystroker:
    """
    Computes keycodes needed to go through route and transforms those keycodes
    into keystrokes or DF macro commands.
    Returns list keystrokes or DF macro lines.
    """
    def __init__(self, grid, buildconfig):
        self.grid = grid
        self.buildconfig = buildconfig
import os
import re

import exetest
from filereader import load_json

# load global BUILD_TYPE_CFG which is used liberally below
# and would be inefficient to constantly reload
BUILD_TYPE_CFG = load_json(
    os.path.join(exetest.get_main_dir(), "config/buildconfig.json"))


class BuildConfig:
    """Represents a build config dictionary and provides .get() for access."""
    def __init__(self, build_type):
        self.build_type = build_type
        self.config = BUILD_TYPE_CFG[build_type]

    def get(self, label, forkey=None):
        """
        Returns build configuration value for given label in the build_type
        section associated with this instance.

        When forkey is specified, the key from the build type section's
        "custom" subsection will be used if forkey matches said custom
        key when treating said custom key as a regex pattern.
        """
        if forkey:
            custom = self.config.get('custom') or {}
            for k in custom:
                if re.match(k, forkey):