Esempio n. 1
0
def main(filename, json, bson, cbor):
    """
    Creates a new & empty Mangofile.

    It will not overwrite anyfiles! 
    """
    if os.path.isfile(filename):
        click.echo("File already exists")
        exit(1)

    file = MangoFile()

    try:
        if json:
            file.save_json(filename)
        elif bson:
            file.save_bson(filename)
        elif cbor:
            file.save_cbor(filename)
        else:
            file.save(filename)

        exit(0)
    except WriteError:
        click.echo("WriteError: could not create " + filename)
        exit(3)
    except PermissionError:
        click.echo("Permission denied: could not create " + filename)
        exit(4)
Esempio n. 2
0
    def get_mango_file(path):
        files = natural_sort(os.listdir(path))
        mango_file = MangoFile()

        for file in files:
            # TODO check if add worked
            img = MangoImage.from_path(path + "/" + file)
            img.compress(CompressionType.GZIP)
            mango_file.add_image(img)

        return mango_file
Esempio n. 3
0
def main(file, verbose, show_images):
    if file is None:
        exit(1)

    mangofile = MangoFile.open(file)
    meta = mangofile.meta_data

    pairs = get_meta_pairs(meta, verbose)

    pairs.append(MetadataPair("images", mangofile.image_count))

    if show_images:
        pairs.append(MetadataPair("images", mangofile.image_count))
        for image in mangofile.images:
            meta = image.meta_data
            pairs.append(ImagePair("filename", meta.filename))
            pairs.append(ImagePair("mime", meta.mime))
            pairs.append(ImagePair("checksum", meta.checksum))

            if meta.compression is not None or verbose:
                pairs.append(ImagePair("compression", meta.compression))

            if meta.encryption is not None or verbose:
                pairs.append(ImagePair("encryption", meta.encryption))

            # add a blank line after every image
            pairs.append(None)

    for pair in pairs:
        if pair.__class__ == MetadataPair:
            click.echo("{:12}: {:<12}".format(pair.key, pair.value))
        elif pair.__class__ == ImagePair:
            click.echo("{:12}  {:12}: {:<12}".format("", pair.key, pair.value))
        elif pair is None:
            click.echo(" ")
Esempio n. 4
0
def element(ctx, value):
    """
    Get & set values from the {0} field


    Calling it without passing in a value will print the current value.
    If a value is given, it will be set inside the file as the new value.
    """
    mangofile = MangoFile.open(ctx.obj.file)
    meta = mangofile.meta_data

    if value is None:
        current_value = getattr(meta, ctx.info_name)
        if current_value is not None:
            click.echo(current_value)
        else:
            exit(1)
    else:
        if validate_key(ctx.info_name, value):
            if ctx.info_name in num_keys:
                value = int(value)
            elif ctx.info_name == "language":
                value = Language(value.upper())

            setattr(meta, ctx.info_name, value)
            mangofile.save(ctx.obj.file)
        else:
            click.echo("Can't set {0}: Invalid value".format(ctx.info_name))
            exit(1)
Esempio n. 5
0
def converter(ctx, path):
    # we can assume that the class that belongs to the command must exist
    # because this command gets registered by the class
    file_format = list(filter(lambda x: x.name == ctx.info_name, formats))[0]

    if ctx.parent.info_name == "from":
        file_format.convert_from(path)
    elif ctx.parent.info_name == "to":
        file_format.convert_to(MangoFile.open(path))
Esempio n. 6
0
def main(file, index):
    mangofile = MangoFile.open(file)

    try:
        index = int(index)
    except ValueError:
        exit(1)

    image = mangofile.get_image(index)

    if image.meta_data.compression is not None:
        image.uncompress()

    if image.meta_data.encryption is not None:
        exit(1)

    sys.stdout.buffer.write(image.image_data)
Esempio n. 7
0
def main(filename, image):
    if not os.path.isfile(filename):
        click.echo("{} does not exist.".format(filename))
        exit(1)

    if not os.path.isfile(image):
        click.echo("{} does not exist.".format(image))
        exit(1)

    file = MangoFile.open(filename)

    file.add_image_by_path(image)

    try:
        file.save(filename)
        exit(0)
    except WriteError:
        click.echo("WriteError: could not save" + filename)
        exit(3)
    except PermissionError:
        click.echo("Permission denied: could not save" + filename)
        exit(4)
Esempio n. 8
0
def test_year():
    mango = MangoFile()
    meta = mango.meta_data
    assert meta.year == None
    meta.year = 2
    assert meta.year == 2
Esempio n. 9
0
def test_language():
    mango = MangoFile()
    meta = mango.meta_data
    assert meta.language == None
    meta.language = Language.EN
    assert meta.language == Language.EN
Esempio n. 10
0
def test_volume():
    mango = MangoFile()
    meta = mango.meta_data
    assert meta.volume == None
    meta.volume = 2
    assert meta.volume == 2
Esempio n. 11
0
def test_chapter():
    mango = MangoFile()
    meta = mango.meta_data
    assert meta.chapter == None
    meta.chapter = 2
    assert meta.chapter == 2
Esempio n. 12
0
def test_source():
    mango = MangoFile()
    meta = mango.meta_data
    assert meta.translation == None
    meta.translation = "test"
    assert meta.translation == "test"
Esempio n. 13
0
def test_title():
    mango = MangoFile()
    meta = mango.meta_data
    assert meta.title == None
    meta.title = "test"
    assert meta.title == "test"
Esempio n. 14
0
def test_source():
    mango = MangoFile()
    meta = mango.meta_data
    assert meta.source == None
    meta.source = "test"
    assert meta.source == "test"
Esempio n. 15
0
def test_publisher():
    mango = MangoFile()
    meta = mango.meta_data
    assert meta.publisher == None
    meta.publisher = "test"
    assert meta.publisher == "test"
Esempio n. 16
0
def test_author():
    mango = MangoFile()
    meta = mango.meta_data
    assert meta.author == None
    meta.author = "test"
    assert meta.author == "test"