<s> """<STR_LIT>""" <EOL> import sys <EOL> from functools import partial <EOL> from optparse import OptionParser <EOL> import pushmanager . core . db as db <EOL> from pushmanager . core . util import add_to_tags_str <EOL> from pushmanager . core . util import del_from_tags_str <EOL> from pushmanager . core . util import tags_contain <EOL> from pushmanager . servlets . checklist import checklist_reminders <EOL> def main ( ) : <EOL> usage = '<STR_LIT>' <EOL> parser = OptionParser ( usage ) <EOL> ( _ , args ) = parser . parse_args ( ) <EOL> if len ( args ) == <NUM_LIT:2> : <EOL> db . init_db ( ) <EOL> convert_tag ( args [ <NUM_LIT:0> ] , args [ <NUM_LIT:1> ] ) <EOL> db . finalize_db ( ) <EOL> else : <EOL> parser . error ( '<STR_LIT>' ) <EOL> def convert_tag ( old , new ) : <EOL> print '<STR_LIT>' % ( old , new ) <EOL> cb = partial ( convert_tag_callback , old , new ) <EOL> rselect_query = db . push_requests . select ( ) <EOL> db . execute_transaction_cb ( [ rselect_query ] , cb ) <EOL> if old in checklist_reminders . keys ( ) : <EOL> print """<STR_LIT>""" % old <EOL> def convert_tag_callback ( oldtag , newtag , success , db_results ) : <EOL> check_db_results ( success , db_results ) <EOL> requests = db_results [ <NUM_LIT:0> ] . fetchall ( ) <EOL> update_queries = [ ] <EOL> for request in requests : <EOL> if tags_contain ( request [ '<STR_LIT>' ] , [ oldtag ] ) : <EOL> updated_tags = del_from_tags_str ( request [ '<STR_LIT>' ] , oldtag ) <EOL> updated_tags = add_to_tags_str ( updated_tags , newtag ) <EOL> update_query = db . push_requests . update ( ) . where ( <EOL> db . push_requests . c . id == request . id <EOL> ) . values ( { '<STR_LIT>' : updated_tags } ) <EOL> update_queries . append ( update_query ) <EOL> db . execute_transaction_cb ( update_queries , check_db_results ) <EOL> def check_db_results ( success , db_results ) : <EOL> if not success : <EOL> raise db . DatabaseError ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> sys . exit ( main ( ) ) </s>
