-
Notifications
You must be signed in to change notification settings - Fork 0
/
latte_main.py
81 lines (61 loc) · 2.41 KB
/
latte_main.py
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
#!/usr/bin/env python3
import os
import re
import subprocess
def count_lines(file_path):
with open(file_path, 'r') as file:
return len(file.readlines())
def count_variables(file_path):
with open(file_path, 'r') as file:
content = file.read()
# This regex pattern matches variable assignments
pattern = r'\b[a-zA-Z_][a-zA-Z0-9_]*\s*='
return len(re.findall(pattern, content))
def install_packages(file_path):
with open(file_path, 'r') as file:
content = file.read()
# This regex pattern matches import statements
pattern = r'(?:from\s+(\w+)(?:\.\w+)*\s+)?import\s+(\w+|\*)'
packages = set(match[0] or match[1] for match in re.findall(pattern, content))
for package in packages:
subprocess.run(['pip', 'install', package])
print(f"Installed {len(packages)} packages.")
def count_functions(file_path):
with open(file_path, 'r') as file:
content = file.read()
# This regex pattern matches function definitions
pattern = r'\bdef\s+[a-zA-Z_][a-zA-Z0-9_]*\s*\('
return len(re.findall(pattern, content))
def print_help():
print("Available commands:")
print("ln - Returns the number of lines of code in the file")
print("xn - Returns the number of variables in the code")
print("pi - Installs all the packages in the file")
print("fn - Returns the number of functions")
print("help - Displays this help message")
print("exit - Exits the program")
def main():
print("Welcome to Latte!")
file_name = input("Enter the full path of the Python file you're editing: ")
if not os.path.exists(file_name):
print(f"Error: File '{file_name}' not found.")
return
print("Type 'help' for a list of commands.")
while True:
command = input("latte> ").strip().lower()
if command == 'exit':
break
elif command == 'help':
print_help()
elif command == 'ln':
print(f"Number of lines: {count_lines(file_name)}")
elif command == 'xn':
print(f"Number of variables: {count_variables(file_name)}")
elif command == 'pi':
install_packages(file_name)
elif command == 'fn':
print(f"Number of functions: {count_functions(file_name)}")
else:
print("Unknown command. Type 'help' for a list of commands.")
if __name__ == "__main__":
main()