Skip to content

Commit

Permalink
Clean script.py; Move more files and functions; ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Jan 14, 2025
1 parent a2b4620 commit 12ba3f1
Show file tree
Hide file tree
Showing 21 changed files with 65 additions and 54 deletions.
12 changes: 6 additions & 6 deletions counterparty-core/counterpartycore/lib/api/api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def get_rows(
def value_to_marker(value):
# if value is an array place holder is (?,?,?,..)
if isinstance(value, list):
return f"""({','.join(['?' for e in range(0, len(value))])})"""
return f"""({",".join(["?" for e in range(0, len(value))])})"""
else:
return """?"""

Expand Down Expand Up @@ -288,10 +288,10 @@ def value_to_marker(value):
for filter_ in filters:
case_sensitive = False if "case_sensitive" not in filter_ else filter_["case_sensitive"]
if filter_["op"] == "LIKE" and case_sensitive == False: # noqa: E712
filter_["field"] = f"""UPPER({filter_['field']})"""
filter_["field"] = f"""UPPER({filter_["field"]})"""
filter_["value"] = filter_["value"].upper()
marker = value_to_marker(filter_["value"])
conditions.append(f"""{filter_['field']} {filter_['op']} {marker}""")
conditions.append(f"""{filter_["field"]} {filter_["op"]} {marker}""")
if isinstance(filter_["value"], list):
bindings += filter_["value"]
else:
Expand Down Expand Up @@ -332,10 +332,10 @@ def value_to_marker(value):
statement += """ WHERE"""
all_conditions = []
if len(conditions) > 0:
all_conditions.append(f"""({f' {filterop.upper()} '.join(conditions)})""")
all_conditions.append(f"""({f" {filterop.upper()} ".join(conditions)})""")
if len(more_conditions) > 0:
all_conditions.append(f"""({' AND '.join(more_conditions)})""")
statement += f""" {' AND '.join(all_conditions)}"""
all_conditions.append(f"""({" AND ".join(more_conditions)})""")
statement += f""" {" AND ".join(all_conditions)}"""

# ORDER BY
if order_by != None: # noqa: E711
Expand Down
5 changes: 3 additions & 2 deletions counterparty-core/counterpartycore/lib/api/dbbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
import sys
import time

from counterpartycore.lib import config
from counterpartycore.lib.cli import log
from yoyo import get_backend, read_migrations
from yoyo.exceptions import LockTimeout
from yoyo.migrations import topological_sort

from counterpartycore.lib import config
from counterpartycore.lib.cli import log

logger = logging.getLogger(config.LOGGER_NAME)

CURRENT_DIR = os.path.dirname(os.path.realpath(__file__))
Expand Down
3 changes: 2 additions & 1 deletion counterparty-core/counterpartycore/lib/api/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import typing
from typing import Literal

from counterpartycore.lib.api.util import divide
from sentry_sdk import start_span as start_sentry_span

from counterpartycore.lib.api.util import divide

OrderStatus = Literal["all", "open", "expired", "filled", "cancelled"]
OrderMatchesStatus = Literal["all", "pending", "completed", "expired"]
BetStatus = Literal["cancelled", "dropped", "expired", "filled", "open"]
Expand Down
3 changes: 2 additions & 1 deletion counterparty-core/counterpartycore/lib/api/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import flask
import requests
import werkzeug
from docstring_parser import parse as parse_docstring

from counterpartycore.lib import (
backend,
composer,
Expand All @@ -20,7 +22,6 @@
util,
)
from counterpartycore.lib.api import compose
from docstring_parser import parse as parse_docstring

D = decimal.Decimal
logger = logging.getLogger(config.LOGGER_NAME)
Expand Down
9 changes: 5 additions & 4 deletions counterparty-core/counterpartycore/lib/api/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
import gunicorn.app.base
import waitress
import waitress.server
from counterpartycore.lib import config, database, ledger, util
from counterpartycore.lib.api import api_watcher
from counterpartycore.lib.api.util import BackendHeight
from counterpartycore.lib.cli import log
from gunicorn import util as gunicorn_util
from gunicorn.arbiter import Arbiter
from gunicorn.errors import AppImportError
from werkzeug.serving import make_server

from counterpartycore.lib import config, database, ledger, util
from counterpartycore.lib.api import api_watcher
from counterpartycore.lib.api.util import BackendHeight
from counterpartycore.lib.cli import log

multiprocessing.set_start_method("spawn", force=True)

logger = logging.getLogger(config.LOGGER_NAME)
Expand Down
3 changes: 2 additions & 1 deletion counterparty-core/counterpartycore/lib/cli/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@

import gnupg
import pyzstd
from termcolor import colored, cprint

from counterpartycore.lib import config
from counterpartycore.lib.cli.public_keys import PUBLIC_KEYS
from termcolor import colored, cprint


def download_zst(data_dir, zst_url):
Expand Down
5 changes: 3 additions & 2 deletions counterparty-core/counterpartycore/lib/cli/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
from multiprocessing import current_process

import zmq
from counterpartycore.lib import config, util
from counterpartycore.lib.api.util import to_json
from dateutil.tz import tzlocal
from halo import Halo
from json_log_formatter import JSONFormatter
from termcolor import colored, cprint

from counterpartycore.lib import config, util
from counterpartycore.lib.api.util import to_json

logging.TRACE = logging.DEBUG - 5
logging.addLevelName(logging.TRACE, "TRACE")
logging.EVENT = logging.DEBUG - 4
Expand Down
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/cli/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def initialise_config(
config.NETWORK_NAME = "testnet4"
if config.REGTEST:
config.NETWORK_NAME = "regtest"
network = f".{config.NETWORK_NAME }" if config.NETWORK_NAME != "mainnet" else ""
network = f".{config.NETWORK_NAME}" if config.NETWORK_NAME != "mainnet" else ""

# Database
if database_file:
Expand Down
1 change: 1 addition & 0 deletions counterparty-core/counterpartycore/lib/cli/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from decimal import Decimal as D

import appdirs

from counterpartycore.lib import config

logger = logging.getLogger(config.LOGGER_NAME)
Expand Down
2 changes: 1 addition & 1 deletion counterparty-core/counterpartycore/lib/messages/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def initialise(db):
"give_get_status_idx",
"source_idx",
"give_asset_idx",
"match_expire_idx" "forward_status_idx",
"match_expire_idxforward_status_idx",
"backward_status_idx",
"id_idx",
"tx0_address_idx",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _solve_asset(db, asset_name, block_index):
def _encode_compress_send_list(db, nbits, send, block_index):
r = BitArray()
r.append(f"uintbe:64={_solve_asset(db, send['assetName'], block_index)}")
r.append(f"uint:{nbits}={len(send['sendList'])-1}")
r.append(f"uint:{nbits}={len(send['sendList']) - 1}")

for send_item in send["sendList"]:
idx = send_item[0]
Expand Down
1 change: 1 addition & 0 deletions counterparty-core/counterpartycore/lib/parser/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings

import requests

from counterpartycore.lib import config, database, ledger, util # noqa: F401

logger = logging.getLogger(config.LOGGER_NAME)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from counterparty_rs import indexer

from counterpartycore.lib import config, util


Expand Down
3 changes: 2 additions & 1 deletion counterparty-core/counterpartycore/lib/parser/follow.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import zmq
import zmq.asyncio
from sentry_sdk import capture_exception

from counterpartycore.lib import (
backend,
config,
Expand All @@ -17,7 +19,6 @@
from counterpartycore.lib.parser import blocks, check, deserialize, mempool
from counterpartycore.lib.tools import sentry
from counterpartycore.lib.tools.telemetry.oneshot import TelemetryOneShot
from sentry_sdk import capture_exception

logger = logging.getLogger(config.LOGGER_NAME)

Expand Down
1 change: 1 addition & 0 deletions counterparty-core/counterpartycore/lib/tools/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

import sentry_sdk

from counterpartycore.lib import config, database
from counterpartycore.lib.tools.telemetry.collectors.base import TelemetryCollectorBase

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
"error": (exceptions.AssetNameError, "non‐numeric asset name starts with ‘A’"),
},
{
"in": (f"A{26 ** 12}", 308000),
"in": (f"A{26**12}", 308000),
"error": (exceptions.AssetNameError, "numeric asset name not in range"),
},
{
"in": (f"A{2 ** 64}", 308000),
"in": (f"A{2**64}", 308000),
"error": (exceptions.AssetNameError, "numeric asset name not in range"),
},
{"in": (f"A{26 ** 12 + 1}", 308000), "out": 26**12 + 1},
{"in": (f"A{2 ** 64 - 1}", 308000), "out": 2**64 - 1},
{"in": (f"A{26**12 + 1}", 308000), "out": 26**12 + 1},
{"in": (f"A{2**64 - 1}", 308000), "out": 2**64 - 1},
{
"in": ("LONGASSETNAMES", 308000),
"error": (exceptions.AssetNameError, "long asset names must be numeric"),
Expand All @@ -45,8 +45,8 @@
{"in": (1, DP["default_block_index"]), "out": "XCP"},
{"in": (26**12 - 1, 308000), "out": "ZZZZZZZZZZZZ"},
{"in": (26**3, 308000), "out": "BAAA"},
{"in": (2**64 - 1, 308000), "out": f"A{2 ** 64 - 1}"},
{"in": (26**12 + 1, 308000), "out": f"A{26 ** 12 + 1}"},
{"in": (2**64 - 1, 308000), "out": f"A{2**64 - 1}"},
{"in": (26**12 + 1, 308000), "out": f"A{26**12 + 1}"},
{"in": (26**3 - 1, 308000), "error": (exceptions.AssetIDError, "too low")},
{"in": (2**64, 308000), "error": (exceptions.AssetIDError, "too high")},
],
Expand Down Expand Up @@ -80,7 +80,7 @@
{"in": ("PARENT",), "out": "PARENT"},
{"in": ("PARENT.nonexistent.subasset",), "out": "PARENT.nonexistent.subasset"},
{"in": ("PARENT.ILEGAL^^^",), "out": "PARENT.ILEGAL^^^"},
{"in": ("PARENT.already.issued",), "out": f"A{26 ** 12 + 101}"},
{"in": ("PARENT.already.issued",), "out": f"A{26**12 + 101}"},
],
"debit": [
{"in": (ADDR[0], "XCP", 1, 0), "out": None},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@
"in": (
ADDR[0],
None,
f"A{26 ** 12 + 1}",
f"A{26**12 + 1}",
1000,
True,
None,
Expand Down Expand Up @@ -610,7 +610,7 @@
"in": (
ADDR[1],
None,
f"A{26 ** 12 + 1}",
f"A{26**12 + 1}",
100000000,
True,
None,
Expand Down Expand Up @@ -641,7 +641,7 @@
"in": (
ADDR[0],
None,
f"A{26 ** 12 + 1}",
f"A{26**12 + 1}",
100000000,
True,
None,
Expand Down Expand Up @@ -673,7 +673,7 @@
"in": (
ADDR[0],
None,
f"A{26 ** 12 + 1}",
f"A{26**12 + 1}",
100000000,
True,
None,
Expand Down Expand Up @@ -704,7 +704,7 @@
"in": (
ADDR[0],
None,
f"A{26 ** 12 + 101}",
f"A{26**12 + 101}",
200000000,
True,
None,
Expand Down Expand Up @@ -979,19 +979,19 @@
),
},
{
"in": (ADDR[0], f"A{2 ** 64 - 1}", 1000, None, None, False, None, None),
"in": (ADDR[0], f"A{2**64 - 1}", 1000, None, None, False, None, None),
"out": (
"mn6q3dS2EnDUx3bmyWc6D4szJNVGtaR7zc",
[],
b"\x00\x00\x00\x16\xff\xff\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x03\xe8\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0NULL",
),
},
{
"in": (ADDR[0], f"A{2 ** 64}", 1000, None, True, False, None, ""),
"in": (ADDR[0], f"A{2**64}", 1000, None, True, False, None, ""),
"error": (exceptions.AssetNameError, "numeric asset name not in range"),
},
{
"in": (ADDR[0], f"A{26 ** 12}", 1000, None, True, False, None, ""),
"in": (ADDR[0], f"A{26**12}", 1000, None, True, False, None, ""),
"error": (exceptions.AssetNameError, "numeric asset name not in range"),
},
{
Expand Down Expand Up @@ -1098,7 +1098,7 @@
{
"in": (
ADDR[0],
f"A{26 ** 12 + 101}",
f"A{26**12 + 101}",
200000000,
None,
True,
Expand Down Expand Up @@ -1592,7 +1592,7 @@
{
"table": "issuances",
"values": {
"asset": f"A{26 ** 12 + 1}",
"asset": f"A{26**12 + 1}",
"asset_longname": "PARENT.child1",
"block_index": DP["default_block_index"],
"description": "",
Expand All @@ -1612,7 +1612,7 @@
"table": "credits",
"values": {
"address": "mn6q3dS2EnDUx3bmyWc6D4szJNVGtaR7zc",
"asset": f"A{26 ** 12 + 1}",
"asset": f"A{26**12 + 1}",
"block_index": DP["default_block_index"],
"calling_function": "issuance",
"event": "71da4fac29d6442ef3ff13f291860f512a888161ae9e574f313562851912aace",
Expand All @@ -1634,7 +1634,7 @@
"table": "assets",
"values": {
"asset_id": int(26**12 + 1),
"asset_name": f"A{26 ** 12 + 1}",
"asset_name": f"A{26**12 + 1}",
"block_index": DP["default_block_index"],
"asset_longname": "PARENT.child1",
},
Expand Down Expand Up @@ -1666,7 +1666,7 @@
{
"table": "issuances",
"values": {
"asset": f"A{26 ** 12 + 1}",
"asset": f"A{26**12 + 1}",
"asset_longname": "PARENT.child1",
"block_index": DP["default_block_index"],
"description": "hello world",
Expand All @@ -1686,7 +1686,7 @@
"table": "credits",
"values": {
"address": "mn6q3dS2EnDUx3bmyWc6D4szJNVGtaR7zc",
"asset": f"A{26 ** 12 + 1}",
"asset": f"A{26**12 + 1}",
"block_index": DP["default_block_index"],
"calling_function": "issuance",
"event": "71da4fac29d6442ef3ff13f291860f512a888161ae9e574f313562851912aace",
Expand All @@ -1708,7 +1708,7 @@
"table": "assets",
"values": {
"asset_id": int(26**12 + 1),
"asset_name": f"A{26 ** 12 + 1}",
"asset_name": f"A{26**12 + 1}",
"block_index": DP["default_block_index"],
"asset_longname": "PARENT.child1",
},
Expand Down Expand Up @@ -1986,7 +1986,7 @@
{
"table": "issuances",
"values": {
"asset": f"A{26 ** 12 + 101}",
"asset": f"A{26**12 + 101}",
"asset_longname": "PARENT.already.issued",
"block_index": DP["default_block_index"],
"description": "description",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@
"create_issuance",
{
"source": ADDR[0],
"asset": f"A{2 ** 64 - 1}",
"asset": f"A{2**64 - 1}",
"quantity": 1000,
"encoding": "multisig",
},
Expand Down
Loading

0 comments on commit 12ba3f1

Please sign in to comment.