1
1
#!/usr/bin/env python3
2
+ #
3
+ # Copyright 2025 ROBOTIS CO., LTD.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the 'License');
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an 'AS IS' BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+ #
17
+ # Author: Woojin Wie
18
+
19
+
2
20
"""
3
21
Script to check formatting of Dynamixel model files.
4
22
15
33
--verbose Show detailed information about each file
16
34
"""
17
35
36
+ import argparse
18
37
import os
19
38
import sys
20
- import argparse
21
- import re
39
+
22
40
23
41
def check_file_formatting (file_path , fix = False , verbose = False ):
24
42
"""
@@ -36,19 +54,19 @@ def check_file_formatting(file_path, fix=False, verbose=False):
36
54
'trailing_spaces' : [],
37
55
'no_eof_newline' : False ,
38
56
'empty_lines_at_end' : 0 ,
39
- 'fixed' : []
57
+ 'fixed' : [],
40
58
}
41
59
42
60
try :
43
61
with open (file_path , 'r' , encoding = 'utf-8' ) as f :
44
62
lines = f .readlines ()
45
63
except Exception as e :
46
- print (f" Error reading { file_path } : { e } " )
64
+ print (f' Error reading { file_path } : { e } ' )
47
65
return issues
48
66
49
67
if not lines :
50
68
if verbose :
51
- print (f" { file_path } : Empty file" )
69
+ print (f' { file_path } : Empty file' )
52
70
return issues
53
71
54
72
# Check for trailing spaces
@@ -74,68 +92,80 @@ def check_file_formatting(file_path, fix=False, verbose=False):
74
92
issues ['empty_lines_at_end' ] = empty_lines_at_end
75
93
76
94
# Fix issues if requested
77
- if fix and (issues ['trailing_spaces' ] or issues ['no_eof_newline' ] or issues ['empty_lines_at_end' ] > 1 ):
95
+ if fix and (
96
+ issues ['trailing_spaces' ]
97
+ or issues ['no_eof_newline' ]
98
+ or issues ['empty_lines_at_end' ] > 1
99
+ ):
78
100
fixed_lines = []
79
101
80
102
for i , line in enumerate (lines ):
81
103
# Remove trailing spaces but preserve newline
82
104
if i + 1 in issues ['trailing_spaces' ]:
83
105
line_content = line .rstrip ('\n ' )
84
106
fixed_lines .append (line_content .rstrip () + '\n ' )
85
- issues ['fixed' ].append (f"Line { i + 1 } : Removed trailing spaces" )
107
+ issues ['fixed' ].append (
108
+ f'Line { i + 1 } : Removed trailing spaces' )
86
109
else :
87
110
fixed_lines .append (line )
88
111
89
112
# Ensure proper EOF
90
113
if issues ['no_eof_newline' ]:
91
114
if fixed_lines and not fixed_lines [- 1 ].endswith ('\n ' ):
92
115
fixed_lines [- 1 ] = fixed_lines [- 1 ] + '\n '
93
- issues ['fixed' ].append (" Added EOF newline" )
116
+ issues ['fixed' ].append (' Added EOF newline' )
94
117
95
118
# Remove excessive empty lines at the end
96
119
if issues ['empty_lines_at_end' ] > 1 :
97
120
# Remove all empty lines at the end, then add one
98
121
while fixed_lines and fixed_lines [- 1 ].strip () == '' :
99
122
fixed_lines .pop ()
100
123
fixed_lines .append ('\n ' ) # Add single newline at end
101
- issues ['fixed' ].append (f"Removed { issues ['empty_lines_at_end' ] - 1 } excessive empty lines at end" )
124
+ issues ['fixed' ].append (
125
+ f'Removed { issues ["empty_lines_at_end" ] - 1 } excessive empty lines at end'
126
+ )
102
127
103
128
# Write fixed file
104
129
try :
105
130
with open (file_path , 'w' , encoding = 'utf-8' ) as f :
106
131
f .writelines (fixed_lines )
107
132
except Exception as e :
108
- print (f" Error writing { file_path } : { e } " )
133
+ print (f' Error writing { file_path } : { e } ' )
109
134
return issues
110
135
111
136
return issues
112
137
138
+
113
139
def main ():
114
- """Main function ."""
140
+ """Run the formatting check or fix process for Dynamixel model files ."""
115
141
parser = argparse .ArgumentParser (
116
- description = " Check formatting of Dynamixel model files" ,
142
+ description = ' Check formatting of Dynamixel model files' ,
117
143
formatter_class = argparse .RawDescriptionHelpFormatter ,
118
144
epilog = """
119
145
Examples:
120
146
python3 check_model_file_formatting.py
121
147
python3 check_model_file_formatting.py --fix
122
148
python3 check_model_file_formatting.py --verbose
123
149
python3 check_model_file_formatting.py --fix --verbose
124
- """
150
+ """ ,
125
151
)
126
152
127
- parser .add_argument ('--fix' , action = 'store_true' ,
128
- help = 'Automatically fix formatting issues' )
129
- parser .add_argument ('--verbose' , action = 'store_true' ,
130
- help = 'Show detailed information about each file' )
153
+ parser .add_argument (
154
+ '--fix' , action = 'store_true' , help = 'Automatically fix formatting issues'
155
+ )
156
+ parser .add_argument (
157
+ '--verbose' ,
158
+ action = 'store_true' ,
159
+ help = 'Show detailed information about each file' ,
160
+ )
131
161
132
162
args = parser .parse_args ()
133
163
134
164
# Find model files
135
- model_dir = " param/dxl_model"
165
+ model_dir = ' param/dxl_model'
136
166
if not os .path .exists (model_dir ):
137
167
print (f"Error: Model directory '{ model_dir } ' not found." )
138
- print (" Please run this script from the dynamixel_hardware_interface directory." )
168
+ print (' Please run this script from the dynamixel_hardware_interface directory.' )
139
169
sys .exit (1 )
140
170
141
171
model_files = []
@@ -144,12 +174,12 @@ def main():
144
174
model_files .append (os .path .join (model_dir , file ))
145
175
146
176
if not model_files :
147
- print (" No .model files found." )
177
+ print (' No .model files found.' )
148
178
sys .exit (0 )
149
179
150
- print (f" Checking { len (model_files )} model files..." )
180
+ print (f' Checking { len (model_files )} model files...' )
151
181
if args .fix :
152
- print (" Auto-fix mode enabled." )
182
+ print (' Auto-fix mode enabled.' )
153
183
print ()
154
184
155
185
total_issues = 0
@@ -159,45 +189,53 @@ def main():
159
189
file_name = os .path .basename (file_path )
160
190
issues = check_file_formatting (file_path , args .fix , args .verbose )
161
191
162
- has_issues = (issues ['trailing_spaces' ] or
163
- issues ['no_eof_newline' ] or
164
- issues ['empty_lines_at_end' ] > 1 )
192
+ has_issues = (
193
+ issues ['trailing_spaces' ]
194
+ or issues ['no_eof_newline' ]
195
+ or issues ['empty_lines_at_end' ] > 1
196
+ )
165
197
166
198
if has_issues :
167
199
files_with_issues += 1
168
- print (f" ❌ { file_name } " )
200
+ print (f' ❌ { file_name } ' )
169
201
170
202
if issues ['trailing_spaces' ]:
171
- print (f" Trailing spaces on lines: { ', ' .join (map (str , issues ['trailing_spaces' ]))} " )
203
+ print (
204
+ f' Trailing spaces on lines: \
205
+ { ", " .join (map (str , issues ["trailing_spaces" ]))} '
206
+ )
172
207
total_issues += len (issues ['trailing_spaces' ])
173
208
174
209
if issues ['no_eof_newline' ]:
175
- print (" Missing EOF newline" )
210
+ print (' Missing EOF newline' )
176
211
total_issues += 1
177
212
178
213
if issues ['empty_lines_at_end' ] > 1 :
179
- print (f" { issues ['empty_lines_at_end' ]} empty lines at end of file" )
214
+ print (
215
+ f' { issues ["empty_lines_at_end" ]} empty lines at end of file' )
180
216
total_issues += 1
181
217
182
218
if args .fix and issues ['fixed' ]:
183
- print (" Fixed:" )
219
+ print (' Fixed:' )
184
220
for fix in issues ['fixed' ]:
185
- print (f" - { fix } " )
221
+ print (f' - { fix } ' )
186
222
187
223
print ()
188
224
elif args .verbose :
189
- print (f" ✅ { file_name } - No issues found" )
225
+ print (f' ✅ { file_name } - No issues found' )
190
226
191
227
# Summary
192
- print ("=" * 50 )
228
+ print ('=' * 50 )
193
229
if files_with_issues == 0 :
194
- print (" 🎉 All model files are properly formatted!" )
230
+ print (' 🎉 All model files are properly formatted!' )
195
231
else :
196
- print (f"Found { total_issues } formatting issues in { files_with_issues } files." )
232
+ print (
233
+ f'Found { total_issues } formatting issues in { files_with_issues } files.' )
197
234
if not args .fix :
198
- print (" Run with --fix to automatically fix these issues." )
235
+ print (' Run with --fix to automatically fix these issues.' )
199
236
200
237
return 0 if files_with_issues == 0 else 1
201
238
202
- if __name__ == "__main__" :
203
- sys .exit (main ())
239
+
240
+ if __name__ == '__main__' :
241
+ sys .exit (main ())
0 commit comments