|
| 1 | +--- |
| 2 | +inclusion: auto |
| 3 | +--- |
| 4 | + |
| 5 | +# Code Standards for NCM Scripts |
| 6 | + |
| 7 | +## Virtual Environment |
| 8 | + |
| 9 | +**Always use the project `.venv`**. Never run scripts with the system Python. |
| 10 | + |
| 11 | +When running any script or command, use: |
| 12 | +```bash |
| 13 | +.venv/bin/python scripts/my_script.py |
| 14 | +``` |
| 15 | + |
| 16 | +When installing packages: |
| 17 | +```bash |
| 18 | +.venv/bin/pip install -r requirements.txt |
| 19 | +``` |
| 20 | + |
| 21 | +The venv uses Python 3.12 and is located at the project root `.venv/`. |
| 22 | + |
| 23 | +## Required Environment Variables |
| 24 | + |
| 25 | +All scripts require these environment variables for API authentication: |
| 26 | + |
| 27 | +| Variable | Description | |
| 28 | +|----------|-------------| |
| 29 | +| `CP_API_ID` | Cradlepoint API ID | |
| 30 | +| `CP_API_KEY` | Cradlepoint API Key | |
| 31 | +| `ECM_API_ID` | ECM API ID | |
| 32 | +| `ECM_API_KEY` | ECM API Key | |
| 33 | + |
| 34 | +Optional (for v3 API): |
| 35 | + |
| 36 | +| Variable | Description | |
| 37 | +|----------|-------------| |
| 38 | +| `CP_API_TOKEN` | Bearer token for API v3 | |
| 39 | + |
| 40 | +### If env vars are not set, scripts must detect this and print setup instructions. |
| 41 | + |
| 42 | +Use the helper at `scripts/utils/env_check.py` (see below) to validate at script startup. |
| 43 | + |
| 44 | +### How to set env vars by OS: |
| 45 | + |
| 46 | +**Recommended: Use the setup script** |
| 47 | +```bash |
| 48 | +.venv/bin/python setup_env.py |
| 49 | +``` |
| 50 | +This prompts for all keys (including v3 token), injects them into the `.venv/bin/activate` |
| 51 | +scripts, and they load automatically every time you `source .venv/bin/activate`. |
| 52 | +Run it again anytime to update credentials. |
| 53 | + |
| 54 | +**Manual setup — macOS / Linux (bash/zsh):** |
| 55 | +```bash |
| 56 | +export CP_API_ID="your_cp_api_id" |
| 57 | +export CP_API_KEY="your_cp_api_key" |
| 58 | +export ECM_API_ID="your_ecm_api_id" |
| 59 | +export ECM_API_KEY="your_ecm_api_key" |
| 60 | +export CP_API_TOKEN="your_v3_token" # optional, for v3 API |
| 61 | +``` |
| 62 | +To persist, add these to `~/.zshrc` (macOS) or `~/.bashrc` (Linux). |
| 63 | + |
| 64 | +**Windows (PowerShell):** |
| 65 | +```powershell |
| 66 | +$env:CP_API_ID = "your_cp_api_id" |
| 67 | +$env:CP_API_KEY = "your_cp_api_key" |
| 68 | +$env:ECM_API_ID = "your_ecm_api_id" |
| 69 | +$env:ECM_API_KEY = "your_ecm_api_key" |
| 70 | +$env:CP_API_TOKEN = "your_v3_token" # optional, for v3 API |
| 71 | +``` |
| 72 | +To persist, use System Properties → Environment Variables, or add to your PowerShell profile. |
| 73 | + |
| 74 | +**Windows (Command Prompt):** |
| 75 | +```cmd |
| 76 | +set CP_API_ID=your_cp_api_id |
| 77 | +set CP_API_KEY=your_cp_api_key |
| 78 | +set ECM_API_ID=your_ecm_api_id |
| 79 | +set ECM_API_KEY=your_ecm_api_key |
| 80 | +set CP_API_TOKEN=your_v3_token &REM optional, for v3 API |
| 81 | +``` |
| 82 | + |
| 83 | +## File Structure |
| 84 | + |
| 85 | +All new scripts should follow this structure: |
| 86 | +```python |
| 87 | +""" |
| 88 | +Script description. |
| 89 | +""" |
| 90 | +import os |
| 91 | +import sys |
| 92 | + |
| 93 | +# Add project root to path if needed |
| 94 | +sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) |
| 95 | + |
| 96 | +from utils.env_check import check_env |
| 97 | +from utils.credentials import get_credentials |
| 98 | +from utils.session import APISession |
| 99 | +from utils.logger import get_logger |
| 100 | + |
| 101 | +def main(): |
| 102 | + """Main entry point.""" |
| 103 | + check_env() # Always call first — exits with instructions if vars missing |
| 104 | + # ... implementation |
| 105 | + |
| 106 | +if __name__ == '__main__': |
| 107 | + main() |
| 108 | +``` |
| 109 | + |
| 110 | +## Authentication |
| 111 | + |
| 112 | +- Never hardcode API keys in source files |
| 113 | +- Use environment variables (preferred) or `scripts/utils/credentials.py` as fallback |
| 114 | +- For the NCM SDK: pass keys as a dictionary |
| 115 | +- For direct API calls: use `scripts/utils/session.py` |
| 116 | + |
| 117 | +## Error Handling |
| 118 | + |
| 119 | +- Always wrap API calls in try/except |
| 120 | +- Implement retry logic for transient errors (408, 429, 503, 504) |
| 121 | +- Log errors with context (which endpoint, what parameters) |
| 122 | + |
| 123 | +## Output |
| 124 | + |
| 125 | +- Use CSV for tabular data exports |
| 126 | +- Use JSON for structured data |
| 127 | +- Print progress for long-running operations |
| 128 | +- Store output files in `scripts/script_manager/csv_files/` when using script_manager |
| 129 | + |
| 130 | +## Dependencies |
| 131 | + |
| 132 | +- Core: `requests`, `ncm` (SDK) |
| 133 | +- Check `requirements.txt` before adding new dependencies |
| 134 | +- If a new dependency is needed, add it to `requirements.txt` |
0 commit comments