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 convenient interface.

The process of saving something usually 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:
  • on_dump – a function that takes a file-like object as argument and writes content to it.
  • text_is_expected (bool) – whether to use text mode (True, default) or binary mode (False) for on_dump.

The dumper calls on_dump with a file-like object every time one of its save methods, e.g. string() or file() is called. The file-like object in the file argument supports the method write() to which the content should be written.

text_is_expected should be

  • True to pass a file to on_dump that you can write strings to
  • False to pass a file to on_dump that you can write bytes to
__repr__()[source]

the string representation for people to read

Returns:the string representation of this object
Return type:str
__weakref__

list of weak references to the object (if defined)

binary_file(file=None)[source]

Same as file() but for binary content.

binary_temporary_file(delete_when_closed=True)[source]

Same as temporary_file() but for binary mode.

bytes()[source]
Returns:the dump as bytes.
encoding
Returns:the encoding for byte to string conversion
Return type:str
file(file=None)[source]

Saves the dump in a file-like object in text mode.

Parameters:fileNone or a file-like object.
Returns:a file-like object

If file is None, a new io.StringIO is returned. If file is not None 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.
string()[source]
Returns:the dump as a string
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 is True (default) the file on the hard drive will be deleted if it is closed or not referenced any more.

If delete_when_closed is False 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