gws-cli 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
gws/exceptions.py ADDED
@@ -0,0 +1,58 @@
1
+ """Custom exceptions and exit codes for GWS CLI."""
2
+
3
+ from enum import IntEnum
4
+
5
+
6
+ class ExitCode(IntEnum):
7
+ """Exit codes for GWS CLI.
8
+
9
+ Exit codes:
10
+ - 0: Success
11
+ - 1: Authentication error
12
+ - 2: API error
13
+ - 3: Invalid arguments
14
+ - 4: Not found
15
+ """
16
+
17
+ SUCCESS = 0
18
+ AUTH_ERROR = 1
19
+ API_ERROR = 2
20
+ INVALID_ARGS = 3
21
+ NOT_FOUND = 4
22
+ # Legacy alias for backwards compatibility
23
+ OPERATION_FAILED = 1
24
+
25
+
26
+ class GWSError(Exception):
27
+ """Base exception for GWS CLI."""
28
+
29
+ exit_code = ExitCode.OPERATION_FAILED
30
+
31
+ def __init__(self, message: str, details: str | None = None):
32
+ super().__init__(message)
33
+ self.message = message
34
+ self.details = details
35
+
36
+
37
+ class AuthError(GWSError):
38
+ """Authentication error."""
39
+
40
+ exit_code = ExitCode.AUTH_ERROR
41
+
42
+
43
+ class APIError(GWSError):
44
+ """Google API error."""
45
+
46
+ exit_code = ExitCode.API_ERROR
47
+
48
+
49
+ class InvalidArgsError(GWSError):
50
+ """Invalid arguments error."""
51
+
52
+ exit_code = ExitCode.INVALID_ARGS
53
+
54
+
55
+ class ConfigError(GWSError):
56
+ """Configuration error."""
57
+
58
+ exit_code = ExitCode.OPERATION_FAILED
gws/output.py ADDED
@@ -0,0 +1,111 @@
1
+ """JSON output formatting for GWS CLI."""
2
+
3
+ import json
4
+ import sys
5
+ from typing import Any
6
+
7
+ from prompt_security import output_external_content as _output_external_content
8
+ from prompt_security import load_config as load_security_config
9
+
10
+ from gws.config import Config
11
+
12
+
13
+ def output_json(data: dict[str, Any]) -> None:
14
+ """Output JSON to stdout."""
15
+ print(json.dumps(data, indent=2, default=str))
16
+
17
+
18
+ def output_success(operation: str, **kwargs: Any) -> None:
19
+ """Output success response."""
20
+ response = {"status": "success", "operation": operation, **kwargs}
21
+ output_json(response)
22
+
23
+
24
+ def output_error(
25
+ error_code: str,
26
+ operation: str,
27
+ message: str,
28
+ details: Any = None,
29
+ ) -> None:
30
+ """Output error response to stdout."""
31
+ response: dict[str, Any] = {
32
+ "status": "error",
33
+ "error_code": error_code,
34
+ "operation": operation,
35
+ "message": message,
36
+ }
37
+ if details is not None:
38
+ response["details"] = details
39
+ output_json(response)
40
+
41
+
42
+ def output_external_content(
43
+ operation: str,
44
+ source_type: str,
45
+ source_id: str,
46
+ content_fields: dict[str, str],
47
+ **kwargs: Any,
48
+ ) -> None:
49
+ """
50
+ Output response with wrapped external content.
51
+
52
+ Security is skipped if:
53
+ - security_enabled=False in GWS config
54
+ - The operation is in disabled_security_operations
55
+ - The source is in the allowlist
56
+
57
+ Args:
58
+ operation: Operation name (e.g., "gmail.read")
59
+ source_type: Type of source ("email", "document", "spreadsheet", "slide")
60
+ source_id: Unique identifier (document ID, message ID, etc.)
61
+ content_fields: Dict mapping field names to content to wrap
62
+ **kwargs: Additional fields to include in response
63
+ """
64
+ from gws.context import get_active_account
65
+
66
+ gws_config = Config.load()
67
+ account = get_active_account()
68
+ if account:
69
+ gws_config = gws_config.load_effective_config(account)
70
+
71
+ # Determine if we should skip wrapping
72
+ skip_wrapping = (
73
+ not gws_config.security_enabled
74
+ or not gws_config.is_security_enabled_for_operation(operation)
75
+ or gws_config.is_allowlisted(source_type, source_id)
76
+ )
77
+
78
+ if skip_wrapping:
79
+ # Output without security wrapping
80
+ response = {
81
+ "status": "success",
82
+ "operation": operation,
83
+ "source_id": source_id,
84
+ **content_fields,
85
+ **kwargs,
86
+ }
87
+ output_json(response)
88
+ return
89
+
90
+ # Security enabled - wrap content with markers
91
+ security_config = load_security_config()
92
+ response = _output_external_content(
93
+ operation=operation,
94
+ source_type=source_type,
95
+ source_id=source_id,
96
+ content_fields=content_fields,
97
+ config=security_config,
98
+ **kwargs,
99
+ )
100
+ output_json(response)
101
+
102
+
103
+ def read_json_stdin() -> dict[str, Any]:
104
+ """Read JSON from stdin."""
105
+ from gws.exceptions import ExitCode
106
+
107
+ try:
108
+ return json.load(sys.stdin)
109
+ except json.JSONDecodeError as e:
110
+ output_error("INVALID_JSON", "stdin", f"Invalid JSON input: {e}")
111
+ raise SystemExit(ExitCode.INVALID_ARGS)
@@ -0,0 +1 @@
1
+ """Google Workspace service modules."""
gws/services/base.py ADDED
@@ -0,0 +1,47 @@
1
+ """Base service class for Google API services."""
2
+
3
+ from abc import ABC
4
+ from typing import Any
5
+
6
+ from googleapiclient.discovery import build, Resource
7
+
8
+ from gws.auth.provider import AuthProvider, resolve_auth_provider
9
+ from gws.context import get_active_account
10
+ from gws.utils.retry import execute_with_retry
11
+
12
+
13
+ class BaseService(ABC):
14
+ """Base class for Google API services."""
15
+
16
+ SERVICE_NAME: str = ""
17
+ VERSION: str = ""
18
+
19
+ def __init__(self, auth_manager: AuthProvider | None = None, account: str | None = None):
20
+ resolved_account = account or get_active_account()
21
+ self.auth_manager = auth_manager or resolve_auth_provider(account=resolved_account)
22
+ self._service: Resource | None = None
23
+ self._drive_service: Resource | None = None
24
+
25
+ @property
26
+ def service(self) -> Resource:
27
+ """Lazy-load the Google API service."""
28
+ if self._service is None:
29
+ credentials = self.auth_manager.get_credentials()
30
+ self._service = build(
31
+ self.SERVICE_NAME,
32
+ self.VERSION,
33
+ credentials=credentials,
34
+ )
35
+ return self._service
36
+
37
+ def execute(self, request: Any) -> Any:
38
+ """Execute a Google API request with automatic retry on transient errors."""
39
+ return execute_with_retry(request)
40
+
41
+ @property
42
+ def drive_service(self) -> Resource:
43
+ """Lazy-load Drive service (used by multiple services)."""
44
+ if self._drive_service is None:
45
+ credentials = self.auth_manager.get_credentials()
46
+ self._drive_service = build("drive", "v3", credentials=credentials)
47
+ return self._drive_service