Example #1
0
def add(force=("", False, "Do not check PDF file presence")):
    backup = db.Backup()
    backup.path = input("Enter path: ")
    backup.path = fixup_path(backup.path)

    library_path = make_library_path(backup.path)
    if not os.path.isfile(library_path) and not force:
        raise ValueError(
            "Original file for this backup was not found in elibrary")

    backup.provenance = input("Enter provenance (markdown supported): ")
    backup.aspect_ratio_x, backup.aspect_ratio_y = map(
        int,
        input(f"Enter aspect ratio ({SIZE_FORMAT}): ").split(
            const.SIZE_DELIMETER))
    backup.image_size_x, backup.image_size_y = map(
        int,
        input(f"Enter image size ({SIZE_FORMAT}): ").split(
            const.SIZE_DELIMETER))
    backup.note = input("Enter note (markdown supported): ")
    if backup.note and backup.note[-1] != '.':
        print("The note must end with a dot.")
        sys.exit(1)

    with db.make_transaction() as session:
        session.add(backup)
        session.commit()
        print(f"Added backup #{backup.id}")
Example #2
0
def delete(id=("", 0, "Id of the backup to be deleted")):
    if id == 0:
        print("Backup id is required")
        sys.exit(1)
    with db.make_transaction() as session:
        backup = session.query(db.Backup).get(id)
        print(f"You are going to delete backup #{id} at '{backup.path}'")
        confirmation = input("Type YES to continue: ")
        if confirmation == "YES":
            session.delete(backup)
            session.commit()
            print(f"Backup #{id} was deleted")
Example #3
0
def get_backups():
	format = utils_flask.extract_string_from_request("format", default="html")
	with db.make_transaction() as session:
		backups = session.query(db.Backup).order_by(db.Backup.path).all()
	if format == "html":
		return flask.render_template("backups.html", backups=backups)
		pass
	elif format == "json":
		response = flask.make_response(json.dumps(backups, cls=db.SqlAlchemyEncoder))
		response.content_type = "application/json; charset=utf-8"
		return response
	else:
		flask.abort(http.client.BAD_REQUEST, f"Unknown format: {format}")
Example #4
0
def get(
	id=("", 0, "Id of the backup to be updated"),
):
	if id == 0:
		print("Backup id is required")
		sys.exit(1)
	with db.make_transaction() as session:
		backup = session.query(db.Backup).get(id)
		print(f"Path:\n{backup.path}")
		print(f"Provenance:\n{backup.provenance}")
		print(f"Note:\n{backup.note}")
		print(f"Image size: {backup.image_size_x}{const.SIZE_DELIMETER}{backup.image_size_y}")
		print(f"Aspect ratio: {backup.aspect_ratio_x}{const.SIZE_DELIMETER}{backup.aspect_ratio_y}")
Example #5
0
def update(
        id=("", 0, "Id of the backup to be updated"),
        # opster does not allow checking if the option was defined.
        # Workaround this by passing ugly default.
        path=("", NOT_DEFINED, "Set backup path to the given value"),
        provenance=(
            "", NOT_DEFINED,
            "Set backup provenance to the given value (markdown supported)"),
        note=("", NOT_DEFINED,
              "Set backup note to the given value (markdown supported)"),
        image_size=(
            "", NOT_DEFINED,
            f"Set backup image size to the given value ({SIZE_FORMAT})"),
        aspect_ratio=(
            "", NOT_DEFINED,
            f"Set backup image aspect ratio to the given value ({SIZE_FORMAT})"
        )):
    if id == 0:
        print("Backup id is required")
        sys.exit(1)
    with db.make_transaction() as session:
        backup = session.query(db.Backup).get(id)
        modified = False
        if path != NOT_DEFINED:
            path = fixup_path(path)
            library_path = make_library_path(path)
            if not os.path.isfile(library_path):
                raise ValueError(
                    "Original file for this backup was not found in elibrary")
            backup.path = path
            modified = True
        if provenance != NOT_DEFINED:
            backup.provenance = provenance
            modified = True
        if note != NOT_DEFINED:
            backup.note = note
            modified = True
        if image_size != NOT_DEFINED:
            backup.image_size_x, backup.image_size_y = map(
                int, image_size.split(const.SIZE_DELIMETER))
            modified = True
        if aspect_ratio != NOT_DEFINED:
            backup.aspect_ratio_x, backup.aspect_ratio_y = map(
                int, aspect_ratio.split(const.SIZE_DELIMETER))
            modified = True
        if modified:
            session.add(backup)
            session.commit()
            print("Updated successfully")
        else:
            print("Nothing to modify")
Example #6
0
def delete(
	id=("", 0, "Id of the backup to be deleted")
):
	if id == 0:
		print("Backup id is required")
		sys.exit(1)
	with db.make_transaction() as session:
		backup = session.query(db.Backup).get(id)
		print(f"You are going to delete backup #{id} at '{backup.path}'")
		confirmation = input("Type YES to continue: ")
		if confirmation == "YES":
			session.delete(backup)
			session.commit()
			print(f"Backup #{id} was deleted")
