Skip to content

Commit 0a2887f

Browse files
committed
Use minimal __main__.py without the __name__ check according to Python doc
1 parent 34b127b commit 0a2887f

File tree

3 files changed

+90
-77
lines changed

3 files changed

+90
-77
lines changed

python/grass/app/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ DSTDIR = $(ETC)/python/grass/app
77

88
MODULES = \
99
__main__ \
10+
cli \
1011
data \
1112
runtime
1213

python/grass/app/__main__.py

Lines changed: 3 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""Experimental low-level CLI interface for the main GRASS executable functionality
1+
"""Minimal file for package execution for low-level CLI interface
22
33
(C) 2025 by Vaclav Petras and the GRASS Development Team
44
@@ -11,80 +11,6 @@
1111
This is not a stable part of the API. Use at your own risk.
1212
"""
1313

14-
import argparse
15-
import os
16-
import sys
17-
from pathlib import Path
14+
from .cli import main
1815

19-
import grass.script as gs
20-
from grass.app.data import lock_mapset, unlock_mapset, MapsetLockingException
21-
22-
23-
def subcommand_lock_mapset(args):
24-
gs.setup.setup_runtime_env()
25-
try:
26-
lock_mapset(
27-
args.mapset_path,
28-
force_lock_removal=args.force_remove_lock,
29-
timeout=args.timeout,
30-
message_callback=print,
31-
process_id=args.process_id,
32-
)
33-
except MapsetLockingException as e:
34-
print(str(e), file=sys.stderr)
35-
36-
37-
def subcommand_unlock_mapset(args):
38-
unlock_mapset(args.mapset_path)
39-
40-
41-
def main():
42-
# Top-level parser
43-
program = os.path.basename(sys.argv[0])
44-
if program == "__main__.py":
45-
program = f"{Path(sys.executable).name} -m grass.app"
46-
parser = argparse.ArgumentParser(
47-
description="Experimental low-level CLI interface to GRASS. Consult developers before using it.",
48-
prog=program,
49-
)
50-
subparsers = parser.add_subparsers(title="subcommands", required=True)
51-
52-
# Subcommand parsers
53-
54-
parser_foo = subparsers.add_parser("lock", help="lock a mapset")
55-
parser_foo.add_argument("mapset_path", type=str)
56-
parser_foo.add_argument(
57-
"--process-id",
58-
metavar="PID",
59-
type=int,
60-
default=1,
61-
help=_(
62-
"process ID of the process locking the mapset (a mapset can be "
63-
"automatically unlocked if there is no process with this PID)"
64-
),
65-
)
66-
parser_foo.add_argument(
67-
"--timeout",
68-
metavar="TIMEOUT",
69-
type=float,
70-
default=30,
71-
help=_("mapset locking timeout in seconds"),
72-
)
73-
parser_foo.add_argument(
74-
"-f",
75-
"--force-remove-lock",
76-
action="store_true",
77-
help=_("remove lock if present"),
78-
)
79-
parser_foo.set_defaults(func=subcommand_lock_mapset)
80-
81-
parser_bar = subparsers.add_parser("unlock", help="unlock a mapset")
82-
parser_bar.add_argument("mapset_path", type=str)
83-
parser_bar.set_defaults(func=subcommand_unlock_mapset)
84-
85-
args = parser.parse_args()
86-
args.func(args)
87-
88-
89-
if __name__ == "__main__":
90-
main()
16+
main()

python/grass/app/cli.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"""Experimental low-level CLI interface for the main GRASS executable functionality
2+
3+
(C) 2025 by Vaclav Petras and the GRASS Development Team
4+
5+
This program is free software under the GNU General Public
6+
License (>=v2). Read the file COPYING that comes with GRASS
7+
for details.
8+
9+
.. sectionauthor:: Vaclav Petras <wenzeslaus gmail com>
10+
11+
This is not a stable part of the API. Use at your own risk.
12+
"""
13+
14+
import argparse
15+
import os
16+
import sys
17+
from pathlib import Path
18+
19+
import grass.script as gs
20+
from grass.app.data import lock_mapset, unlock_mapset, MapsetLockingException
21+
22+
23+
def subcommand_lock_mapset(args):
24+
gs.setup.setup_runtime_env()
25+
try:
26+
lock_mapset(
27+
args.mapset_path,
28+
force_lock_removal=args.force_remove_lock,
29+
timeout=args.timeout,
30+
message_callback=print,
31+
process_id=args.process_id,
32+
)
33+
except MapsetLockingException as e:
34+
print(str(e), file=sys.stderr)
35+
36+
37+
def subcommand_unlock_mapset(args):
38+
unlock_mapset(args.mapset_path)
39+
40+
41+
def main():
42+
# Top-level parser
43+
program = os.path.basename(sys.argv[0])
44+
if program == "__main__.py":
45+
program = f"{Path(sys.executable).name} -m grass.app"
46+
parser = argparse.ArgumentParser(
47+
description="Experimental low-level CLI interface to GRASS. Consult developers before using it.",
48+
prog=program,
49+
)
50+
subparsers = parser.add_subparsers(title="subcommands", required=True)
51+
52+
# Subcommand parsers
53+
54+
parser_foo = subparsers.add_parser("lock", help="lock a mapset")
55+
parser_foo.add_argument("mapset_path", type=str)
56+
parser_foo.add_argument(
57+
"--process-id",
58+
metavar="PID",
59+
type=int,
60+
default=1,
61+
help=_(
62+
"process ID of the process locking the mapset (a mapset can be "
63+
"automatically unlocked if there is no process with this PID)"
64+
),
65+
)
66+
parser_foo.add_argument(
67+
"--timeout",
68+
metavar="TIMEOUT",
69+
type=float,
70+
default=30,
71+
help=_("mapset locking timeout in seconds"),
72+
)
73+
parser_foo.add_argument(
74+
"-f",
75+
"--force-remove-lock",
76+
action="store_true",
77+
help=_("remove lock if present"),
78+
)
79+
parser_foo.set_defaults(func=subcommand_lock_mapset)
80+
81+
parser_bar = subparsers.add_parser("unlock", help="unlock a mapset")
82+
parser_bar.add_argument("mapset_path", type=str)
83+
parser_bar.set_defaults(func=subcommand_unlock_mapset)
84+
85+
args = parser.parse_args()
86+
args.func(args)

0 commit comments

Comments
 (0)