-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathgetTaxi-v3.py
More file actions
98 lines (88 loc) · 2.6 KB
/
Copy pathgetTaxi-v3.py
File metadata and controls
98 lines (88 loc) · 2.6 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/python
import sys, string, urllib2
# strip out quotes, white-spaces, colon and dash
strip_out = '-:\"\' '
# for test purpose
yaml_temp = '''---
foo: "bar"
baz:
- "qux"
- "quxx"
corge: null
grault: 1
garply: true
waldo: "false"
fred: "undefined"
emptyArray: []
emptyObject: {}
emptyString: ""
strakovich:
iliya:
- 328800065
- 1975'''
# open an URL
response = urllib2.urlopen('https://gist.githubusercontent.com/dmitrypa/ee74d809505bf6c69f6f/raw/01e84c2bcb791153346b57cca4e4d2751b8ca111/example.yml')
yaml_temp = response.read()
# convert string to list delimited by new-lines
yaml_temp = yaml_temp.split('\n')
# check if it's YAML
if yaml_temp.pop(0) != '---':
sys.exit()
# wipe out comments and empty lines
yaml = []
for i in yaml_temp:
if not (i == '' or i.startswith('#')):
yaml.append(i)
del yaml_temp
json = []
for index in range(len(yaml)):
item = yaml[index].split(':')
key = item[0].strip(strip_out)
# add list
if len(item) == 1:
json[anchor] += '"' + key + '"' + ', '
# add key
elif len(item) == 2 and (len(item[1]) == 0 or item[1] is ' '):
try:
json[anchor] += '"' + key + '"' + ':'
except:
json.append('"' + key + '"' + ':')
# anchor to know where to appennd nested elements
anchor = len(json) - 1
# add whole item
else:
val = item[1].strip(strip_out)
if val == '{}': # preserve dictionary type
val = '{}'
elif val == '[]': # preserve list type
val = '[]'
elif val == 'true': # preserve spec data type type
val = 'true'
elif val == 'false': # preserve spec data type type
val = 'false'
elif val == 'null': # preserve spec data type type
val = 'null'
else:
val = '"' + val + '"'
json.append('"' + key + '"' + ': ' + val)
try:
del anchor
except:
continue
# put in order nested elements
for ind in range(len(json)):
if json[ind].endswith(', '):
lst = json[ind].split(':')
mod_list = lst[:-1]
lst[-1] = '[' + lst[-1].rstrip(', ') + ']'
json[ind] = ':'.join(lst)
if json[ind].count(':') >= 2:
index = json[ind].find(':') + 1
json[ind] = json[ind][:index] + '{ ' + json[ind][index:] + ' }'
# Strings should be wrapped in double quotes
trantab = string.maketrans("'", ' ') # create translation table
json = str(json).translate(trantab)
# print out to the file
json = '{ ' + json.strip('[ ]') + ' }'
with open('gett.json', 'w') as file:
file.write(json)