forked from in4lio/ev3dev-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
43 lines (34 loc) · 1.23 KB
/
tools.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
# -*- coding: utf-8 -*-
# ____ __ ____ ___ ____ __ (((((()
# | |_ \ \ / ) ) | | ) | |_ \ \ / \(@)- /
# |_|__ \_\/ __)_) |_|_/ |_|__ \_\/ /(@)- \
# ((())))
import os, re
def clip_h( fn ):
M_CUT = '* \\{'
RE_CUT = re.compile( r'^\s*\*\s*\\\{' )
M_CUT_END = '/** \\} */'
RE_CUT_END = re.compile( r'^\s*/\*\*\s*\\\}\s*\*/' )
M_IMPORT = '($import "%sev3_'
try:
with open( fn, 'r' ) as f:
txt = f.readlines()
except:
raise Exception( '* ERROR * File "%s" not found.' % ( fn ))
b = 0;
for i, ln in enumerate( txt ):
if not b:
# Cut from the 2nd line after
# * \{
if RE_CUT.match( ln ):
b = i + 2
else:
# ... up to
# /** \} */
if RE_CUT_END.match( ln ):
e = i
break
else:
raise Exception( '* ERROR * File "%s": label "%s" not found.' % ( fn, M_CUT if not b else M_CUT_END ))
# ... and add the directory name to each ($import) of 'ev3_' files.
return ''.join( txt[ b : e ]).replace( M_IMPORT % ( '' ), M_IMPORT % ( os.path.dirname( fn ) + '/' ))