3 import datetime
, time
, re
, os
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" )
15 ln_split
= ln
[:-1].split( "=", 1 )
16 len_ln_split
= len( ln_split
)
19 if len( ln_split
) == 2:
20 ( key
, value
) = ln_split
26 if datetime_re
.match( value
):
27 self
.set_value( key
, rtv_utils
.parse_datetime( value
,
29 elif len( value
) == 0:
30 self
.set_value( key
, value
)
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 )
38 self
.set_value( key
, True )
41 self
.set_value( key
, int(value
) )
44 self
.set_value( key
, float(value
) )
46 self
.set_value( key
, value
)
50 def save( self
, filename
):
52 f
= file( filename
, "w" )
53 for k
in self
.get_keys():
55 k
= self
._prepare
_key
( k
)
56 v
= self
._prepare
_value
( self
.get_value( k
) )
59 f
.write( rtv_utils
.encode_text( k
) )
61 f
.write( rtv_utils
.encode_text( v
) )
66 def _prepare_key( self
, key
):
67 return key
.replace( "=", "_" )
69 def _prepare_value( self
, value
):
72 if value
== None or value
== "":
74 elif tp
== int or tp
== float:
76 elif tp
== str or tp
== unicode:
77 if len(value
) > 0 and value
[0] == "|":
78 value
= "_|" + value
[1:]
79 elif value
== "False":
84 elif tp
== tuple or tp
== list:
91 if vtp
== int or vtp
== float:
94 elif vtp
== str or vtp
== unicode:
96 ret
+= v
.replace( "|", "_" )
98 ret
+= "|<Unknown list type %s>>" % vtp
100 elif tp
== datetime
.datetime
:
101 return value
.strftime( DATETIME_FORMAT
)
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." )