예제 #1
0
파일: __init__.py 프로젝트: thatch/libioc
    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
예제 #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)
예제 #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
예제 #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))
예제 #5
0
파일: Pkg.py 프로젝트: 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))
예제 #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))
예제 #7
0
파일: helpers.py 프로젝트: iocage/libiocage
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))
예제 #8
0
파일: Pkg.py 프로젝트: iocage/libiocage
 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))
예제 #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)
예제 #10
0
 def test_unicode(self):
     self.assertEqual(ucl.dump({unicode("a") : unicode("b")}), unicode("a = \"b\";\n"))
예제 #11
0
 def test_boolean(self):
     totest = {"a": True, "b": False}
     correct = "a = true;\nb = false;\n"
     self.assertEqual(ucl.dump(totest), correct)
예제 #12
0
 def test_boolean(self):
     totest = {"a" : True, "b" : False}
     correct = "a = true;\nb = false;\n"
     self.assertEqual(ucl.dump(totest), correct)
예제 #13
0
 def test_none(self):
     self.assertEqual(ucl.dump(None), None)
예제 #14
0
 def test_no_args(self):
     with self.assertRaises(TypeError):
         ucl.dump()
예제 #15
0
 def test_empty_ucl(self):
     self.assertEqual(ucl.dump({}), "")
예제 #16
0
 def test_unicode(self):
     self.assertEqual(ucl.dump({u"a" : u"b"}), u"a = \"b\";\n")
예제 #17
0
 def test_multi_args(self):
     self.assertRaises(TypeError, lambda: ucl.dump(0, 0))
예제 #18
0
 def test_no_args(self):
     self.assertRaises(TypeError, lambda: ucl.dump())
예제 #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}')
예제 #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}')
예제 #21
0
 def test_float(self):
     self.assertEqual(ucl.dump({"a" : 1.1}), "a = 1.100000;\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)
예제 #23
0
 def test_int(self):
     data = {"a": 1}
     valid = "a = 1;\n"
     self.assertEqual(ucl.dump(data), valid)
예제 #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)
예제 #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)
예제 #26
0
 def test_multi_args(self):
     with self.assertRaises(TypeError):
         ucl.dump(0, 0)
예제 #27
0
 def test_unicode(self):
     data = {unicode("a"): unicode("b")}
     valid = unicode("a = \"b\";\n")
     self.assertEqual(ucl.dump(data), valid)
예제 #28
0
 def test_null(self):
     data = {"a": None}
     valid = "a = null;\n"
     self.assertEqual(ucl.dump(data), valid)
예제 #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)
예제 #30
0
 def test_nested_int(self):
     data = {"a": {"b": 1}}
     valid = "a {\n    b = 1;\n}\n"
     self.assertEqual(ucl.dump(data), valid)
예제 #31
0
 def test_none(self):
     self.assertEqual(ucl.dump(None), None)
예제 #32
0
 def test_str(self):
     data = {"a": "b"}
     valid = "a = \"b\";\n"
     self.assertEqual(ucl.dump(data), valid)
예제 #33
0
 def test_int(self):
     self.assertEqual(ucl.dump({ "a" : 1 }), "a = 1;\n")
예제 #34
0
 def test_float(self):
     data = {"a": 1.1}
     valid = "a = 1.100000;\n"
     self.assertEqual(ucl.dump(data), valid)
예제 #35
0
 def test_nested_int(self):
     self.assertEqual(ucl.dump({ "a" : { "b" : 1 } }), "a {\n    b = 1;\n}\n")
예제 #36
0
 def test_empty_ucl(self):
     self.assertEqual(ucl.dump({}), "")
예제 #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")
예제 #38
0
 def test_no_args(self):
     with self.assertRaises(TypeError):
         ucl.dump()
예제 #39
0
 def test_str(self):
     self.assertEqual(ucl.dump({"a" : "b"}), "a = \"b\";\n")