import os
import glob
import re
from flask import Flask, request, jsonify, abort, json
import psycopg2, psycopg2.extras
import msc_apps

sqlconn = msc_apps.sql_connect()
data_dir_root = os.environ.get('DATADIR')

app = Flask(__name__)
app.debug = True

#TODO COnversion
@app.route('/properties')
def properties():
  sqlconn.execute("select * from smartproperties")
  ROWS= sqlconn.fetchall()

  def dehexify(hex_str):
      temp_str=[]
      for let in hex_str:
          if ord(let) < 128:
              temp_str.append(let)
          else:
              temp_str.append('?')
      return ''.join(temp_str)

  response = []
  for sprow in ROWS:
      print sprow
      res = {
          'currencyID': sprow[1],
          'name': dehexify(sprow[6]) 
      }
      response.append(res)

  json_response = json.dumps( sorted(response, key=lambda x:  int(x['currencyID']) ))
  return json_response

@app.route('/addresses')
def addresses():
  currency_id = request.args.get('currency_id')
  response = []

  currency_id = re.sub(r'\D+', '', currency_id) #check alphanumeric
  sqlconn.execute("select * from addressbalances where propertyid=" + str(currency_id))
  ROWS= sqlconn.fetchall()

  for addrrow in ROWS:
      res = {
          'address': addrrow[0]
      }

      if currency_id == '0': #BTC
        res['balance'] = ('%.8f' % float(addrrow[4])).rstrip('0').rstrip('.')
        response.append(res)
      else:
        res['balance'] = ('%.8f' % float(addrrow[4])).rstrip('0').rstrip('.')
        res['reserved_balance'] = ('%.8f' % float(addrrow[5])).rstrip('0').rstrip('.')
        response.append(res)

  json_response = json.dumps(response)
  return json_response


@app.route('/transactions/<address>')
def transactions(address=None):
  currency_id = request.args.get('currency_id')

  print address, currency_id

  if address == None:
    abort(400)

  currency_id = re.sub(r'\D+', '', currency_id) #check alphanumeric

  sqlconn.execute("select * from addressesintxs a, transactions t where a.address=\'"+address+"\' and a.txdbserialnum = t.txdbserialnum and a.propertyid=" + str(currency_id))
  ROWS= sqlconn.fetchall()

  transactions = []
  for txrow in ROWS:
      transactions.append(txrow[9])

  return jsonify({ 'address': address, 'transactions': transactions })


@@ -643,26 +643,8 @@
 WS:%0A
-      print sprow%0A
     
@@ -724,9 +724,18 @@
 row%5B
-6
+-1%5D%5B'name'
 %5D) %0A

Fix display of names for /properties verification