Exemplo n.º 1
0
    def save(self) -> bool:
        """Save the changes to the file."""
        if self.changed is False:
            if self.logger is not None:
                self.logger.debug(
                    f"{self._file} was not modified - skipping write")
            return False

        with open(self.path, "w") as rcconf:
            import ucl
            output = ucl.dump(self, ucl.UCL_EMIT_CONFIG)
            output = output.replace(" = \"", "=\"")
            output = output.replace("\";\n", "\"\n")

            if self.logger is not None:
                self.logger.verbose(f"Writing {self._file} to {self.path}")

            rcconf.write(output)
            rcconf.truncate()
            rcconf.close()

            self._file_content_changed = False
            if self.logger is not None:
                self.logger.spam(output[:-1], indent=1)

            return True
Exemplo n.º 2
0
 def test_json(self):
     data = {"a": 1, "b": "bleh;"}
     valid = [
         '{\n    "a": 1,\n    "b": "bleh;"\n}',
         '{\n    "b": "bleh;",\n    "a": 1\n}'
     ]
     self.assertIn(ucl.dump(data, ucl.UCL_EMIT_JSON), valid)
Exemplo n.º 3
0
    def save(self) -> bool:
        """Save the changes to the file."""
        if self.changed is False:
            if self.logger is not None:
                self.logger.debug(
                    f"{self._file} was not modified - skipping write"
                )
            return False

        with open(self.path, "w") as rcconf:
            import ucl
            output = ucl.dump(self, ucl.UCL_EMIT_CONFIG)
            output = output.replace(" = \"", "=\"")
            output = output.replace("\";\n", "\"\n")

            if self.logger is not None:
                self.logger.verbose(f"Writing {self._file} to {self.path}")

            rcconf.write(output)
            rcconf.truncate()
            rcconf.close()

            self._file_content_changed = False
            if self.logger is not None:
                self.logger.spam(output[:-1], indent=1)

            return True
Exemplo n.º 4
0
def to_ucl(data: typing.Dict[str, typing.Any]) -> str:
    output_data = {}
    for key, value in data.items():
        output_data[key] = to_string(value,
                                     true="on",
                                     false="off",
                                     none="none")
    return str(ucl.dump(output_data))
Exemplo n.º 5
0
Arquivo: Pkg.py Projeto: thatch/libioc
 def _update_pkg_conf(self, filename: str,
                      data: typing.Dict[str, _PkgConfDataType]) -> None:
     if os.path.exists(filename) and os.path.islink(filename):
         raise libioc.errors.SecurityViolation(
             reason="Refusing to write to a symlink", logger=self.logger)
     import ucl
     with open(filename, "w", encoding="utf-8") as f:
         f.write(ucl.dump(data, ucl.UCL_EMIT_JSON))
Exemplo n.º 6
0
def to_ucl(data: typing.Dict[str, typing.Any]) -> str:
    """Create UCL content from the input data."""
    output_data = {}
    for key, value in data.items():
        output_data[key] = to_string(value,
                                     true="on",
                                     false="off",
                                     none="none")
    import ucl
    return str(ucl.dump(output_data))
Exemplo n.º 7
0
def to_ucl(data: typing.Dict[str, typing.Any]) -> str:
    """Create UCL content from the input data."""
    output_data = {}
    for key, value in data.items():
        output_data[key] = to_string(
            value,
            true="on",
            false="off",
            none="none"
        )
    import ucl
    return str(ucl.dump(output_data))
Exemplo n.º 8
0
 def _update_pkg_conf(
     self,
     filename: str,
     data: typing.Dict[str, _PkgConfDataType]
 ) -> None:
     if os.path.exists(filename) and os.path.islink(filename):
         raise libioc.errors.SecurityViolation(
             reason="Refusing to write to a symlink",
             logger=self.logger
         )
     import ucl
     with open(filename, "w", encoding="utf-8") as f:
         f.write(ucl.dump(data, ucl.UCL_EMIT_JSON))
Exemplo n.º 9
0
    def save(self):

        if self._file_content_changed is False:
            self.logger.debug("rc.conf was not modified - skipping write")
            return

        with open(self.path, "w") as rcconf:
            output = ucl.dump(self, ucl.UCL_EMIT_CONFIG)
            output = output.replace(" = \"", "=\"")
            output = output.replace("\";\n", "\"\n")

            self.logger.verbose(f"Writing rc.conf to {self.path}",
                                jail=self.jail)

            rcconf.write(output)
            rcconf.truncate()
            rcconf.close()

            self.logger.spam(output[:-1], jail=self.jail, indent=1)
Exemplo n.º 10
0
 def test_unicode(self):
     self.assertEqual(ucl.dump({unicode("a") : unicode("b")}), unicode("a = \"b\";\n"))
Exemplo n.º 11
0
 def test_boolean(self):
     totest = {"a": True, "b": False}
     correct = "a = true;\nb = false;\n"
     self.assertEqual(ucl.dump(totest), correct)
