File: __init__.py

package info (click to toggle)
python-trubar 0.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 716 kB
  • sloc: python: 2,968; sh: 375; makefile: 3; javascript: 1
file content (35 lines) | stat: -rw-r--r-- 940 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import shutil
import tempfile
import unittest

from trubar.messages import dict_to_msg_nodes, dict_from_msg_nodes


def yamlized(func):
    def yamlized_func(*args, **kwargs):
        args = [dict_to_msg_nodes(arg) if isinstance(arg, dict) else arg
                for arg in args]
        res = func(*args, **kwargs)
        return dict_from_msg_nodes(res) if isinstance(res, dict) else res
    return yamlized_func


class TestBase(unittest.TestCase):
    def setUp(self) -> None:
        super().setUp()
        self.tmpdir = None

    def tearDown(self) -> None:
        super().tearDown()
        if self.tmpdir is not None:
            shutil.rmtree(self.tmpdir)

    def prepare_file(self, filename, s):
        if self.tmpdir is None:
            self.tmpdir = tempfile.mkdtemp()

        fn = os.path.join(self.tmpdir, filename)
        with open(fn, "w", encoding="utf-8") as f:
            f.write(s)
        return fn