1 """ Common signal export functions
6 class WriteError(Exception):
8 Class WriteError -- Errors encountered when writing file
11 Assign the error message
14 Return a string with the error message
16 def __init__(self
, msg
):
17 """ Assign the error message
27 The object instanciated
32 """ Returns the error message
46 """ Writer -- Provide common function for exporting signals into files
47 The purpose of this class is to provide some basic functions to write the
48 Signals to files (file validation) thus simplifying the definition of
49 Writers for many different file formats.
51 The derived classes must redefine _get_format_name, _format_check and
55 """ Instanciate the Reader
64 The object instanciated
67 self
._ow
= False # Overwrite flag
68 self
._opts
= {} # Options passed to write()
70 def write(self
, fn
, sigs
, opts
= {}):
71 """ Do some checks before calling write_signals
76 Path to the output file
79 List of Signals to write in the file
81 opts: dict of various data with strings as keys
82 Options to be considered for the writing
87 True if operation completed successfully
92 In case of invalid path or invalid file format
94 if isinstance(opts
, dict):
97 if self
._opts
.has_key("ow"):
98 if self
._opts
["ow"] in ['True', 'true', '1']:
102 if self
._check
(fn
) and self
._format
_check
(sigs
):
103 return self
.write_signals(sigs
)
107 def _check(self
, fn
):
108 """ Common checks on file access
113 Path to the file to validate
122 In case no file is specified, file exist and overwrite is not allowed
125 if not (isinstance(fn
, str) or isinstance(fn
, unicode)):
126 raise WriteError("No string specified")
128 raise WriteError("No file specified")
129 if os
.path
.exists(fn
):
132 raise WriteError("File already exist")
133 elif not os
.path
.isfile(fn
):
134 raise WriteError("File exists but is not a file")
135 elif not os
.access(fn
, os
.W_OK
):
136 raise WriteError("Cannot access file")
137 elif not os
.access(os
.path
.dirname(fn
), os
.W_OK
):
138 raise WriteError("Cannot access destination directory")
142 def _format_check(self
, sigs
):
143 """ Format specific checks, to be overridden by derived classes
147 sigs: dict of Signals
148 The Signal list to write
153 True if no issue found to write the Signal list in this format
157 def _get_format_name(self
):
158 """ Return the format name
167 The format identifier
171 def detect(self
, format
):
172 """ Return True if format format is supported
177 Identifier of the whised format
182 True if this Writer manage this format
184 if (isinstance(format
, str) or isinstance(format
, unicode))\
185 and format
== self
._get
_format
_name
():
190 def write_signals(self
, sigs
):
191 """ Write Signals to file
192 This function must be redefined in the derived class
196 sigs: dict of Signals
197 The list of Signals to write