Skip to content

Commit

Permalink
more whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Al Niessner authored and Al Niessner committed Aug 1, 2023
1 parent 10cf278 commit 1a92b53
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
34 changes: 18 additions & 16 deletions src/pds/registrysweepers/repairkit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'''repairkit is an executable package
"""repairkit is an executable package
The reason repairkit is an executable package is for extension as new repairs
are needed in the future. They can be added by updating the REPAIR_TOOLS mapping
with the new field name and functional requirements. All the additions can then
be modules with this executable package.
'''
"""
import logging
import re
from typing import Union
Expand All @@ -16,7 +16,7 @@

from . import allarrays

'''
"""
dictionary repair tools is {field_name:[funcs]} where field_name can be:
1: re.compile().fullmatch for the equivalent of "fred" == "fred"
2: re.compile().match for more complex matching of subparts of the string
Expand All @@ -35,36 +35,38 @@ def function_name (document:{}, fieldname:str)->{}
To get str_a == str_b, re.compile(str_a).fullmatch
'''
"""

REPAIR_TOOLS = {
re.compile('^ops:Data_File_Info/').match: [allarrays.repair],
re.compile('^ops:Label_File_Info/').match: [allarrays.repair],
re.compile("^ops:Data_File_Info/").match: [allarrays.repair],
re.compile("^ops:Label_File_Info/").match: [allarrays.repair],
}

log = logging.getLogger(__name__)


def run(base_url: str,
username: str,
password: str,
verify_host_certs: bool = True,
log_filepath: Union[str, None] = None,
log_level: int = logging.INFO):
def run(
base_url: str,
username: str,
password: str,
verify_host_certs: bool = True,
log_filepath: Union[str, None] = None,
log_level: int = logging.INFO,
):
configure_logging(filepath=log_filepath, log_level=log_level)
log.info("starting CLI processing")
host = Host(password, base_url, username, verify_host_certs)
for document in query_registry_db(host, {"match_all": {}}, {}):
id = document['_id']
src = document['_source']
id = document["_id"]
src = document["_source"]
repairs = {}
log.debug(f'working on document: {id}')
log.debug(f"working on document: {id}")
for fieldname, data in src.items():
for regex, funcs in REPAIR_TOOLS.items():
if regex(fieldname):
for func in funcs:
repairs.update(func(src, fieldname))
if repairs:
log.info(f'Writing repairs to document: {id}')
log.info(f"Writing repairs to document: {id}")
write_updated_docs(host, {id: repairs})
return
8 changes: 5 additions & 3 deletions src/pds/registrysweepers/repairkit/__main__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from pds.registrysweepers.repairkit import run
from pds.registrysweepers.utils import parse_args

args = parse_args(description='sweep through the registry documents and fix common errors')
args = parse_args(description="sweep through the registry documents and fix common errors")

run(base_url=args.base_URL,
run(
base_url=args.base_URL,
username=args.username,
password=args.password,
verify_host_certs=not args.insecure,
log_level=args.log_level,
log_filepath=args.log_file)
log_filepath=args.log_file,
)
6 changes: 3 additions & 3 deletions src/pds/registrysweepers/repairkit/allarrays.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'''change single strings to array of strings'''
"""change single strings to array of strings"""
import logging
from typing import Dict

log = logging.getLogger(__name__)


def repair(document: Dict, fieldname: str) -> Dict:
log.debug(f'checking {fieldname}')
log.debug(f"checking {fieldname}")
if isinstance(document[fieldname], str):
log.info(f'found string for {fieldname} where it should be an array')
log.info(f"found string for {fieldname} where it should be an array")
return {fieldname: [document[fieldname]]}
return {}
13 changes: 7 additions & 6 deletions tests/pds/registrysweepers/repairkit/test_allarrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@

class AllArrays(unittest.TestCase):
def test_valid_field(self):
src = {'apple': ['orange']}
repair = allarrays.repair(src, 'apple')
src = {"apple": ["orange"]}
repair = allarrays.repair(src, "apple")
self.assertEqual({}, repair)

def test_invalid_field(self):
src = {'apple': 'orange'}
repair = allarrays.repair(src, 'apple')
self.assertEqual({'apple': ['orange']}, repair)
src = {"apple": "orange"}
repair = allarrays.repair(src, "apple")
self.assertEqual({"apple": ["orange"]}, repair)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

0 comments on commit 1a92b53

Please sign in to comment.