def iterate_bytechunks(hashme, is_string, use_json, hash_many):
"""
Prep our bytes.
"""
# URL
if not is_string and validators.url(hashme):
if not use_json:
click.echo("Hashing content of URL " + click.style(hashme, bold=True) + "..", err=not hash_many)
try:
response = requests.get(hashme)
except requests.exceptions.ConnectionError as e:
raise ValueError("Not a valid URL. :(")
except Exception as e:
raise ValueError("Not a valid URL. {}.".format(e))
if response.status_code != 200:
click.echo("Response returned %s. :(" % response.status_code, err=True)
bytechunks = response.iter_content()
# File
elif os.path.exists(hashme) and not is_string:
if os.path.isdir(hashme):
if not use_json:
click.echo(click.style("Skipping", fg="yellow") + " directory " + "'" + hashme + "'..", err=True)
return None
if not use_json:
click.echo("Hashing file " + click.style(hashme, bold=True) + "..", err=not hash_many)
bytechunks = FileIter(open(hashme, mode='rb'))
# String
else:
if not use_json:
click.echo("Hashing string " + click.style(hashme, bold=True) + "..", err=not hash_many)
bytechunks = (hashme.encode('utf-8'), )
return bytechunks
评论列表
文章目录