Skip to content

Commit

Permalink
format python files with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
daywalker90 authored and chrisguida committed Jul 10, 2024
1 parent 7b3ad06 commit 1d3356c
Show file tree
Hide file tree
Showing 35 changed files with 1,730 additions and 1,181 deletions.
211 changes: 129 additions & 82 deletions .ci/test.py

Large diffs are not rendered by default.

34 changes: 21 additions & 13 deletions .ci/update_badges.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
from utils import configure_git, enumerate_plugins


def update_and_commit_badge(plugin_name: str, passed: bool, workflow: str, has_tests: bool) -> bool:
def update_and_commit_badge(
plugin_name: str, passed: bool, workflow: str, has_tests: bool
) -> bool:
json_data = {"schemaVersion": 1, "label": "", "message": "✔", "color": "green"}
if not passed:
json_data.update({"message": "✗", "color": "red"})
Expand All @@ -34,16 +36,18 @@ def update_and_commit_badge(plugin_name: str, passed: bool, workflow: str, has_t

def cleanup_old_results(plugin_name: str, file: Path) -> bool:
os.remove(file)
print(f"Removed deprecated result {file.name} for {plugin_name}, we no longer test for this version!")
print(
f"Removed deprecated result {file.name} for {plugin_name}, we no longer test for this version!"
)
subprocess.run(["git", "add", "-v", file])
subprocess.run(
[
"git",
"commit",
"-m",
f'Remove deprecated result {file.name}',
]
)
[
"git",
"commit",
"-m",
f"Remove deprecated result {file.name}",
]
)
return True


Expand Down Expand Up @@ -96,9 +100,9 @@ def push_badges_data(workflow: str, python_versions_tested: list):
if any_changes:
for _ in range(10):
subprocess.run(["git", "pull", "--rebase"])
output = subprocess.run(["git", "push", "origin", "badges"],
capture_output=True,
text=True)
output = subprocess.run(
["git", "push", "origin", "badges"], capture_output=True, text=True
)
if output.returncode == 0:
print("Push successful")
break
Expand All @@ -117,7 +121,11 @@ def push_badges_data(workflow: str, python_versions_tested: list):
parser = argparse.ArgumentParser(description="Plugins completion script")
parser.add_argument("workflow", type=str, help="Name of the GitHub workflow")
parser.add_argument(
"python_versions_tested", nargs="*", type=str, default=[], help="Python versions tested"
"python_versions_tested",
nargs="*",
type=str,
default=[],
help="Python versions tested",
)
args = parser.parse_args()

Expand Down
10 changes: 9 additions & 1 deletion .ci/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ def get_testfiles(p: Path) -> List[PosixPath]:
test_files = []
for x in p.iterdir():
if x.is_dir() and x.name == "tests":
test_files.extend([y for y in x.iterdir() if y.is_file() and y.name.startswith("test_") and y.name.endswith(".py")])
test_files.extend(
[
y
for y in x.iterdir()
if y.is_file()
and y.name.startswith("test_")
and y.name.endswith(".py")
]
)
elif x.is_file() and x.name.startswith("test_") and x.name.endswith(".py"):
test_files.append(x)
return test_files
Expand Down
26 changes: 12 additions & 14 deletions backup/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# This is used by the plugin from time to time to allow the backend to compress
# the changelog and forms a new basis for the backup.
# If `Change` contains a snapshot and a transaction, they apply in that order.
Change = namedtuple('Change', ['version', 'snapshot', 'transaction'])
Change = namedtuple("Change", ["version", "snapshot", "transaction"])


class Backend(object):
Expand All @@ -37,14 +37,11 @@ def add_change(self, change: Change) -> bool:
raise NotImplementedError

def initialize(self) -> bool:
"""Set up any resources needed by this backend.
"""
"""Set up any resources needed by this backend."""
raise NotImplementedError

def stream_changes(self) -> Iterator[Change]:
"""Retrieve changes from the backend in order to perform a restore.
"""
"""Retrieve changes from the backend in order to perform a restore."""
raise NotImplementedError

def rewind(self) -> bool:
Expand All @@ -63,8 +60,7 @@ def rewind(self) -> bool:
raise NotImplementedError

def compact(self):
"""Apply some incremental changes to the snapshot to reduce our size.
"""
"""Apply some incremental changes to the snapshot to reduce our size."""
raise NotImplementedError

def _db_open(self, dest: str) -> sqlite3.Connection:
Expand All @@ -75,7 +71,7 @@ def _db_open(self, dest: str) -> sqlite3.Connection:
def _restore_snapshot(self, snapshot: bytes, dest: str):
if os.path.exists(dest):
os.unlink(dest)
with open(dest, 'wb') as f:
with open(dest, "wb") as f:
f.write(snapshot)
self.db = self._db_open(dest)

