Skip to content

Commit

Permalink
created nox session for formaters
Browse files Browse the repository at this point in the history
  • Loading branch information
dreyjo committed Jan 31, 2024
1 parent 9a6708a commit 51d35dd
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 77 deletions.
13 changes: 8 additions & 5 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ def test(session):
for v in versions:
session.run("pytest")

@nox.session(python=versions[-1])
def lint(session):
#install linter
session.install("flake8")
@nox.session(python=versions)
def format(session):
#install code formatters
session.install("black", "isort")
#run code formatters
session.run("black""src","tests")
session.run("black","src","tests")
session.run("isort","src","tests")

@nox.session(python=versions[-1])
def lint(session):
#install linter
session.install("flake8")
#run linter
session.run("flake8")
130 changes: 68 additions & 62 deletions src/born_digital_docs_scripts/bd_validator.py
Original file line number Diff line number Diff line change
@@ -1,86 +1,89 @@
import pathlib
from pathlib import Path
import bagit
import argparse
import logging
import pathlib
import re
from pathlib import Path

import bagit


def parse_args():
#validate and return paths for main directory and subdirs
# validate and return paths for main directory and subdirs
def main_dir(arg):
path = Path(arg)
if not path.is_dir():
raise argparse.ArgumentTypeError(
f'{path} is not a directory'
)
raise argparse.ArgumentTypeError(f"{path} is not a directory")
return path

def dir_of_dirs(arg):
path = main_dir(arg)
subdirs = []
for child in path.iterdir():
if child.is_dir():
subdirs.append(child)
return subdirs

parser = argparse.ArgumentParser(
description="takes package directory"
)
#argument for single package to be validated

parser = argparse.ArgumentParser(description="takes package directory")
# argument for single package to be validated
parser.add_argument(
'-p', '--package',
"-p",
"--package",
type=main_dir,
help='input path to an ami package',
#required=True,
dest='packages',
action='append'
help="input path to an ami package",
# required=True,
dest="packages",
action="append",
)
parser.add_argument(
'-d', '--directory',
"-d",
"--directory",
type=dir_of_dirs,
help='input path to a directory of ami packages',
#required=True,
dest='packages',
action='extend'
help="input path to a directory of ami packages",
# required=True,
dest="packages",
action="extend",
)

return parser.parse_args()


def is_valid_bag(package: pathlib.Path) -> bool:
bag = bagit.Bag(str(package))
return bag.validate()

#get structure would have to change to incorporate a list of packages

# get structure would have to change to incorporate a list of packages
def get_structure(package: pathlib.Path) -> list:
contents = []
meta = []
for item in package.iterdir():
if item.is_dir() and item.name == 'data':
if item.is_dir() and item.name == "data":
for subdir in Path(item).iterdir():
contents.append(subdir)

else:
meta.append(item.name)
#print(contents)
#print(f'the following files are on the first level: {meta}')

# print(contents)
# print(f'the following files are on the first level: {meta}')
return contents


def valid_structure(contents: list[Path]) -> bool:
expected = ['ArchiveOriginals',
'EditMasters',
'ServiceCopies',
'Images',
'Transcripts',
'Captions',
'Releases',
'Project Files']

expected = [
"ArchiveOriginals",
"EditMasters",
"ServiceCopies",
"Images",
"Transcripts",
"Captions",
"Releases",
"Project Files",
]

result = True
for item in contents:
if not item.name in expected:
if item.name not in expected:
result = False

return result
Expand All @@ -91,24 +94,25 @@ def get_files(package: pathlib.Path) -> list:
all_files = [x for x in all_items if x.is_file()]
files_dict = []
for file in all_files:
dict ={'name':file.name,
'strpath':file.absolute(),
'pospath':file}
files_dict.append(dict)
dict = {"name": file.name, "strpath": file.absolute(), "pospath": file}
files_dict.append(dict)
# print(test)
return files_dict

#check to see the expected folders are in package based on file extension

# check to see the expected folders are in package based on file extension
def validate_folder_content_types(files_dict):
types = {"_ao":"ArchiveOriginals",
"_em":"EditMasters",
"_sc":"ServiceCopies",
"_pm":"PreservationMasters"}
types = {
"_ao": "ArchiveOriginals",
"_em": "EditMasters",
"_sc": "ServiceCopies",
"_pm": "PreservationMasters",
}

# dict ={'name': file.name,
# 'path':file}

inspect =[]
inspect = []

# for item in files_dict:
# for key in types:
Expand All @@ -117,35 +121,37 @@ def validate_folder_content_types(files_dict):

for item in files_dict:
for key in types:
if re.search(key, item['name']) and re.search(types[key], item['strpath']):
if re.search(key, item["name"]) and re.search(types[key], item["strpath"]):
print(f'{item["name"]} is in {types[key]} as expected')
else:
inspect.append(item)

# for item in inspect:
# print(f'what is this?: {item}')

#if this works, try with not and result = true/false as written below
# result = True
# for item in contents:
# if not item.name in expected:
# result = False

# return result

# if this works, try with not and result = true/false as written below
# result = True
# for item in contents:
# if not item.name in expected:
# result = False

# return result

# #check to see files are in appropriate folders:
# def validate_folders_file_match():
# return True


def main():
args = parse_args()
print(args)
#for loop for accessing namespace list of one or more
# for loop for accessing namespace list of one or more
for source in args.packages:
folders = get_structure(source)
files = get_files(source)
files = get_files(source)
validate_folder_content_types(files)


if __name__ == '__main__':
main()
if __name__ == "__main__":
main()
27 changes: 17 additions & 10 deletions tests/test_bd_validator.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import born_digital_docs_scripts.bd_validator as bv
import pytest
from pathlib import Path

import pytest

import born_digital_docs_scripts.bd_validator as bv


@pytest.fixture
def good_package():
return Path("fixtures/simple_video_pk")


@pytest.fixture
def good_structure(good_package):
return bv.get_structure(good_package)


def test_is_package_bag(good_package):
result = bv.is_valid_bag(good_package)
assert result is True


def test_expected_folders_present(good_structure):
result = bv.valid_structure(good_structure)
assert result


def test_warning_unexpected_folder(good_structure):
good_structure.append(Path('unknown_folder')) #not sure if this is correct
good_structure.append(Path("unknown_folder")) # not sure if this is correct
result = bv.valid_structure(good_structure)
assert not result


def test_required_folders_present(good_structure):
# do we have these?
assert False
Expand All @@ -32,18 +40,17 @@ def test_warn_on_required_folders_missing(good_structure):
# do we have these?
assert False


# def test_expected_folders_match_package_contents(good_package):
# present = bv.get_structure(good_package)
assert result


# def test_expected_folders_match_package_contents(good_package):
# present = bv.get_structure(good_package)
assert result
# filetypes = {'ArchiveOriginals':'ao', 'EditMasters':'em','ServiceCopies':'sc','Images':['.jpg','.JPEG','.tif','.tiff'],'Transcripts':['.pdf'],'Captions','Releases', 'Project Files'}

#@pytest.parametrize(filetypes)
# @pytest.parametrize(filetypes)
# def test_warn_on_folder_file_mismatch(good_package, filetypes):
# corrupt one folder at a time and get the right warning message
# corrupt one folder at a time and get the right warning message

# def arguments_capture_valid_package_path(good_package)

#def arguments_capture_valid_directory_paths(good_package)
# def arguments_capture_valid_directory_paths(good_package)

0 comments on commit 51d35dd

Please sign in to comment.