<s> import re , os , time , codecs , sys , _strptime <EOL> if sys . version < '<STR_LIT:3>' : <EOL> from task import * <EOL> from c import * <EOL> else : <EOL> from . task import * <EOL> from . c import * <EOL> class TodoDbFile ( ) : <EOL> name = '<STR_LIT>' <EOL> dbOk = True <EOL> lastId = None <EOL> maxId = <NUM_LIT:0> <EOL> maxIdSaved = <NUM_LIT:0> <EOL> settings = None <EOL> parentDB = False <EOL> dbId = None <EOL> def __init__ ( self , _parentDB , _settings , _dbId ) : <EOL> self . settings = _settings <EOL> self . parentDB = _parentDB <EOL> self . dbId = _dbId <EOL> def flush ( self ) : <EOL> if not self . parentDB . todoAInited : <EOL> return True <EOL> if not self . dbOk : <EOL> print ( "<STR_LIT>" ) <EOL> return False <EOL> dirty = False <EOL> for iT in self . parentDB . todoA : <EOL> curTodo = self . parentDB . todoA [ iT ] <EOL> if curTodo . savePending ( self . dbId ) : <EOL> dirty = True <EOL> if dirty or self . maxIdSaved != self . maxId : <EOL> try : <EOL> with codecs . open ( self . settings . file , '<STR_LIT>' , '<STR_LIT>' ) as f : <EOL> f . write ( self . settings . head ) <EOL> f . write ( "<STR_LIT:\n>" ) <EOL> for iT in sorted ( self . parentDB . todoA ) : <EOL> curTodo = self . parentDB . todoA [ iT ] <EOL> if curTodo . initial : <EOL> continue <EOL> stateSign = curTodo . state <EOL> if stateSign == '<STR_LIT>' : stateSign = '<STR_LIT:->' <EOL> lvl = curTodo . lvl <EOL> if curTodo . lvl >= <NUM_LIT:0> : lvl = '<STR_LIT:+>' + str ( curTodo . lvl ) <EOL> gmtTime = time . localtime ( curTodo . stamp ) <EOL> f . write ( stateSign + '<STR_LIT:U+002CU+0020>' . join ( curTodo . tagsA ) + '<STR_LIT:U+0020>' + str ( curTodo . id ) + '<STR_LIT>' + '<STR_LIT:U+0020>' . join ( [ str ( lvl ) , '<STR_LIT:">' + curTodo . fileName + '<STR_LIT:">' , curTodo . editor , time . strftime ( '<STR_LIT>' , gmtTime ) ] ) + "<STR_LIT>" + curTodo . comment + "<STR_LIT>" ) <EOL> f . write ( '<STR_LIT>' % self . maxId ) <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" ) <EOL> print ( e ) <EOL> return False <EOL> for iT in self . parentDB . todoA : <EOL> self . parentDB . todoA [ iT ] . setSaved ( SAVE_STATES . IDLE , self . dbId ) <EOL> self . maxIdSaved = self . maxId <EOL> return True <EOL> def newId ( self , _wantedId = <NUM_LIT:0> ) : <EOL> if _wantedId == self . lastId : <EOL> return self . lastId <EOL> self . fetch ( ) <EOL> self . maxId += <NUM_LIT:1> <EOL> if _wantedId > self . maxId : <EOL> self . maxId = _wantedId <EOL> self . flush ( ) <EOL> self . lastId = self . maxId <EOL> return self . lastId <EOL> def releaseId ( self , _atExit = False ) : <EOL> self . fetch ( ) <EOL> if self . lastId == self . maxId : <EOL> if _atExit : <EOL> self . maxId -= <NUM_LIT:1> <EOL> self . flush ( ) <EOL> else : <EOL> self . maxId = <NUM_LIT:0> <EOL> self . lastId = None <EOL> return True <EOL> def fetch ( self , _id = False ) : <EOL> if not os . path . isfile ( self . settings . file ) : <EOL> print ( "<STR_LIT>" ) <EOL> return False <EOL> todoA = { } <EOL> try : <EOL> with codecs . open ( self . settings . file , '<STR_LIT:r>' , '<STR_LIT>' ) as f : <EOL> ctxTodo = None <EOL> for ln in f : <EOL> ln = ln . splitlines ( ) [ <NUM_LIT:0> ] <EOL> matchParse = RE_TODO_STORED . match ( ln ) <EOL> if matchParse : <EOL> cId = int ( matchParse . group ( '<STR_LIT:id>' ) ) <EOL> if _id and _id != cId : <EOL> continue <EOL> rxEDate = matchParse . group ( '<STR_LIT>' ) <EOL> rxETime = matchParse . group ( '<STR_LIT>' ) <EOL> rxESecs = matchParse . group ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> gmtTime = time . mktime ( time . strptime ( '<STR_LIT>' % ( rxEDate , rxETime , rxESecs ) , '<STR_LIT>' ) ) <EOL> if cId not in todoA : <EOL> todoA [ cId ] = TodoTask ( cId , self . parentDB . config . projectName , self . parentDB ) <EOL> ctxTodo = matchParse <EOL> if cId > self . maxId : <EOL> self . maxIdSaved = self . maxId = cId <EOL> continue <EOL> if ctxTodo : <EOL> __state = ctxTodo . group ( '<STR_LIT>' ) <EOL> if ctxTodo . group ( <NUM_LIT:1> ) == '<STR_LIT:->' : __state = '<STR_LIT>' <EOL> matchComment = RE_TODO_STORED_COMMENT . match ( ln ) <EOL> todoA [ int ( ctxTodo . group ( '<STR_LIT:id>' ) ) ] . set ( __state , ctxTodo . group ( '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) , int ( ctxTodo . group ( '<STR_LIT>' ) ) , ctxTodo . group ( '<STR_LIT>' ) , matchComment . group ( '<STR_LIT>' ) , ctxTodo . group ( '<STR_LIT>' ) , gmtTime ) <EOL> ctxTodo = None <EOL> continue <EOL> maxIdParse = RE_TODO_FILE_MAXID . match ( ln ) <EOL> if maxIdParse : <EOL> cId = int ( maxIdParse . group ( '<STR_LIT>' ) ) <EOL> if cId > self . maxId : <EOL> self . maxIdSaved = self . maxId = cId <EOL> continue <EOL> except Exception as e : <EOL> print ( "<STR_LIT>" ) <EOL> print ( e ) <EOL> self . dbOk = False <EOL> return False <EOL> return todoA </s>
