Exemplo n.º 1
0
def test_dumps_comments(c, ensure_ascii_comments):
    s = dumps(
        {"key": "value"},
        timestamp=False,
        comments=c,
        ensure_ascii_comments=ensure_ascii_comments,
    )
    assert s == to_comment(c, ensure_ascii=ensure_ascii_comments) \
                + '\nkey=value\n'
    if ensure_ascii_comments is None:
        assert s == dumps({"key": "value"}, timestamp=False, comments=c)
Exemplo n.º 2
0
def _encode_env(target: BuildTarget) -> str:
    assert target.compiler_instance.has_cached_compiler_env()

    target_compiler_env: dict[str, str] = _multi_line_compiler_env_to_single_line(compiler_env=target.compiler_instance.cached_compiler_env)
    encoded_env: str = javaproperties.dumps(props=target_compiler_env, timestamp=False)

    return encoded_env
Exemplo n.º 3
0
def test_dumps_ensure_ascii_cross_ensure_ascii_comments(pout, ea, cout, eac):
    assert dumps(
        {"x\xF0": "\u2603\U0001F410"},
        timestamp=False,
        comments="x\xF0\u2603\U0001F410",
        ensure_ascii=ea,
        ensure_ascii_comments=eac,
    ) == cout + pout
Exemplo n.º 4
0
    def _write(self, spectra, spec_file, as_bytes):
        """
        Writes the spectra to the filehandle.

        :param spectra: the list of spectra
        :type spectra: list
        :param spec_file: the file handle to use
        :type spec_file: file
        :param as_bytes: whether to write as bytes or string
        :type as_bytes: bool
        """
        # Create a writing function which handles the as_bytes argument
        if as_bytes:
            def write(string: str):
                spec_file.write(string.encode())
        else:
            write = spec_file.write

        first = True
        for spectrum in spectra:
            if not first:
                write(SEPARATOR + "\n")

            if self._options_parsed.output_sampledata:
                # prefix sample data with '# '
                props = Properties()
                for k in spectrum.sample_data:
                    v = spectrum.sample_data[k]
                    props[k] = str(v)
                    if isinstance(v, int) or isinstance(v, float):
                        props[k + DATATYPE_SUFFIX] = "N"
                    elif isinstance(v, bool):
                        props[k + DATATYPE_SUFFIX] = "B"
                    elif isinstance(v, str):
                        props[k + DATATYPE_SUFFIX] = "S"
                    else:
                        props[k + DATATYPE_SUFFIX] = "U"
                samplestr = dumps(props)
                lines = samplestr.split("\n")
                for i in range(len(lines)):
                    lines[i] = COMMENT + lines[i]

                # sample data
                for line in lines:
                    write(line + "\n")

            # header
            write(HEADER + "\n")

            # spectral data
            for i in range(len(spectrum)):
                write("%s,%s\n" % (spectrum.waves[i], spectrum.amplitudes[i]))

            first = False
Exemplo n.º 5
0
def disable_tls13():
    security_file = "/usr/lib/jvm/default-jvm/jre/conf/security/java.security"

    with open(security_file) as f:
        data = javaproperties.loads(f.read())

        if "TLSv1.3" in data["jdk.tls.disabledAlgorithms"]:
            return

    with open(security_file, "w") as f:
        data["jdk.tls.disabledAlgorithms"] = "TLSv1.3, " + data[
            "jdk.tls.disabledAlgorithms"]
        f.write(javaproperties.dumps(data))
Exemplo n.º 6
0
def dumps(obj, fmt):

    if fmt == constants.JSON:
        return json.dumps(obj=obj, sort_keys=True, indent=2)

    elif fmt == constants.YAML:
        stream = six.StringIO()
        yaml.safe_dump(data=obj, stream=stream, default_flow_style=False)
        return stream.getvalue()

    elif fmt == constants.PROPERTIES:

        # only dictionaries can be represented as a string
        # in the java properties file format
        if not isinstance(obj, dict):
            raise exceptions.InvalidValueTypeException(expected_types=[dict],
                                                       actual_type=type(obj))
        for key, value in obj.items():
            if isinstance(value, (dict, list, set)):
                raise exceptions.InvalidValueTypeException(
                    expected_types=[str, int, float], actual_type=type(value))
            obj[key] = str(value)

        return javaproperties.dumps(props=obj, timestamp=False)

    elif fmt == constants.INI:

        ini_parser = configparser.ConfigParser()
        string = six.StringIO()
        ini_parser.read_dict(dictionary=obj)
        ini_parser.write(fp=string, space_around_delimiters=False)

        return string.getvalue()

    else:
        raise exceptions.UnsupportedFormatException(fmt=fmt)
