-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerators.py
166 lines (147 loc) · 5.61 KB
/
generators.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
# Generators module
# Copyright (C) 2014 Musikhin Andrey <melomansegfault@gmail.com>
import os
from subprocess import Popen, PIPE
from pprint import pformat
class CmdDictGenerator(object):
""" Command list dictionary generator """
def __init__(self, mplayer='mplayer'):
self._cmdlist = [line for line in Popen(
[mplayer, '-input', 'cmdlist'], stdout=PIPE).stdout][:-1]
self._doc_file = open(
os.path.join(os.path.dirname(os.path.abspath(__file__)),
'data/mplayer_cmdlist_docs'), 'r').readlines()
self._cmdlist_dict = {}
def _get_cmd_dict(self):
""" Convert txt file to python dictionary """
# Creating the comments dictionary
doc_string = 'MPlayer doc:\n'
doc_key = ''
doc_dict = {}
for line in self._doc_file:
if line == '\n':
doc_dict[doc_key] = doc_string
doc_string = 'MPlayer doc: '
continue
if line[0] == ' ':
doc_string += line[4:]
else:
doc_key = line.strip().split(' ')[0]
doc_string += line
# Creating the command dictionary
cmds_dict = {}
for line in self._cmdlist:
line_dict = {}
list_line = line.split()
line_dict['command'] = list_line[0]
if list_line[0] not in doc_dict.keys():
line_dict['comment'] = self._new_cpt(line)[0]
line_dict['pycommand'] = self._new_cpt(line)[1]
line_dict['types'] = self._new_cpt(line)[2]
else:
line_dict['comment'] = '%s\n%s' % (self._new_cpt(line)[0],
doc_dict[list_line[0]])
line_dict['pycommand'] = self._new_cpt(line)[1]
line_dict['types'] = self._new_cpt(line)[2]
cmds_dict[list_line[0]] = line_dict
self._cmdlist_dict = cmds_dict
def _new_cpt(self, comment):
"""
Return mplayer command string, python method string and types list
"""
comment_list = comment.split()
_comment = ' '.join(comment_list)
types = []
_python_comment = ''
for num, item in enumerate(comment_list[1:]):
if 'Integer' in item:
string = 'int'
elif 'String' in item:
string = 'str'
elif 'Float' in item:
string = 'float'
else: pass
types.append(string)
if '[' in item and num!=0:
string = '[, <%s>]' % string
elif '[' in item and num==0:
string = '[<%s>]' % string
elif num != 0: string = ', <%s>' % string
elif num == 0: string = '<%s>' % string
_python_comment += string
command_comment = 'MPlayer command: %s' % _comment
python_comment = '%s(%s)' % (comment_list[0], _python_comment)
return [command_comment, python_comment, types]
def get_cmdlist(self):
self._get_cmd_dict()
return self._cmdlist_dict
class PropDictGenerator(object):
'''
Generates a dictionary of MPlayer properties
'''
def __init__(self):
self.txt_file = open(
os.path.join(os.path.dirname(os.path.abspath(__file__)),
'data/mplayer_properties'), 'r').readlines()
def _get_prop_dict(self):
'''
Take and parse method. Return the properties dictionary
'''
def convert_min_max(string):
# Converting max/min values
try:
if '.' in string:
value = float(string)
return value
else:
value = int(string)
return value
except: return False
def convert_type(string):
# Converting type string
if 'pos' in string:
string = 'int'
if 'time' in string:
string = 'float'
if 'str list' in string:
string = 'str'
if 'string' in string:
string = 'str'
return string
property_dict = {}
for line in self.txt_file:
command = line[:19].strip()
if command:
prop_dict = {
'command': command,
'type': convert_type(line[19:29].strip()),
'min': convert_min_max(line[29:37].strip()),
'max': convert_min_max(line[37:45].strip()),
'get': line[45:49].strip()=='X',
'set': line[49:53].strip()=='X',
'step': line[53:58].strip()=='X',
'comment': line[58:].strip(),
}
else:
command = prop_dict['command']
prop_dict['comment']+= '\n' + line[58:].strip()
property_dict[command] = prop_dict
print property_dict
return property_dict
def run(self):
'''
Run generator method
'''
output_prop_file = open(
os.path.join(os.path.dirname(os.path.abspath(__file__)),
'player_properties.py'), 'w')
text = 'property_dict = %s'
output_prop_file.write(text % pformat(self._get_prop_dict()))
output_prop_file.close()
if __name__ == '__main__':
DG = CmdDictGenerator()
print pformat(DG.get_cmdlist())
PG = PropDictGenerator()
PG.run()