-
Notifications
You must be signed in to change notification settings - Fork 6
/
file_object.py
58 lines (46 loc) · 1.77 KB
/
file_object.py
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
## @package PyHoot.file_object
# Object to handle reading from files
## @file file_object.py Implementation of @ref PyHoot.file_object
"""lkkkkkkkkkkkkkkkzssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
ssssssssssssssssssssssssssssssssssssssssssssssssssx
Never let your cat document the code for you"""
import os
import base
class FileObject(base.Base):
""" This class reprsent an file that we read and send to the Server
self._fd = the file descriptor of the file
self.finished_reading: Did we finished reading the file?
"""
## Type is file
NAME = "FILE"
def __init__(self, file_name, base_directory):
""" Creat FileObject
Arguemnts:
file_name: The name of the file for the FileObject
base_directory: The base directory whice we will use for file
locations
"""
super(FileObject, self).__init__()
# Build the address and open the file
self._fd = os.open(os.path.normpath('%s\Files\%s' % (
base_directory, os.path.normpath(file_name))),
os.O_RDONLY | os.O_BINARY
)
## Finished reading the file
self.finished_reading = False
def read_buff(self, length):
""" Read the file up to length of the file"""
ret = ""
while len(ret) < length:
buff = os.read(self._fd, length)
if buff == "": # got to the end of the file
break
ret += buff
self.logger.debug("read %s, length %s", ret, len(ret))
return ret
def get_file_size(self):
""" Return the size of the file"""
return os.fstat(self._fd).st_size
def close(self):
"""Closing the file"""
os.close(self._fd)