Skip to content

add protected_dirs to config #641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,15 @@ post_hooks:
- "black ."
```

### protected_dirs

Specify directories to preserve when updating the client.

```yaml
protected_dirs:
- "util"
- "cli"
```

[changelog.md]: CHANGELOG.md
[poetry]: https://python-poetry.org/
19 changes: 17 additions & 2 deletions openapi_python_client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import mimetypes
import os
import shutil
import subprocess
import sys
Expand Down Expand Up @@ -127,7 +128,21 @@ def update(self) -> Sequence[GeneratorError]:
if not self.package_dir.is_dir():
return [GeneratorError(detail=f"Directory {self.package_dir} not found")]
print(f"Updating {self.package_name}")
shutil.rmtree(self.package_dir)

if not self.config.protected_dirs:
shutil.rmtree(self.package_dir)
else:
files_and_dirs: List[str] = os.listdir(self.package_dir)
for protected_dir in self.config.protected_dirs:
files_and_dirs.remove(protected_dir)

for file_or_dir in files_and_dirs:
path = os.path.join(self.package_dir, file_or_dir)
if os.path.isfile(path):
os.remove(path)
if os.path.isdir(path):
shutil.rmtree(path)

self._create_package()
self._build_models()
self._build_api()
Expand Down Expand Up @@ -170,7 +185,7 @@ def _get_errors(self) -> List[GeneratorError]:
return errors

def _create_package(self) -> None:
self.package_dir.mkdir()
self.package_dir.mkdir(exist_ok=True)
# Package __init__.py
package_init = self.package_dir / "__init__.py"

Expand Down
1 change: 1 addition & 0 deletions openapi_python_client/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Config(BaseModel):
project_name_override: Optional[str]
package_name_override: Optional[str]
package_version_override: Optional[str]
protected_dirs: Optional[List[str]]
post_hooks: List[str] = [
"autoflake -i -r --remove-all-unused-imports --remove-unused-variables --ignore-init-module-imports .",
"isort .",
Expand Down