Skip to content

Commit f755199

Browse files
committed
- Ruff formatter added and applied to the project
- Ruff linter and CONTRIBUTING.md added
1 parent ff747e1 commit f755199

File tree

8 files changed

+275
-187
lines changed

8 files changed

+275
-187
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,5 +160,5 @@ cython_debug/
160160
# and can be added to the global gitignore or merged into this file. For a more nuclear
161161
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
162162
#.idea/
163-
163+
.ruff_cache/
164164
reference.py

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"[python]": {
3+
"editor.formatOnSave": true
4+
},
5+
}

CONTRIBUTING.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## To set up automated Ruff linting and formatting with VS Code, Follow the instructions:
2+
3+
### 1. Python Extension for VS Code (if not already installed):
4+
5+
The Python extension for VS Code is needed to enable Python-specific features, such as linting and formatting integration. Open VS Code, go to the Extensions view (Ctrl+Shift+X or Cmd+Shift+X on macOS),search for "Python" by Microsoft and install it if you haven’t already.
6+
7+
### 2. Ruff VS Code Extension:
8+
9+
Install the Ruff extension in VS Code to enable real-time linting and formatting.
10+
In the Extensions view, search for "Ruff," and install the extension.
11+
12+
## Manually Running Ruff Formatter and Linter
13+
14+
### Run the `Ruff formatter` with the following command in the root directory
15+
16+
```bash
17+
poetry run ruff format .
18+
```
19+
20+
### Run the `Ruff Linter` with the following command in the root directory
21+
22+
```bash
23+
poetry run ruff check .
24+
```

code_mage/api.py

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,48 @@
22
import sys
33
from openai import OpenAI
44

5+
56
class Api:
6-
def __init__(self, model, config):
7-
self.supported_model = ["groq", "openrouter"]
8-
self.model = model if model is not None else "openrouter"
9-
10-
if self.model not in self.supported_model:
11-
sys.exit(f"{self.model} api model is not suppored. Model Supported: {self.supported_model}")
12-
13-
# default api_url and api_model
14-
self.api_url = "https://openrouter.ai/api/v1"
15-
self.api_model = "sao10k/l3-euryale-70b"
16-
self.api_key = os.getenv("OPENROUTER_API_KEY") or config.get('OPENROUTER_API_KEY')
17-
18-
# api_url and api_model when the provider is groq
19-
if self.model is "groq":
20-
self.api_url = "https://api.groq.com/openai/v1"
21-
self.api_model = "llama3-8b-8192"
22-
self.api_key = api_key = os.getenv("GROQ_API_KEY") or config.get('GROQ_API_KEY')
23-
24-
25-
def call_api(self, target_lang, code, stream_flag = False):
26-
27-
client = OpenAI(
28-
base_url = self.api_url,
29-
api_key = self.api_key,
30-
)
31-
32-
completion = client.chat.completions.create(
33-
extra_headers={
34-
},
35-
model=self.api_model,
36-
messages=[
37-
{"role": "system", "content": "only display the code without any explanation"},
38-
{"role": "user", "content": f"translate this to {target_lang} language: {code}"},
39-
],
40-
stream=stream_flag,
41-
)
42-
43-
return completion
7+
def __init__(self, model, config):
8+
self.supported_model = ["groq", "openrouter"]
9+
self.model = model if model is not None else "openrouter"
10+
11+
if self.model not in self.supported_model:
12+
sys.exit(
13+
f"{self.model} api model is not suppored. Model Supported: {self.supported_model}"
14+
)
15+
16+
# default api_url and api_model
17+
self.api_url = "https://openrouter.ai/api/v1"
18+
self.api_model = "sao10k/l3-euryale-70b"
19+
self.api_key = os.getenv("OPENROUTER_API_KEY") or config.get("OPENROUTER_API_KEY")
20+
21+
# api_url and api_model when the provider is groq
22+
if self.model == "groq":
23+
self.api_url = "https://api.groq.com/openai/v1"
24+
self.api_model = "llama3-8b-8192"
25+
self.api_key = os.getenv("GROQ_API_KEY") or config.get("GROQ_API_KEY")
26+
27+
def call_api(self, target_lang, code, stream_flag=False):
28+
client = OpenAI(
29+
base_url=self.api_url,
30+
api_key=self.api_key,
31+
)
32+
33+
completion = client.chat.completions.create(
34+
extra_headers={},
35+
model=self.api_model,
36+
messages=[
37+
{
38+
"role": "system",
39+
"content": "only display the code without any explanation",
40+
},
41+
{
42+
"role": "user",
43+
"content": f"translate this to {target_lang} language: {code}",
44+
},
45+
],
46+
stream=stream_flag,
47+
)
48+
49+
return completion

