Skip to content

Commit a46b959

Browse files
authored
fix(python): template cleanup and playgrounds (#2286)
1 parent 9b77943 commit a46b959

39 files changed

+1015
-565
lines changed

.github/actions/restore-artifacts/action.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ inputs:
2727
dart:
2828
description: Whether this job ran or not
2929
required: false
30+
python:
31+
description: Whether this job ran or not
32+
required: false
3033

3134
runs:
3235
using: composite

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,6 @@ generators/bin
3737
.dart_tool/
3838
.packages
3939
pubspec.lock
40+
41+
# Python
42+
**/__pycache__

clients/algoliasearch-client-python/algoliasearch/.gitignore

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
[metadata]
2+
name = algoliasearch
3+
version = attr: algoliasearch.__version__
4+
url = https://github.com/algolia/algoliasearch-client-python
5+
project_urls =
6+
CI: GitHub Actions = https://github.com/algolia/api-clients-automation/actions
7+
Docs = https://algolia.com/docs
8+
GitHub: issues = https://github.com/algolia/algoliasearch-client-python/issues
9+
GitHub: repo = https://github.com/algolia/algoliasearch-client-python/issues
10+
description = ⚡️ A fully-featured and blazing-fast Python API client to interact with Algolia.
11+
long_description = file: README.md
12+
long_description_content_type = textmarkdown
13+
maintainer = support team <[email protected]>
14+
maintainer_email = [email protected]
15+
license = MIT
16+
license_files = LICENSE
17+
keywords = algolia, py, search, backend, hosted, cloud, full-text search, faceted search
18+
platform = any
19+
classifiers =
20+
Intended Audience :: Developers
21+
License :: OSI Approved :: MIT License
22+
Operating System :: OS Independent
23+
Programming Language :: Python
24+
Programming Language :: Python :: 3.8
25+
Programming Language :: Python :: 3.9
26+
Programming Language :: Python :: 3.10
27+
Programming Language :: Python :: 3.11
28+
Programming Language :: Python :: 3.12
29+
Topic :: Internet
30+
Topic :: Internet :: WWW/HTTP
31+
Topic :: Internet :: WWW/HTTP :: Indexing/Search
32+
Development Status :: 5 - Production/Stable
33+
34+
[options]
35+
zip_safe = True
36+
include_package_data = True
37+
packages = find:
38+
install_requires =
39+
urllib3>=1.25.3,<2.0
40+
aiohttp>=3.9.0,<4.0
41+
python-dateutil>=2.8.2,<3.0
42+
pydantic>=2,3.0
43+
typing-extensions>=4.7.1,<5.0
44+
python_requires = >= 3.8
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import sys
2+
3+
if sys.version_info < (3, 8):
4+
raise RuntimeError("algoliasearch 4.x requires Python 3.8+")

config/.python-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.12.0
1+
3.11.6

config/generation.config.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,8 @@ export const patterns = [
8080
// Python
8181
'clients/algoliasearch-client-python/**',
8282
'!clients/algoliasearch-client-python/*',
83-
'!clients/algoliasearch-client-python/algoliasearch/*/__init__.py',
83+
'clients/algoliasearch-client-python/pyproject.toml',
84+
'clients/algoliasearch-client-python/poetry.lock',
85+
'clients/algoliasearch-client-python/requirements.txt',
86+
'clients/algoliasearch-client-python/.gitignore',
8487
];

generators/src/main/java/com/algolia/codegen/AlgoliaPythonGenerator.java

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,31 @@
55
import org.openapitools.codegen.*;
66
import org.openapitools.codegen.CodegenConstants;
77
import org.openapitools.codegen.languages.PythonClientCodegen;
8+
import org.openapitools.codegen.model.ModelMap;
9+
import org.openapitools.codegen.model.OperationsMap;
810

911
public class AlgoliaPythonGenerator extends PythonClientCodegen {
1012

13+
private String CLIENT;
14+
1115
@Override
1216
public String getName() {
1317
return "algolia-python";
1418
}
1519

1620
@Override
1721
public void processOpts() {
18-
String client = (String) additionalProperties.get("client");
22+
CLIENT = (String) additionalProperties.get("client");
1923

20-
additionalProperties.put("isSearchClient", client.equals("search"));
24+
additionalProperties.put("isSearchClient", CLIENT.equals("search"));
2125
additionalProperties.put("packageVersion", Utils.getClientConfigField("python", "packageVersion"));
2226
additionalProperties.put(CodegenConstants.EXCLUDE_TESTS, true);
2327

2428
setApiNameSuffix(Utils.API_SUFFIX);
2529

26-
setPackageName(Utils.toSnakeCase(client));
27-
setApiPackage("api");
30+
String packageName = Utils.toSnakeCase(CLIENT);
31+
setPackageName(packageName);
32+
setApiPackage("");
2833
setModelPackage("models");
2934

3035
super.processOpts();
@@ -48,23 +53,57 @@ public void processOpts() {
4853
file.getTemplateFile().equals("setup.mustache") ||
4954
file.getTemplateFile().equals("pyproject.mustache") ||
5055
file.getTemplateFile().equals("py.typed.mustache") ||
51-
file.getTemplateFile().equals("rest.mustache") ||
5256
file.getTemplateFile().equals("README.mustache") ||
5357
file.getTemplateFile().equals("api_test.mustache") ||
5458
file.getTemplateFile().equals("model_test.mustache") ||
5559
file.getTemplateFile().equals("github-workflow.mustache") ||
5660
file.getTemplateFile().equals("travis.mustache") ||
61+
file.getTemplateFile().equals("__init__api.mustache") ||
5762
file.getTemplateFile().equals("gitlab-ci.mustache")
5863
);
5964

6065
// repository
6166
supportingFiles.add(new SupportingFile("pyproject.mustache", "../", "pyproject.toml"));
67+
supportingFiles.add(new SupportingFile("gitignore.mustache", "../", ".gitignore"));
68+
supportingFiles.add(new SupportingFile("__init__.mustache", "", "__init__.py"));
6269

6370
try {
64-
Utils.generateServer(client, additionalProperties);
71+
Utils.generateServer(CLIENT, additionalProperties);
6572
} catch (GeneratorException e) {
6673
e.printStackTrace();
6774
System.exit(1);
6875
}
6976
}
77+
78+
@Override
79+
public String toApiFilename(String name) {
80+
return "client";
81+
}
82+
83+
@Override
84+
public String toApiImport(String name) {
85+
return super.toApiImport(name).replace("from ", "from algoliasearch.");
86+
}
87+
88+
@Override
89+
public String toModelImport(String name) {
90+
return super.toModelImport(name).replace("from ", "from algoliasearch.");
91+
}
92+
93+
// we need to override imports of all operations because somehow the methods `toModelImport` and
94+
// `toApiImport` does not override the imports used in the API client file (client.py).
95+
@Override
96+
public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<ModelMap> models) {
97+
OperationsMap operations = super.postProcessOperationsWithModels(objs, models);
98+
99+
List<Map<String, String>> imports = operations.getImports();
100+
101+
for (Map<String, String> imp : imports) {
102+
imp.put("import", imp.get("import").replace("from " + CLIENT + ".", "from algoliasearch." + CLIENT + "."));
103+
}
104+
105+
operations.setImports(imports);
106+
107+
return operations;
108+
}
70109
}

playground/python/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The python playground

playground/python/app/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)