Example #1
0
def read_embroidery(reader, f, settings=None, pattern=None):
    """Reads fileobject or filename with reader."""
    if pattern is None:
        pattern = EmbPattern()
    if is_string(f):
        text_mode = False
        try:
            text_mode = reader.READ_FILE_IN_TEXT_MODE
        except AttributeError:
            pass
        if text_mode:
            try:
                with open(f, "r") as stream:
                    reader.read(stream, pattern, settings)
                    stream.close()
            except IOError:
                pass
        else:
            try:
                with open(f, "rb") as stream:
                    reader.read(stream, pattern, settings)
                    stream.close()
            except IOError:
                pass
    else:
        reader.read(f, pattern, settings)
    return pattern
Example #2
0
def write_embroidery(writer, pattern, stream, settings=None):
    if settings is None:
        settings = {}
    else:
        settings = settings.copy()
    if settings.get("encode", True):
        if not ("max_jump" in settings):
            try:
                settings["max_jump"] = writer.MAX_JUMP_DISTANCE
            except AttributeError:
                pass
        if not ("max_stitch" in settings):
            try:
                settings["max_stitch"] = writer.MAX_STITCH_DISTANCE
            except AttributeError:
                pass
        if not ("full_jump" in settings):
            try:
                settings["full_jump"] = writer.FULL_JUMP
            except AttributeError:
                pass
        if not ("strip_speeds" in settings):
            try:
                settings["strip_speeds"] = writer.STRIP_SPEEDS
            except AttributeError:
                pass
        if not ("sequin_contingency" in settings):
            try:
                settings["sequin_contingency"] = writer.SEQUIN_CONTINGENCY
            except AttributeError:
                pass
        pattern = pattern.get_normalized_pattern(settings)

    if is_string(stream):
        stream = open(stream, "wb")

    writer.write(pattern, stream, settings)
    stream.close()