Skip to content

Commit 877bd83

Browse files
committed
Made single-use imports lazy
1 parent 01388b8 commit 877bd83

36 files changed

Lines changed: 106 additions & 68 deletions

File tree

find-project-root/utils/bump.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import argparse, re, sys
21
from pathlib import Path
32
from types import SimpleNamespace as sn
43

5-
from lib import data, git, log, toml
4+
from lib import data, log, toml
65

76
paths = sn(root=Path(__file__).parent.parent)
87
paths.pyproject = paths.root / 'pyproject.toml'
@@ -12,6 +11,7 @@
1211
msgs = sn(**{ key:val['message'] for key,val in data.json.read(paths.util_msgs)['bump'].items() })
1312

1413
def parse_args():
14+
import argparse
1515
argp = argparse.ArgumentParser(description=msgs.app_DESC, add_help=False)
1616
argp.add_argument('-M', '--major', action='store_true', help=msgs.help_MAJOR)
1717
argp.add_argument('-m', '--minor', action='store_true', help=msgs.help_MINOR)
@@ -47,6 +47,7 @@ def bump_pyproject_vers(pyproject, project, new_ver):
4747
log.success(msgs.log_BUMPED_CLOG_URL_VER_TAG.format(**locals()))
4848

4949
def update_readme_vers(new_ver):
50+
import re
5051
log.info(f'{msgs.log_UPDATING_VERS_IN} docs/README.md...')
5152
updated_readme_content = re.sub(r'\b(?>\d{1,3}\.\d{1,3}\.\d{1,3})\b', new_ver, data.file.read(paths.readme))
5253
data.file.write(paths.readme, updated_readme_content)
@@ -58,6 +59,7 @@ def main():
5859
args = parse_args()
5960
bump_type = 'major' if args.major else 'minor' if args.minor else 'patch' if args.patch else None
6061
if not bump_type:
62+
import sys
6163
log.error(msgs.err_MISSING_BUMP_TYPE_ARG)
6264
sys.exit(1)
6365

