Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New attributes CLI methods #62

Merged
merged 4 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mwdblib/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
try:
from .attribute import attribute_command
from .comment import comment_command
from .fetch import fetch_command
from .get import get_command
Expand All @@ -22,6 +23,7 @@
"login_command",
"logout_command",
"metakey_command",
"attribute_command",
"search_command",
"share_command",
"tag_command",
Expand Down
20 changes: 20 additions & 0 deletions mwdblib/cli/attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import click

from .formatters import confirm_action
from .main import main, pass_mwdb
from .types import HashFile


@main.command("attribute")
@click.argument("file-or-hash", type=HashFile())
@click.argument("key")
@click.argument("value")
@confirm_action
@pass_mwdb
def attribute_command(mwdb, file_or_hash, key, value):
"""
Add attribute to object
"""
obj = mwdb.query(file_or_hash)
obj.add_attribute(key, value)
return dict(message="Added attribute to {object_id}", object_id=obj.id)
3 changes: 3 additions & 0 deletions mwdblib/cli/formatters/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def format_comments_list(self, comments):
def format_metakeys_list(self, metakeys):
raise NotImplementedError()

def format_attributes_list(self, attributes):
raise NotImplementedError()

def print_lines(self, lines):
for line in lines:
click.echo(line.rstrip("\n"))
Expand Down
3 changes: 3 additions & 0 deletions mwdblib/cli/formatters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ def format_comments_list(self, comments):

def format_metakeys_list(self, metakeys):
return json.dumps(metakeys)

def format_attributes_list(self, attributes):
return json.dumps(attributes)
7 changes: 7 additions & 0 deletions mwdblib/cli/formatters/short.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import click

from .abstract import ObjectFormatter
Expand Down Expand Up @@ -33,6 +35,11 @@ def format_metakeys_list(self, metakeys):
for value in metakeys[key]:
yield " ".join([key, value])

def format_attributes_list(self, attributes):
for key in sorted(attributes.keys()):
for value in attributes[key]:
yield " ".join([key, json.dumps(value)])

def print_confirmation(self, **params):
if "object_id" in params:
click.echo(params["object_id"])
14 changes: 14 additions & 0 deletions mwdblib/cli/formatters/tabular.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import beautifultable
import click

Expand Down Expand Up @@ -212,6 +214,18 @@ def format_metakeys_list(self, metakeys):
]
)

def format_attributes_list(self, attributes):
return self.format_attr_table(
[
[
key,
AttributeFormatter(),
"\n".join(json.dumps(value) for value in attributes[key]),
]
for key in sorted(attributes.keys())
]
)

def print_empty_list(self):
click.echo("No results.", err=True)

Expand Down
12 changes: 12 additions & 0 deletions mwdblib/cli/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,15 @@ def get_metakeys(mwdb, formatter, file_or_hash):
"""
obj = mwdb.query(file_or_hash)
click.echo(formatter.format_metakeys_list(obj.metakeys))


@get_command.command("attributes")
@click.argument("file-or-hash", type=HashFile())
@pass_formatter
@pass_mwdb
def get_attributes(mwdb, formatter, file_or_hash):
"""
Get list of attributes
"""
obj = mwdb.query(file_or_hash)
click.echo(formatter.format_attributes_list(obj.attributes))