Expand All @@ -87,8 +83,12 @@ def _rewrite_stmt(self, stmt: str) -> str:
re-inserts the space.
"""
stmt = re.sub(r'reserved_til=([0-9]+)WHERE', r'reserved_til=\1 WHERE', stmt)
stmt = re.sub(r'peer_id=([0-9]+)WHERE channels.id=', r'peer_id=\1 WHERE channels.id=', stmt)
stmt = re.sub(r"reserved_til=([0-9]+)WHERE", r"reserved_til=\1 WHERE", stmt)
stmt = re.sub(
r"peer_id=([0-9]+)WHERE channels.id=",
r"peer_id=\1 WHERE channels.id=",
stmt,
)
return stmt

def _restore_transaction(self, tx: Iterator[str]):
Expand All @@ -109,9 +109,7 @@ def restore(self, dest: str, remove_existing: bool = False):
if os.path.exists(dest):
if not remove_existing:
raise ValueError(
"Destination for backup restore exists: {dest}".format(
dest=dest
)
"Destination for backup restore exists: {dest}".format(dest=dest)
)
os.unlink(dest)

Expand Down
22 changes: 14 additions & 8 deletions backup/backends.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'''Create a backend instance based on URI scheme dispatch.'''
"""Create a backend instance based on URI scheme dispatch."""

from typing import Type
from urllib.parse import urlparse

Expand All @@ -8,10 +9,9 @@


def resolve_backend_class(backend_url):

backend_map: Mapping[str, Type[Backend]] = {
'file': FileBackend,
'socket': SocketBackend,
"file": FileBackend,
"socket": SocketBackend,
}
p = urlparse(backend_url)
backend_cl = backend_map.get(p.scheme, None)
Expand All @@ -21,13 +21,19 @@ def resolve_backend_class(backend_url):
def get_backend(destination, create=False, require_init=False):
backend_cl = resolve_backend_class(destination)
if backend_cl is None:
raise ValueError("No backend implementation found for {destination}".format(
destination=destination,
))
raise ValueError(
"No backend implementation found for {destination}".format(
destination=destination,
)
)
backend = backend_cl(destination, create=create)
initialized = backend.initialize()
if require_init and not initialized:
kill("Could not initialize the backup {}, please use 'backup-cli' to initialize the backup first.".format(destination))
kill(
"Could not initialize the backup {}, please use 'backup-cli' to initialize the backup first.".format(
destination
)
)
assert backend.version is not None
assert backend.prev_version is not None
return backend
42 changes: 22 additions & 20 deletions backup/backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

handler = logging.StreamHandler(sys.stdout)
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(message)s')
formatter = logging.Formatter("%(message)s")
handler.setFormatter(formatter)
root.addHandler(handler)

Expand All @@ -37,9 +37,11 @@ def check_first_write(plugin, data_version):
"""
backend = plugin.backend

logging.info("Comparing backup version {} versus first write version {}".format(
backend.version, data_version
))
logging.info(
"Comparing backup version {} versus first write version {}".format(
backend.version, data_version
)
)

if backend.version == data_version - 1:
logging.info("Versions match up")
Expand All @@ -50,13 +52,15 @@ def check_first_write(plugin, data_version):
return True

elif backend.prev_version > data_version - 1:
kill("Core-Lightning seems to have lost some state (failed restore?). Emergency shutdown.")
kill(
"Core-Lightning seems to have lost some state (failed restore?). Emergency shutdown."
)

else:
kill("Backup is out of date, we cannot continue safely. Emergency shutdown.")


@plugin.hook('db_write')
@plugin.hook("db_write")
def on_db_write(writes, data_version, plugin, **kwargs):
change = Change(data_version, None, writes)
if not plugin.initialized:
Expand Down Expand Up @@ -85,14 +89,14 @@ def compact(plugin):

@plugin.init()
def on_init(options, **kwargs):
dest = options.get('backup-destination', 'null')
if dest != 'null':
dest = options.get("backup-destination", "null")
if dest != "null":
plugin.log(
"The `--backup-destination` option is deprecated and will be "
"removed in future versions of the backup plugin. Please remove "
"it from your configuration. The destination is now determined by "
"the `backup.lock` file in the lightning directory",
level="warn"
level="warn",
)

# IMPORTANT NOTE
Expand All @@ -109,12 +113,9 @@ def kill(message: str):
# Search for lightningd in my ancestor processes:
procs = [p for p in psutil.Process(os.getpid()).parents()]
for p in procs:
if p.name() != 'lightningd':
if p.name() != "lightningd":
continue
plugin.log("Killing process {name} ({pid})".format(
name=p.name(),
pid=p.pid
))
plugin.log("Killing process {name} ({pid})".format(name=p.name(), pid=p.pid))
p.kill()

# Sleep forever, just in case the master doesn't die on us...
Expand All @@ -123,8 +124,9 @@ def kill(message: str):


plugin.add_option(
'backup-destination', None,
'UNUSED. Kept for backward compatibility only. Please update your configuration to remove this option.'
"backup-destination",
None,
"UNUSED. Kept for backward compatibility only. Please update your configuration to remove this option.",
)


Expand All @@ -135,10 +137,10 @@ def kill(message: str):
kill("Could not find backup.lock in the lightning-dir")

try:
d = json.load(open("backup.lock", 'r'))
destination = d['backend_url']
d = json.load(open("backup.lock", "r"))
destination = d["backend_url"]
plugin.backend = get_backend(destination, require_init=True)
plugin.run()
except Exception:
logging.exception('Exception while initializing backup plugin')
kill('Exception while initializing plugin, terminating lightningd')
logging.exception("Exception while initializing backup plugin")
kill("Exception while initializing plugin, terminating lightningd")
Loading

0 comments on commit 1d3356c

Please sign in to comment.