Example #7
0
def get(id=("", 0, "Id of the backup to be updated"), ):
    if id == 0:
        print("Backup id is required")
        sys.exit(1)
    with db.make_transaction() as session:
        backup = session.query(db.Backup).get(id)
        print(f"Path:\n{backup.path}")
        print(f"Provenance:\n{backup.provenance}")
        print(f"Note:\n{backup.note}")
        print(
            f"Image size: {backup.image_size_x}{const.SIZE_DELIMETER}{backup.image_size_y}"
        )
        print(
            f"Aspect ratio: {backup.aspect_ratio_x}{const.SIZE_DELIMETER}{backup.aspect_ratio_y}"
        )
Example #8
0
def add():
	backup = db.Backup()
	backup.path = input("Enter path: ")
	backup.path = fixup_path(backup.path)

	backup.provenance = input("Enter provenance (markdown supported): ")
	backup.aspect_ratio_x, backup.aspect_ratio_y = map(
		int,
		input(f"Enter aspect ratio ({SIZE_FORMAT}): ").split(const.SIZE_DELIMETER)
	)
	backup.image_size_x, backup.image_size_y = map(
		int,
		input(f"Enter image size ({SIZE_FORMAT}): ").split(const.SIZE_DELIMETER)
	)
	backup.note = input("Enter note (markdown supported): ")
	if backup.note and backup.note[-1] != '.':
		print("The note must end with a dot.")
		sys.exit(1)

	with db.make_transaction() as session:
		session.add(backup)
		session.commit()
		print(f"Added backup #{backup.id}")
Example #9
0
def add():
    backup = db.Backup()
    backup.path = input("Enter path: ")
    backup.path = fixup_path(backup.path)

    backup.provenance = input("Enter provenance (markdown supported): ")
    backup.aspect_ratio_x, backup.aspect_ratio_y = map(
        int,
        input(f"Enter aspect ratio ({SIZE_FORMAT}): ").split(
            const.SIZE_DELIMETER))
    backup.image_size_x, backup.image_size_y = map(
        int,
        input(f"Enter image size ({SIZE_FORMAT}): ").split(
            const.SIZE_DELIMETER))
    backup.note = input("Enter note (markdown supported): ")
    if backup.note and backup.note[-1] != '.':
        print("The note must end with a dot.")
        sys.exit(1)

    with db.make_transaction() as session:
        session.add(backup)
        session.commit()
        print(f"Added backup #{backup.id}")
Example #10
0
def update(
	id=("", 0, "Id of the backup to be updated"),
	# opster does not allow checking if the option was defined.
	# Workaround this by passing ugly default.
	path=("", NOT_DEFINED, "Set backup path to the given value"),
	provenance=("", NOT_DEFINED, "Set backup provenance to the given value (markdown supported)"),
	note=("", NOT_DEFINED, "Set backup note to the given value (markdown supported)"),
	image_size=("", NOT_DEFINED, f"Set backup image size to the given value ({SIZE_FORMAT})"),
	aspect_ratio=("", NOT_DEFINED, f"Set backup image aspect ratio to the given value ({SIZE_FORMAT})")
):
	if id == 0:
		print("Backup id is required")
		sys.exit(1)
	with db.make_transaction() as session:
		backup = session.query(db.Backup).get(id)
		modified = False
		if path != NOT_DEFINED:
			backup.path = fixup_path(path)
			modified = True
		if provenance != NOT_DEFINED:
			backup.provenance = provenance
			modified = True
		if note != NOT_DEFINED:
			backup.note = note
			modified = True
		if image_size != NOT_DEFINED:
			backup.image_size_x, backup.image_size_y = map(int, image_size.split(const.SIZE_DELIMETER))
			modified = True
		if aspect_ratio != NOT_DEFINED:
			backup.aspect_ratio_x, backup.aspect_ratio_y = map(int, image_size.split(const.SIZE_DELIMETER))
			modified = True
		if modified:
			session.add(backup)
			session.commit()
			print("Updated successfully")
		else:
			print("Nothing to modify")
Example #11
0
			paths, provenance, aspect_ratio, image_size, note = list(map(str.strip, line.split('|')))
			aspect_ratio_x, aspect_ratio_y = map(int, aspect_ratio.strip('`').split(const.SIZE_DELIMETER))
			image_size_x, image_size_y = map(int, image_size.strip('`').split(const.SIZE_DELIMETER))
			unquote = lambda s: s.strip('`')
			paths = list(map(unquote, paths.split('<br/>')))
			if note and note[-1] != '.':
				print(f"Comment on line {idx} does not end with dot.")
				warnings_count += 1
			for path in paths:
				backup = db.Backup(
					path=path,
					provenance=provenance,
					aspect_ratio_x=aspect_ratio_x,
					aspect_ratio_y=aspect_ratio_y,
					image_size_x=image_size_x,
					image_size_y=image_size_y,
					note=note
				)
				backups.append(backup)
		except Exception as ex:
			print(f"While processing line {idx} got exception: {ex!r}")
			sys.exit(1)
	if warnings_count > 0:
		print(f"You have to fix {warnings_count} warnings first")
		sys.exit(1)
	print(f"Going to upload: {len(backups)} backups")
	with db.make_transaction() as session:
		session.add_all(backups)
		session.commit()
	print(f"Uploaded: {len(backups)} backups")