Exemplo n.º 7
0
def dumps(obj, fmt):

    if fmt == constants.JSON:
        return json.dumps(obj=obj, sort_keys=True, indent=2)

    elif fmt == constants.YAML:
        stream = six.StringIO()
        yaml.safe_dump(data=obj, stream=stream, default_flow_style=False)
        return stream.getvalue()

    elif fmt == constants.PROPERTIES:

        # only dictionaries can be represented as a string
        # in the java properties file format
        if not isinstance(obj, dict):
            raise exceptions.InvalidValueTypeException(expected_types=[dict],
                                                       actual_type=type(obj))
        for key, value in obj.items():
            if isinstance(value, (dict, list, set)):
                raise exceptions.InvalidValueTypeException(expected_types=[str, int, float],
                                                           actual_type=type(value))
            obj[key] = str(value)

        return javaproperties.dumps(props=obj, timestamp=False)

    elif fmt == constants.INI:

        ini_parser = configparser.ConfigParser()
        string = six.StringIO()
        ini_parser.read_dict(dictionary=obj)
        ini_parser.write(fp=string, space_around_delimiters=False)

        return string.getvalue()

    else:
        raise exceptions.UnsupportedFormatException(fmt=fmt)
Exemplo n.º 8
0
def test_dumps_latin_1():
    assert dumps({"edh": "\xF0"}, timestamp=False) == 'edh=\\u00f0\n'
Exemplo n.º 9
0
def test_dumps_tilde():
    assert dumps({"tilde": "~"}, timestamp=False) == 'tilde=~\n'
Exemplo n.º 10
0
def test_dumps_three_space_value():
    assert dumps({"key": "   "}, timestamp=False) == 'key=\\ \\ \\ \n'
Exemplo n.º 11
0
def test_dumps_space_in_value():
    assert dumps({"key": "two words"}, timestamp=False) == 'key=two words\n'
Exemplo n.º 12
0
def test_dumps_ordereddict_rev_sorted():
    assert dumps(
        OrderedDict([("zebra", "apple"), ("key", "value")]),
        timestamp=False,
        sort_keys=True,
    ) == 'key=value\nzebra=apple\n'
Exemplo n.º 13
0
def test_dumps_two_simple_rev_sorted():
    assert dumps(
        [("zebra", "apple"), ("key", "value")],
        timestamp=False,
        sort_keys=True,
    ) == 'key=value\nzebra=apple\n'
Exemplo n.º 14
0
def test_dumps_simple():
    assert dumps({"key": "value"}, timestamp=False) == 'key=value\n'
Exemplo n.º 15
0
def test_dumps_naive_datetime():
    assert dumps(
        {"key": "value"},
        timestamp=datetime.fromtimestamp(1473703254)
    ) == '#Mon Sep 12 14:00:54 EDT 2016\nkey=value\n'
Exemplo n.º 16
0
def test_dumps_backspace():
    assert dumps({"backspace": "\b"}, timestamp=False) == 'backspace=\\u0008\n'
Exemplo n.º 17
0
def test_dumps_aware_datetime():
    assert dumps(
        {"key": "value"},
        timestamp=datetime.fromtimestamp(1473703254, tzoffset('PDT', -25200))
    ) == '#Mon Sep 12 11:00:54 PDT 2016\nkey=value\n'
Exemplo n.º 18
0
def test_dumps_two_simple_rev():
    assert dumps([("zebra", "apple"), ("key", "value")], timestamp=False) == \
        'zebra=apple\nkey=value\n'
Exemplo n.º 19
0
def test_dumps_timestamp_and_comment():
    assert dumps(
        {"key": "value"},
        comments='This is a comment.',
        timestamp=1473703254
    ) == '#This is a comment.\n#Mon Sep 12 14:00:54 EDT 2016\nkey=value\n'
Exemplo n.º 20
0
def test_dumps_ordereddict_rev():
    assert dumps(
        OrderedDict([("zebra", "apple"), ("key", "value")]),
        timestamp=False,
    ) == 'zebra=apple\nkey=value\n'
Exemplo n.º 21
0
def test_dumps_equals():
    assert dumps({"equals": "="}, timestamp=False) == 'equals=\\=\n'
Exemplo n.º 22
0
def test_dumps_space_in_key():
    assert dumps({"two words": "value"}, timestamp=False) == \
        'two\\ words=value\n'
Exemplo n.º 23
0
def test_dumps_nothing():
    assert dumps({}, timestamp=False) == ''
Exemplo n.º 24
0
def test_dumps_trailing_space_in_value():
    assert dumps({"key": "value "}, timestamp=False) == 'key=value \n'
Exemplo n.º 25
0
def test_dumps_colon():
    assert dumps({"colon": ":"}, timestamp=False) == 'colon=\\:\n'
Exemplo n.º 26
0
def test_dumps_x1F():
    assert dumps({"US": "\x1F"}, timestamp=False) == 'US=\\u001f\n'
Exemplo n.º 27
0
def test_dumps_hash():
    assert dumps({"hash": "#"}, timestamp=False) == 'hash=\\#\n'
Exemplo n.º 28
0
def test_dumps_delete():
    assert dumps({"delete": "\x7F"}, timestamp=False) == 'delete=\\u007f\n'
Exemplo n.º 29
0
def test_dumps_exclamation():
    assert dumps({"exclamation": "!"}, timestamp=False) == 'exclamation=\\!\n'
Exemplo n.º 30
0
def test_dumps_non_latin_1():
    assert dumps({"snowman": "\u2603"}, timestamp=False) == 'snowman=\\u2603\n'
Exemplo n.º 31
0
def test_dumps_null():
    assert dumps({"null": "\0"}, timestamp=False) == 'null=\\u0000\n'