<commit_before>import os.path
import shutil

from collections import OrderedDict

from .base import Generator, run_process, MobileLayoutView
from ..base import get_logger
import json

logger = get_logger(__file__)


class JSONGenerator(Generator):
    def generate(self, base="."):
        out_dir = os.path.abspath(base)
        os.makedirs(out_dir, exist_ok=True)
        fn = os.path.join(out_dir, "%s.json" % os.path.splitext(self._bundle.path)[0])

        layouts = OrderedDict()

        for name, layout in self._bundle.layouts.items():
            layouts[name] = layout

        with open(fn, "w") as f:
            json.dump({"layouts": layouts}, f, indent=2, ensure_ascii=False)

class QRGenerator(Generator):
    def generate(self, base="."):
        if not shutil.which("qrencode"):
            logger.error("`qrencode` not found on PATH.")
            return

        for name, layout in self._bundle.layouts.items():
            logger.info("Choosing first layout from project: %s" % name)
            tree = layout
            break

        layout_view = MobileLayoutView(tree, "ios")

        o = {
            "name": layout.native_display_name,
            "space": tree["strings"]["space"],
            "enter": tree["strings"]["return"],
            "normal": layout_view.mode("default"),
            "shifted": layout_view.mode("shift"),
            "longPress": tree["longpress"]
        }

        data = json.dumps(o, ensure_ascii=False, separators=(',', ':'))
        logger.debug(data)
        fn_path = os.path.abspath(os.path.join(base, "%s.png" % name))
        run_process(["qrencode", data, "-o", fn_path], shell=False)
        logger.info("QR code generated at: %s" % fn_path)<commit_msg>Fix QR code generator<commit_after>import os.path
import shutil

from collections import OrderedDict

from .base import Generator, run_process, MobileLayoutView
from ..base import get_logger
import json

logger = get_logger(__file__)


class JSONGenerator(Generator):
    def generate(self, base="."):
        out_dir = os.path.abspath(base)
        os.makedirs(out_dir, exist_ok=True)
        fn = os.path.join(out_dir, "%s.json" % os.path.splitext(self._bundle.path)[0])

        layouts = OrderedDict()

        for name, layout in self._bundle.layouts.items():
            layouts[name] = layout

        with open(fn, "w") as f:
            json.dump({"layouts": layouts}, f, indent=2, ensure_ascii=False)

class QRGenerator(Generator):
    def generate(self, base="."):
        if not shutil.which("qrencode"):
            logger.error("`qrencode` not found on PATH.")
            return

        command = self._args.get("command", None)
        logger.trace("Command: %s" % command)
        preferred_locale = None
        if command is not None:
            preferred_locale = command

        tree = None
        for name, layout in self._bundle.layouts.items():
            if preferred_locale is not None:
                if name == preferred_locale:
                    logger.info("Using given locale: %s", preferred_locale)
                    tree = layout
                    break
            else:
                logger.info("Choosing first layout from project: %s" % name)
                tree = layout
                break
        
        if tree is None:
            logger.error("No locale found.")
            return

        layout_view = MobileLayoutView(tree, "ios")

        o = {
            "name": layout.display_names[name],
            "space": tree.strings.space,
            "enter": tree.strings._return,
            "normal": layout_view.mode("default"),
            "shifted": layout_view.mode("shift"),
            "longPress": tree.longpress
        }

        data = json.dumps(o, ensure_ascii=False, separators=(',', ':'))
        logger.debug(data)
        fn_path = os.path.abspath(os.path.join(base, "%s.png" % name))
        run_process(["qrencode", data, "-o", fn_path], shell=False)
        logger.info("QR code generated at: %s" % fn_path)
{'fraction_non_alphanumeric': 0.1290483308420528, 'fraction_numerical': 0.000996512207274539, 'mean_word_length': 2.269543973941368, 'pattern_counts': {'":': 14, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6466923', 'n_tokens_mistral': 1201, 'n_tokens_neox': 1166, 'n_words': 282}