"""The Core (:mod:`harp`) package is the root namespace of the Harp framework.It mostly contains a reference to the :class:`Config` class, because it's the only object you need to start using Harpusing the python API (you don't *need* to use this API, configuration files should be enough for most use cases, butif you want to, this is the starting point).For convenience, the :func:`run` function is also available, which is a simple way to start the default serverimplementation for your configuration object.Example usage:.. code-block:: python from harp import Config, run config = Config() config.add_defaults() if __name__ == "__main__": run(config)You can find more information about how configuration works in the :mod:`harp.config` module.Contents--------"""importosfromsubprocessimportcheck_outputfromtypingimportTYPE_CHECKINGfrompackaging.versionimportInvalidVersion,VersionifTYPE_CHECKING:fromharp.configimportConfigurationBuilderas_ConfigurationBuilderROOT_DIR:str=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))#: Debug mode flag. When enabled, HARP will output additional debugging information that may not be safe for#: production environments (e.g., exception tracebacks in error responses). Set via HARP_DEBUG or DEBUG environment#: variables.DEBUG:bool=bool(os.environ.get("HARP_DEBUG")oros.environ.get("DEBUG"))defget_relative_path(path:str)->str:""" Returns the relative path of the given path from the root directory. :param path: The path to get the relative path of. :return: The relative path. """returnos.path.relpath(path,ROOT_DIR)def_parse_version(version:str,/,*,default=None)->Version:try:returnVersion(version)exceptInvalidVersion:if"-"inversion:return_parse_version(version.rsplit("-",1)[0],default=default)returndefault# Version Detection Strategy## HARP uses a three-layer version detection strategy:## Layer 1 (Development): Git-based version# - Only active in development mode (git repo exists and not in CI)# - Uses `git describe` to provide detailed version info (e.g., "0.9.0-3-gd1234ab-dirty")# - Helps developers identify exact commits during debugging# - Disabled in CI to prevent git-based versions in documentation builds## Layer 2 (Installed): Package metadata version# - Uses importlib.metadata to read version from installed package# - Ensures installed wheels report the correct version from pyproject.toml# - Works for both regular installs and editable installs## Layer 3 (Fallback): Unknown version# - Used when both git and package metadata are unavailable# - Indicates an unusual setup that should be investigated__title__="Core"__version__="unknown"# Fallback version__hardcoded_version__=__version____revision__=__version__# Will be set to git commit hash if available# Layer 1: Git-based version (development mode)ifnotos.environ.get("CI")andos.path.exists(os.path.join(ROOT_DIR,".git")):__revision__=check_output(["git","rev-parse","HEAD"],cwd=ROOT_DIR).decode("utf-8").strip()try:# Use git describe for detailed version info (e.g., "0.9.0-3-gd1234ab-dirty")__version__=(check_output(["git","describe","--tags","--always","--dirty"],cwd=ROOT_DIR).decode("utf-8").strip())exceptException:# Fallback to short commit hash if git describe fails__version__=__revision__[:7]else:# Layer 2: Installed package versiontry:importimportlib.metadata__version__=importlib.metadata.version("harp-proxy")exceptException:# Layer 3: Keep fallback version "unknown"pass__parsed_version__=_parse_version(__version__)from._loggingimportget_logger# noqa: E402asyncdefarun(builder:"_ConfigurationBuilder"):fromharp.config.adapters.hypercornimportHypercornAdaptersystem=awaitbuilder.abuild_system()server=HypercornAdapter(system)try:returnawaitserver.serve()finally:awaitsystem.dispose()
[docs]defrun(builder:"_ConfigurationBuilder"):""" Run the default server using provided configuration. :param builder: Config :return: """importasyncioreturnasyncio.run(arun(builder))