Skip to content

Commit 1fa436a

Browse files
committed
Abstracted init.py
1 parent 33cf4ee commit 1fa436a

4 files changed

Lines changed: 40 additions & 30 deletions

File tree

remove-json-keys/lib/init.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from types import SimpleNamespace as sns
2+
3+
def cli():
4+
return sns(
5+
name='remove-json-keys',
6+
urls=sns(jsdelivr='https://cdn.jsdelivr.net/gh/adamlui/python-utils')
7+
)

remove-json-keys/remove-json-keys.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
Name: remove-json-keys.py
3-
Version: 2026.2.10.13
3+
Version: 2026.2.10.14
44
Author: Adam Lui
55
Description: Remove key/value pairs from json_dir/**.json
66
Homepage: https://github.com/adamlui/python-utils
@@ -10,12 +10,9 @@
1010
'''
1111

1212
import argparse, os, re
13-
from types import SimpleNamespace as sns
13+
import lib.init as init
1414

15-
cli = sns(
16-
name='remove-json-keys',
17-
urls=sns(jsdelivr='https://cdn.jsdelivr.net/gh/adamlui/python-utils')
18-
)
15+
cli = init.cli()
1916

2017
# Parse CLI args
2118
parser = argparse.ArgumentParser(description='Remove key/value pairs from JSON files')

translate-messages/lib/init.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import json, os
2+
from types import SimpleNamespace as sns
3+
4+
def cli():
5+
return sns(
6+
name='translate-messages',
7+
urls=sns(jsdelivr='https://cdn.jsdelivr.net/gh/adamlui/python-utils'),
8+
default_target_locales=[
9+
'af', 'am', 'ar', 'az', 'be', 'bem', 'bg', 'bn', 'bo', 'bs', 'ca', 'ceb', 'cs', 'cy', 'da', 'de', 'dv', 'dz',
10+
'el', 'en', 'en-GB', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'gd', 'gl', 'gu', 'haw', 'he', 'hi', 'hr',
11+
'ht', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'ka', 'kab', 'kk', 'km', 'kn', 'ko', 'ku', 'ky', 'la', 'lb', 'lo',
12+
'lt', 'lv', 'mg', 'mi', 'mk', 'ml', 'mn', 'ms', 'mt', 'my', 'ne', 'nl', 'no', 'ny', 'pa', 'pap', 'pl', 'ps',
13+
'pt', 'ro', 'ru', 'rw', 'sg', 'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sr', 'sv', 'sw', 'ta', 'te', 'tg', 'th',
14+
'ti', 'tk', 'tn', 'tpi', 'tr', 'uk', 'ur', 'uz', 'vi', 'xh', 'yi', 'zh', 'zh-CN', 'zh-HK', 'zh-SG', 'zh-TW',
15+
'zu'
16+
]
17+
)
18+
19+
def configFile(cli, caller_file):
20+
cli.script_name = os.path.splitext(os.path.basename(caller_file))[0]
21+
cli.config_filename = f'{cli.script_name}.config.json'
22+
cli.config_path = os.path.join(os.path.dirname(caller_file), cli.config_filename)
23+
cli.config_data = {}
24+
if os.path.exists(cli.config_path):
25+
with open(cli.config_path, 'r', encoding='utf-8') as f:
26+
cli.config_data.update(json.load(f))
27+

translate-messages/translate-en-messages.py

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'''
22
Name: translate-en-messages.py
3-
Version: 2026.2.10.25
3+
Version: 2026.2.10.26
44
Author: Adam Lui
55
Description: Translate en/messages.json to other locales
66
Homepage: https://github.com/adamlui/python-utils
@@ -10,33 +10,12 @@
1010
'''
1111

1212
import argparse, os, json
13-
from types import SimpleNamespace as sns
13+
import lib.init as init
1414
from sys import stdout
1515
from translate import Translator
1616
from urllib.request import urlopen
1717

18-
cli = sns(
19-
name='translate-messages',
20-
urls=sns(jsdelivr='https://cdn.jsdelivr.net/gh/adamlui/python-utils'),
21-
default_target_locales=[
22-
'af', 'am', 'ar', 'az', 'be', 'bem', 'bg', 'bn', 'bo', 'bs', 'ca', 'ceb', 'cs', 'cy', 'da', 'de', 'dv', 'dz',
23-
'el', 'en', 'en-GB', 'eo', 'es', 'et', 'eu', 'fa', 'fi', 'fo', 'fr', 'gd', 'gl', 'gu', 'haw', 'he', 'hi', 'hr',
24-
'ht', 'hu', 'hy', 'id', 'is', 'it', 'ja', 'ka', 'kab', 'kk', 'km', 'kn', 'ko', 'ku', 'ky', 'la', 'lb', 'lo',
25-
'lt', 'lv', 'mg', 'mi', 'mk', 'ml', 'mn', 'ms', 'mt', 'my', 'ne', 'nl', 'no', 'ny', 'pa', 'pap', 'pl', 'ps',
26-
'pt', 'ro', 'ru', 'rw', 'sg', 'si', 'sk', 'sl', 'sm', 'sn', 'so', 'sr', 'sv', 'sw', 'ta', 'te', 'tg', 'th',
27-
'ti', 'tk', 'tn', 'tpi', 'tr', 'uk', 'ur', 'uz', 'vi', 'xh', 'yi', 'zh', 'zh-CN', 'zh-HK', 'zh-SG', 'zh-TW',
28-
'zu'
29-
]
30-
)
31-
32-
# Init/load config file
33-
cli.script_name = os.path.splitext(os.path.basename(__file__))[0]
34-
cli.config_filename = f'{cli.script_name}.config.json'
35-
cli.config_path = os.path.join(os.path.dirname(__file__), cli.config_filename)
36-
cli.config_data = {}
37-
if os.path.exists(cli.config_path):
38-
with open(cli.config_path, 'r', encoding='utf-8') as file_config:
39-
cli.config_data.update(json.load(file_config))
18+
cli = init.cli() ; init.configFile(cli, __file__)
4019

4120
# Parse CLI args
4221
parser = argparse.ArgumentParser(description='Translate en/messages.json to other locales')

0 commit comments

Comments
 (0)