-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_naming.py
More file actions
62 lines (49 loc) · 2.07 KB
/
code_naming.py
File metadata and controls
62 lines (49 loc) · 2.07 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
#!/usr/local/bin/python3.11
from pathlib import Path
import os, sys, re
# Validate the amount of command line arguments
if len(sys.argv) != 2:
print('Please enter a command that follows this convention: code_naming.py topic_name.ext')
sys.exit(1)
# Database for a file's attributes based on its extension
languages = {
'.py': ['Python', 'python.py'],
'.html': ['HTML', 'html.html'],
'.css': ['CSS', 'css.css'],
'.cpp': ['CPP', 'cpp.cpp'],
'.java': ['Java', 'java.java'],
'.js': ['Javascript', 'javascript.js'],
'.swift': ['iOS', 'swift.swift']
}
# Get the file's name. Validate file's name.
target_file = sys.argv[1]
file_pattern = r'([A-Za-z\d\s]+)_([A-Za-z\d\s]+)(\.(py|html|java|cpp|css|js|swift))$'
file_validation = re.fullmatch(file_pattern, target_file)
if not file_validation:
print('Please use a file name that conforms to the following convention: topic_name.ext\n'
'- These are the available ext types: py, html, java, cpp, css, js, swift\n'
'- Don\'t include whitespace at the end of the command')
sys.exit(1)
# Get the file's type (ext) and topic
file_ext = file_validation[3]
topic = file_validation[1]
# Create the path for the language folder that the file will go in
language_subdirectory = languages[file_ext][0]
language_directory = Path(f'/xxx/{language_subdirectory}')
os.chdir(language_directory)
# Create the path for the file
os.makedirs(topic, exist_ok=True)
target_file_path = Path(topic) / target_file
# Create the path for the file that contains the applicable template
template_name = languages[file_ext][1]
template_path = f'/xxx/template_{template_name}'
template_path = Path(template_path)
# Copy the template to the file if it doesn't exist. Otherwise, just open the file
if not target_file_path.exists():
with open(template_path, 'r') as file:
template = file.readlines()
with open(target_file_path, 'w') as file:
file.writelines(template)
else:
print('This file path already exists. Opening now ...')
os.system(f'chmod +x {target_file_path} && open -a \'xxx\' {topic}')