root/trunk/libffado/support/mixer-qt4/ffado/configuration.py

Revision 1640, 4.5 kB (checked in by arnonym, 12 years ago)

Make it more pythonic.

All ffado stuff is now in packages getting installed to the official python dirs. Ideally this would allow other apps to use the stuff from us.

Maybe the generic widgets (that are used by multiple mixers) should go into ffado/widgets...

  • Property svn:mergeinfo set to
Line 
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2008-2009 by Arnold Krille
4 #
5 # This file is part of FFADO
6 # FFADO = Free Firewire (pro-)audio drivers for linux
7 #
8 # FFADO is based upon FreeBoB.
9 #
10 # This program is free software: you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation, either version 3 of the License, or
13 # (at your option) any later version.
14 #
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 # GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License
21 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 #
23
24 import re, os
25
26 import shlex
27
28 import logging
29 log = logging.getLogger('configparser')
30
31 class DeviceList:
32         def __init__( self, filename="" ):
33                 self.devices = list()
34                 if not filename == "":
35                         self.updateFromFile( filename )
36
37         def updateFromFile( self, filename, must_exist=False):
38                 filename = os.path.expanduser(filename)
39                 log.debug("DeviceList::updateFromFile(%s)" % filename)
40                 if not os.path.exists( filename ):
41                         if must_exist:
42                                 log.error("cannot open file")
43                         return
44                 f = open( filename, "r" )
45
46                 lex = Parser( f )
47                 config = {}
48                 while lex.peaktoken() != lex.eof:
49                         tmp = lex.parsenamedvalue()
50                         if tmp != None:
51                                 config[ tmp[0] ] = tmp[1]
52                 for dev in config["device_definitions"]:
53                         self.addDevice( dev )
54
55         def getDeviceById( self, vendor, model ):
56                 log.debug("DeviceList::getDeviceById( %s, %s )" % (vendor, model ))
57                 for dev in self.devices:
58                         if int("%s" % dev['vendorid'], 0) == int("%s" % vendor, 0) and \
59                            int("%s" % dev['modelid'], 0) == int("%s" % model, 0):
60                                 return dev
61                 tmp = dict()
62                 self.devices.append( tmp )
63                 return tmp
64
65         def addDevice( self, device_dict ):
66                 log.debug("DeviceList::addDevice()")
67                 dev = self.getDeviceById( device_dict['vendorid'], device_dict['modelid'] )
68                 dev.update( device_dict )
69
70 class Parser:
71         def __init__( self, file ):
72                 self.lex = shlex.shlex( file )
73                 self.eof = self.lex.eof
74
75         def peaktoken( self ):
76                 token = self.lex.get_token()
77                 self.lex.push_token( token )
78                 return token
79
80         def parselist( self, level="" ):
81                 token = self.peaktoken()
82                 if token != "(":
83                         return
84                 self.lex.get_token()
85                 log.debug("%sWill parse list" % level)
86                 ret = []
87                 token = self.peaktoken()
88                 while token != ")":
89                         ret.append( self.parsenamedvalue( level+"  " ) )
90                         token = self.peaktoken()
91                 log.debug("%slist is %s" % (level, str(ret)))
92                 self.lex.get_token()
93                 if self.peaktoken() == ",":
94                         log.debug("%sFound a delimiter" % level)
95                         self.lex.get_token()
96                 return ret
97
98         def parsemap( self, level="" ):
99                 token = self.lex.get_token()
100                 if token != "{":
101                         return
102                 log.debug("%sWill parse map" % level)
103                 ret = {}
104                 token = self.peaktoken()
105                 while token != "}":
106                         #self.push_token( token )
107                         tmp = self.parsenamedvalue( level+"  " )
108                         if tmp != None:
109                                 ret[ tmp[0] ] = tmp[1]
110                         token = self.peaktoken()
111                 token = self.lex.get_token()
112                 log.debug("%sMap ended with '%s' and '%s'"% (level,token,self.peaktoken()))
113                 if self.peaktoken() in (",",";"):
114                         log.debug("%sFound a delimiter!" % level)
115                         self.lex.get_token()
116                 return ret
117
118         def parsevalue( self, level="" ):
119                 token = self.lex.get_token()
120                 log.debug("%sparsevalue() called on token '%s'" % (level, token))
121                 self.lex.push_token( token )
122                 if token == "(":
123                         value = self.parselist( level+"  " )
124                 elif token == "{":
125                         value = self.parsemap( level+"  " )
126                 else:
127                         value = self.lex.get_token().replace( "\"", "" )
128                 token = self.peaktoken()
129                 if token == ";":
130                         log.debug("%sFound a delimiter!" % level)
131                         self.lex.get_token()
132                 return value
133
134         def parsenamedvalue( self, level="" ):
135                 token = self.lex.get_token()
136                 log.debug("%sparsenamedvalue() called on token '%s'" % (level, token))
137                 if token == "{":
138                         self.lex.push_token( token )
139                         return self.parsemap( level+"  " )
140                 if len(token) > 0 and token not in ("{","}","(",")",",",";"):
141                         log.debug("%sGot name '%s'" %(level,token))
142                         name = token
143                         token = self.lex.get_token()
144                         if token in ("=",":"):
145                                 #print "%sWill parse value" % level
146                                 value = self.parsevalue( level )
147                                 return (name,value)
148                 log.debug("%sparsenamedvalue() will return None!" % level)
149                 return
150
151 #
152 # Have kind of a test directly included...
153 #
154 if __name__ == "__main__":
155
156         import sys
157
158         file = sys.argv[1]
159
160         log.setLevel(logging.DEBUG)
161         devs = DeviceList( file )
162
163         print devs.devices
164
165 # vim: et
Note: See TracBrowser for help on using the browser.