Skip to content

Commit

Permalink
Add actor field to database if defined in tagpack (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
soad003 committed Feb 10, 2023
1 parent 4abeab4 commit 16ae73d
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ RELEASESEM := 'v1.3.0'
all: format lint test build

tag-version:
git diff --exit-code && git diff --staged --exit-code && git tag -a $(RELEASE) -m 'Release $(RELEASE)' && git tag -a $(RELEASESEM) -m 'Release $(RELEASE)' || (echo "Repo is dirty please commit first" && exit 1)

git diff --exit-code && git diff --staged --exit-code && git tag -a $(RELEASE) -m 'Release $(RELEASE)' || (echo "Repo is dirty please commit first" && exit 1)
git diff --exit-code && git diff --staged --exit-code && git tag -a $(RELEASESEM) -m 'Release $(RELEASE)' || (echo "Repo is dirty please commit first" && exit 1)
test:
pytest -v -m "not slow" --cov=src

Expand Down
3 changes: 3 additions & 0 deletions src/tagpack/conf/tagpack_schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ tag:
address:
type: text
mandatory: true
actor:
type: text
mandatory: false
15 changes: 14 additions & 1 deletion src/tagpack/tagpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,18 @@ def validate(self):
e2 = "Mandatory tag field {} missing in {}"
e3 = "Field {} not allowed in {}"
e4 = "Value of body field {} must not be empty (None) in {}"
for tag in self.get_unique_tags():

ut = self.get_unique_tags()
nr_no_actors = 0
for tag in ut:
# check if mandatory tag fields are defined
if not isinstance(tag, Tag):
raise ValidationError("Unknown tag type {}".format(tag))

actor = tag.all_fields.get("actor", None)
if actor is None:
nr_no_actors += 1

for schema_field in self.schema.mandatory_tag_fields:
if (
schema_field not in tag.explicit_fields
Expand All @@ -304,6 +311,12 @@ def validate(self):
except ValidationError as e:
raise ValidationError(f"{e} in {tag}")

if nr_no_actors > 0:
print_warn(
f"{nr_no_actors}/{len(ut)} have no actor configured. "
"Please consider connecting the tag to an actor."
)

if self._duplicates:
msg = f"{len(self._duplicates)} duplicate(s) found, starting "
msg += f"with {self._duplicates[0]}\n"
Expand Down
5 changes: 3 additions & 2 deletions src/tagpack/tagstore.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def insert_tagpack(
ON CONFLICT DO NOTHING"
tag_sql = "INSERT INTO tag (label, source, category, abuse, address, \
currency, is_cluster_definer, confidence, lastmod, \
context, tagpack ) VALUES \
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
context, tagpack, actor ) VALUES \
(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"

tag_data = []
address_data = []
Expand Down Expand Up @@ -498,6 +498,7 @@ def _get_tag(tag, tagpack_id):
lastmod,
tag.all_fields.get("context"),
tagpack_id,
tag.all_fields.get("actor", None),
)


Expand Down
13 changes: 13 additions & 0 deletions tests/test_actorpack.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ def schema(monkeypatch):
def taxonomies():
tax_entity = Taxonomy("entity", "http://example.com/entity")
tax_entity.add_concept("exchange", "Exchange", None, "Some description")
tax_entity.add_concept("organization", "Orga", None, "Some description")

tax_abuse = Taxonomy("abuse", "http://example.com/abuse")
tax_abuse.add_concept("bad_coding", "Bad coding", None, "Really bad")

country = Taxonomy("country", "http://example.com/abuse")
country.add_concept("AT", "Austria", None, "nice for vacations")
country.add_concept("BE", "Belgium", None, "nice for vacations")
country.add_concept("US", "USA", None, "nice for vacations")

taxonomies = {"entity": tax_entity, "abuse": tax_abuse, "country": country}
return taxonomies
Expand Down Expand Up @@ -60,3 +62,14 @@ def test_context_there(actorpack):

def test_validate(actorpack):
assert actorpack.validate()


def test_load_actorpack_from_file(taxonomies):
ap = ActorPack.load_from_file(
"test uri",
"tests/testfiles/actors/ex_actorpack.yaml",
ActorPackSchema(),
taxonomies,
)

assert ap.validate()
4 changes: 2 additions & 2 deletions tests/testfiles/actors/ex_actorpack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ actors:
uri: https://archive.org
label: Internet Archive
jurisdictions: [US]
context: {'test': b}
context: '{"test": 1}'
categories: [organization]
- id: binance
uri: https://binance.com
label: Binance
jurisdictions: [US, AT]
categories: [exchange]
context: {'coingecko-id': 'binance'}
context: '{"coingecko-id": "binance"}'

0 comments on commit 16ae73d

Please sign in to comment.