@@ -10,22 +10,23 @@ class FileHandler(object):
10
10
11
11
:cvar string infile: name of the input file to be processed.
12
12
:cvar string outfile: name of the output file where to write in.
13
- :cvar string extension: extension of the input/output files. It is specific for each
13
+ :cvar list extensions: extensions of the input/output files. It is specific for each
14
14
subclass.
15
15
"""
16
16
17
17
def __init__ (self ):
18
18
self .infile = None
19
19
self .outfile = None
20
- self .extension = None
20
+ self .extensions = []
21
21
22
22
def parse (self , * args ):
23
23
"""
24
24
Abstract method to parse a specific file.
25
25
26
26
Not implemented, it has to be implemented in subclasses.
27
27
"""
28
- raise NotImplementedError ("Subclass must implement abstract method " \
28
+ raise NotImplementedError (
29
+ "Subclass must implement abstract method " \
29
30
+ self .__class__ .__name__ + ".parse" )
30
31
31
32
def write (self , * args ):
@@ -34,21 +35,22 @@ def write(self, *args):
34
35
35
36
Not implemented, it has to be implemented in subclasses.
36
37
"""
37
- raise NotImplementedError ("Subclass must implement abstract method " \
38
- + self .__class__ .__name__ + ".write" )
38
+ raise NotImplementedError (
39
+ "Subclass must implement abstract method " \
40
+ + self .__class__ .__name__ + ".write" )
39
41
40
42
def _check_extension (self , filename ):
41
43
"""
42
- This private method checks if the given `filename` has the proper `extension` set
44
+ This private class method checks if the given `filename` has the proper `extension` set
43
45
in the child class. If not it raises a ValueError.
44
46
45
47
:param string filename: file to check.
46
48
"""
47
49
__ , file_ext = os .path .splitext (filename )
48
- if not file_ext in self .extension :
50
+ if file_ext not in self .extensions :
49
51
raise ValueError (
50
52
'The input file does not have the proper extension. \
51
- It is {0!s}, instead of {1!s}.' .format (file_ext , self .extension )
53
+ It is {0!s}, instead of {1!s}.' .format (file_ext , self .extensions )
52
54
)
53
55
54
56
@staticmethod
@@ -63,16 +65,14 @@ def _check_filename_type(filename):
63
65
'The given filename ({0!s}) must be a string' .format (filename )
64
66
)
65
67
66
- @staticmethod
67
- def _check_infile_instantiation (infile ):
68
+ def _check_infile_instantiation (self ):
68
69
"""
69
- This private static method checks if the input file ` infile` is instantiated. If not it means
70
- that nobody called the parse method, i.e. `self.infile` is None. If the check fails
70
+ This private method checks if `self. infile` is instantiated. If not it means
71
+ that nobody called the parse method and `self.infile` is None. If the check fails
71
72
it raises a RuntimeError.
72
73
73
- :param string infile: file to check.
74
74
"""
75
- if not infile :
75
+ if not self . infile :
76
76
raise RuntimeError (
77
77
"You can not write a file without having parsed one."
78
78
)
0 commit comments