From d8416aa56aebbe1df4554392ffbcf75069ddf129 Mon Sep 17 00:00:00 2001 From: stevenhua0320 Date: Fri, 5 Jul 2024 22:57:35 +0800 Subject: [PATCH 1/2] Fix E501 F541 F841 for main, mongoclient, conftest, schemas.py --- regolith/main.py | 9 +++++---- regolith/mongoclient.py | 6 ++++-- regolith/schemas.py | 8 -------- tests/conftest.py | 37 ++++++++++++++++++------------------- 4 files changed, 27 insertions(+), 33 deletions(-) diff --git a/regolith/main.py b/regolith/main.py index d183b2f64..be9062378 100644 --- a/regolith/main.py +++ b/regolith/main.py @@ -161,7 +161,7 @@ def create_parser(): ) # deploy subparser - depp = subp.add_parser("deploy", help="deploys what was built by regolith") + subp.add_parser("deploy", help="deploys what was built by regolith") # email subparser emlp = subp.add_parser("email", help="automates emailing") @@ -225,9 +225,10 @@ def create_parser(): # mongo-to-fs subparser mtf = subp.add_parser( "mongo-to-fs", - help="Backup database from mongodb to filesystem as json. The database will be imported to the destination " - "specified by the 'database':'dst_url' key. For this to work, ensure that the database is included in the " - "dst_url, and that local is set to true.", + help="Backup database from mongodb to filesystem as json. " + "The database will be imported to the destination specified by the 'database':'dst_url' key. " + "For this to work, ensure that the database is included in the " + "dst_url, and that local is set to true.", ) mtf.add_argument( diff --git a/regolith/mongoclient.py b/regolith/mongoclient.py index 939fbab75..677a30d11 100644 --- a/regolith/mongoclient.py +++ b/regolith/mongoclient.py @@ -196,7 +196,8 @@ def change_keys_id_and_date(obj, convert): Recursively goes through the dictionary obj and replaces keys with the convert function. """ if isinstance(obj, datetime.date): - # Mongo cannot handle datetime.date format, but we have infrastructure to handle iso date strings present + # Mongo cannot handle datetime.date format, + # but we have infrastructure to handle iso date strings present # Do not convert to datetime.datetime. Mongo can handle this, but regolith cannot. return obj.isoformat() if isinstance(obj, (str, int, float)): @@ -342,7 +343,8 @@ def open(self): except AttributeError: print( "ERROR:\n" - "Add a username and password to user.json in user/.config/regolith/user.json with the keys\n" + "Add a username and password to user.json in " + "user/.config/regolith/user.json with the keys\n" "mongo_id and mongo_db_password respectively.\n\n" "'uname_from_config' and 'pwd_from_config' can/should stand in for these field in the\n" "mongo URL string in regolithrc.json.\n" diff --git a/regolith/schemas.py b/regolith/schemas.py index 6aece0067..c192520fb 100644 --- a/regolith/schemas.py +++ b/regolith/schemas.py @@ -157,14 +157,6 @@ def load_exemplars(): return exemplars -def load_exemplars(): - here = Path(__file__).parent - exemplars_file = here / "exemplars.json" - with open(exemplars_file, "r", encoding="utf-8") as exemplars_file: - exemplars = json.load(exemplars_file) - return exemplars - - EXEMPLARS = load_exemplars() SCHEMAS = load_schemas() diff --git a/tests/conftest.py b/tests/conftest.py index 25cf70c41..7feec7b4f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,8 +4,7 @@ import json import os import tempfile -from copy import deepcopy, copy -from unittest import mock +from copy import deepcopy import pytest from pymongo import MongoClient @@ -94,7 +93,6 @@ def make_mongodb(): directory, as well as a mongo database. This will yield the path to the repo. """ - cwd = os.getcwd() forked = False name = "regolith_mongo_fake" repo = os.path.join(tempfile.gettempdir(), name) @@ -133,10 +131,10 @@ def make_mongodb(): f, ) if os.name == "nt": - # If on windows, the mongod command cannot be run with the fork or syslog options. Instead, it is installed as - # a service and the exceptions that would typically be log outputs are handled by the exception handlers below. - # In addition, the database must always be manually deleted from the windows mongo instance before running a - # fresh test. + # If on windows, the mongod command cannot be run with the fork or syslog options. + # Instead, it is installed as a service and the exceptions that would typically be log outputs + # are handled by the exception handlers below. In addition, the database must always be manually + # deleted from the windows mongo instance before running a fresh test. cmd = ["mongo", REGOLITH_MONGODB_NAME, "--eval", "db.dropDatabase()"] try: subprocess.check_call(cmd, cwd=repo) @@ -156,8 +154,9 @@ def make_mongodb(): subprocess.check_call(cmd, cwd=repo) except CalledProcessError: print( - "If using linux or mac, Mongod command failed to execute. If using windows, the status of mongo could \n" - "not be retrieved. In order to run mongodb tests, make sure to install the mongodb community edition with" + "If using linux or mac, Mongod command failed to execute. " + "If using windows, the status of mongo could \n not be retrieved. " + "In order to run mongodb tests, make sure to install the mongodb community edition with " "\nthe following link:\n" "https://docs.mongodb.com/manual/installation/" ) @@ -240,10 +239,10 @@ def make_mixed_db(): f, ) if os.name == "nt": - # If on windows, the mongod command cannot be run with the fork or syslog options. Instead, it is installed as - # a service and the exceptions that would typically be log outputs are handled by the exception handlers below. - # In addition, the database must always be manually deleted from the windows mongo instance before running a - # fresh test. + # If on windows, the mongod command cannot be run with the fork or syslog options. + # Instead, it is installed as a service and the exceptions that would typically be log outputs + # are handled by the exception handlers below. In addition, the database must always be manually + # deleted from the windows mongo instance before running a fresh test. cmd = ["mongo", REGOLITH_MONGODB_NAME, "--eval", "db.dropDatabase()"] try: subprocess.check_call(cmd, cwd=repo) @@ -411,10 +410,10 @@ def make_migration_db(fs_to_mongo_true__mongo_to_fs_false): f, ) if os.name == "nt": - # If on windows, the mongod command cannot be run with the fork or syslog options. Instead, it is installed as - # a service and the exceptions that would typically be log outputs are handled by the exception handlers below. - # In addition, the database must always be manually deleted from the windows mongo instance before running a - # fresh test. + # If on windows, the mongod command cannot be run with the fork or syslog options. + # Instead, it is installed as a service and the exceptions that would typically be log outputs + # are handled by the exception handlers below. In addition, the database must always be manually + # deleted from the windows mongo instance before running a fresh test. cmd = ["mongo", ALTERNATE_REGOLITH_MONGODB_NAME, "--eval", "db.dropDatabase()"] try: subprocess.check_call(cmd, cwd=repo) @@ -467,8 +466,8 @@ def shut_down_fork(forked, repo): subprocess.check_call(cmd, cwd=repo) except CalledProcessError: print( - f'Deleting the test database failed, insert "mongo admin --eval ' - f'"db.shutdownServer()"" into command line manually' + 'Deleting the test database failed, insert "mongo admin --eval ' + '"db.shutdownServer()"" into command line manually' ) From 3d69841a08d055240f13c24078e615fe35f046a6 Mon Sep 17 00:00:00 2001 From: stevenhua0320 Date: Fri, 5 Jul 2024 23:46:10 +0800 Subject: [PATCH 2/2] change load_exemplars to desired format, pass flake8 and regolith test. --- regolith/schemas.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/regolith/schemas.py b/regolith/schemas.py index c192520fb..40dd4fd47 100644 --- a/regolith/schemas.py +++ b/regolith/schemas.py @@ -151,9 +151,8 @@ def load_schemas(): def load_exemplars(): here = Path(__file__).parent exemplar_file = here / "exemplars.json" - with open(exemplar_file, "r", encoding="utf-8") as exemplar_file: - raw_exemplars = json.load(exemplar_file) - exemplars = raw_exemplars + with open(exemplar_file, "r", encoding="utf-8") as efile: + exemplars = json.load(efile) return exemplars