Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/SDK/Language/Python.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ public function getFiles(): array
'destination' => '{{ spec.title | caseSnake}}/__init__.py',
'template' => 'python/package/__init__.py.twig',
],
[
'scope' => 'default',
'destination' => '{{ spec.title | caseSnake}}/utils/deprecated.py',
'template' => 'python/package/utils/deprecated.py.twig',
],
[
'scope' => 'default',
'destination' => '{{ spec.title | caseSnake}}/utils/__init__.py',
'template' => 'python/package/utils/__init__.py.twig',
],
[
'scope' => 'default',
'destination' => '{{ spec.title | caseSnake}}/client.py',
Expand Down
8 changes: 8 additions & 0 deletions templates/python/package/services/service.py.twig
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ..service import Service
from typing import List, Dict, Any
from ..exception import AppwriteException
from appwrite.utils.deprecated import deprecated
{% set added = [] %}
{% for method in service.methods %}
{% for parameter in method.parameters.all %}
Expand Down Expand Up @@ -39,6 +40,13 @@ class {{ service.name | caseUcfirst }}(Service):
{% endif %}
{% if not shouldSkip %}

{% if method.deprecated %}
{% if method.since and method.replaceWith %}
@deprecated("This API has been deprecated since {{ method.since }}. Please use `{{ method.replaceWith | caseSnakeExceptFirstDot }}` instead.")
{% else %}
@deprecated("This API has been deprecated.")
{% endif %}
{% endif %}
def {{ method.name | caseSnake }}(self{% if method.parameters.all|length > 0 %}, {% endif %}{% for parameter in method.parameters.all %}{{ parameter.name | escapeKeyword | caseSnake }}: {{ parameter | getPropertyType(method) | raw }}{% if not parameter.required %} = None{% endif %}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, on_progress = None{% endif %}) -> {% if method.type == 'webAuth' %}str{% elseif method.type == 'location' %}bytes{% else %}Dict[str, Any]{% endif %}:
"""
{% autoescape false %}{{ method.description | replace({"\n": "\n "}) }}{% endautoescape %}
Expand Down
1 change: 1 addition & 0 deletions templates/python/package/utils/__init__.py.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# This file makes the 'utils' directory a Python package.
51 changes: 51 additions & 0 deletions templates/python/package/utils/deprecated.py.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""
A decorator to mark functions as deprecated.

When the function is called, a DeprecationWarning is emitted.
Compatible with Python 2.7+ and Python 3.x.
"""

import functools
import warnings

def deprecated(reason=None):
"""
Decorator to mark functions as deprecated.
Emits a DeprecationWarning when the function is called.

Args:
reason (str, optional): Reason for deprecation. Defaults to None.

Usage:
@deprecated("Use another_function instead.")
def old_function(...):
...
"""
def decorator(func):
message = "Call to deprecated function '{}'.{}".format(
func.__name__,
" " + reason if reason else ""
)

@functools.wraps(func)
def wrapped(*args, **kwargs):
warnings.simplefilter('always', DeprecationWarning) # show warning every time
try:
warnings.warn(
message,
category=DeprecationWarning,
stacklevel=2
)
return func(*args, **kwargs)
finally:
warnings.simplefilter('default', DeprecationWarning) # reset filter
return wrapped

# Support both @deprecated and @deprecated("reason")
if callable(reason):
# Used as @deprecated without arguments
func = reason
reason = None
return decorator(func)
else:
return decorator
9 changes: 2 additions & 7 deletions templates/python/setup.py.twig
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ long_description: str

with open("README.md", "r", encoding="utf-8") as readme_file_desc:
long_description = readme_file_desc.read()

setuptools.setup(
name = '{{spec.title | caseSnake}}',
packages = [
'{{spec.title | caseSnake}}',
'{{spec.title | caseSnake}}/services',
'{{spec.title | caseSnake}}/encoders',
'{{spec.title | caseSnake}}/enums',
],
packages = setuptools.find_packages(),
version = '{{sdk.version}}',
license='{{spec.licenseName}}',
description = '{{sdk.shortDescription}}',
Expand Down
Loading