Add library for writing toml files

This commit is contained in:
Patrick Elmer 2024-03-10 21:43:02 +01:00
parent afb98a5fea
commit 60d428e388

View File

@ -7,6 +7,7 @@ try:
import tomllib import tomllib
except ModuleNotFoundError: except ModuleNotFoundError:
import tomli as tomllib import tomli as tomllib
import tomli_w
__version__ = '0.3.0' __version__ = '0.3.0'
@ -317,39 +318,77 @@ def load_pyproject_toml(dirname, maxdepth=3, filename='pyproject.toml'):
return {} return {}
def write_pyproject_toml(path): # def write_pyproject_toml(path):
# config = {
# 'Module name': '',
# 'Author': '',
# 'Author email': '',
# 'CLI command': '',
# 'CLI path': '',
# }
# for key in config.keys():
# config[key] = input(f"{key}: ")
# author_and_email = '{name = "' + config['Author'] + '", email = "' + config['Author email'] + '"}'
# with open(path, 'w') as f:
# f.write(f"""\
# [build-system]
# requires = ["flit_core >=3.2,<4"]
# build-backend = "flit_core.buildapi"
# [project]
# name = "{config['Module name']}"
# authors = [{author_and_email}]
# dynamic = ["version", "description"]
# dependencies = ["magicli"]
# [project.scripts]
# {config['CLI command']} = "{config['CLI path']}"
# [tool.magicli]
# test = "test"
# """)
def write_pyproject_toml(path):
if os.path.exists(path):
with open(path, "rb") as f:
config = tomli.load(f)
else:
config = { config = {
'Module name': '', "build-system": {
'Author': '', "requires": ["flit_core >=3.2,<4"],
'Author email': '', "build-backend": "flit_core.buildapi",
'CLI command': '', },
'CLI path': '', "project": {
"name": "",
"authors": [],
"dynamic": ["version", "description"],
"dependencies": ["magicli"],
},
} }
for key in config.keys(): if not 'name' in config['project'] or not config['project']['name']:
config[key] = input(f"{key}: ") config['project']['name'] = input('Module name: ')
author_and_email = '{name = "' + config['Author'] + '", email = "' + config['Author email'] + '"}' if not 'authors' in config['project'] or not config['project']['authors']:
author = input('Author: ')
email = input('Author email: ')
config['project']['authors'] = [{"author": author, "email": email}]
with open(path, 'w') as f: if not 'scripts' in config['project']:
f.write(f"""\ config['project']['scripts'] = {}
[build-system]
requires = ["flit_core >=3.2,<4"]
build-backend = "flit_core.buildapi"
[project] if not config['project']['scripts']:
name = "{config['Module name']}" cli_name = input('CLI name: ')
authors = [{author_and_email}] cli_path = input('CLI path: ')
dynamic = ["version", "description"] config['project']['scripts'][cli_name] = cli_path
dependencies = ["magicli"]
[project.scripts] with open(path, 'wb') as f:
{config['CLI command']} = "{config['CLI path']}" tomli_w.dump(config, f)
[tool.magicli]
test = "test"
""")
def first_calling_frame(): def first_calling_frame():