codefetch-cli 1.2.1__tar.gz → 1.2.3__tar.gz
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.
- {codefetch_cli-1.2.1 → codefetch_cli-1.2.3}/LICENSE +21 -21
- codefetch_cli-1.2.3/PKG-INFO +193 -0
- codefetch_cli-1.2.3/README.md +171 -0
- codefetch_cli-1.2.3/codefetch/__init__.py +146 -0
- {codefetch_cli-1.2.1 → codefetch_cli-1.2.3}/codefetch/cli.py +278 -228
- codefetch_cli-1.2.3/codefetch_cli.egg-info/PKG-INFO +193 -0
- {codefetch_cli-1.2.1 → codefetch_cli-1.2.3}/pyproject.toml +1 -1
- {codefetch_cli-1.2.1 → codefetch_cli-1.2.3}/setup.py +1 -1
- codefetch_cli-1.2.1/PKG-INFO +0 -187
- codefetch_cli-1.2.1/README.md +0 -165
- codefetch_cli-1.2.1/codefetch/__init__.py +0 -5
- codefetch_cli-1.2.1/codefetch_cli.egg-info/PKG-INFO +0 -187
- {codefetch_cli-1.2.1 → codefetch_cli-1.2.3}/codefetch_cli.egg-info/SOURCES.txt +0 -0
- {codefetch_cli-1.2.1 → codefetch_cli-1.2.3}/codefetch_cli.egg-info/dependency_links.txt +0 -0
- {codefetch_cli-1.2.1 → codefetch_cli-1.2.3}/codefetch_cli.egg-info/entry_points.txt +0 -0
- {codefetch_cli-1.2.1 → codefetch_cli-1.2.3}/codefetch_cli.egg-info/top_level.txt +0 -0
- {codefetch_cli-1.2.1 → codefetch_cli-1.2.3}/setup.cfg +0 -0
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 InzamamulQureshi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: codefetch-cli
|
|
3
|
+
Version: 1.2.3
|
|
4
|
+
Summary: Quickly fetch code files from GitHub repositories with simple commands
|
|
5
|
+
Home-page: https://github.com/InzamamulQureshi/Codefetch
|
|
6
|
+
Author: Inzamamul Qureshi
|
|
7
|
+
Author-email: Inzamamul Qureshi <inzamamulqureshi@gmail.com>
|
|
8
|
+
License-Expression: MIT
|
|
9
|
+
Project-URL: Homepage, https://github.com/InzamamulQureshi/Codefetch
|
|
10
|
+
Project-URL: Repository, https://github.com/InzamamulQureshi/Codefetch
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Environment :: Console
|
|
14
|
+
Classifier: Topic :: Utilities
|
|
15
|
+
Requires-Python: >=3.8
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Dynamic: author
|
|
19
|
+
Dynamic: home-page
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
Dynamic: requires-python
|
|
22
|
+
|
|
23
|
+
# CodeFetch
|
|
24
|
+
|
|
25
|
+
Quickly fetch code files from GitHub repositories with simple commands.
|
|
26
|
+
|
|
27
|
+
CodeFetch is a lightweight command-line utility and Python library designed to quickly inspect, preview, and download individual source code files directly from any GitHub repository without needing to clone full repositories.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## Features
|
|
32
|
+
|
|
33
|
+
- **Any User & Repository**: Fetch files from any public GitHub repository using intuitive syntax (`codefetch <owner>/<repo> <filename>`).
|
|
34
|
+
- **Terminal Preview**: Inspect code directly in your console with `--show` without saving it to disk.
|
|
35
|
+
- **Automatic Path & Case Resolution**: Automatically resolves case mismatches and finds files nested inside subdirectories.
|
|
36
|
+
- **Repository Discovery**: List all public repositories for any GitHub user with `--repos`.
|
|
37
|
+
- **Tree Exploration**: List all supported code files within a repository with `--list`.
|
|
38
|
+
- **Python Library Support**: Import and use programmatically with `import codefetch as cf`.
|
|
39
|
+
- **Optional Persistent Defaults**: Save a default repository or user with `--set-default` if you frequently work with the same repository.
|
|
40
|
+
- **Zero Runtime Dependencies**: Built using only the Python standard library with zero third-party dependencies.
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
### From PyPI
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install codefetch-cli
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### From GitHub
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
pip install git+https://github.com/InzamamulQureshi/Codefetch.git
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### From Local Source
|
|
59
|
+
|
|
60
|
+
Clone the repository and install locally:
|
|
61
|
+
|
|
62
|
+
```bash
|
|
63
|
+
git clone https://github.com/InzamamulQureshi/Codefetch.git
|
|
64
|
+
cd Codefetch
|
|
65
|
+
pip install .
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## Quick Start & Usage
|
|
71
|
+
|
|
72
|
+
### 1. Download Files
|
|
73
|
+
|
|
74
|
+
Fetch a single file from any GitHub repository:
|
|
75
|
+
|
|
76
|
+
```bash
|
|
77
|
+
# Fetch a file from any user and repository
|
|
78
|
+
codefetch owner/repo main.py
|
|
79
|
+
|
|
80
|
+
# Download a configuration or dataset
|
|
81
|
+
codefetch owner/repo data.csv
|
|
82
|
+
codefetch owner/repo config.yaml
|
|
83
|
+
|
|
84
|
+
# Download using explicit flags
|
|
85
|
+
codefetch -u owner -r repo main.py
|
|
86
|
+
|
|
87
|
+
# Save with a custom filename or destination path
|
|
88
|
+
codefetch owner/repo main.py -o custom_name.py
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### 2. Preview in Terminal (Without Downloading)
|
|
92
|
+
|
|
93
|
+
Display the contents of a file directly in stdout with `-s` / `--show`:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
# Preview file content in terminal
|
|
97
|
+
codefetch -s owner/repo main.py
|
|
98
|
+
codefetch -s torvalds/linux Makefile
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 3. Explore Repositories and Files
|
|
102
|
+
|
|
103
|
+
```bash
|
|
104
|
+
# List all code files in a repository
|
|
105
|
+
codefetch -l owner/repo
|
|
106
|
+
codefetch --list owner/repo
|
|
107
|
+
|
|
108
|
+
# List all public repositories for any GitHub user
|
|
109
|
+
codefetch -R -u username
|
|
110
|
+
codefetch --repos username
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### 4. Configuration and Defaults (Optional)
|
|
114
|
+
|
|
115
|
+
By default, CodeFetch has no preset repository or user. If you frequently fetch files from the same repository, you can optionally configure defaults:
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# Set a default repository (saves both user and repo)
|
|
119
|
+
codefetch --set-default owner/repo
|
|
120
|
+
|
|
121
|
+
# Now you can fetch files directly without typing the repo name!
|
|
122
|
+
codefetch main.py
|
|
123
|
+
codefetch -l
|
|
124
|
+
|
|
125
|
+
# View current saved configuration
|
|
126
|
+
codefetch --config
|
|
127
|
+
|
|
128
|
+
# Reset all configuration back to defaults (no default repository)
|
|
129
|
+
codefetch --reset-config
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## Python API Usage
|
|
135
|
+
|
|
136
|
+
You can also use CodeFetch programmatically within your Python scripts:
|
|
137
|
+
|
|
138
|
+
```python
|
|
139
|
+
import codefetch as cf
|
|
140
|
+
|
|
141
|
+
# 1. Fetch file content as a string directly in memory
|
|
142
|
+
makefile_text = cf.get("torvalds/linux", "Makefile")
|
|
143
|
+
# Or shorthand single argument syntax:
|
|
144
|
+
readme_text = cf.get("psf/requests/README.md")
|
|
145
|
+
|
|
146
|
+
# 2. Download a file to disk
|
|
147
|
+
cf.download("torvalds/linux", "Makefile", output="linux_Makefile")
|
|
148
|
+
|
|
149
|
+
# 3. List all code files in a repository
|
|
150
|
+
files = cf.list("torvalds/linux")
|
|
151
|
+
print(f"Found {len(files)} files")
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
---
|
|
155
|
+
|
|
156
|
+
## Command Reference
|
|
157
|
+
|
|
158
|
+
| Flag | Description | Example |
|
|
159
|
+
|---|---|---|
|
|
160
|
+
| `-s`, `--show` | Print file contents to terminal | `codefetch -s owner/repo main.py` |
|
|
161
|
+
| `-o`, `--output` | Specify destination output filename | `codefetch owner/repo main.py -o script.py` |
|
|
162
|
+
| `-l`, `--list` | List available code files in a repository | `codefetch -l owner/repo` |
|
|
163
|
+
| `-R`, `--repos` | List public repositories for a user | `codefetch -R -u username` |
|
|
164
|
+
| `-r`, `--repo` | Specify repository name or URL | `codefetch -r repo main.py` |
|
|
165
|
+
| `-u`, `--user` | Specify GitHub username or organization | `codefetch -u username -r repo main.py` |
|
|
166
|
+
| `-b`, `--branch` | Branch name (default: `main`) | `codefetch -b master owner/repo Makefile` |
|
|
167
|
+
| `--set-default` | Set persistent default repository | `codefetch --set-default owner/repo` |
|
|
168
|
+
| `--set-default-user` | Set persistent default user | `codefetch --set-default-user username` |
|
|
169
|
+
| `--config` | Display active configuration | `codefetch --config` |
|
|
170
|
+
| `--reset-config` | Reset configuration back to defaults | `codefetch --reset-config` |
|
|
171
|
+
| `-v`, `--version` | Display version information | `codefetch --version` |
|
|
172
|
+
| `-h`, `--help` | Display help and usage message | `codefetch --help` |
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## Supported File Extensions
|
|
177
|
+
|
|
178
|
+
CodeFetch recognizes and lists common source and document files:
|
|
179
|
+
|
|
180
|
+
- **Data & Tables**: `.csv`, `.tsv`, `.json`, `.jsonl`, `.yaml`, `.yml`, `.toml`, `.xml`, `.ini`, `.cfg`, `.conf`, `.env`
|
|
181
|
+
- **Data Science**: `.ipynb`, `.py`, `.r`, `.rmd`, `.m`
|
|
182
|
+
- **Languages**: `.c`, `.cpp`, `.h`, `.hpp`, `.rs`, `.go`, `.java`, `.kt`, `.cs`, `.swift`, `.dart`, `.php`
|
|
183
|
+
- **Web**: `.html`, `.css`, `.scss`, `.js`, `.jsx`, `.ts`, `.tsx`, `.vue`, `.svelte`
|
|
184
|
+
- **Scripts & Systems**: `.sh`, `.bash`, `.zsh`, `.fish`, `.bat`, `.cmd`, `.ps1`, `.lua`, `.rb`, `.asm`, `.s`, `.hex`
|
|
185
|
+
- **Database & Schemas**: `.sql`, `.graphql`, `.gql`, `.proto`
|
|
186
|
+
- **Documentation**: `.md`, `.markdown`, `.rst`, `.txt`, `.tex`, `.log`
|
|
187
|
+
- **Build Files**: `Dockerfile`, `Makefile`, `CMakeLists.txt`, `Gemfile`, `Procfile`
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## License
|
|
192
|
+
|
|
193
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
# CodeFetch
|
|
2
|
+
|
|
3
|
+
Quickly fetch code files from GitHub repositories with simple commands.
|
|
4
|
+
|
|
5
|
+
CodeFetch is a lightweight command-line utility and Python library designed to quickly inspect, preview, and download individual source code files directly from any GitHub repository without needing to clone full repositories.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Any User & Repository**: Fetch files from any public GitHub repository using intuitive syntax (`codefetch <owner>/<repo> <filename>`).
|
|
12
|
+
- **Terminal Preview**: Inspect code directly in your console with `--show` without saving it to disk.
|
|
13
|
+
- **Automatic Path & Case Resolution**: Automatically resolves case mismatches and finds files nested inside subdirectories.
|
|
14
|
+
- **Repository Discovery**: List all public repositories for any GitHub user with `--repos`.
|
|
15
|
+
- **Tree Exploration**: List all supported code files within a repository with `--list`.
|
|
16
|
+
- **Python Library Support**: Import and use programmatically with `import codefetch as cf`.
|
|
17
|
+
- **Optional Persistent Defaults**: Save a default repository or user with `--set-default` if you frequently work with the same repository.
|
|
18
|
+
- **Zero Runtime Dependencies**: Built using only the Python standard library with zero third-party dependencies.
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
### From PyPI
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install codefetch-cli
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### From GitHub
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
pip install git+https://github.com/InzamamulQureshi/Codefetch.git
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### From Local Source
|
|
37
|
+
|
|
38
|
+
Clone the repository and install locally:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
git clone https://github.com/InzamamulQureshi/Codefetch.git
|
|
42
|
+
cd Codefetch
|
|
43
|
+
pip install .
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
---
|
|
47
|
+
|
|
48
|
+
## Quick Start & Usage
|
|
49
|
+
|
|
50
|
+
### 1. Download Files
|
|
51
|
+
|
|
52
|
+
Fetch a single file from any GitHub repository:
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
# Fetch a file from any user and repository
|
|
56
|
+
codefetch owner/repo main.py
|
|
57
|
+
|
|
58
|
+
# Download a configuration or dataset
|
|
59
|
+
codefetch owner/repo data.csv
|
|
60
|
+
codefetch owner/repo config.yaml
|
|
61
|
+
|
|
62
|
+
# Download using explicit flags
|
|
63
|
+
codefetch -u owner -r repo main.py
|
|
64
|
+
|
|
65
|
+
# Save with a custom filename or destination path
|
|
66
|
+
codefetch owner/repo main.py -o custom_name.py
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### 2. Preview in Terminal (Without Downloading)
|
|
70
|
+
|
|
71
|
+
Display the contents of a file directly in stdout with `-s` / `--show`:
|
|
72
|
+
|
|
73
|
+
```bash
|
|
74
|
+
# Preview file content in terminal
|
|
75
|
+
codefetch -s owner/repo main.py
|
|
76
|
+
codefetch -s torvalds/linux Makefile
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
### 3. Explore Repositories and Files
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
# List all code files in a repository
|
|
83
|
+
codefetch -l owner/repo
|
|
84
|
+
codefetch --list owner/repo
|
|
85
|
+
|
|
86
|
+
# List all public repositories for any GitHub user
|
|
87
|
+
codefetch -R -u username
|
|
88
|
+
codefetch --repos username
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### 4. Configuration and Defaults (Optional)
|
|
92
|
+
|
|
93
|
+
By default, CodeFetch has no preset repository or user. If you frequently fetch files from the same repository, you can optionally configure defaults:
|
|
94
|
+
|
|
95
|
+
```bash
|
|
96
|
+
# Set a default repository (saves both user and repo)
|
|
97
|
+
codefetch --set-default owner/repo
|
|
98
|
+
|
|
99
|
+
# Now you can fetch files directly without typing the repo name!
|
|
100
|
+
codefetch main.py
|
|
101
|
+
codefetch -l
|
|
102
|
+
|
|
103
|
+
# View current saved configuration
|
|
104
|
+
codefetch --config
|
|
105
|
+
|
|
106
|
+
# Reset all configuration back to defaults (no default repository)
|
|
107
|
+
codefetch --reset-config
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
## Python API Usage
|
|
113
|
+
|
|
114
|
+
You can also use CodeFetch programmatically within your Python scripts:
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
import codefetch as cf
|
|
118
|
+
|
|
119
|
+
# 1. Fetch file content as a string directly in memory
|
|
120
|
+
makefile_text = cf.get("torvalds/linux", "Makefile")
|
|
121
|
+
# Or shorthand single argument syntax:
|
|
122
|
+
readme_text = cf.get("psf/requests/README.md")
|
|
123
|
+
|
|
124
|
+
# 2. Download a file to disk
|
|
125
|
+
cf.download("torvalds/linux", "Makefile", output="linux_Makefile")
|
|
126
|
+
|
|
127
|
+
# 3. List all code files in a repository
|
|
128
|
+
files = cf.list("torvalds/linux")
|
|
129
|
+
print(f"Found {len(files)} files")
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## Command Reference
|
|
135
|
+
|
|
136
|
+
| Flag | Description | Example |
|
|
137
|
+
|---|---|---|
|
|
138
|
+
| `-s`, `--show` | Print file contents to terminal | `codefetch -s owner/repo main.py` |
|
|
139
|
+
| `-o`, `--output` | Specify destination output filename | `codefetch owner/repo main.py -o script.py` |
|
|
140
|
+
| `-l`, `--list` | List available code files in a repository | `codefetch -l owner/repo` |
|
|
141
|
+
| `-R`, `--repos` | List public repositories for a user | `codefetch -R -u username` |
|
|
142
|
+
| `-r`, `--repo` | Specify repository name or URL | `codefetch -r repo main.py` |
|
|
143
|
+
| `-u`, `--user` | Specify GitHub username or organization | `codefetch -u username -r repo main.py` |
|
|
144
|
+
| `-b`, `--branch` | Branch name (default: `main`) | `codefetch -b master owner/repo Makefile` |
|
|
145
|
+
| `--set-default` | Set persistent default repository | `codefetch --set-default owner/repo` |
|
|
146
|
+
| `--set-default-user` | Set persistent default user | `codefetch --set-default-user username` |
|
|
147
|
+
| `--config` | Display active configuration | `codefetch --config` |
|
|
148
|
+
| `--reset-config` | Reset configuration back to defaults | `codefetch --reset-config` |
|
|
149
|
+
| `-v`, `--version` | Display version information | `codefetch --version` |
|
|
150
|
+
| `-h`, `--help` | Display help and usage message | `codefetch --help` |
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## Supported File Extensions
|
|
155
|
+
|
|
156
|
+
CodeFetch recognizes and lists common source and document files:
|
|
157
|
+
|
|
158
|
+
- **Data & Tables**: `.csv`, `.tsv`, `.json`, `.jsonl`, `.yaml`, `.yml`, `.toml`, `.xml`, `.ini`, `.cfg`, `.conf`, `.env`
|
|
159
|
+
- **Data Science**: `.ipynb`, `.py`, `.r`, `.rmd`, `.m`
|
|
160
|
+
- **Languages**: `.c`, `.cpp`, `.h`, `.hpp`, `.rs`, `.go`, `.java`, `.kt`, `.cs`, `.swift`, `.dart`, `.php`
|
|
161
|
+
- **Web**: `.html`, `.css`, `.scss`, `.js`, `.jsx`, `.ts`, `.tsx`, `.vue`, `.svelte`
|
|
162
|
+
- **Scripts & Systems**: `.sh`, `.bash`, `.zsh`, `.fish`, `.bat`, `.cmd`, `.ps1`, `.lua`, `.rb`, `.asm`, `.s`, `.hex`
|
|
163
|
+
- **Database & Schemas**: `.sql`, `.graphql`, `.gql`, `.proto`
|
|
164
|
+
- **Documentation**: `.md`, `.markdown`, `.rst`, `.txt`, `.tex`, `.log`
|
|
165
|
+
- **Build Files**: `Dockerfile`, `Makefile`, `CMakeLists.txt`, `Gemfile`, `Procfile`
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## License
|
|
170
|
+
|
|
171
|
+
This project is licensed under the [MIT License](LICENSE).
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"""
|
|
2
|
+
CodeFetch - Easily preview and download code files from GitHub repositories.
|
|
3
|
+
|
|
4
|
+
Usage in Python:
|
|
5
|
+
import codefetch as cf
|
|
6
|
+
|
|
7
|
+
# 1. Fetch file content as string:
|
|
8
|
+
content = cf.get("torvalds/linux", "Makefile")
|
|
9
|
+
# or shorthand target string:
|
|
10
|
+
content = cf.get("torvalds/linux/Makefile")
|
|
11
|
+
|
|
12
|
+
# 2. Download file to local disk:
|
|
13
|
+
cf.download("torvalds/linux", "Makefile", output="linux_makefile.txt")
|
|
14
|
+
|
|
15
|
+
# 3. List available code files:
|
|
16
|
+
files = cf.list("torvalds/linux")
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
__version__ = "1.2.3"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def get(target, filename=None, branch="main"):
|
|
23
|
+
"""
|
|
24
|
+
Fetch and return the text content of a file from GitHub.
|
|
25
|
+
|
|
26
|
+
Args:
|
|
27
|
+
target: 'owner/repo' or full path 'owner/repo/path/to/file' or full GitHub URL.
|
|
28
|
+
filename: Optional path to file in repo if target is 'owner/repo'.
|
|
29
|
+
branch: Branch name (default: 'main', automatically falls back to 'master').
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
str: File content.
|
|
33
|
+
|
|
34
|
+
Raises:
|
|
35
|
+
FileNotFoundError: If the file does not exist in the repository.
|
|
36
|
+
ValueError: If arguments cannot be resolved into a repository and filename.
|
|
37
|
+
"""
|
|
38
|
+
import urllib.request
|
|
39
|
+
from .cli import (
|
|
40
|
+
fetch_raw_direct,
|
|
41
|
+
get_repo_tree_with_branch,
|
|
42
|
+
resolve_file_in_tree,
|
|
43
|
+
parse_repo_identifier,
|
|
44
|
+
resolve_invocation,
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
if filename:
|
|
48
|
+
owner, repo = parse_repo_identifier(target)
|
|
49
|
+
file_path = filename.strip("/\\")
|
|
50
|
+
else:
|
|
51
|
+
owner, repo, resolved_branch, file_path = resolve_invocation(
|
|
52
|
+
[target], None, None, branch, None, None, "main"
|
|
53
|
+
)
|
|
54
|
+
if resolved_branch:
|
|
55
|
+
branch = resolved_branch
|
|
56
|
+
|
|
57
|
+
if not owner or not repo or not file_path:
|
|
58
|
+
raise ValueError(f"Could not resolve owner, repo, and filename from target='{target}', filename='{filename}'")
|
|
59
|
+
|
|
60
|
+
# 1. Try fast direct raw fetch
|
|
61
|
+
found_branch, content = fetch_raw_direct(owner, repo, branch, file_path)
|
|
62
|
+
if content is not None:
|
|
63
|
+
return content
|
|
64
|
+
|
|
65
|
+
# 2. Try repository tree search
|
|
66
|
+
actual_branch, tree_data = get_repo_tree_with_branch(owner, repo, branch)
|
|
67
|
+
matched = resolve_file_in_tree(tree_data, file_path)
|
|
68
|
+
if matched:
|
|
69
|
+
url = f"https://raw.githubusercontent.com/{owner}/{repo}/{actual_branch}/{matched}"
|
|
70
|
+
req = urllib.request.Request(url, headers={"User-Agent": "codefetch"})
|
|
71
|
+
with urllib.request.urlopen(req, timeout=15) as resp:
|
|
72
|
+
return resp.read().decode("utf-8")
|
|
73
|
+
|
|
74
|
+
raise FileNotFoundError(f"File '{file_path}' not found in {owner}/{repo} ({actual_branch})")
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def download(target, filename=None, output=None, branch="main"):
|
|
78
|
+
"""
|
|
79
|
+
Download a file from GitHub and save it locally.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
target: 'owner/repo' or full path 'owner/repo/path/to/file'.
|
|
83
|
+
filename: Optional path to file in repo if target is 'owner/repo'.
|
|
84
|
+
output: Destination filename/path on local disk (default: original filename).
|
|
85
|
+
branch: Branch name (default: 'main').
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
str: Path to the downloaded file.
|
|
89
|
+
"""
|
|
90
|
+
content = get(target, filename=filename, branch=branch)
|
|
91
|
+
dest_name = output
|
|
92
|
+
if not dest_name:
|
|
93
|
+
resolved_file = filename if filename else target.rsplit("/", 1)[-1]
|
|
94
|
+
dest_name = resolved_file.rsplit("/", 1)[-1]
|
|
95
|
+
|
|
96
|
+
with open(dest_name, "w", encoding="utf-8") as f:
|
|
97
|
+
f.write(content)
|
|
98
|
+
|
|
99
|
+
return dest_name
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def list(target, branch="main"):
|
|
103
|
+
"""
|
|
104
|
+
List supported code files in a repository.
|
|
105
|
+
|
|
106
|
+
Args:
|
|
107
|
+
target: 'owner/repo' or repository URL.
|
|
108
|
+
branch: Branch name (default: 'main').
|
|
109
|
+
|
|
110
|
+
Returns:
|
|
111
|
+
list[str]: Relative paths of code files in the repository.
|
|
112
|
+
"""
|
|
113
|
+
from .cli import (
|
|
114
|
+
parse_repo_identifier,
|
|
115
|
+
get_repo_tree_with_branch,
|
|
116
|
+
CODE_EXTENSIONS,
|
|
117
|
+
NAMED_FILES,
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
owner, repo = parse_repo_identifier(target)
|
|
121
|
+
if not owner or not repo:
|
|
122
|
+
raise ValueError(f"Invalid repository identifier: '{target}'")
|
|
123
|
+
|
|
124
|
+
actual_branch, tree_data = get_repo_tree_with_branch(owner, repo, branch)
|
|
125
|
+
files = []
|
|
126
|
+
for item in tree_data.get("tree", []):
|
|
127
|
+
if item.get("type") != "blob":
|
|
128
|
+
continue
|
|
129
|
+
path = item.get("path", "")
|
|
130
|
+
base_name = path.rsplit("/", 1)[-1].lower()
|
|
131
|
+
suffix = ""
|
|
132
|
+
if "." in base_name:
|
|
133
|
+
suffix = "." + base_name.rsplit(".", 1)[-1]
|
|
134
|
+
if suffix in CODE_EXTENSIONS or base_name in NAMED_FILES:
|
|
135
|
+
files.append(path)
|
|
136
|
+
|
|
137
|
+
files.sort(key=str.lower)
|
|
138
|
+
return files
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
__all__ = [
|
|
142
|
+
"__version__",
|
|
143
|
+
"get",
|
|
144
|
+
"download",
|
|
145
|
+
"list",
|
|
146
|
+
]
|