xulbux 1.6.8__tar.gz → 1.7.0__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.
Potentially problematic release.
This version of xulbux might be problematic. Click here for more details.
- {xulbux-1.6.8/src/xulbux.egg-info → xulbux-1.7.0}/PKG-INFO +18 -39
- {xulbux-1.6.8 → xulbux-1.7.0}/README.md +14 -14
- {xulbux-1.6.8 → xulbux-1.7.0}/pyproject.toml +11 -6
- xulbux-1.7.0/src/xulbux/__init__.py +25 -0
- xulbux-1.7.0/src/xulbux/_cli_.py +46 -0
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux/_consts_.py +1 -0
- xulbux-1.7.0/src/xulbux/xx_code.py +115 -0
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux/xx_color.py +223 -159
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux/xx_console.py +152 -78
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux/xx_data.py +79 -71
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux/xx_env_path.py +6 -9
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux/xx_file.py +22 -26
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux/xx_format_codes.py +55 -33
- xulbux-1.7.0/src/xulbux/xx_json.py +159 -0
- xulbux-1.7.0/src/xulbux/xx_path.py +167 -0
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux/xx_regex.py +11 -10
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux/xx_string.py +4 -1
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux/xx_system.py +6 -10
- {xulbux-1.6.8 → xulbux-1.7.0/src/xulbux.egg-info}/PKG-INFO +18 -39
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux.egg-info/SOURCES.txt +8 -3
- xulbux-1.7.0/tests/test_code.py +78 -0
- xulbux-1.7.0/tests/test_color.py +171 -0
- xulbux-1.7.0/tests/test_color_types.py +198 -0
- xulbux-1.7.0/tests/test_console.py +249 -0
- xulbux-1.7.0/tests/test_data.py +212 -0
- xulbux-1.7.0/tests/test_file.py +120 -0
- xulbux-1.7.0/tests/test_format_codes.py +62 -0
- xulbux-1.7.0/tests/test_json.py +202 -0
- xulbux-1.7.0/tests/test_path.py +160 -0
- xulbux-1.7.0/tests/test_string.py +160 -0
- xulbux-1.6.8/src/xulbux/__init__.py +0 -57
- xulbux-1.6.8/src/xulbux/_cli_.py +0 -53
- xulbux-1.6.8/src/xulbux/xx_code.py +0 -99
- xulbux-1.6.8/src/xulbux/xx_json.py +0 -103
- xulbux-1.6.8/src/xulbux/xx_path.py +0 -117
- xulbux-1.6.8/tests/test_color.py +0 -18
- xulbux-1.6.8/tests/test_color_types.py +0 -103
- xulbux-1.6.8/tests/test_console_info.py +0 -37
- xulbux-1.6.8/tests/test_data.py +0 -51
- xulbux-1.6.8/tests/test_format_codes.py +0 -42
- {xulbux-1.6.8 → xulbux-1.7.0}/LICENSE +0 -0
- {xulbux-1.6.8 → xulbux-1.7.0}/setup.cfg +0 -0
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux.egg-info/dependency_links.txt +0 -0
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux.egg-info/entry_points.txt +0 -0
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux.egg-info/requires.txt +0 -0
- {xulbux-1.6.8 → xulbux-1.7.0}/src/xulbux.egg-info/top_level.txt +0 -0
- /xulbux-1.6.8/tests/test_env_vars.py → /xulbux-1.7.0/tests/test_env_path.py +0 -0
|
@@ -1,30 +1,9 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: xulbux
|
|
3
|
-
Version: 1.
|
|
3
|
+
Version: 1.7.0
|
|
4
4
|
Summary: A Python library which includes lots of helpful classes, types and functions aiming to make common programming tasks simpler.
|
|
5
5
|
Author-email: XulbuX <xulbux.real@gmail.com>
|
|
6
|
-
License: MIT
|
|
7
|
-
|
|
8
|
-
Copyright (c) 2024 XulbuX
|
|
9
|
-
|
|
10
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
11
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
12
|
-
in the Software without restriction, including without limitation the rights
|
|
13
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
14
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
15
|
-
furnished to do so, subject to the following conditions:
|
|
16
|
-
|
|
17
|
-
The above copyright notice and this permission notice shall be included in all
|
|
18
|
-
copies or substantial portions of the Software.
|
|
19
|
-
|
|
20
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
21
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
22
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
23
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
24
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
25
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
26
|
-
SOFTWARE.
|
|
27
|
-
|
|
6
|
+
License-Expression: MIT
|
|
28
7
|
Project-URL: Bug Reports, https://github.com/XulbuX/PythonLibraryXulbuX/issues
|
|
29
8
|
Project-URL: Changelog, https://github.com/XulbuX/PythonLibraryXulbuX/blob/main/CHANGELOG.md
|
|
30
9
|
Project-URL: Documentation, https://github.com/XulbuX/PythonLibraryXulbuX/wiki
|
|
@@ -37,7 +16,6 @@ Classifier: Programming Language :: Python :: 3
|
|
|
37
16
|
Classifier: Programming Language :: Python :: 3.10
|
|
38
17
|
Classifier: Programming Language :: Python :: 3.11
|
|
39
18
|
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
-
Classifier: License :: OSI Approved :: MIT License
|
|
41
19
|
Classifier: Operating System :: OS Independent
|
|
42
20
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
43
21
|
Requires-Python: >=3.10.0
|
|
@@ -54,6 +32,7 @@ Requires-Dist: black>=23.7.0; extra == "dev"
|
|
|
54
32
|
Requires-Dist: isort>=5.12.0; extra == "dev"
|
|
55
33
|
Requires-Dist: flake8>=6.1.0; extra == "dev"
|
|
56
34
|
Requires-Dist: flake8-pyproject>=1.2.3; extra == "dev"
|
|
35
|
+
Dynamic: license-file
|
|
57
36
|
|
|
58
37
|
# **$\color{#8085FF}\Huge\textsf{XulbuX}$**
|
|
59
38
|
|
|
@@ -102,20 +81,20 @@ from xulbux import rgba, hsla, hexa
|
|
|
102
81
|
|
|
103
82
|
## Modules
|
|
104
83
|
|
|
105
|
-
| Module
|
|
106
|
-
|
|
|
107
|
-
|
|
|
108
|
-
|
|
|
109
|
-
|
|
|
110
|
-
|
|
|
111
|
-
|
|
|
112
|
-
|
|
|
113
|
-
|
|
|
114
|
-
|
|
|
115
|
-
|
|
|
116
|
-
|
|
|
117
|
-
|
|
|
118
|
-
|
|
|
84
|
+
| Module | Short Description |
|
|
85
|
+
| :---------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------- |
|
|
86
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_code) | advanced code-string operations (*changing the indent, finding function calls, ...*) |
|
|
87
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_color) | everything around colors (*converting, blending, searching colors in strings, ...*) |
|
|
88
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_console) | advanced actions related to the console (*pretty logging, advanced inputs, ...*) |
|
|
89
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_data) | advanced operations with data structures (*compare, generate path ID's, pretty print/format, ...*) |
|
|
90
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_env_path) | getting and editing the PATH variable (*get paths, check for paths, add paths, ...*) |
|
|
91
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_file) | advanced working with files (*create files, rename file-extensions, ...*) |
|
|
92
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_format_codes) | easy pretty printing with custom format codes (*print, inputs, custom format codes to ANSI, ...*) |
|
|
93
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_json) | advanced working with json files (*read, create, update, ...*) |
|
|
94
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_path) | advanced path operations (*get paths, smart-extend relative paths, delete paths, ...*) |
|
|
95
|
+
|  | generated regex pattern-templates (*match bracket- and quote pairs, match colors, ...*) |
|
|
96
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_string) | helpful actions when working with strings. (*normalize, escape, decompose, ...*) |
|
|
97
|
+
|  | advanced system actions (*restart with message, check installed Python libs, ...*) |
|
|
119
98
|
|
|
120
99
|
<br>
|
|
121
100
|
|
|
@@ -45,20 +45,20 @@ from xulbux import rgba, hsla, hexa
|
|
|
45
45
|
|
|
46
46
|
## Modules
|
|
47
47
|
|
|
48
|
-
| Module
|
|
49
|
-
|
|
|
50
|
-
|
|
|
51
|
-
|
|
|
52
|
-
|
|
|
53
|
-
|
|
|
54
|
-
|
|
|
55
|
-
|
|
|
56
|
-
|
|
|
57
|
-
|
|
|
58
|
-
|
|
|
59
|
-
|
|
|
60
|
-
|
|
|
61
|
-
|
|
|
48
|
+
| Module | Short Description |
|
|
49
|
+
| :---------------------------------------------------------------------------------------------------------------------------------------------------------------- | :------------------------------------------------------------------------------------------------- |
|
|
50
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_code) | advanced code-string operations (*changing the indent, finding function calls, ...*) |
|
|
51
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_color) | everything around colors (*converting, blending, searching colors in strings, ...*) |
|
|
52
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_console) | advanced actions related to the console (*pretty logging, advanced inputs, ...*) |
|
|
53
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_data) | advanced operations with data structures (*compare, generate path ID's, pretty print/format, ...*) |
|
|
54
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_env_path) | getting and editing the PATH variable (*get paths, check for paths, add paths, ...*) |
|
|
55
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_file) | advanced working with files (*create files, rename file-extensions, ...*) |
|
|
56
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_format_codes) | easy pretty printing with custom format codes (*print, inputs, custom format codes to ANSI, ...*) |
|
|
57
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_json) | advanced working with json files (*read, create, update, ...*) |
|
|
58
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_path) | advanced path operations (*get paths, smart-extend relative paths, delete paths, ...*) |
|
|
59
|
+
|  | generated regex pattern-templates (*match bracket- and quote pairs, match colors, ...*) |
|
|
60
|
+
| [](https://github.com/XulbuX/PythonLibraryXulbuX/wiki/xx_string) | helpful actions when working with strings. (*normalize, escape, decompose, ...*) |
|
|
61
|
+
|  | advanced system actions (*restart with message, check installed Python libs, ...*) |
|
|
62
62
|
|
|
63
63
|
<br>
|
|
64
64
|
|
|
@@ -4,11 +4,12 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "xulbux"
|
|
7
|
-
version = "1.
|
|
7
|
+
version = "1.7.0"
|
|
8
8
|
authors = [{ name = "XulbuX", email = "xulbux.real@gmail.com" }]
|
|
9
9
|
description = "A Python library which includes lots of helpful classes, types and functions aiming to make common programming tasks simpler."
|
|
10
10
|
readme = "README.md"
|
|
11
|
-
license =
|
|
11
|
+
license = "MIT"
|
|
12
|
+
license-files = ["LICENSE"]
|
|
12
13
|
requires-python = ">=3.10.0"
|
|
13
14
|
dependencies = [
|
|
14
15
|
"keyboard>=0.13.5",
|
|
@@ -30,7 +31,6 @@ classifiers = [
|
|
|
30
31
|
"Programming Language :: Python :: 3.10",
|
|
31
32
|
"Programming Language :: Python :: 3.11",
|
|
32
33
|
"Programming Language :: Python :: 3.12",
|
|
33
|
-
"License :: OSI Approved :: MIT License",
|
|
34
34
|
"Operating System :: OS Independent",
|
|
35
35
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
36
36
|
]
|
|
@@ -151,10 +151,15 @@ minversion = "7.0"
|
|
|
151
151
|
addopts = "-ra -q"
|
|
152
152
|
pythonpath = ["src"]
|
|
153
153
|
testpaths = [
|
|
154
|
-
"tests/
|
|
155
|
-
"tests/test_color_types.py",
|
|
154
|
+
"tests/test_code.py",
|
|
156
155
|
"tests/test_color.py",
|
|
156
|
+
"tests/test_color_types.py",
|
|
157
|
+
"tests/test_console.py",
|
|
157
158
|
"tests/test_data.py",
|
|
158
|
-
"tests/
|
|
159
|
+
"tests/test_env_path.py",
|
|
160
|
+
"tests/test_file.py",
|
|
159
161
|
"tests/test_format_codes.py",
|
|
162
|
+
"tests/test_json.py",
|
|
163
|
+
"tests/test_path.py",
|
|
164
|
+
"tests/test_string.py",
|
|
160
165
|
]
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
__version__ = "1.7.0"
|
|
2
|
+
__author__ = "XulbuX"
|
|
3
|
+
__email__ = "xulbux.real@gmail.com"
|
|
4
|
+
__license__ = "MIT"
|
|
5
|
+
__copyright__ = "Copyright (c) 2024 XulbuX"
|
|
6
|
+
__url__ = "https://github.com/XulbuX/PythonLibraryXulbuX"
|
|
7
|
+
__description__ = "A library which includes a lot of really helpful functions."
|
|
8
|
+
__all__ = [
|
|
9
|
+
"_consts_", "xx_code", "xx_color", "xx_console", "xx_data", "xx_env_path", "xx_file", "xx_format_codes", "xx_json",
|
|
10
|
+
"xx_path", "xx_regex", "xx_string", "xx_system"
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
from ._consts_ import *
|
|
14
|
+
from .xx_code import *
|
|
15
|
+
from .xx_color import *
|
|
16
|
+
from .xx_console import *
|
|
17
|
+
from .xx_data import *
|
|
18
|
+
from .xx_env_path import *
|
|
19
|
+
from .xx_file import *
|
|
20
|
+
from .xx_format_codes import *
|
|
21
|
+
from .xx_json import *
|
|
22
|
+
from .xx_path import *
|
|
23
|
+
from .xx_regex import *
|
|
24
|
+
from .xx_string import *
|
|
25
|
+
from .xx_system import *
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from ._consts_ import COLOR
|
|
2
|
+
from . import __version__
|
|
3
|
+
from .xx_format_codes import FormatCodes
|
|
4
|
+
from .xx_console import Console
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def help_command():
|
|
8
|
+
"""Show some info about the library, with a brief explanation of how to use it."""
|
|
9
|
+
color = {
|
|
10
|
+
"class": COLOR.tangerine,
|
|
11
|
+
"const": COLOR.red,
|
|
12
|
+
"func": COLOR.cyan,
|
|
13
|
+
"import": COLOR.neongreen,
|
|
14
|
+
"lib": COLOR.orange,
|
|
15
|
+
"punctuators": COLOR.darkgray,
|
|
16
|
+
"code_border": COLOR.gray,
|
|
17
|
+
}
|
|
18
|
+
FormatCodes.print(
|
|
19
|
+
rf""" [_|b|#7075FF] __ __
|
|
20
|
+
[b|#7075FF] _ __ __ __/ / / /_ __ ___ __
|
|
21
|
+
[b|#7075FF] | |/ // / / / / / __ \/ / / | |/ /
|
|
22
|
+
[b|#7075FF] > , </ /_/ / /_/ /_/ / /_/ /> , <
|
|
23
|
+
[b|#7075FF]/_/|_|\____/\__/\____/\____//_/|_| [*|BG:{COLOR.gray}|#000] v[b]{__version__} [*]
|
|
24
|
+
|
|
25
|
+
[i|{COLOR.coral}]A TON OF COOL FUNCTIONS, YOU NEED![*]
|
|
26
|
+
|
|
27
|
+
[b|#FCFCFF]Usage:[*]
|
|
28
|
+
[dim|{color['code_border']}](╭────────────────────────────────────────────────────╮)
|
|
29
|
+
[dim|{color['code_border']}](│) [{color['punctuators']}]# CONSTANTS[*] [dim|{color['code_border']}](│)
|
|
30
|
+
[dim|{color['code_border']}](│) [{color['import']}]from [{color['lib']}]xulbux [{color['import']}]import [{color['const']}]COLOR[{color['punctuators']}], [{color['const']}]CHARS[{color['punctuators']}], [{color['const']}]ANSI[*] [dim|{color['code_border']}](│)
|
|
31
|
+
[dim|{color['code_border']}](│) [{color['punctuators']}]# Classes[*] [dim|{color['code_border']}](│)
|
|
32
|
+
[dim|{color['code_border']}](│) [{color['import']}]from [{color['lib']}]xulbux [{color['import']}]import [{color['class']}]Code[{color['punctuators']}], [{color['class']}]Color[{color['punctuators']}], [{color['class']}]Console[{color['punctuators']}], ...[*] [dim|{color['code_border']}](│)
|
|
33
|
+
[dim|{color['code_border']}](│) [{color['punctuators']}]# types[*] [dim|{color['code_border']}](│)
|
|
34
|
+
[dim|{color['code_border']}](│) [{color['import']}]from [{color['lib']}]xulbux [{color['import']}]import [{color['func']}]rgba[{color['punctuators']}], [{color['func']}]hsla[{color['punctuators']}], [{color['func']}]hexa[*] [dim|{color['code_border']}](│)
|
|
35
|
+
[dim|{color['code_border']}](╰────────────────────────────────────────────────────╯)
|
|
36
|
+
[b|#FCFCFF]Documentation:[*]
|
|
37
|
+
[dim|{color['code_border']}](╭────────────────────────────────────────────────────╮)
|
|
38
|
+
[dim|{color['code_border']}](│) [#DADADD]For more information see the GitHub page. [dim|{color['code_border']}](│)
|
|
39
|
+
[dim|{color['code_border']}](│) [u|#8085FF](https://github.com/XulbuX/PythonLibraryXulbuX/wiki) [dim|{color['code_border']}](│)
|
|
40
|
+
[dim|{color['code_border']}](╰────────────────────────────────────────────────────╯)
|
|
41
|
+
[_]
|
|
42
|
+
[dim](Press any key to exit...)
|
|
43
|
+
""",
|
|
44
|
+
default_color=COLOR.text
|
|
45
|
+
)
|
|
46
|
+
Console.pause_exit(pause=True)
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
from .xx_string import String
|
|
2
|
+
from .xx_regex import Regex
|
|
3
|
+
from .xx_data import Data
|
|
4
|
+
|
|
5
|
+
import regex as _rx
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class Code:
|
|
9
|
+
|
|
10
|
+
@staticmethod
|
|
11
|
+
def add_indent(code: str, indent: int) -> str:
|
|
12
|
+
"""Adds `indent` spaces at the beginning of each line."""
|
|
13
|
+
indented_lines = [" " * indent + line for line in code.splitlines()]
|
|
14
|
+
return "\n".join(indented_lines)
|
|
15
|
+
|
|
16
|
+
@staticmethod
|
|
17
|
+
def get_tab_spaces(code: str) -> int:
|
|
18
|
+
"""Will try to get the amount of spaces used for indentation."""
|
|
19
|
+
code_lines = String.get_lines(code, remove_empty_lines=True)
|
|
20
|
+
indents = [len(line) - len(line.lstrip()) for line in code_lines]
|
|
21
|
+
non_zero_indents = [i for i in indents if i > 0]
|
|
22
|
+
return min(non_zero_indents) if non_zero_indents else 0
|
|
23
|
+
|
|
24
|
+
@staticmethod
|
|
25
|
+
def change_tab_size(code: str, new_tab_size: int, remove_empty_lines: bool = False) -> str:
|
|
26
|
+
"""Replaces all tabs with `new_tab_size` spaces.\n
|
|
27
|
+
----------------------------------------------------------------------------------
|
|
28
|
+
If `remove_empty_lines` is `True`, empty lines will be removed in the process."""
|
|
29
|
+
code_lines = String.get_lines(code, remove_empty_lines=True)
|
|
30
|
+
lines = code_lines if remove_empty_lines else String.get_lines(code)
|
|
31
|
+
tab_spaces = Code.get_tab_spaces(code)
|
|
32
|
+
if (tab_spaces == new_tab_size) or tab_spaces == 0:
|
|
33
|
+
if remove_empty_lines:
|
|
34
|
+
return "\n".join(code_lines)
|
|
35
|
+
return code
|
|
36
|
+
result = []
|
|
37
|
+
for line in lines:
|
|
38
|
+
stripped = line.lstrip()
|
|
39
|
+
indent_level = (len(line) - len(stripped)) // tab_spaces
|
|
40
|
+
new_indent = " " * (indent_level * new_tab_size)
|
|
41
|
+
result.append(new_indent + stripped)
|
|
42
|
+
return "\n".join(result)
|
|
43
|
+
|
|
44
|
+
@staticmethod
|
|
45
|
+
def get_func_calls(code: str) -> list:
|
|
46
|
+
"""Will try to get all function calls and return them as a list."""
|
|
47
|
+
funcs = _rx.findall(r"(?i)" + Regex.func_call(), code)
|
|
48
|
+
nested_func_calls = []
|
|
49
|
+
for _, func_attrs in funcs:
|
|
50
|
+
nested_calls = _rx.findall(r"(?i)" + Regex.func_call(), func_attrs)
|
|
51
|
+
if nested_calls:
|
|
52
|
+
nested_func_calls.extend(nested_calls)
|
|
53
|
+
return list(Data.remove_duplicates(funcs + nested_func_calls))
|
|
54
|
+
|
|
55
|
+
@staticmethod
|
|
56
|
+
def is_js(code: str, funcs: list = ["__", "$t", "$lang"]) -> bool:
|
|
57
|
+
"""Will check if the code is very likely to be JavaScript."""
|
|
58
|
+
if not code or len(code.strip()) < 3:
|
|
59
|
+
return False
|
|
60
|
+
for func in funcs:
|
|
61
|
+
if _rx.match(r"^[\s\n]*" + _rx.escape(func) + r"\([^\)]*\)[\s\n]*$", code):
|
|
62
|
+
return True
|
|
63
|
+
direct_js_patterns = [
|
|
64
|
+
r"^[\s\n]*\$\(['\"][^'\"]+['\"]\)\.[\w]+\([^\)]*\);?[\s\n]*$", # jQuery calls
|
|
65
|
+
r"^[\s\n]*\$\.[a-zA-Z]\w*\([^\)]*\);?[\s\n]*$", # $.ajax(), etc.
|
|
66
|
+
r"^[\s\n]*\(\s*function\s*\(\)\s*\{.*\}\s*\)\(\);?[\s\n]*$", # IIFE
|
|
67
|
+
r"^[\s\n]*document\.[a-zA-Z]\w*\([^\)]*\);?[\s\n]*$", # document.getElementById()
|
|
68
|
+
r"^[\s\n]*window\.[a-zA-Z]\w*\([^\)]*\);?[\s\n]*$", # window.alert()
|
|
69
|
+
r"^[\s\n]*console\.[a-zA-Z]\w*\([^\)]*\);?[\s\n]*$", # console.log()
|
|
70
|
+
]
|
|
71
|
+
for pattern in direct_js_patterns:
|
|
72
|
+
if _rx.match(pattern, code):
|
|
73
|
+
return True
|
|
74
|
+
arrow_function_patterns = [
|
|
75
|
+
r"^[\s\n]*\b[\w_]+\s*=\s*\([^\)]*\)\s*=>\s*[^;{]*[;]?[\s\n]*$", # const x = (y) => y*2;
|
|
76
|
+
r"^[\s\n]*\b[\w_]+\s*=\s*[\w_]+\s*=>\s*[^;{]*[;]?[\s\n]*$", # const x = y => y*2;
|
|
77
|
+
r"^[\s\n]*\(\s*[\w_,\s]+\s*\)\s*=>\s*[^;{]*[;]?[\s\n]*$", # (x) => x*2
|
|
78
|
+
r"^[\s\n]*[\w_]+\s*=>\s*[^;{]*[;]?[\s\n]*$", # x => x*2
|
|
79
|
+
]
|
|
80
|
+
for pattern in arrow_function_patterns:
|
|
81
|
+
if _rx.match(pattern, code):
|
|
82
|
+
return True
|
|
83
|
+
funcs_pattern = r"(" + "|".join(_rx.escape(f) for f in funcs) + r")" + Regex.brackets("()")
|
|
84
|
+
js_indicators = [(r"\b(var|let|const)\s+[\w_$]+", 2), # JS variable declarations
|
|
85
|
+
(r"\$[\w_$]+\s*=", 2), # jQuery-style variables
|
|
86
|
+
(r"\$[\w_$]+\s*\(", 2), # jQuery function calls
|
|
87
|
+
(r"\bfunction\s*[\w_$]*\s*\(", 2), # Function declarations
|
|
88
|
+
(r"[\w_$]+\s*=\s*function\s*\(", 2), # Function assignments
|
|
89
|
+
(r"\b[\w_$]+\s*=>\s*[\{\(]", 2), # Arrow functions
|
|
90
|
+
(r"\(function\s*\(\)\s*\{", 2), # IIFE pattern
|
|
91
|
+
(funcs_pattern, 2), # Custom predefined functions
|
|
92
|
+
(r"\b(true|false|null|undefined)\b", 1), # JS literals
|
|
93
|
+
(r"===|!==|\+\+|--|\|\||&&", 1.5), # JS-specific operators
|
|
94
|
+
(r"\bnew\s+[\w_$]+\s*\(", 1.5), # Object instantiation with new
|
|
95
|
+
(r"\b(document|window|console|Math|Array|Object|String|Number)\.", 2), # JS objects
|
|
96
|
+
(r"\basync\s+function|\bawait\b", 2), # Async/await
|
|
97
|
+
(r"\b(if|for|while|switch)\s*\([^)]*\)\s*\{", 1), # Control structures with braces
|
|
98
|
+
(r"\btry\s*\{[^}]*\}\s*catch\s*\(", 1.5), # Try-catch
|
|
99
|
+
(r";[\s\n]*$", 0.5), # Semicolon line endings
|
|
100
|
+
]
|
|
101
|
+
js_score = 0
|
|
102
|
+
line_endings = [line.strip() for line in code.splitlines() if line.strip()]
|
|
103
|
+
semicolon_endings = sum(1 for line in line_endings if line.endswith(';'))
|
|
104
|
+
if semicolon_endings >= 1:
|
|
105
|
+
js_score += min(semicolon_endings, 2)
|
|
106
|
+
opening_braces = code.count('{')
|
|
107
|
+
closing_braces = code.count('}')
|
|
108
|
+
if opening_braces > 0 and opening_braces == closing_braces:
|
|
109
|
+
js_score += 1
|
|
110
|
+
for pattern, score in js_indicators:
|
|
111
|
+
regex = _rx.compile(pattern, _rx.IGNORECASE)
|
|
112
|
+
matches = regex.findall(code)
|
|
113
|
+
if matches:
|
|
114
|
+
js_score += len(matches) * score
|
|
115
|
+
return js_score >= 2
|