Skip to content

Commit

Permalink
extend remote parsing to include more combinations
Browse files Browse the repository at this point in the history
includes f'string apocalypse
  • Loading branch information
saltydk committed Sep 2, 2022
1 parent 1a6d794 commit a8b8277
Show file tree
Hide file tree
Showing 19 changed files with 260 additions and 387 deletions.
234 changes: 71 additions & 163 deletions cloudplow.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ requests~=2.20.0
GitPython~=2.1.10
sqlitedict~=1.6.0
apprise~=0.8.2
jsonpickle~=2.2.0
jsonpickle~=2.2.0
urllib3~=1.24.3
22 changes: 9 additions & 13 deletions utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,9 @@ def __inner_upgrade(self, settings1, settings2, key=None, overwrite=False):
merged[k] = v
sub_upgraded = True
if not key:
log.info("Added %r config option: %s", str(k), str(v))
log.info(f"Added {k} config option: {v}")
else:
log.info("Added %r to config option %r: %s", str(k), str(key), str(v))
log.info(f"Added {k} to config option {key}: {v}")
continue

# iterate children
Expand All @@ -205,7 +205,7 @@ def __inner_upgrade(self, settings1, settings2, key=None, overwrite=False):
if v not in settings2:
merged.append(v)
sub_upgraded = True
log.info("Added to config option %r: %s", str(key), str(v))
log.info(f"Added to config option {key}: {v}")
continue

return merged, sub_upgraded
Expand All @@ -218,7 +218,7 @@ def upgrade_settings(self, currents):
if name in os.environ:
# Use JSON decoder to get same behaviour as config file
fields_env[name] = json.JSONDecoder().decode(os.environ[name])
log.info("Using ENV setting %s=%s", name, fields_env[name])
log.info(f"Using ENV setting {name}={fields_env[name]}")

# Update in-memory config with environment settings
currents.update(fields_env)
Expand All @@ -240,11 +240,11 @@ def upgrade(self, cfg):
if name in os.environ:
# Use JSON decoder to get same behaviour as config file
fields_env[name] = json.JSONDecoder().decode(os.environ[name])
log.info("Using ENV setting %s=%s", name, fields_env[name])
log.info(f"Using ENV setting {name}={fields_env[name]}")

# Only rewrite config file if new fields added
if len(fields):
log.info("Upgraded config. Added %d new field(s): %r", len(fields), fields)
log.info(f"Upgraded config. Added {len(fields)} new field(s): {fields}")
self.save(cfg)

# Update in-memory config with environment settings
Expand Down Expand Up @@ -273,11 +273,7 @@ def load(self):
def save(self, cfg):
with open(self.settings['config'], 'w') as fp:
json.dump(cfg, fp, indent=4, sort_keys=True)

log.info(
"Your config was upgraded. You may check the changes here: %r",
self.settings['config']
)
log.info(f"Your config was upgraded. You may check the changes here: {self.settings['config']}")

exit(0)

Expand All @@ -289,7 +285,7 @@ def get_settings(self):
# Command line argument
if self.args[name]:
value = self.args[name]
log.info("Using ARG setting %s=%s", name, value)
log.info(f"Using ARG setting {name}={value}")

elif data['env'] in os.environ:
value = os.environ[data['env']]
Expand All @@ -302,7 +298,7 @@ def get_settings(self):
setts[name] = value

except Exception:
log.exception("Exception retrieving setting value: %r" % name)
log.exception(f"Exception retrieving setting value: {name}")

return setts

Expand Down
4 changes: 1 addition & 3 deletions utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ def timer(*args, **kw):
result = method(*args, **kw)
time_taken = timeit.default_timer() - start_time
try:
log.info("%r from %r finished in %s", method.__name__,
os.path.basename(method.__code__.co_filename),
misc.seconds_to_string(time_taken))
log.info(f"{method.__name__} from {os.path.basename(method.__code__.co_filename)} finished in {misc.seconds_to_string(time_taken)}")
except Exception:
log.exception("Exception while logging time taken to run function: ")
return result
Expand Down
4 changes: 2 additions & 2 deletions utils/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ def ensure_lock_folder():
try:
if not os.path.exists(lock_folder):
os.mkdir(lock_folder)
log.info("Created lock folder: %r", lock_folder)
log.info(f"Created lock folder: {lock_folder}")
except Exception:
log.exception("Exception verifying/creating lock folder %r: ", lock_folder)
log.exception(f"Exception verifying/creating lock folder {lock_folder}: ")
sys.exit(1)


Expand Down
17 changes: 8 additions & 9 deletions utils/misc.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import logging
import re
import time
Expand All @@ -18,22 +19,22 @@ def seconds_to_string(seconds):
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
if days:
resp += '%d days' % days
resp += f'{days} days'
if hours:
if len(resp):
resp += ', '
resp += '%d hours' % hours
resp += f'{hours} hours'
if minutes:
if len(resp):
resp += ', '
resp += '%d minutes' % minutes
resp += f'{minutes} minutes'
if seconds:
if len(resp):
resp += ' and '
resp += '%d seconds' % seconds
resp += f'{seconds} seconds'
except Exception:
log.exception("Exception occurred converting %d seconds to readable string: ", seconds)
resp = '%d seconds' % seconds
log.exception(f"Exception occurred converting {seconds} seconds to readable string: ")
resp = f'{seconds} seconds'
return resp