Exemplo n.º 12
0
 def test_boolean(self):
     totest = {"a" : True, "b" : False}
     correct = "a = true;\nb = false;\n"
     self.assertEqual(ucl.dump(totest), correct)
Exemplo n.º 13
0
 def test_none(self):
     self.assertEqual(ucl.dump(None), None)
Exemplo n.º 14
0
 def test_no_args(self):
     with self.assertRaises(TypeError):
         ucl.dump()
Exemplo n.º 15
0
 def test_empty_ucl(self):
     self.assertEqual(ucl.dump({}), "")
Exemplo n.º 16
0
 def test_unicode(self):
     self.assertEqual(ucl.dump({u"a" : u"b"}), u"a = \"b\";\n")
Exemplo n.º 17
0
 def test_multi_args(self):
     self.assertRaises(TypeError, lambda: ucl.dump(0, 0))
Exemplo n.º 18
0
 def test_no_args(self):
     self.assertRaises(TypeError, lambda: ucl.dump())
Exemplo n.º 19
0
 def test_json(self):
     self.assertEqual(ucl.dump({
         "a": 1,
         "b": "bleh;"
     }, ucl.UCL_EMIT_JSON), '{\n    "a": 1,\n    "b": "bleh;"\n}')
Exemplo n.º 20
0
 def test_json(self):
     self.assertEqual(ucl.dump({ "a" : 1, "b": "bleh;" }, ucl.UCL_EMIT_JSON), '{\n    "a": 1,\n    "b": "bleh;"\n}')
Exemplo n.º 21
0
 def test_float(self):
     self.assertEqual(ucl.dump({"a" : 1.1}), "a = 1.100000;\n")
Exemplo n.º 22
0
 def test_boolean(self):
     totest = {"a" : True, "b" : False}
     correct = ["a = true;\nb = false;\n", "b = false;\na = true;\n"]
     self.assertIn(ucl.dump(totest), correct)
Exemplo n.º 23
0
 def test_int(self):
     data = {"a": 1}
     valid = "a = 1;\n"
     self.assertEqual(ucl.dump(data), valid)
Exemplo n.º 24
0
 def test_json(self):
     totest = { "a" : 1, "b": "bleh;" }
     correct = ['{\n    "a": 1,\n    "b": "bleh;"\n}',
                '{\n    "b": "bleh;",\n    "a": 1\n}']
     self.assertIn(ucl.dump(totest, ucl.UCL_EMIT_JSON), correct)
Exemplo n.º 25
0
 def test_int_array(self):
     data = {"a": [1, 2, 3, 4]}
     valid = "a [\n    1,\n    2,\n    3,\n    4,\n]\n"
     self.assertEqual(ucl.dump(data), valid)
Exemplo n.º 26
0
 def test_multi_args(self):
     with self.assertRaises(TypeError):
         ucl.dump(0, 0)
Exemplo n.º 27
0
 def test_unicode(self):
     data = {unicode("a"): unicode("b")}
     valid = unicode("a = \"b\";\n")
     self.assertEqual(ucl.dump(data), valid)
Exemplo n.º 28
0
 def test_null(self):
     data = {"a": None}
     valid = "a = null;\n"
     self.assertEqual(ucl.dump(data), valid)
Exemplo n.º 29
0
 def test_boolean(self):
     data = {"a": True, "b": False}
     valid = ["a = true;\nb = false;\n", "b = false;\na = true;\n"]
     self.assertIn(ucl.dump(data), valid)
Exemplo n.º 30
0
 def test_nested_int(self):
     data = {"a": {"b": 1}}
     valid = "a {\n    b = 1;\n}\n"
     self.assertEqual(ucl.dump(data), valid)
Exemplo n.º 31
0
 def test_none(self):
     self.assertEqual(ucl.dump(None), None)
Exemplo n.º 32
0
 def test_str(self):
     data = {"a": "b"}
     valid = "a = \"b\";\n"
     self.assertEqual(ucl.dump(data), valid)
Exemplo n.º 33
0
 def test_int(self):
     self.assertEqual(ucl.dump({ "a" : 1 }), "a = 1;\n")
Exemplo n.º 34
0
 def test_float(self):
     data = {"a": 1.1}
     valid = "a = 1.100000;\n"
     self.assertEqual(ucl.dump(data), valid)
Exemplo n.º 35
0
 def test_nested_int(self):
     self.assertEqual(ucl.dump({ "a" : { "b" : 1 } }), "a {\n    b = 1;\n}\n")
Exemplo n.º 36
0
 def test_empty_ucl(self):
     self.assertEqual(ucl.dump({}), "")
Exemplo n.º 37
0
 def test_int_array(self):
     self.assertEqual(ucl.dump({ "a" : [1,2,3,4]}), "a [\n    1,\n    2,\n    3,\n    4,\n]\n")
Exemplo n.º 38
0
 def test_no_args(self):
     with self.assertRaises(TypeError):
         ucl.dump()
Exemplo n.º 39
0
 def test_str(self):
     self.assertEqual(ucl.dump({"a" : "b"}), "a = \"b\";\n")