-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindnivas.py
More file actions
80 lines (67 loc) · 2.12 KB
/
findnivas.py
File metadata and controls
80 lines (67 loc) · 2.12 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
import re
import sys, os
import getopt
def transform(inFile, outFile):
'''
With a java file as input produces a python file as output
'''
processed={}
processed['elem'] = False
processed['niva'] = False
nextElement = ''
with open(inFile, 'r') as iFile:
with open(outFile, 'w+') as oFile:
for oldLine in iFile:
line = oldLine
match = re.search(r'<idFactura>(.*)</idFactura>', line)
if match and not processed['elem']:
nextElement += re.sub(r'^(\s*)<idFactura>(.*)</idFactura>(\s*)$', r'\2', line) + ';'
processed['elem'] = True
continue
match = re.search(r'<nivaEmisor>(.*)</nivaEmisor>(\s*)$', line)
if match and not processed['niva']:
nextElement += re.sub(r'(\s*)<nivaEmisor>(.*)</nivaEmisor>', r'\2', line)
processed['niva'] = True
continue
if processed['elem'] and processed['niva']:
oFile.write(nextElement)
nextElement = ''
for key in processed:
processed[key]= False
def processFile(filename):
'''
Processes a single file
'''
# This line makes a regular expression with re.compile() to capture a java file name
# and then sub() replaces the extension in it with "py"
outFilename = re.compile(r'(?P<name>.*)\.(?P<ext>\w*)$', re.I).sub(r'\g<name>_out.csv', filename)
transform(filename, outFilename)
def main(argv):
'''
First we process options, then we do the code translation from java to python
'''
# BASE_DIR is the current working directory
BASE_DIR = os.getcwd()
try:
# Options and arguments processing
print('processing opts')
opts, args = getopt.getopt(argv, "f:", ["file", ])
print('opts processed: '+ str(opts))
print('args processed: '+str(args))
except getopt.GetoptError:
# Errors with options and arguments
print('Invalid usage')
print('Proper usage is findnivas -f fileName ')
sys.exit(-1)
# Proceeding to the code translation
for opt,val in opts:
print('value is '+val)
if opt in ['-f', '--file']:
print('Entra con '+BASE_DIR)
processFile(os.path.join(BASE_DIR, val))
else:
os.chdir(BASE_DIR)
processFile(os.path.join(BASE_DIR, val))
if __name__ == "__main__":
main(sys.argv[1:])