-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenit.py
36 lines (27 loc) · 985 Bytes
/
genit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def generate_init_file(package_name, modules, ppath = ""):
content = f"""# Auto-generated __init__.py for {package_name} package
# Import necessary modules
{chr(10).join(f"from .{module} import {module}" for module in modules)}
# Public API of the package
__all__ = {str(modules)}
# Version checking
from importlib.metadata import version, PackageNotFoundError
try:
__version__ = version("{package_name}")
except PackageNotFoundError:
# package is not installed
__version__ = "unknown"
# You can add any initialization code here if needed
"""
if not ppath:
ppath = "src/" + package_name + "/"
with open(ppath, 'w') as f:
f.write(content)
# Usage
import os
from pathlib import Path
package_name = "pyfunc_config"
directory = "src/" + package_name + "/"
py_files = [f for f in os.listdir(directory) if f.endswith('.py') and f != '__init__.py']
modules = [os.path.splitext(f)[0] for f in py_files]
generate_init_file(package_name, modules)