Import tensorflow

This commit is contained in:
2026-02-15 21:45:42 -08:00
parent f3e8b90764
commit c530630153
20524 changed files with 9017694 additions and 25 deletions
@@ -0,0 +1,3 @@
from namex.convert import convert_codebase
from namex.export import export
from namex.generate import generate_api_files
@@ -0,0 +1,72 @@
import os
import shutil
def rewrite_python_file_imports(target_dir, root_name, offset_name, verbose=False):
"""Rewrite internal package imports for move to a `src/` dir structure.
If your project has its code in `package_name/`, and you want to instead
move that code to `package_name/src/`, this script will change all lines
of the form e.g.
`from package_name.x.y import z` to `from package_name.src.x.y import z`
if you call it as
`rewrite_python_file_imports("package_name/", "package_name", "src")`
"""
assert "." not in offset_name
for root, _, files in os.walk(target_dir):
for fname in files:
if fname.endswith(".py"):
fpath = os.path.join(root, fname)
if verbose:
print(f"...processing {fpath}")
with open(fpath) as f:
contents = f.read()
lines = contents.split("\n")
in_string = False
new_lines = []
for line in lines:
if line.strip().startswith('"""') or line.strip().endswith('"""'):
if line.count('"') % 2 == 1:
in_string = not in_string
else:
if not in_string:
# Imports starting from `root_name`.
if line.strip() == f"import {root_name}":
line = line.replace(
f"import {root_name}",
f"import {root_name}.{offset_name} as {root_name}",
)
else:
line = line.replace(
f"import {root_name}.",
f"import {root_name}.{offset_name}.",
)
line = line.replace(
f"from {root_name}.", f"from {root_name}.{offset_name}."
)
line = line.replace(
f"from {root_name} import",
f"from {root_name}.{offset_name} import",
)
new_lines.append(line)
with open(fpath, "w") as f:
f.write("\n".join(new_lines) + "\n")
def convert_codebase(package, code_directory="src"):
if not os.path.exists(package):
raise ValueError(f"No directory named '{package}'.")
os.rename(package, code_directory)
os.mkdir(package)
shutil.move(code_directory, os.path.join(package, code_directory))
rewrite_python_file_imports(
target_dir=package, root_name=package, offset_name="src", verbose=True
)
# Create blank init file at root to make package detectable / importable.
with open(os.path.join(package, "__init__.py"), "w"):
pass
@@ -0,0 +1,68 @@
class export:
"""Decorator to export a public API in a given package.
Example usage:
```python
@export(package="keras_tuner", path="keras_tuner.applications.HyperResNet")
class HyperResNet:
...
```
You can also pass a list of paths as `path`, to make
the same symbol visible under various aliases:
```python
@export(
package="keras_tuner",
path=[
"keras_tuner.applications.HyperResNet",
"keras_tuner.applications.resnet.HyperResNet",
])
class HyperResNet:
...
```
**Note:** All export packages must start with the package name.
Yes, that is redundant, but that is a helpful sanity check.
The expectation is that each package will customize
`export_api` to provide a default value for `package`,
which will serve to validate all `path` values
and avoid users inadvertendly ending up with non-exported
symbols due to a bad path (e.g. `path="applications.HyperResNet"`
instead of `path="keras_tuner.applications.HyperResNet"`).
"""
def __init__(self, package, path):
if isinstance(path, str):
export_paths = [path]
elif isinstance(path, list):
export_paths = path
else:
raise ValueError(
f"Invalid type for `path` argument: "
f"Received '{path}' "
f"of type {type(path)}"
)
for p in export_paths:
if not p.startswith(package + "."):
raise ValueError(
f"All `export_path` values should start with '{package}.'. "
f"Received: path={path}"
)
self.package = package
self.path = path
def __call__(self, symbol):
if hasattr(symbol, "_api_export_path") and symbol._api_export_symbol_id == id(
symbol
):
raise ValueError(
f"Symbol {symbol} is already exported as '{symbol._api_export_path}'. "
f"Cannot also export it to '{self.path}'."
)
symbol._api_export_path = self.path
symbol._api_export_symbol_id = id(symbol)
return symbol
@@ -0,0 +1,167 @@
import os
import sys
import importlib
INIT_FILE_HEADER = '''"""DO NOT EDIT.
This file was autogenerated. Do not edit it by hand,
since your modifications would be overwritten.
"""
'''
def generate_api_files(
package,
code_directory="src",
verbose=False,
target_directory=None,
exclude_directories=()
):
"""Writes out API export `__init__.py` files.
Given a codebase structured as such:
```
package/
...src/
......__init__.py
......(Python files that use e.g. `@export_api(package="package", export_path="package.x.y.Z")`)
```
this script generates `__init__.py` files within `package/`
to export the public API described by the `@api_export` calls.
Important notes:
* Any existing `__init__.py` files in `package/` but outside of
`package/code_directory/` may be overwritten.
* This script must be run in an environment that includes
all dependencies used by `package`. Make sure to install
them before running the script.
"""
if verbose:
print(
f"Generating files for package '{package}' "
f"from sources found in '{package}/{code_directory}'."
)
if not os.path.exists(package):
raise ValueError(f"No directory named '{package}'.")
if not os.path.exists(os.path.join(package, code_directory)):
raise ValueError(f"No directory named '{package}/{code_directory}'.")
exclude_directories = [os.path.join(package, d) for d in exclude_directories]
# Make list of all Python files (modules) to visit.
codebase_walk_entry_points = []
for root, dirs, files in os.walk(os.path.join(package, code_directory)):
if root in exclude_directories:
dirs.clear()
continue
for fname in files:
if fname == "__init__.py":
codebase_walk_entry_points.append(".".join(root.split("/")))
elif fname.endswith(".py") and not fname.endswith("_test.py"):
module_name = fname[:-3]
codebase_walk_entry_points.append(
".".join(root.split("/")) + "." + module_name
)
# Import all Python modules found in the code directory.
sys.path.insert(0, os.getcwd())
modules = []
for entry_point in codebase_walk_entry_points:
mod = importlib.import_module(entry_point, package=".")
modules.append(mod)
if verbose:
print("Compiling list of symbols to export.")
# Populate list of all symbols to register.
all_symbols = set()
for module in modules:
for name in dir(module):
symbol = getattr(module, name)
if not hasattr(symbol, "_api_export_path"):
continue
if symbol._api_export_symbol_id != id(symbol):
# This symbol is a non-exported subclass
# of an exported symbol.
continue
if not all(
[
path.startswith(package + ".")
for path in to_list(symbol._api_export_path)
]
):
continue
all_symbols.add(symbol)
# Generate __init__ files content.
init_files_content = {}
for symbol in all_symbols:
if verbose:
print(f"...processing symbol '{symbol.__name__}'")
for export_path in to_list(symbol._api_export_path):
export_modules = export_path.split(".")
if export_modules[0] == package and target_directory is not None:
export_modules = [export_modules[0], target_directory] + export_modules[1:]
export_name = export_modules[-1]
parent_path = os.path.join(*export_modules[:-1])
if parent_path not in init_files_content:
init_files_content[parent_path] = []
init_files_content[parent_path].append(
{"symbol": symbol, "export_name": export_name}
)
for i in range(1, len(export_modules[:-1])):
intermediate_path = os.path.join(*export_modules[:i])
if intermediate_path not in init_files_content:
init_files_content[intermediate_path] = []
init_files_content[intermediate_path].append(
{
"module": export_modules[i],
"location": ".".join(export_modules[:i]),
}
)
if verbose:
print("Writing out API files.")
# Go over init_files_content, make dirs,
# create __init__.py file, populate file with public symbol imports.
for path, contents in init_files_content.items():
os.makedirs(path, exist_ok=True)
init_file_lines = []
modules_included = set()
for symbol_metadata in contents:
if "symbol" in symbol_metadata:
symbol = symbol_metadata["symbol"]
name = symbol_metadata["export_name"]
init_file_lines.append(
f"from {symbol.__module__} import {symbol.__name__} as {name}"
)
elif "module" in symbol_metadata:
if symbol_metadata["module"] not in modules_included:
module = symbol_metadata["module"]
init_file_lines.append(
f"from {'.'.join(path.split('/'))} import {module} as {module}"
)
modules_included.add(symbol_metadata["module"])
init_path = os.path.join(path, "__init__.py")
if verbose:
print(f"...writing {init_path}")
init_file_lines = sorted(init_file_lines)
with open(init_path, "w") as f:
contents = INIT_FILE_HEADER + "\n".join(init_file_lines) + "\n"
f.write(contents)
def to_list(x):
if isinstance(x, (list, tuple)):
return list(x)
elif isinstance(x, str):
return [x]
return []