Skip to content

Commit 9ba05a8

Browse files
committed
2 parents c497eb6 + 4d35ab6 commit 9ba05a8

4 files changed

Lines changed: 55 additions & 5 deletions

File tree

GEMINI.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ This document outlines mandatory operational guidelines, constraints, and best p
113113
- Use language-appropriate tooling for formatting and linting where available.
114114
- Pass `customer_id` as a command-line argument.
115115
- Use type hints, annotations, or other static typing features if the language supports them.
116+
117+
#### 3.4.1. Python Configuration Loading
118+
- **Code Generation (to `saved_code/`):** When generating Python code that uses the `google-ads-python` client library and saves it to the `saved_code/` directory, any calls to `GoogleAdsClient.load_from_storage()` MUST NOT include a `path` argument. This ensures that the generated code, when run by the user outside of the Gemini CLI, will look for `google-ads.yaml` in their home directory (or other default locations as per the client library's behavior).
119+
- **Execution within Gemini CLI:** When executing Python code that uses `GoogleAdsClient.load_from_storage()` within the Gemini CLI, you MUST set the environment variable `GOOGLE_ADS_CONFIGURATION_FILE_PATH` to `config/google-ads.yaml` before running the script. This ensures the script uses the project's configuration file located at `config/google-ads.yaml` during execution within the CLI environment.
116120
- **Error Handling:** When using the Python client library, catch `GoogleAdsException` and inspect the `error` attribute. For other languages, use the equivalent exception type.
117121

118122
#### 3.5. Troubleshooting

hooks/SessionEnd/cleanup_config.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import os
2+
import shutil
3+
import sys
4+
import datetime
5+
6+
def cleanup():
7+
"""Removes all files in the config directory."""
8+
# Determine paths
9+
script_dir = os.path.dirname(os.path.abspath(__file__))
10+
# hooks/SessionEnd -> root is 2 levels up
11+
root_dir = os.path.abspath(os.path.join(script_dir, "../.."))
12+
config_dir = os.path.join(root_dir, "config")
13+
14+
if not os.path.exists(config_dir):
15+
print(f"Config directory {config_dir} does not exist. Nothing to clean.", file=sys.stderr)
16+
return
17+
18+
try:
19+
# User requested to remove *all files* in the config directory.
20+
# We could also remove the directory itself. Let's remove content.
21+
for filename in os.listdir(config_dir):
22+
file_path = os.path.join(config_dir, filename)
23+
try:
24+
if os.path.isfile(file_path) or os.path.islink(file_path):
25+
os.unlink(file_path)
26+
elif os.path.isdir(file_path):
27+
shutil.rmtree(file_path)
28+
except Exception as e:
29+
print(f"Failed to delete {file_path}. Reason: {e}", file=sys.stderr)
30+
31+
timestamp = datetime.datetime.now()
32+
print(f"SUCCESS: SessionEnd hook cleaned up config directory at {timestamp}", file=sys.stdout)
33+
34+
except Exception as e:
35+
print(f"Error cleaning up config directory: {e}", file=sys.stderr)
36+
sys.exit(1)
37+
38+
if __name__ == "__main__":
39+
timestamp = datetime.datetime.now()
40+
message = f"SUCCESS: SessionEnd hook 'cleanup_config.py' ran at {timestamp}"
41+
print(message, file=sys.stderr)
42+
cleanup()

hooks/SessionStart/custom_config_python.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,4 @@ def configure():
5959
print(f"export GOOGLE_ADS_CONFIGURATION_FILE_PATH=\"{target_yaml}\"", file=sys.stdout)
6060

6161
if __name__ == "__main__":
62-
timestamp = datetime.datetime.now()
63-
message = f"SUCCESS: SessionStart hook 'custom_config_python.py' ran at {timestamp}"
64-
# Print to standard output (should appear in Gemini CLI console)
65-
print(message, file=sys.stdout)
66-
print("This message goes to stderr", file=sys.stderr)
6762
configure()

hooks/hooks.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@
88
"description": "Configures Google Ads environment at session start with a custom google-ads.yaml for Python.",
99
"timeout": 30000
1010
}
11+
],
12+
"SessionEnd": [
13+
{
14+
"name": "cleanup-config",
15+
"type": "command",
16+
"command": "python3 ${extensionPath}/hooks/SessionEnd/cleanup_config.py",
17+
"description": "Cleans up the configuration directory at session end.",
18+
"timeout": 30000
19+
}
1120
]
1221
}
1322
}

0 commit comments

Comments
 (0)