Skip to content

Commit

Permalink
Adding JSON engine
Browse files Browse the repository at this point in the history
Modeled closely off of csv engine.

The python file cannot be name json.py as this conflicts with the
import of the json module.
  • Loading branch information
ethanwhite committed Jan 25, 2014
1 parent b98e1ea commit ea00518
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
3 changes: 2 additions & 1 deletion engines/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"postgres",
"sqlite",
"msaccess",
"csv"
"csv",
"jsonengine"
]

engine_module_list = [
Expand Down
91 changes: 91 additions & 0 deletions engines/jsonengine.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
"""Engine for writing data to a JSON file"""

import os
import json
from retriever.lib.models import Engine, no_cleanup


class DummyConnection:
def cursor(self):
pass
def commit(self):
pass
def rollback(self):
pass
def close(self):
pass

class DummyCursor(DummyConnection):
pass


class engine(Engine):
"""Engine instance for writing data to a CSV file."""
name = "JSON"
abbreviation = "json"
datatypes = {
"auto": "INTEGER",
"int": "INTEGER",
"bigint": "INTEGER",
"double": "REAL",
"decimal": "REAL",
"char": "TEXT",
"bool": "INTEGER",
}
required_opts = [
("table_name",
"Format of table name",
"{db}_{table}.json"),
]

def create_db(self):
"""Override create_db since there is no database just a JSON file"""
return None

def create_table(self):
"""Create the table by creating an empty json"""
self.output_file = open(self.table_name(), "w")
self.output_file.write('[')

def execute(self, statement, commit=True):
"""Write a line to the output file"""
self.output_file.write('\n' + statement + ',')

def format_insert_value(self, value, datatype):
"""Cleanup values for inserting
Replace nulls with empty strings and single quotes with double quotes
"""
v = Engine.format_insert_value(self, value, datatype)
if v == 'null': return ""
try:
if len(v) > 1 and v[0] == v[-1] == "'":
v = '"%s"' % v[1:-1]
except:
pass
return v

def insert_statement(self, values):
if not hasattr(self, 'auto_column_number'):
self.auto_column_number = 1
offset = 0
for i in range(len(self.table.columns)):
column = self.table.columns[i]
if 'auto' in column[1][0]:
values = values[:i+offset] + [self.auto_column_number] + values[i+offset:]
self.auto_column_number += 1
offset += 1

datadict = {column[0]: value for column, value in zip(self.table.columns, values)}
return json.dumps(datadict)

def table_exists(self, dbname, tablename):
"""Check to see if the data file currently exists"""
tablename = self.table_name(name=tablename, dbname=dbname)
return os.path.exists(tablename)

def get_connection(self):
"""Gets the db connection."""
self.get_input()
return DummyConnection()

0 comments on commit ea00518

Please sign in to comment.