Expand All @@ -42,10 +43,8 @@ def merge_dicts(*dicts):
d = {}
for dict in dicts:
for key in dict:
try:
with contextlib.suppress(KeyError):
d[key] = dict[key]
except KeyError:
pass
return d


Expand Down
2 changes: 1 addition & 1 deletion utils/notifications/apprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ def send(self, **kwargs):
)

except Exception:
log.exception("Error sending notification to %r", self.url)
log.exception(f"Error sending notification to {self.url}")
return False
2 changes: 1 addition & 1 deletion utils/notifications/pushover.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ def send(self, **kwargs):
return resp.status_code == 200

except Exception:
log.exception("Error sending notification to %r", self.user_token)
log.exception(f"Error sending notification to {self.user_token}")
return False
2 changes: 1 addition & 1 deletion utils/notifications/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ def send(self, **kwargs):
return resp.status_code == 200

except Exception:
log.exception("Error sending notification to %r", self.webhook_url)
log.exception(f"Error sending notification to {self.webhook_url}")
return False
23 changes: 3 additions & 20 deletions utils/path.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,13 @@ def get_file_hash(filepath):
try:
file_size = os.path.getsize(filepath)
except Exception:
log.exception("Exception getting file size of %r: ", filepath)
log.exception(f"Exception getting file size of {filepath}: ")
# set basic string to use for hash
key = "{filename}-{size}".format(filename=os.path.basename(filepath), size=file_size)
return hashlib.md5(key.encode('utf-8')).hexdigest()


def find_files(folder, extension=None, depth=None):
file_list = []
start_count = folder.count(os.sep)
for path, subdirs, files in os.walk(folder, topdown=True):
for name in files:
if depth and path.count(os.sep) - start_count >= depth:
del subdirs[:]
continue
filepath = os.path.join(path, name)
if not extension:
file_list.append(filepath)
elif filepath.lower().endswith(extension.lower()):
file_list.append(filepath)

return sorted(file_list, key=lambda x: x.count(os.path.sep), reverse=True)


def find_folders(folder, extension=None, depth=None):
def find_items(folder, extension=None, depth=None):
folder_list = []
start_count = folder.count(os.sep)
for path, subdirs, files in os.walk(folder, topdown=True):
Expand All @@ -76,7 +59,7 @@ def opened_files(path):
return files

except Exception:
log.exception("Exception retrieving open files from %r: ", path)
log.exception(f"Exception retrieving open files from {path}: ")
return []


Expand Down
18 changes: 8 additions & 10 deletions utils/plex.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ def validate(self):
request_url = urljoin(self.url, 'status/sessions')
r = requests.get(request_url, headers=self.headers, timeout=15, verify=False)
if r.status_code == 200 and r.headers['Content-Type'] == 'application/json':
log.debug("Server responded with status_code=%r, content: %r", r.status_code, r.json())
log.debug(f"Server responded with status_code={r.status_code}, content: {r.json()}")
return True
else:
log.error("Server responded with status_code=%r, content: %r", r.status_code, r.content)
log.error(f"Server responded with status_code={r.status_code}, content: {r.content}")
return False
except Exception:
log.exception("Exception validating server token=%r, url=%r: ", self.token, self.url)
log.exception(f"Exception validating server token={self.token}, url={self.url}: ")
return False

def get_streams(self):
Expand All @@ -43,13 +43,13 @@ def get_streams(self):
r = requests.get(request_url, headers=self.headers, timeout=15, verify=False)
if r.status_code == 200 and r.headers['Content-Type'] == 'application/json':
result = r.json()
log.debug("Server responded with status_code=%r, content: %r", r.status_code, r.content)
log.debug(f"Server responded with status_code={r.status_code}, content: {r.content}")

if 'MediaContainer' not in result:
log.error("Failed to retrieve streams from server at %r", self.url)
log.error(f"Failed to retrieve streams from server at {self.url}")
return None
elif 'Video' not in result['MediaContainer'] and 'Metadata' not in result['MediaContainer']:
log.debug("There were no streams to check for server at %r", self.url)
log.debug(f"There were no streams to check for server at {self.url}")
return []

return [
Expand All @@ -62,12 +62,10 @@ def get_streams(self):
]

else:
log.error(
"Server url or token was invalid, token=%r, request_url=%r, status_code=%r, content: %r",
self.token, request_url, r.status_code, r.content)
log.error(f"Server url or token was invalid, token={self.token}, request_url={request_url}, status_code={r.status_code}, content: {r.content}")
return None
except Exception:
log.exception("Exception retrieving streams from request_url=%r, token=%r: ", request_url, self.token)
log.exception(f"Exception retrieving streams from request_url={request_url}, token={self.token}: ")
return None


Expand Down
4 changes: 1 addition & 3 deletions utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ def execute(command, callback=None, env=None, logs=True, shell=False):
else:
total_output += "%s\n" % output

if not callback:
return total_output
return process.poll()
return process.poll() if callback else total_output


def popen(command, shell=False):
Expand Down
Loading

0 comments on commit a8b8277

Please sign in to comment.