This repository was archived by the owner on Apr 17, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Add Schema for Calibanconfig #37
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
fbbb4e9
serious restructure
8170450
another pass
309d380
get tests restructured
a07ef6b
more conversion
76fe3e0
rename push
bd2e414
getting closer
88f675e
getting closer
f613c11
all works
3329f46
refactor auth
36ff6b1
add basic calibanconfig schema
2d89a20
Merge branch 'master' into sritchie/calibanconfig_schema
f317b23
making it happen
00835e4
Merge branch 'master' into sritchie/calibanconfig_schema
c1f9aa8
convert argparse to schema
baa3eeb
remove unused functions
bf99232
calibanconfig
d67ae41
remove unused
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ | |
|
|
||
| import caliban.util as u | ||
| import caliban.util.fs as ufs | ||
| import schema as s | ||
|
|
||
| t = Terminal() | ||
|
|
||
|
|
@@ -39,6 +40,25 @@ def expand_args(items: Dict[str, str]) -> List[str]: | |
| return list(it.chain.from_iterable(pairs)) | ||
|
|
||
|
|
||
| def argparse_schema(schema): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typing annotations would be helpful for me here, as I'm new to schema. SchemaType = Union[s.And, s.Or, s.Regex, s.Use, s.Schema, s.Const] |
||
| """Wrapper that performs validation and converts SchemaErrors into | ||
| ArgumentTypeErrors for better argument error reporting. | ||
|
|
||
| """ | ||
|
|
||
| def check(x): | ||
| try: | ||
| return schema.validate(x) | ||
| except s.SchemaError as e: | ||
| raise argparse.ArgumentTypeError(e.code) from None | ||
|
|
||
| return check | ||
|
|
||
|
|
||
| # TODO: Now that we use schema, validated_package and parse_kv_pair should be | ||
| # converted to schema instances. | ||
|
|
||
|
|
||
| def validated_package(path: str) -> u.Package: | ||
| """similar to generate_package but runs argparse validation on packages that | ||
| don't actually exist in the filesystem. | ||
|
|
@@ -89,26 +109,3 @@ def is_key(k: Optional[str]) -> bool: | |
|
|
||
| """ | ||
| return k is not None and len(k) > 0 and k[0] == "-" | ||
|
|
||
|
|
||
| def validated_directory(path: str) -> str: | ||
| """This validates that the supplied directory exists locally. | ||
|
|
||
| """ | ||
| if not os.path.isdir(path): | ||
| raise argparse.ArgumentTypeError( | ||
| """Directory '{}' doesn't exist in this directory. Check yourself!""". | ||
| format(path)) | ||
| return path | ||
|
|
||
|
|
||
| def validated_file(path: str) -> str: | ||
| """This validates that the supplied file exists. Tilde expansion is supported. | ||
|
|
||
| """ | ||
| expanded = os.path.expanduser(path) | ||
| if not os.path.isfile(expanded): | ||
| raise argparse.ArgumentTypeError( | ||
| """File '{}' isn't a valid file on your system. Try again!""".format( | ||
| path)) | ||
| return path | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| #!/usr/bin/python | ||
| # | ||
| # Copyright 2020 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """ | ||
| Useful shared schemas. | ||
| """ | ||
| import os | ||
| import sys | ||
| from contextlib import contextmanager | ||
| from typing import Optional | ||
|
|
||
| import commentjson | ||
|
|
||
| import caliban.util as u | ||
| import schema as s | ||
|
|
||
|
|
||
| class FatalSchemaError(Exception): | ||
| """Wrapper for an exception that can bubble itself up to the top level of the | ||
| program.""" | ||
|
|
||
| def __init__(self, message, context): | ||
| self.message = message | ||
| self.context = context | ||
| super().__init__(self.message) | ||
|
|
||
|
|
||
| @contextmanager | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, context managers are very nice. |
||
| def error_schema(context: Optional[str] = None): | ||
| """Wrap functions that check schemas in this context manager to throw an | ||
| appropriate error with a nice message. | ||
|
|
||
| """ | ||
| prefix = "" | ||
| if context is not None: | ||
| prefix = f"\nValidation error while parsing {context}:\n" | ||
|
|
||
| try: | ||
| yield | ||
| except s.SchemaError as e: | ||
| raise FatalSchemaError(e.code, prefix) | ||
|
|
||
|
|
||
| @contextmanager | ||
| def fatal_errors(): | ||
| """Context manager meant to wrap an entire program and present schema errors in | ||
| an easy-to-read way. | ||
|
|
||
| """ | ||
| try: | ||
| yield | ||
| except FatalSchemaError as e: | ||
| u.err(f"{e.context}\n{e.message}\n\n") | ||
| sys.exit(1) | ||
| except s.SchemaError as e: | ||
| u.err(f"\n{e.code}\n\n") | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| def load_json(path): | ||
| with open(path) as f: | ||
| return commentjson.load(f) | ||
|
|
||
|
|
||
| # TODO Once a release with this patch happens: | ||
| # https://github.com/keleshev/schema/pull/238,, Change `Or` to `Schema`. This | ||
| # problem only occurs for callable validators. | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The composition here is quite nice. |
||
| Directory = s.Or( | ||
| os.path.isdir, | ||
| False, | ||
| error="""Directory '{}' doesn't exist in this directory. Check yourself!""") | ||
|
|
||
| File = s.Or(lambda path: os.path.isfile(os.path.expanduser(path)), | ||
| False, | ||
| error="""File '{}' isn't a valid file on your system. Try again!""") | ||
|
|
||
| Json = s.And( | ||
| File, | ||
| s.Use(load_json, | ||
| error="""File '{}' doesn't seem to contain valid JSON. Try again!""")) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typing annotations would be good here, just given the ambiguity of the argument.