code_mage/codeMage.py

Lines changed: 58 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,70 @@
55
from .translator import Translator
66
from .loadConfig import load_config
77

8+
89
def main():
9-
VERSION = "release 0.1"
10+
VERSION = "release 0.1"
11+
12+
# Load config file
13+
config = load_config()
14+
15+
parser = argparse.ArgumentParser(
16+
description="This Tool translates a source file into another programming language."
17+
)
18+
19+
# Arguments
20+
parser.add_argument("source_files", nargs="*", help="The path to the source file to translate.")
1021

11-
# Load config file
12-
config = load_config()
22+
# Options
23+
parser.add_argument(
24+
"--language",
25+
"-l",
26+
help="The language to translate the source files into",
27+
default=config.get("language"),
28+
)
29+
parser.add_argument(
30+
"--output",
31+
"-o",
32+
help="Specify the output file name(without extension)",
33+
default=config.get("output"),
34+
)
35+
parser.add_argument(
36+
"--version",
37+
"-v",
38+
action="version",
39+
version=f"CodeMage {VERSION}",
40+
help="Show program's version number and exit",
41+
)
42+
parser.add_argument(
43+
"--token-usage",
44+
"-t",
45+
action="store_true",
46+
help="Get information about token usage for the prompt and response",
47+
default=config.get("token_usage", False),
48+
)
49+
parser.add_argument(
50+
"--model", "-m", help="Specify the LLM API model name", default=config.get("model")
51+
)
52+
parser.add_argument(
53+
"--stream",
54+
"-s",
55+
action="store_true",
56+
help="Stream out the output into stdout",
57+
default=config.get("stream", False),
58+
)
1359

14-
parser = argparse.ArgumentParser(description="This Tool translates a source file into another programming language.")
15-
16-
# Arguments
17-
parser.add_argument('source_files', nargs='*', help="The path to the source file to translate.")
60+
args = parser.parse_args()
1861

19-
# Options
20-
parser.add_argument('--language', '-l', help='The language to translate the source files into', default=config.get('language'))
21-
parser.add_argument('--output', '-o', help="Specify the output file name(without extension)", default=config.get('output'))
22-
parser.add_argument('--version', '-v', action='version', version=f'CodeMage {VERSION}', help="Show program's version number and exit")
23-
parser.add_argument('--token-usage', '-t', action='store_true', help='Get information about token usage for the prompt and response', default=config.get('token_usage', False))
24-
parser.add_argument('--model', '-m', help="Specify the LLM API model name", default=config.get('model'))
25-
parser.add_argument('--stream', '-s', action='store_true', help='Stream out the output into stdout', default=config.get('stream', False))
26-
27-
args = parser.parse_args()
62+
if not args.source_files:
63+
sys.exit(
64+
"Welcome To CodeMage!\nIf you need a Help, Type the fllowing command:\npoetry run codemage -h"
65+
)
2866

29-
if not args.source_files:
30-
sys.exit("Welcome To CodeMage!\nIf you need a Help, Type the fllowing command:\npoetry run codemage -h")
67+
translator = Translator(args, config)
3168

32-
translator = Translator(args, config)
33-
34-
for index, file in enumerate(args.source_files):
35-
translator.translate(file, index + 1)
69+
for index, file in enumerate(args.source_files):
70+
translator.translate(file, index + 1)
3671

3772

3873
if __name__ == "__main__":
39-
main()
74+
main()

code_mage/loadConfig.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import os
22
import toml
33

4+
45
def load_config():
5-
# Load config file from the project's root directory
6-
config_path = os.path.expanduser("~/.codemage-config.toml") # Adjust path to project root
7-
if os.path.exists(config_path):
8-
config = toml.load(config_path)
9-
else:
10-
config = {}
6+
# Load config file from the project's root directory
7+
config_path = os.path.expanduser("~/.codemage-config.toml") # Adjust path to project root
8+
if os.path.exists(config_path):
9+
config = toml.load(config_path)
10+
else:
11+
config = {}
1112

12-
return config
13+
return config

0 commit comments

Comments
 (0)