Skip to content

Commit a071050

Browse files
anderdcwdeveloper16
authored andcommitted
refactor: move GITTENSOR_DIR and CONFIG_FILE to helpers.py
2 parents 3b145c3 + 7f9e932 commit a071050

6 files changed

Lines changed: 63 additions & 42 deletions

File tree

gittensor/cli/issue_commands/helpers.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,19 @@
1515
import urllib.request
1616
from contextlib import nullcontext
1717
from decimal import Decimal, InvalidOperation
18+
from pathlib import Path
1819
from typing import Any, Callable, ContextManager, Dict, List, Optional, Tuple, TypeVar
1920

2021
import click
2122
from rich.console import Console
2223
from rich.panel import Panel
2324

2425
from gittensor.cli.issue_commands.tables import build_pr_table
25-
from gittensor.constants import CONFIG_FILE, CONTRACT_ADDRESS, GITTENSOR_DIR, NETWORK_MAP
26+
from gittensor.constants import CONTRACT_ADDRESS, NETWORK_MAP
27+
28+
# Default CLI config paths
29+
GITTENSOR_DIR = Path.home() / '.gittensor'
30+
CONFIG_FILE = GITTENSOR_DIR / 'config.json'
2631

2732
# ALPHA token conversion
2833
ALPHA_DECIMALS = 9

gittensor/cli/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
from gittensor.cli.issue_commands import register_commands
2424
from gittensor.cli.issue_commands.help import StyledAliasGroup, StyledGroup
25-
from gittensor.constants import CONFIG_FILE, GITTENSOR_DIR
25+
from gittensor.cli.issue_commands.helpers import CONFIG_FILE, GITTENSOR_DIR
2626

2727
console = Console()
2828

gittensor/cli/miner_commands/check.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
from rich.console import Console
1111
from rich.table import Table
1212

13-
from .helpers import _get_validator_axons
14-
from .post import NETUID_DEFAULT, _error, _load_config_value, _resolve_endpoint
13+
from .helpers import NETUID_DEFAULT, _error, _get_validator_axons, _load_config_value, _resolve_endpoint
1514

1615
console = Console()
1716

gittensor/cli/miner_commands/helpers.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,18 @@
44

55
from __future__ import annotations
66

7+
import json
8+
from pathlib import Path
9+
10+
import click
11+
from rich.console import Console
12+
13+
from gittensor.constants import NETWORK_MAP
14+
15+
console = Console()
16+
17+
NETUID_DEFAULT = 74
18+
719

820
def _get_validator_axons(metagraph) -> tuple[list, list]:
921
"""Return (axons, uids) for all active validators (vtrust > 0.1, serving)."""
@@ -14,3 +26,38 @@ def _get_validator_axons(metagraph) -> tuple[list, list]:
1426
axons.append(metagraph.axons[uid])
1527
uids.append(uid)
1628
return axons, uids
29+
30+
31+
def _load_config_value(key: str):
32+
"""Load a value from ~/.gittensor/config.json, or None."""
33+
config_file = Path.home() / '.gittensor' / 'config.json'
34+
if not config_file.exists():
35+
return None
36+
try:
37+
config = json.loads(config_file.read_text())
38+
return config.get(key)
39+
except (json.JSONDecodeError, OSError):
40+
return None
41+
42+
43+
def _resolve_endpoint(network: str | None, rpc_url: str | None) -> str:
44+
"""Resolve the subtensor endpoint from CLI args or config."""
45+
if rpc_url:
46+
return rpc_url
47+
if network:
48+
return NETWORK_MAP.get(network.lower(), network)
49+
config_network = _load_config_value('network')
50+
config_endpoint = _load_config_value('ws_endpoint')
51+
if config_endpoint:
52+
return config_endpoint
53+
if config_network:
54+
return NETWORK_MAP.get(config_network.lower()) or config_network
55+
return NETWORK_MAP['finney']
56+
57+
58+
def _error(msg: str, json_mode: bool) -> None:
59+
"""Print an error message in the appropriate format."""
60+
if json_mode:
61+
click.echo(json.dumps({'success': False, 'error': msg}))
62+
else:
63+
console.print(f'[red]Error: {msg}[/red]')

gittensor/cli/miner_commands/post.py

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,17 @@
1414
from rich.console import Console
1515
from rich.table import Table
1616

17-
from gittensor.cli.issue_commands.helpers import resolve_network
18-
from gittensor.cli.miner_commands.helpers import _get_validator_axons
17+
from gittensor.cli.miner_commands.helpers import (
18+
NETUID_DEFAULT,
19+
_error,
20+
_get_validator_axons,
21+
_load_config_value,
22+
_resolve_endpoint,
23+
)
1924
from gittensor.constants import BASE_GITHUB_API_URL
2025

2126
console = Console()
2227

23-
# Shared CLI options for wallet/network configuration
24-
NETUID_DEFAULT = 74
25-
26-
27-
def _resolve_endpoint(network: str | None, rpc_url: str | None) -> str:
28-
"""Resolve miner network options to a websocket endpoint."""
29-
ws_endpoint, _ = resolve_network(network=network, rpc_url=rpc_url)
30-
return ws_endpoint
31-
3228

3329
@click.command()
3430
@click.option('--wallet', 'wallet_name', default=None, help='Bittensor wallet name.')
@@ -89,7 +85,7 @@ def miner_post(wallet_name, wallet_hotkey, netuid, network, rpc_url, pat, json_m
8985
# 2. Resolve wallet and network
9086
wallet_name = wallet_name or _load_config_value('wallet') or 'default'
9187
wallet_hotkey = wallet_hotkey or _load_config_value('hotkey') or 'default'
92-
ws_endpoint = _resolve_endpoint(network=network, rpc_url=rpc_url)
88+
ws_endpoint = _resolve_endpoint(network, rpc_url)
9389

9490
if not json_mode:
9591
console.print(f'[dim]Wallet: {wallet_name}/{wallet_hotkey} | Network: {ws_endpoint} | Netuid: {netuid}[/dim]')
@@ -223,25 +219,3 @@ def _validate_pat_locally(pat: str) -> bool:
223219
return True
224220
except requests.RequestException:
225221
return False
226-
227-
228-
def _load_config_value(key: str):
229-
"""Load a value from ~/.gittensor/config.json, or None."""
230-
from pathlib import Path
231-
232-
config_file = Path.home() / '.gittensor' / 'config.json'
233-
if not config_file.exists():
234-
return None
235-
try:
236-
config = json.loads(config_file.read_text())
237-
return config.get(key)
238-
except (json.JSONDecodeError, OSError):
239-
return None
240-
241-
242-
def _error(msg: str, json_mode: bool):
243-
"""Print an error message in the appropriate format."""
244-
if json_mode:
245-
click.echo(json.dumps({'success': False, 'error': msg}))
246-
else:
247-
console.print(f'[red]Error: {msg}[/red]')

gittensor/constants.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
# Entrius 2025
22
import re
3-
from pathlib import Path
43
from typing import Dict
54

65
# =============================================================================
76
# General
87
# =============================================================================
9-
GITTENSOR_DIR = Path.home() / '.gittensor'
10-
CONFIG_FILE = GITTENSOR_DIR / 'config.json'
11-
128
SECONDS_PER_DAY = 86400
139
SECONDS_PER_HOUR = 3600
1410

0 commit comments

Comments
 (0)