Skip to content

Commit

Permalink
Fix style issues #515
Browse files Browse the repository at this point in the history
    * Docstrings should be in the imperative mood
    * Use f strings instead of %

Signed-off-by: Jono Yang <[email protected]>
  • Loading branch information
JonoYang committed Aug 13, 2024
1 parent 887359e commit 3e63936
Show file tree
Hide file tree
Showing 28 changed files with 81 additions and 77 deletions.
3 changes: 0 additions & 3 deletions minecode/management/commands/priority_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,6 @@ def process_request(priority_resource_uri, _priority_router=priority_router):
if priority:
kwargs["priority"] = priority
errors = _priority_router.process(purl_to_visit, **kwargs)
if TRACE:
new_uris_to_visit = list(new_uris_to_visit or [])
logger.debug(f"visit_uri: new_uris_to_visit: {new_uris_to_visit}")

return errors

Expand Down
2 changes: 1 addition & 1 deletion minecode/management/commands/run_visit.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def visit_uri(
if visit_error:
msg = f"Visit error for URI: {uri_to_visit}"
msg += "\n".format()
msg += get_error_message(e)
msg += get_error_message(e) # NOQA
visit_errors.append(msg)
logger.error(msg)

Expand Down
2 changes: 1 addition & 1 deletion minecode/management/commands/update_maven_package_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def update_maven_packages(
version=version,
qualifiers=normalized_qualifiers or "",
)
if existing_package.exists():
if existing_packages.exists():
duplicate_packages = []
for existing_package in existing_packages:
if existing_package.download_url != maven_package.download_url:
Expand Down
4 changes: 2 additions & 2 deletions minecode/mappers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def __call__(self, uri, resource_uri):

def get_packages(self, uri, resource_uri):
"""
This method must yield ScannedPackage objects (or return a list) built
from a resource_uri ResourceURI object.
Yield ScannedPackage objects (or return a list) built from a
resource_uri ResourceURI object.
"""
raise NotImplementedError

Expand Down
4 changes: 1 addition & 3 deletions minecode/mappers/debian.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ def get_vcs_repo(description):
repos.append((vcs_tool, vcs_repo))

if len(repos) > 1:
raise TypeError(
"Debian description with more than one Vcs repos: %(repos)r" % locals()
)
raise TypeError(f"Debian description with more than one Vcs repos: {repos}")

if repos:
vcs_tool, vcs_repo = repos[0]
Expand Down
2 changes: 1 addition & 1 deletion minecode/mappers/repomd.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@map_router.route(".+/repomd.xml")
def map_repomd_data(uris, resource_uri):
"""Returns a list of RpmPackage objects collected from visitors."""
"""Return a list of RpmPackage objects collected from visitors."""
if not resource_uri.data:
return
packages = []
Expand Down
4 changes: 2 additions & 2 deletions minecode/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,8 @@ def merge_or_create_package(scanned_package, visit_level, override=False):

def update_or_create_resource(package, resource_data):
"""
Using Resource data from `resource_data`, create or update the
corresponding purldb Resource from `package`.
Create or update the corresponding purldb Resource from `package` using
`resource_data`.
Return a 3-tuple of the corresponding purldb Resource of `resource_data`,
`resource`, as well as booleans representing whether the Resource was
Expand Down
5 changes: 3 additions & 2 deletions minecode/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ def append(self, pattern, endpoint):

def route(self, *patterns):
"""
Decorator to make a callable 'endpoint' routed to one or more patterns.
Return a decorator to make a callable 'endpoint' routed to one or more
patterns.
Example:
-------
Expand Down Expand Up @@ -178,7 +179,7 @@ def resolve(self, string):
# this can happen when multiple patterns match the same string
# we raise an exception with enough debugging information
pats = repr([r.pattern for r in candidates])
msg = "%(string)r matches multiple patterns %(pats)r" % locals()
msg = f"{string} matches multiple patterns {pats}"
raise MultipleRoutesDefined(msg)

return candidates[0].endpoint
Expand Down
2 changes: 1 addition & 1 deletion minecode/rsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def fetch_directory(uri, recurse=True):
file_name = tmp.name
ends = not uri.endswith("/") and "/" or ""
recursive = recurse and "--recursive" or "--no-recursive"
cmd = 'rsync --no-motd %(recursive)s -d "%(uri)s%(ends)s"' % locals()
cmd = f'rsync --no-motd {recursive} -d "{uri}{ends}"'
rsync = command.Command(cmd)
out, err = rsync.execute()

Expand Down
4 changes: 2 additions & 2 deletions minecode/saneyaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def ordered_dumper(dumper, data):


def null_dumper(dumper, value):
"""Always dump nulls as empty string."""
"""Dump nulls as empty string."""
return dumper.represent_scalar("tag:yaml.org,2002:null", "")


Expand All @@ -149,7 +149,7 @@ def string_dumper(dumper, value, _tag="tag:yaml.org,2002:str"):
Ensure that all scalars are dumped as UTF-8 unicode, folded and
quoted in the sanest and most readable way.
"""
if not isinstance(value, basestring):
if not isinstance(value, str):
value = repr(value)

if isinstance(value, str):
Expand Down
2 changes: 1 addition & 1 deletion minecode/tests/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class CommandTest(MiningTestCase):
def test_listing_command(self):
td = self.get_test_loc("command")
osc = "ls" if not ON_WINDOWS else "dir"
c = '%(osc)s "%(td)s"' % locals()
c = f'{osc} "{td}"'
cmd = command.Command(c)
out, err = cmd.execute()
err = [e for e in err]
Expand Down
3 changes: 1 addition & 2 deletions minecode/tests/test_debian.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def check_objects_expected(self, results, expected_loc, regen=FIXTURES_REGEN):
result = ""
for item in results:
if isinstance(item, str):
item = unicode(item, "utf-8")
result += item.encode("utf-8")
else:
if isinstance(item, debcon.Debian822):
Expand Down Expand Up @@ -66,7 +65,7 @@ def check_expected_deb822(self, deb_object, expected_loc, regen=FIXTURES_REGEN):
assert expected == result

def get_tmp_gz_file(self, loc):
"""Creates a .gz file at a temporary location, and returns that location."""
"""Create a .gz file at a temporary location, and returns that location."""
temp_gz_location = self.get_temp_file(extension=".gz")
with open(loc, "rb") as f:
file_content = f.read()
Expand Down
2 changes: 1 addition & 1 deletion minecode/tests/test_rsync.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def test_fetch_directory_no_recurse(self):
self.assertTrue("bar/this" not in results)

def get_dirs(self, input_path):
"""Returns only the type and path from rsync entries."""
"""Return only the type and path from rsync entries."""
return [
(e["type"], e["path"])
for e in rsync.directory_entries(input_path)
Expand Down
4 changes: 2 additions & 2 deletions minecode/tests/test_rubygems.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def test_check_gem_file_visitor_routes(self):
"https://rubygems.org/downloads/O365RubyEasy-0.0.1.gem", # upper
]

for route in routes:
self.assertTrue(visit_router.resolve(route))
for gem_file_visitor_route in routes:
self.assertTrue(visit_router.resolve(gem_file_visitor_route))

def test_RubyGemsIndexVisitor_latest(self):
uri = "http://rubygems.org/specs.4.8.gz"
Expand Down
25 changes: 12 additions & 13 deletions minecode/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ def stringify_null_purl_fields(data):


def sha1(content):
"""Returns the sha1 hash of the given content."""
"""Return the sha1 hash of the given content."""
h = hashlib.sha1()
h.update(content)
return h.hexdigest()


def md5(content):
"""Returns the md5 hash of the given content."""
"""Return the md5 hash of the given content."""
h = hashlib.md5()
h.update(content)
return h.hexdigest()
Expand Down Expand Up @@ -86,14 +86,14 @@ def __eq__(self, other):


def normalize_trailing_slash(uri):
"""Appends a trailing slash if the URI is not ending with one already."""
"""Append a trailing slash if the URI is not ending with one already."""
if not uri.endswith("/"):
uri += "/"
return uri


def is_ascii(s):
"""Returns True is the string is ASCII."""
"""Return True is the string is ASCII."""
return all(ord(c) < 128 for c in s)


Expand All @@ -109,7 +109,7 @@ def clean_html_entities(text):


def clean_description(text):
"""Cleans the description text from HTML entities and from extra whitespaces."""
"""Clean the description text from HTML entities and from extra whitespaces."""
return " ".join(clean_html_entities(text.strip()).split())


Expand Down Expand Up @@ -212,19 +212,18 @@ def get_http_response(uri, timeout=10):
requests_args["timeout"] = timeout

if not uri.lower().startswith("http"):
raise Exception("get_http_response: Not an HTTP URI: %(uri)r" % locals())
raise Exception(f"get_http_response: Not an HTTP URI: {uri}")

try:
response = requests.get(uri, **requests_args)
except (ConnectionError, InvalidSchema):
logger.error("get_http_response: Download failed for %(uri)r" % locals())
logger.error(f"get_http_response: Download failed for {uri}")
raise

status = response.status_code
if status != 200:
raise Exception(
"get_http_response: Download failed for %(uri)r "
"with %(status)r" % locals()
f"get_http_response: Download failed for {uri} " f"with {status}"
)
return response

Expand All @@ -249,8 +248,8 @@ def get_package_sha1(package, field="repository_download_url"):

def fetch_and_write_file_from_url(url):
"""
Fetches a file from the `url` and returns the location for the
temporary file. Return None if the url is not reachable.
Fetch a file from the `url` and return the location for the temporary file.
Return None if the url is not reachable.
"""
response = requests.get(url)
if not response.ok:
Expand Down Expand Up @@ -327,7 +326,7 @@ def extract_file(location):
target = event.target
break
except Exception as e:
logger.error("extract_file: failed for %(location)r" % locals())
logger.error(f"extract_file: failed for {location}")
raise e
return target

Expand Down Expand Up @@ -389,7 +388,7 @@ def _setup(self):
smaller_queryset = copy.deepcopy(self._base_queryset)[
i : i + self.max_obj_num
]
logger.debug("Grabbing next %s objects from DB" % self.max_obj_num)
logger.debug(f"Grabbing next {self.max_obj_num} objects from DB")
yield from smaller_queryset.iterator()

def __iter__(self):
Expand Down
22 changes: 11 additions & 11 deletions minecode/utils_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,16 @@ def mocked_requests_get_for_uris(url_to_location, *args, **kwargs):


def response_403(url, request):
"""Returns a HTTP response with status 403."""
"""Return a HTTP response with status 403."""
return {"status_code": 403, "content": ""}


class JsonBasedTestingMixin(TestCase):
def _normalize_results(self, data, fields_to_remove=[]):
"""
Returns the `data`, where any `package_uid` value has been normalized
with `purl_with_fake_uuid()` and fields from `fields_to_remove` have
been removed from `data`.
Return `data`, where any `package_uid` value has been normalized with
`purl_with_fake_uuid()` and fields from `fields_to_remove` have been
removed from it.
"""
if type(data) in (list, ReturnList):
return [self._normalize_results(entry, fields_to_remove) for entry in data]
Expand Down Expand Up @@ -265,9 +265,9 @@ def check_expected_results(
class JsonBasedTesting(JsonBasedTestingMixin, FileBasedTesting):
def _normalize_results(self, data, fields_to_remove=[]):
"""
Returns the `data`, where any `package_uid` value has been normalized
with `purl_with_fake_uuid()` and fields from `fields_to_remove` have
been removed from `data`.
Return the `data`, where any `package_uid` value has been normalized
with `purl_with_fake_uuid()` and fields from `fields_to_remove` that
have been removed from `data`.
"""
if type(data) in (list, ReturnList):
return [self._normalize_results(entry, fields_to_remove) for entry in data]
Expand Down Expand Up @@ -354,10 +354,6 @@ def check_expected_uris(

def model_to_dict(instance, fields=None, exclude=None):
"""
Copied from django.forms.models. model_to_dict
license: bsd-new
see ABOUT file for details
Return a mapping containing the data in ``instance``.
``fields`` is an optional list of field names. If provided, only the
Expand All @@ -369,6 +365,10 @@ def model_to_dict(instance, fields=None, exclude=None):
Note that all field with the word "date" in their name is converted
to a boolean value to abstract test results from dates.
Copied from django.forms.models. model_to_dict
license: bsd-new
see ABOUT file for details
"""
opts = instance._meta
data = dict()
Expand Down
2 changes: 1 addition & 1 deletion minecode/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def strip_version_tags(path):


def strip_extensions(path):
""" "Remove well known archive extensions from end of path."""
"""Remove well known archive extensions from end of path."""
for rext in ARCHIVE_FILE_EXT_RES:
path = rext.sub("", path)
return path
Expand Down
2 changes: 2 additions & 0 deletions minecode/visitors/bower.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class BowerTopJsonVisitor(HttpJsonVisitor):

def get_uris(self, content):
"""
Yield Bower URIs from `content`.
The json content is a list with name and url, like the following format:
...
{
Expand Down
8 changes: 2 additions & 6 deletions minecode/visitors/maven.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,13 +1410,9 @@ def get_entries(location, fields=frozenset(ENTRY_FIELDS)):
except EOFError:
if TRACE_DEEP:
print(
"Index version: %(_index_version)r last_modified: %(_last_modified)r"
% locals()
)
print(
"Processed %(entries_count)d docs. Last entry: %(entry)r"
% locals()
f"Index version: {_index_version} last_modified: {_last_modified}"
)
print(f"Processed {entries_count} docs. Last entry: {entry}")
print("Unique keys:")
for k in sorted(keys):
print(k)
Expand Down
2 changes: 1 addition & 1 deletion minecode/visitors/pypi.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def fetch(self, uri, timeout=None):
return temp_file

def dumps(self, content):
"""The content is huge json and should not be dumped."""
"""Do not dump the content, as it is huge json."""
return None

def get_uris(self, content):
Expand Down
Loading

0 comments on commit 3e63936

Please sign in to comment.