{"id":18456290,"url":"https://github.com/martmists-gh/kpy-plugin","last_synced_at":"2025-04-08T04:34:41.763Z","repository":{"id":37237017,"uuid":"483431384","full_name":"Martmists-GH/kpy-plugin","owner":"Martmists-GH","description":"Gradle/KSP plugin to compile Kotlin/Native to Python C API","archived":false,"fork":false,"pushed_at":"2024-06-02T01:12:28.000Z","size":335,"stargazers_count":31,"open_issues_count":1,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-23T06:31:39.580Z","etag":null,"topics":["gradle-plugin","kotlin","kotlin-multiplatform","kotlin-multiplatform-library","kotlin-native","python-3","python-extension","python3"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-4-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Martmists-GH.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-04-19T22:42:53.000Z","updated_at":"2025-01-12T13:53:10.000Z","dependencies_parsed_at":"2024-11-06T08:11:20.566Z","dependency_job_id":"10b2e39f-a1e4-499f-bb18-50644196c9af","html_url":"https://github.com/Martmists-GH/kpy-plugin","commit_stats":null,"previous_names":[],"tags_count":41,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Martmists-GH%2Fkpy-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Martmists-GH%2Fkpy-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Martmists-GH%2Fkpy-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Martmists-GH%2Fkpy-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Martmists-GH","download_url":"https://codeload.github.com/Martmists-GH/kpy-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247779796,"owners_count":20994569,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gradle-plugin","kotlin","kotlin-multiplatform","kotlin-multiplatform-library","kotlin-native","python-3","python-extension","python3"],"created_at":"2024-11-06T08:11:04.720Z","updated_at":"2025-04-08T04:34:41.470Z","avatar_url":"https://github.com/Martmists-GH.png","language":"Kotlin","readme":"# KPy Plugin\n\nThe KPy gradle plugin allows you to write Kotlin/Native code and use it from python.\n\n\u003e Note: Modules built with KPy still require XCode when building on macOS, this is a Kotlin/Native limitation.\n\nA huge thank you to the [indygreg/python-build-standalone](https://github.com/indygreg/python-build-standalone/) project for providing prebuilt python binaries to build against. This project would be impossible to maintain without it.\n\n## Features\n\n### Implemented\n\n- Export Kotlin/Native functions and classes without having to touch the Python API directly\n- Convert between Kotlin and Python types with .toPython() and .toKotlin()\n- Conversions handled mostly automatically\n- Class inheritance mapped to python\n- Generate Python stubs\n- Catch Kotlin exceptions and raise them as Python exceptions\n\n## Setup\n\nChange your gradle version to 7.5 (nightly builds only as of writing)\nEnable the plugin in your build.gradle.kts file:\n\n```kotlin\nplugins {\n    kotlin(\"multiplatform\") version \"2.0.0\"\n    id(\"com.martmists.kpy.kpy-plugin\") version \"1.0.1\"\n}\n\nkotlin {\n    val hostOs = System.getProperty(\"os.name\")\n    val isMingwX64 = hostOs.startsWith(\"Windows\")\n    val isArm64 = System.getProperty(\"os.arch\") == \"aarch64\"\n    // You can rename the target from `native` to something else, \n    // but make sure to also change setup.py to match this change!\n    val nativeTarget = when {\n        hostOs == \"Mac OS X\" \u0026\u0026 !isArm64 -\u003e macosX64(\"native\")\n        hostOs == \"Linux\" \u0026\u0026 !isArm64 -\u003e linuxX64(\"native\")\n        hostOs == \"Mac OS X\" \u0026\u0026 isArm64 -\u003e macosArm64(\"native\")\n        hostOs == \"Linux\" \u0026\u0026 isArm64 -\u003e linuxArm64(\"native\")\n        isMingwX64 -\u003e mingwX64(\"native\")\n        else -\u003e throw GradleException(\"Host OS is not supported in Kotlin/Native.\")\n    }\n}\n```\n\nUse the following setup.py template (note: may be outdated, see kpy-sample for an up-to-date example):\n\n```python\nfrom os.path import dirname, abspath\nfrom platform import system\nfrom setuptools import setup, Extension, find_packages\nfrom subprocess import Popen, PIPE\n\nosname = system()\ndebug = False  # Debug currently has some issues\ndir_name = dirname(abspath(__file__))\n\nif osname == \"Linux\" or osname == \"Darwin\":\n    gradle_bin = \"./gradlew\"\nelse:\n    gradle_bin = \".\\\\gradlew.bat\"\n\n# Build the project\nproc = Popen([gradle_bin, \"build\"])\nif proc.wait() != 0:\n    raise Exception(\"Build failed\")\n\n# Fetch configuration from gradle task\nproc = Popen([gradle_bin, \"setupMetadata\"], stdout=PIPE)\nif proc.wait() != 0:\n    raise Exception(\"Failed to fetch metadata\")\noutput = proc.stdout.read().decode()\nreal_output = output.split(\"===METADATA START===\")[1].split(\"===METADATA END===\")[0]\n\nexec(real_output, globals(), locals())\n# Types of variables from gradle metadata\nhas_stubs: bool\nproject_name: str\nmodule_name: str\nproject_version: str\nbuild_dir: str\nroot_dir: str\ntarget: str\n\nprint(\"name: \" + project_name)\nprint(\"version: \" + project_version)\n\n\ndef snake_case(name):\n    return name.replace(\"-\", \"_\").lower()\n\n\ndef extensions():\n    folder = \"debugStatic\" if debug else \"releaseStatic\"\n    prefix = \"_\" if has_stubs else \"\"\n    native = Extension(prefix + module_name,\n                       sources=[f'{build_dir}/generated/ksp/{target}/{target}Main/resources/entrypoint.cpp'],\n                       include_dirs=[f\"{build_dir}/bin/{target}/{folder}/\"],\n                       library_dirs=[f\"{build_dir}/bin/{target}/{folder}/\"],\n                       libraries=[project_name])\n\n    return [native]\n\n\nwith open(\"README.md\", \"r\") as fh:\n    long_description = fh.read()\n\n\nattrs = {}\n\nif has_stubs:\n    stub_root = f'{build_dir}/generated/ksp/{target}/{target}Main/resources'\n    attrs[\"packages\"] = find_packages(where=stub_root)\n    attrs[\"package_dir\"] = {\"\": stub_root}\nelse:\n    attrs[\"packages\"] = []\n\nsetup(\n    name=module_name,\n    version=project_version,\n    description=long_description,\n    ext_modules=extensions(),\n    **attrs\n)\n\n```\n\n## Configuration\n\nTo configure the plugin, you can use the `kpy` configuration.\n\n```kotlin\nkpy {\n    // Pass properties to setup.py, the exec() command will pass them to the context\n    // Note: the second parameter is an expression, and must be valid python.\n    metadata(\"my_key\", \"'my' + 'value'\")  // in setup.py you can now use my_key and it evaluates to 'myvalue'\n\n    // Specify the python version to build against.\n    // Currently supported: [3.9, 3.10]\n    pyVersion.set(PythonVersion.Py310)\n\n    // Generate python stubs for the native sources\n    // These are stored to `build/generated/ksp/\u003ctarget\u003e/\u003ctarget\u003eMain/resources/`\n    // Note: these will be overwritten every time you build the project\n    generateStubs.set(true)\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartmists-gh%2Fkpy-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmartmists-gh%2Fkpy-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartmists-gh%2Fkpy-plugin/lists"}