Skip to content

Commit c68ce0b

Browse files
committed
add protected_dirs to config
1 parent 82a9ad6 commit c68ce0b

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

openapi_python_client/__init__.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" Generate modern Python clients from OpenAPI """
22

3+
import os
34
import json
45
import mimetypes
56
import shutil
@@ -127,7 +128,21 @@ def update(self) -> Sequence[GeneratorError]:
127128
if not self.package_dir.is_dir():
128129
return [GeneratorError(detail=f"Directory {self.package_dir} not found")]
129130
print(f"Updating {self.package_name}")
130-
shutil.rmtree(self.package_dir)
131+
132+
if not self.config.protected_dirs:
133+
shutil.rmtree(self.package_dir)
134+
else:
135+
files_and_dirs: List[str] = os.listdir(self.package_dir)
136+
for protected_dir in self.config.protected_dirs:
137+
files_and_dirs.remove(protected_dir)
138+
139+
for file_or_dir in files_and_dirs:
140+
path = os.path.join(self.package_dir, file_or_dir)
141+
if os.path.isfile(path):
142+
os.remove(path)
143+
if os.path.isdir(path):
144+
shutil.rmtree(path)
145+
131146
self._create_package()
132147
self._build_models()
133148
self._build_api()
@@ -170,7 +185,7 @@ def _get_errors(self) -> List[GeneratorError]:
170185
return errors
171186

172187
def _create_package(self) -> None:
173-
self.package_dir.mkdir()
188+
self.package_dir.mkdir(exist_ok=True)
174189
# Package __init__.py
175190
package_init = self.package_dir / "__init__.py"
176191

openapi_python_client/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import codecs
22
import pathlib
33
from pprint import pformat
4-
from typing import Optional, Sequence
4+
from typing import Optional, List, Sequence
55

66
import typer
77

@@ -159,7 +159,7 @@ def update(
159159
meta: MetaType = _meta_option,
160160
file_encoding: str = typer.Option("utf-8", help="Encoding used when writing generated"),
161161
config_path: Optional[pathlib.Path] = CONFIG_OPTION,
162-
fail_on_warning: bool = False,
162+
fail_on_warning: bool = False
163163
) -> None:
164164
"""Update an existing OpenAPI Client library
165165
@@ -188,6 +188,6 @@ def update(
188188
meta=meta,
189189
custom_template_path=custom_template_path,
190190
file_encoding=file_encoding,
191-
config=config,
191+
config=config
192192
)
193193
handle_errors(errors, fail_on_warning)

openapi_python_client/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Config(BaseModel):
2727
project_name_override: Optional[str]
2828
package_name_override: Optional[str]
2929
package_version_override: Optional[str]
30+
protected_dirs: Optional[List[str]]
3031
post_hooks: List[str] = [
3132
"autoflake -i -r --remove-all-unused-imports --remove-unused-variables --ignore-init-module-imports .",
3233
"isort .",

0 commit comments

Comments
 (0)