Added more verbose error logging to flv conversion script, and ensured that the ...
[recordtv.git] / src / rtv_abstractpropertiesfile.py
blob0d6ab64121b0dbcb7962c12bb2911d76db7a7331
1 #!/usr/bin/python
3 import datetime, time, re, os
4 import rtv_utils
6 DATETIME_FORMAT = "%Y-%m-%d-%H:%M"
7 datetime_re = re.compile( "\\d{4}-\\d{2}-\\d{2}-\\d{2}:\\d{2}" )
9 class AbstractPropertiesFile( object ):
11 def load( self, filename ):
12 f = file( filename, "r" )
14 for ln in f:
15 ln_split = ln[:-1].split( "=", 1 )
16 len_ln_split = len( ln_split )
18 if len_ln_split > 0:
19 if len( ln_split ) == 2:
20 ( key, value ) = ln_split
21 else:
22 key = ln_split[0]
23 value = ""
24 key = key.strip()
25 value = value.strip()
26 if datetime_re.match( value ):
27 self.set_value( key, rtv_utils.parse_datetime( value,
28 DATETIME_FORMAT ) )
29 elif len( value ) == 0:
30 self.set_value( key, value )
31 elif value[0] == "|":
32 self.set_value( key, value[1:].split( "|" ) )
33 elif value[:2] == "_|" or value == "_False" or value == "_True":
34 self.set_value( key, value[1:] )
35 elif value == "False":
36 self.set_value( key, False )
37 elif value == "True":
38 self.set_value( key, True )
39 else:
40 try:
41 self.set_value( key, int(value) )
42 except ValueError:
43 try:
44 self.set_value( key, float(value) )
45 except ValueError:
46 self.set_value( key, value )
48 f.close()
50 def save( self, filename ):
52 f = file( filename, "w" )
53 for k in self.get_keys():
54 if k[0] != "_":
55 k = self._prepare_key( k )
56 v = self._prepare_value( self.get_value( k ) )
58 if v != None:
59 f.write( rtv_utils.encode_text( k ) )
60 f.write( "=" )
61 f.write( rtv_utils.encode_text( v ) )
62 f.write( "\n" )
64 f.close()
66 def _prepare_key( self, key ):
67 return key.replace( "=", "_" )
69 def _prepare_value( self, value ):
70 tp = type( value )
72 if value == None or value == "":
73 return None
74 elif tp == int or tp == float:
75 return str( value )
76 elif tp == str or tp == unicode:
77 if len(value) > 0 and value[0] == "|":
78 value = "_|" + value[1:]
79 elif value == "False":
80 value = "_False"
81 elif value == "True":
82 value = "_True"
83 return value
84 elif tp == tuple or tp == list:
85 if len( value ) == 0:
86 return None
87 else:
88 ret = ""
89 for v in value:
90 vtp = type( v )
91 if vtp == int or vtp == float:
92 ret += "|"
93 ret += str( v )
94 elif vtp == str or vtp == unicode:
95 ret += "|"
96 ret += v.replace( "|", "_" )
97 else:
98 ret += "|<Unknown list type %s>>" % vtp
99 return ret
100 elif tp == datetime.datetime:
101 return value.strftime( DATETIME_FORMAT )
102 elif tp == bool:
103 return str( value )
104 else:
105 return "<<Unknown type %s>>" % tp
107 def set_value( self, key, value ):
108 raise Exception( "Method not implemented." )
110 def get_value( self, key ):
111 raise Exception( "Method not implemented." )
113 def get_keys( self ):
114 raise Exception( "Method not implemented." )