-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathcurrent.py
More file actions
130 lines (83 loc) · 3.28 KB
/
current.py
File metadata and controls
130 lines (83 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""Singleton for the current language being used in the codeflash session.
This module provides a centralized way to access and set the current language
throughout the codeflash codebase, eliminating scattered language checks and
string comparisons.
Usage:
from codeflash.languages import current_language, set_current_language, is_python
# Set the language at the start of a session
set_current_language(Language.PYTHON)
# or
set_current_language("javascript")
# Check the current language anywhere in the codebase
if is_python():
# Python-specific code
...
# Get the current language
lang = current_language()
# Get language support for the current language
support = current_language_support()
"""
from __future__ import annotations
from typing import TYPE_CHECKING
from codeflash.languages.base import Language
if TYPE_CHECKING:
from codeflash.languages.base import LanguageSupport
_JAVA = Language.JAVA
# Module-level singleton for the current language
_current_language: Language | None = None
def current_language() -> Language:
"""Get the current language being used in this codeflash session.
Returns:
The current Language enum value.
"""
return _current_language
def set_current_language(language: Language | str) -> None:
"""Set the current language for this codeflash session.
This should be called once at the start of an optimization run,
typically after reading the project configuration.
Args:
language: Either a Language enum value or a string like "python", "javascript", "typescript".
"""
global _current_language
if _current_language is not None:
return
_current_language = Language(language) if isinstance(language, str) else language
def reset_current_language() -> None:
"""Reset the current language to the default (Python).
Useful for testing or when starting a new session.
"""
global _current_language
_current_language = Language.PYTHON
def is_python() -> bool:
"""Check if the current language is Python.
Returns:
True if the current language is Python.
"""
return _current_language == Language.PYTHON
def is_javascript() -> bool:
"""Check if the current language is JavaScript or TypeScript.
This returns True for both JavaScript and TypeScript since they are
typically treated the same way in the optimization pipeline.
Returns:
True if the current language is JavaScript or TypeScript.
"""
return _current_language in (Language.JAVASCRIPT, Language.TYPESCRIPT)
def is_typescript() -> bool:
"""Check if the current language is TypeScript specifically.
Returns:
True if the current language is TypeScript.
"""
return _current_language == Language.TYPESCRIPT
def is_java() -> bool:
"""Check if the current language is Java.
Returns:
True if the current language is Java.
"""
return _current_language == _JAVA
def current_language_support() -> LanguageSupport:
"""Get the LanguageSupport instance for the current language.
Returns:
The LanguageSupport instance for the current language.
"""
from codeflash.languages.registry import get_language_support
return get_language_support(_current_language)