forked from medbenali/CyberScan
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathFileUtils.py
More file actions
155 lines (122 loc) · 3.73 KB
/
Copy pathFileUtils.py
File metadata and controls
155 lines (122 loc) · 3.73 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python
# -*- coding utf-8 -*-
#
# This file is part of CyberScan
#
# CyberScan is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# CyberScan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor Boston,
# MA 02110-1301, USA.
#
# Author: Mohamed BEN ALI
import os
import os.path
class File(object):
def __init__(self, *pathComponents):
self._path = FileUtils.buildPath(*pathComponents)
self.content = None
@property
def path(self):
return self._path
@path.setter
def path(self, value):
raise NotImplemented
def isValid(self):
return FileUtils.isFile(self.path)
def exists(self):
return FileUtils.exists(self.path)
def canRead(self):
return FileUtils.canRead(self.path)
def canWrite(self):
return FileUtils.canWrite(self.path)
def read(self):
return FileUtils.read(self.path)
def update(self):
self.content = self.read()
def content(self):
if not self.content:
self.content = FileUtils.read()
return self.content()
def getLines(self):
for line in FileUtils.getLines(self.path):
yield line
def __cmp__(self, other):
if not isinstance(other, File):
raise NotImplemented
return cmp(self.content(), other.content())
def __enter__(self):
return self
def __exit__(self, type, value, tb):
pass
class FileUtils(object):
@staticmethod
def buildPath(*pathComponents):
if pathComponents:
path = os.path.join(*pathComponents)
else:
path = ''
return path
@staticmethod
def exists(fileName):
return os.access(fileName, os.F_OK)
@staticmethod
def canRead(fileName):
if not os.access(fileName, os.R_OK):
return False
try:
with open(fileName):
pass
except IOError:
return False
return True
@staticmethod
def canWrite(fileName):
return os.access(fileName, os.W_OK)
@staticmethod
def read(fileName):
result = ''
with open(fileName, 'r') as fd:
for line in fd.readlines():
result += line
return result
@staticmethod
def getLines(fileName):
with open(fileName, 'r', errors="replace") as fd:
return fd.read().splitlines()
@staticmethod
def isDir(fileName):
return os.path.isdir(fileName)
@staticmethod
def isFile(fileName):
return os.path.isfile(fileName)
@staticmethod
def createDirectory(directory):
if not FileUtils.exists(directory):
os.makedirs(directory)
@staticmethod
def sizeHuman(num):
base = 1024
for x in ['B ', 'KB', 'MB', 'GB']:
if num < base and num > -base:
return "%3.0f%s" % (num, x)
num /= base
return "%3.0f %s" % (num, 'TB')
@staticmethod
def writeLines(fileName, lines):
content = None
if type(lines) is list:
content = "\n".join(lines)
else:
content = lines
with open(fileName, "w") as f:
f.writelines(content)