@@ -75,6 +77,7 @@ def main():
7577
if args.no_commit:
7678
print(f'\n{msgs.log_SKIPPING_GIT_COMMIT}...')
7779
else:
80+
from lib import git
7881
git.init_kudo_sync_bot(msgs)
7982
log.info(f'{msgs.log_COMMITTING_CHANGES}...')
8083
git.commit([str(paths.pyproject)],

find-project-root/utils/lib/data/json.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import json5
66

7-
from . import file
8-
97
def flatten(json: Dict[str, Any], key: str = 'message') -> Dict[str, Any]: # eliminate need to ref nested keys
108
flat_obj = {}
119
for json_key in json:
@@ -38,6 +36,7 @@ def read(input: Union[Path, str], encoding: str = 'utf-8') -> Any:
3836

3937
def write(file_path: Union[Path, str], data: Any, encoding: str = 'utf-8', ensure_ascii: bool = False,
4038
style: str = 'pretty', atomic: bool =True):
39+
from . import file
4140
Path(file_path).parent.mkdir(parents=True, exist_ok=True)
4241
if style == 'pretty': # single key/val spans multi-lines
4342
json_str = json.dumps(data, indent=2, ensure_ascii=ensure_ascii)

find-project-root/utils/lib/git.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import os, subprocess, sys
21
from pathlib import Path
2+
import subprocess
33

44
def commit(files, msg, *args) : run('add', *files) ; run('commit', '-m', msg, *args)
55

66
def init_kudo_sync_bot(msgs):
7+
import os
78
print(f'\n{msgs.log_SWITCHING_TO_KUDO_SYNC_BOT}...\n')
89
with open(Path.home() / '.gitconfig.backup', 'w') as file: # back up git config
910
file.write(run('config', '--global', '--list'))
@@ -38,6 +39,7 @@ def restore_og_config(msgs):
3839
def run(*args):
3940
result = subprocess.run(['git'] + list(args), capture_output=True, text=True)
4041
if result.returncode != 0:
42+
import sys
4143
print(f"Git command failed: {' '.join(['git'] + list(args))}")
4244
print(result.stderr)
4345
sys.exit(1)

find-project-root/utils/lib/log.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from typing import Optional
44
if sys.platform == 'win32' : import colorama ; colorama.init() # enable ANSI color support
55

6-
from . import env
7-
86
try : terminal_width = os.get_terminal_size()[0]
97
except OSError : terminal_width = 80
108

@@ -33,6 +31,7 @@ def tip(msg: str, *args, **kwargs) -> None : print(f'\n{colors.bc}TIP: {msg.form
3331
def warn(msg: str, *args, **kwargs) -> None : print(f'\n{colors.bo}WARNING: {msg.format(*args, **kwargs)}{colors.nc}')
3432

3533
def debug(msg: str, cli: Optional[sn] = None, *args, **kwargs) -> None:
34+
from . import env
3635
if not env.is_debug_mode() : return
3736

3837
# Init --debug [target]
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import tomli, tomli_w
2-
31
def read(file_path):
2+
import tomli
43
with open(file_path, 'rb') as file:
54
return tomli.load(file)
65

76
def write(file_path, data):
7+
import tomli_w
88
with open(file_path, 'wb') as file:
99
tomli_w.dump(data, file)

latin-locales/utils/bump.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import argparse, re, sys
21
from pathlib import Path
32
from types import SimpleNamespace as sn
43

5-
from lib import data, git, log, toml
4+
from lib import data, log, toml
65

76
paths = sn(root=Path(__file__).parent.parent)
87
paths.pyproject = paths.root / 'pyproject.toml'
@@ -12,6 +11,7 @@
1211
msgs = sn(**{ key:val['message'] for key,val in data.json.read(paths.util_msgs)['bump'].items() })
1312

1413
def parse_args():
14+
import argparse
1515
argp = argparse.ArgumentParser(description=msgs.app_DESC, add_help=False)
1616
argp.add_argument('-M', '--major', action='store_true', help=msgs.help_MAJOR)
1717
argp.add_argument('-m', '--minor', action='store_true', help=msgs.help_MINOR)
@@ -47,6 +47,7 @@ def bump_pyproject_vers(pyproject, project, new_ver):
4747
log.success(msgs.log_BUMPED_CLOG_URL_VER_TAG.format(**locals()))
4848

4949
def update_readme_vers(new_ver):
50+
import re
5051
log.info(f'{msgs.log_UPDATING_VERS_IN} docs/README.md...')
5152
updated_readme_content = re.sub(r'\b(?>\d{1,3}\.\d{1,3}\.\d{1,3})\b', new_ver, data.file.read(paths.readme))
5253
data.file.write(paths.readme, updated_readme_content)
@@ -58,6 +59,7 @@ def main():
5859
args = parse_args()
5960
bump_type = 'major' if args.major else 'minor' if args.minor else 'patch' if args.patch else None
6061
if not bump_type:
62+
import sys
6163
log.error(msgs.err_MISSING_BUMP_TYPE_ARG)
6264
sys.exit(1)
6365

@@ -75,6 +77,7 @@ def main():
7577
if args.no_commit:
7678
print(f'\n{msgs.log_SKIPPING_GIT_COMMIT}...')
7779
else:
80+
from lib import git
7881
git.init_kudo_sync_bot(msgs)
7982
log.info(f'{msgs.log_COMMITTING_CHANGES}...')
8083
git.commit([str(paths.pyproject)],

latin-locales/utils/lib/data/json.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
import json5
66

7-
from . import file
8-
97
def flatten(json: Dict[str, Any], key: str = 'message') -> Dict[str, Any]: # eliminate need to ref nested keys
108
flat_obj = {}
119
for json_key in json:
@@ -38,6 +36,7 @@ def read(input: Union[Path, str], encoding: str = 'utf-8') -> Any:
3836

3937
def write(file_path: Union[Path, str], data: Any, encoding: str = 'utf-8', ensure_ascii: bool = False,
4038
style: str = 'pretty', atomic: bool =True):
39+
from . import file
4140
Path(file_path).parent.mkdir(parents=True, exist_ok=True)
4241
if style == 'pretty': # single key/val spans multi-lines
4342
json_str = json.dumps(data, indent=2, ensure_ascii=ensure_ascii)

latin-locales/utils/lib/git.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import os, subprocess, sys
21
from pathlib import Path
2+
import subprocess
33

44
def commit(files, msg, *args) : run('add', *files) ; run('commit', '-m', msg, *args)
55

66
def init_kudo_sync_bot(msgs):
7+
import os
78
print(f'\n{msgs.log_SWITCHING_TO_KUDO_SYNC_BOT}...\n')
89
with open(Path.home() / '.gitconfig.backup', 'w') as file: # back up git config
910
file.write(run('config', '--global', '--list'))
@@ -38,6 +39,7 @@ def restore_og_config(msgs):
3839
def run(*args):
3940
result = subprocess.run(['git'] + list(args), capture_output=True, text=True)
4041
if result.returncode != 0:
42+
import sys
4143
print(f"Git command failed: {' '.join(['git'] + list(args))}")
4244
print(result.stderr)
4345
sys.exit(1)

latin-locales/utils/lib/log.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from typing import Optional
44
if sys.platform == 'win32' : import colorama ; colorama.init() # enable ANSI color support
55

6-
from . import env
7-
86
try : terminal_width = os.get_terminal_size()[0]
97
except OSError : terminal_width = 80
108

@@ -33,6 +31,7 @@ def tip(msg: str, *args, **kwargs) -> None : print(f'\n{colors.bc}TIP: {msg.form
3331
def warn(msg: str, *args, **kwargs) -> None : print(f'\n{colors.bo}WARNING: {msg.format(*args, **kwargs)}{colors.nc}')
3432

3533
def debug(msg: str, cli: Optional[sn] = None, *args, **kwargs) -> None:
34+
from . import env
3635
if not env.is_debug_mode() : return
3736

3837
# Init --debug [target]

latin-locales/utils/lib/toml.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import tomli, tomli_w
2-
31
def read(file_path):
2+
import tomli
43
with open(file_path, 'rb') as file:
54
return tomli.load(file)
65

76
def write(file_path, data):
7+
import tomli_w
88
with open(file_path, 'wb') as file:
99
tomli_w.dump(data, file)

0 commit comments

Comments
 (0)