file
Module¶
Save strings to files.
-
class
knittingpattern.Dumper.file.
ContentDumper
(on_dump, text_is_expected=True, encoding='UTF-8')[source]¶ Bases:
object
This class is a unified interface for saving objects.
The idea is to decouple the place to save to from the process used to dump the content. We are saving several objects such as patterns and SVGs. They should all have the same convinient interface.
The process of saving something usally requires writing to some file. However, users may want to have the result as a string, an open file, a file on the hard drive on a fixed or temporary location, posted to some url or in a zip file. This class should provide for all those needs while providing a uniform interface for the dumping.
-
__init__
(on_dump, text_is_expected=True, encoding='UTF-8')[source]¶ Create a new dumper object with a function
on_dump
Parameters: The dumper calls
on_dump
with a file-like object every time one of its save methods, e.g.string()
orfile()
is called. The file-like object in thefile
argument supports the methodwrite()
to which the content should be written.text_is_expected
should be
-
__repr__
()[source]¶ the string representation for people to read
Returns: the string represenation of this object Return type: str
-
__weakref__
¶ list of weak references to the object (if defined)
-
binary_temporary_file
(delete_when_closed=True)[source]¶ Same as
temporary_file()
but for binary mode.
-
file
(file=None)[source]¶ Saves the dump in a file-like object in text mode.
Parameters: file¶ – None
or a file-like object.Returns: a file-like object If
file
isNone
, a newio.StringIO
is returned. Iffile
is notNone
it should be a file-like object.The content is written to the file. After writing, the file’s read/write position points behind the dumped content.
-
path
(path)[source]¶ Saves the dump in a file named
path
.Parameters: path¶ (str) – a valid path to a file location. The file can exist.
-
temporary_binary_file
(delete_when_closed=True)¶ Same as
temporary_file()
but for binary mode.
-
temporary_file
(delete_when_closed=True)[source]¶ Saves the dump in a temporary file and returns the open file object.
Parameters: delete_when_closed¶ (bool) – whether to delete the temporary file when it is closed. Returns: a file-like object If
delete_when_closed
isTrue
(default) the file on the hard drive will be deleted if it is closed or not referenced any more.If
delete_when_closed
isFalse
the returned temporary file is not deleted when closed or unreferenced. The user of this method has then the responsibility to free the space on the host system.The returned file-like object has an attribute
name
that holds the location of the file.
-
temporary_path
(extension='')[source]¶ Saves the dump in a temporary file and returns its path.
Warning
The user of this method is responsible for deleting this file to save space on the hard drive. If you only need a file object for a short period of time you can use the method
temporary_file()
.Parameters: extension¶ (str) – the ending ot the file name e.g. ".png"
Returns: a path to the temporary file Return type: str
-