Passed
Pull Request — master (#372)
by
unknown
02:30
created

asyncua.client.ua_file.UaFile.close()   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
from asyncua import ua
2
3
class UaFile:
4
    def __init__(self, file_node):
5
        self._file_node = file_node
6
7
    async def open(self, open_mode):
8
        """ open file method """
9
        open_node = await self._file_node.get_child("Open")
10
        arg = ua.Variant(open_mode, ua.VariantType.Byte)
11
        return await self._file_node.call_method(open_node, arg)
12
13
    async def get_size(self):
14
        """ gets size of file """
15
        size_node = await self._file_node.get_child("Size")
16
        return await size_node.read_value()
17
18
    async def close(self, handle):
19
        """ close file method """
20
        read_node = await self._file_node.get_child("Close")
21
        arg1 = ua.Variant(handle, ua.VariantType.UInt32)
22
        return await self._file_node.call_method(read_node, arg1)
23
24
    async def read(self, handle, size):
25
        """
26
        :param handle: handle from open()
27
        :param size: size of file from from get_size()
28
        """
29
        read_node = await self._file_node.get_child("Read")
30
        arg1 = ua.Variant(handle, ua.VariantType.UInt32)
31
        arg2 = ua.Variant(size, ua.VariantType.Int32)
32
        return await self._file_node.call_method(read_node, arg1, arg2)
33
34
    async def read_once(self):
35
        """ open, read, close in one operation """
36
        handle = await self.open(ua.OpenFileMode.Read.value)
37
        size = await self.get_size()
38
        contents = await self.read(handle, size)
39
        await self.close(handle)
40
        return contents
41