<s> from flask import Flask , request , jsonify , Response <EOL> from flask . ext . restful import Resource , Api <EOL> import psycopg2 , psycopg2 . extras , copy , json , subprocess , random , logging , io , sys , os , StringIO , uuid , csv <EOL> from src import helper <EOL> logging . basicConfig ( filename = '<STR_LIT>' , level = logging . DEBUG ) <EOL> class dataTree ( Resource ) : <EOL> def get ( self , tree_name ) : <EOL> name = helper . valid_tree_name ( tree_name ) <EOL> conn = helper . setConnection ( ) <EOL> if conn is False : <EOL> return jsonify ( dict ( status = <NUM_LIT> , success = False , message = '<STR_LIT>' ) ) <EOL> cur = conn . cursor ( cursor_factory = psycopg2 . extras . RealDictCursor ) <EOL> psycopg2 . extras . register_json ( conn ) <EOL> if not helper . tree_exists ( conn , name + '<STR_LIT>' ) : <EOL> conn . close ( ) <EOL> return jsonify ( dict ( status = <NUM_LIT> , success = False , message = '<STR_LIT>' + name + '<STR_LIT>' ) ) <EOL> cur . execute ( '<STR_LIT>' + name + '<STR_LIT>' ) <EOL> lines = cur . fetchall ( ) <EOL> cur . execute ( '<STR_LIT>' + name + '<STR_LIT>' ) <EOL> bulk = cur . fetchall ( ) <EOL> conn . close ( ) <EOL> return jsonify ( { '<STR_LIT:status>' : <NUM_LIT:200> , '<STR_LIT:success>' : True , '<STR_LIT>' : name , '<STR_LIT>' : lines , '<STR_LIT>' : bulk } ) <EOL> def post ( self , tree_name ) : <EOL> name = helper . valid_tree_name ( tree_name ) <EOL> conn = helper . setConnection ( ) <EOL> if conn is False : <EOL> return jsonify ( dict ( status = <NUM_LIT> , success = False , message = '<STR_LIT>' ) ) <EOL> cur = conn . cursor ( cursor_factory = psycopg2 . extras . RealDictCursor ) <EOL> psycopg2 . extras . register_json ( conn ) <EOL> psycopg2 . extensions . register_adapter ( dict , psycopg2 . extras . Json ) <EOL> if not helper . tree_exists ( conn , name + '<STR_LIT>' ) : <EOL> conn . close ( ) <EOL> return jsonify ( dict ( status = <NUM_LIT> , success = False , message = '<STR_LIT>' + name + '<STR_LIT>' ) ) <EOL> data = json . loads ( request . data ) <EOL> branch = data . get ( '<STR_LIT>' , None ) <EOL> branch_id , branch_name = helper . ensure_branch_id ( conn , name , branch ) <EOL> if not branch_id : <EOL> cur . execute ( '<STR_LIT>' , ( name , ) ) <EOL> branch_id = cur . fetchone ( ) [ '<STR_LIT:id>' ] <EOL> if not helper . branch_exists ( conn , branch_name ) : <EOL> conn . close ( ) <EOL> return jsonify ( dict ( status = <NUM_LIT> , success = False , message = '<STR_LIT>' + str ( branch_name ) + '<STR_LIT>' ) ) <EOL> updates = data . get ( '<STR_LIT:data>' , None ) <EOL> error = [ ] <EOL> lines = [ ] <EOL> ids = [ ] <EOL> inserted = [ ] <EOL> states = [ ] <EOL> if updates is None : <EOL> conn . close ( ) <EOL> return jsonify ( dict ( status = <NUM_LIT> , success = False , message = '<STR_LIT>' ) ) <EOL> for u in updates : <EOL> if not '<STR_LIT>' in u : <EOL> error . append ( u ) <EOL> continue <EOL> if not '<STR_LIT:primary_key>' in u : <EOL> error . append ( u ) <EOL> continue <EOL> if not '<STR_LIT>' in u : <EOL> cur . execute ( '<STR_LIT>' + name + '<STR_LIT>' + '<STR_LIT>' , ( u [ '<STR_LIT:primary_key>' ] , ) ) <EOL> u [ '<STR_LIT>' ] = cur . fetchone ( ) <EOL> u [ '<STR_LIT:id>' ] = uuid . uuid1 ( ) <EOL> ids . append ( str ( u [ '<STR_LIT:id>' ] ) ) <EOL> if '<STR_LIT>' in u : <EOL> states . append ( { '<STR_LIT:primary_key>' : u [ '<STR_LIT:primary_key>' ] , '<STR_LIT:id>' : str ( u [ '<STR_LIT:id>' ] ) , '<STR_LIT>' : u [ '<STR_LIT>' ] , '<STR_LIT>' : u [ '<STR_LIT>' ] } ) <EOL> else : <EOL> states . append ( { '<STR_LIT:primary_key>' : u [ '<STR_LIT:primary_key>' ] , '<STR_LIT:id>' : str ( u [ '<STR_LIT:id>' ] ) , '<STR_LIT>' : { } , '<STR_LIT>' : u [ '<STR_LIT>' ] } ) <EOL> l = helper . missing_columns_no_encoding ( copy . deepcopy ( u ) ) <EOL> inserted . append ( l ) <EOL> u = helper . missing_columns ( u ) <EOL> u [ '<STR_LIT>' ] = branch_id <EOL> lines . append ( { k : v . encode ( '<STR_LIT:utf8>' ) if isinstance ( v , unicode ) else v for k , v in u . items ( ) } ) <EOL> cols = [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:primary_key>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> strcsv = StringIO . StringIO ( ) <EOL> w = csv . DictWriter ( strcsv , cols , quoting = csv . QUOTE_NONNUMERIC , quotechar = "<STR_LIT:'>" , doublequote = False , escapechar = "<STR_LIT:\\>" ) <EOL> w . writerows ( lines ) <EOL> strcsv . seek ( <NUM_LIT:0> ) <EOL> sql = "<STR_LIT>" % ( name + '<STR_LIT>' , ) <EOL> cur . copy_expert ( sql , strcsv ) <EOL> if data . get ( '<STR_LIT>' , True ) : <EOL> bulk_id = None <EOL> bulk_meta = data . get ( '<STR_LIT>' , { } ) <EOL> cur . execute ( '<STR_LIT>' + name + '<STR_LIT>' + name + '<STR_LIT>' , ( branch_id , psycopg2 . extras . Json ( states ) , ids , psycopg2 . extras . Json ( bulk_meta ) ) ) <EOL> conn . commit ( ) <EOL> bulk_id = cur . fetchone ( ) [ '<STR_LIT:id>' ] <EOL> conn . close ( ) <EOL> return jsonify ( { '<STR_LIT:status>' : <NUM_LIT:200> , '<STR_LIT:success>' : True , '<STR_LIT>' : bulk_id , '<STR_LIT>' : inserted , '<STR_LIT>' : error } ) <EOL> conn . commit ( ) <EOL> conn . close ( ) <EOL> return jsonify ( { '<STR_LIT:status>' : <NUM_LIT:200> , '<STR_LIT:success>' : True , '<STR_LIT>' : inserted , '<STR_LIT>' : error } ) <EOL> def put ( self , tree_name ) : <EOL> name = helper . valid_tree_name ( tree_name ) <EOL> conn = helper . setConnection ( ) <EOL> if conn is False : <EOL> return jsonify ( dict ( status = <NUM_LIT> , success = False , message = '<STR_LIT>' ) ) <EOL> cur = conn . cursor ( ) <EOL> if not helper . tree_exists ( conn , name + '<STR_LIT>' ) : <EOL> conn . close ( ) <EOL> return jsonify ( dict ( status = <NUM_LIT> , success = False , message = '<STR_LIT>' + name + '<STR_LIT>' ) ) <EOL> data = json . loads ( request . data ) <EOL> return jsonify ( { name : tree_name } ) <EOL> def delete ( self , tree_name ) : <EOL> name = helper . valid_tree_name ( tree_name ) <EOL> conn = helper . setConnection ( ) <EOL> if conn is False : <EOL> return jsonify ( dict ( status = <NUM_LIT> , success = False , message = '<STR_LIT>' ) ) <EOL> cur = conn . cursor ( ) <EOL> if not helper . tree_exists ( conn , name + '<STR_LIT>' ) : <EOL> conn . close ( ) <EOL> return jsonify ( dict ( status = <NUM_LIT> , success = False , message = '<STR_LIT>' + name + '<STR_LIT>' ) ) <EOL> try : <EOL> os . system ( helper . dump_command ( name , os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' , '<STR_LIT>' , name + '<STR_LIT>' ) ) ) ) <EOL> except : <EOL> logging . debug ( '<STR_LIT>' + name + '<STR_LIT>' ) <EOL> conn . close ( ) <EOL> return jsonify ( dict ( status = <NUM_LIT> , message = '<STR_LIT>' + name + '<STR_LIT>' ) ) <EOL> try : <EOL> branches_file = os . path . abspath ( os . path . join ( os . path . dirname ( __file__ ) , '<STR_LIT:..>' , '<STR_LIT>' , name + '<STR_LIT>' ) ) <EOL> bf = io . open ( branches_file , '<STR_LIT:w>' ) <EOL> sql = "<STR_LIT>" % ( name , ) <EOL> cur . copy_expert ( sql , bf ) <EOL> bf . close ( ) <EOL> except Exception , e : <EOL> logging . debug ( '<STR_LIT>' + name + '<STR_LIT>' + str ( e ) ) <EOL> conn . close ( ) <EOL> return jsonify ( dict ( status = <NUM_LIT> , message = '<STR_LIT>' + name + '<STR_LIT>' ) ) <EOL> cur . execute ( '<STR_LIT>' + name + '<STR_LIT>' + name + '<STR_LIT>' ) <EOL> cur . execute ( '<STR_LIT>' , ( name , ) ) <EOL> conn . commit ( ) <EOL> conn . close ( ) <EOL> return jsonify ( dict ( status = <NUM_LIT:200> , success = True , message = '<STR_LIT>' + name + '<STR_LIT>' ) ) </s>
