# Path: scrapcore/database.py
# class Link(Base):
#     __tablename__ = 'link'
# 
#     id = Column(Integer, primary_key=True)
#     title = Column(String)
#     snippet = Column(String)
#     link = Column(String)
#     domain = Column(String)
#     visible_link = Column(String)
#     rank = Column(Integer)
#     link_type = Column(String)
#     rating = Column(String)
#     sitelinks = Column(String)
# 
#     serp_id = Column(Integer, ForeignKey('serp.id'))
#     serp = relationship(
#         SearchEngineResultsPage,
#         backref=backref('links', uselist=True)
#     )
# 
#     def __str__(self):
#         return '<Link at rank {rank} has url: {link}>'.format(**self.__dict__)
# 
#     def __repr__(self):
#         return self.__str__()
# 
# SERP = SearchEngineResultsPage
# 
# Path: scrapcore/tools.py
# class CsvStreamWriter():
#     """
#     Writes consecutive objects to an csv output file.
#     """
#     def __init__(self, filename, csv_fieldnames):
#         self.csv_fieldnames = csv_fieldnames
#         self.file = open(filename, 'wt')
#         self.dict_writer = csv.DictWriter(
#             self.file,
#             fieldnames=csv_fieldnames,
#             delimiter=','
#         )
#         self.dict_writer.writeheader()
# 
#     def write(self, data, serp):
#         for row in data['results']:
#             d = serp
#             d.update(row)
#             d = ({k: v if type(v) is str else v for k, v in d.items() if k in self.csv_fieldnames})
#             self.dict_writer.writerow(d)
# 
#     def end(self):
#         self.file.close()
# 
# class JsonStreamWriter():
#     """Writes consecutive objects to an json output file."""
# 
#     def __init__(self, filename):
#         self.file = open(filename, 'wt')
#         self.file.write('[')
#         self.last_object = None
# 
#     def write(self, obj):
#         if self.last_object:
#             self.file.write(',')
#         json.dump(obj, self.file, indent=2, sort_keys=True)
#         self.last_object = id(obj)
# 
#     def end(self):
#         self.file.write(']')
#         self.file.close()


import pprint
import sys
from scrapcore.database import Link, SERP
from scrapcore.tools import CsvStreamWriter, JsonStreamWriter
# -*- coding: utf-8 -*-
"""Stores SERP results in the appropriate output format."""


class ResultWriter():

    output_format = 'stdout'
    outfile = sys.stdout

    def init_outfile(self, config, force_reload=False):

        if not self.outfile or force_reload:

            output_file = config.get('output_filename', '')

            if output_file is None:
                self.output_format = None
            elif output_file.endswith('.json'):
                self.output_format = 'json'
            elif output_file.endswith('.csv'):
                self.output_format = 'csv'
            elif output_file is 'stdout':
                self.output_format = 'stdout'

            if self.output_format == 'json':

# Path: scrapcore/database.py
# class Link(Base):
#     __tablename__ = 'link'
# 
#     id = Column(Integer, primary_key=True)
#     title = Column(String)
#     snippet = Column(String)
#     link = Column(String)
#     domain = Column(String)
#     visible_link = Column(String)
#     rank = Column(Integer)
#     link_type = Column(String)
#     rating = Column(String)
#     sitelinks = Column(String)
# 
#     serp_id = Column(Integer, ForeignKey('serp.id'))
#     serp = relationship(
#         SearchEngineResultsPage,
#         backref=backref('links', uselist=True)
#     )
# 
#     def __str__(self):
#         return '<Link at rank {rank} has url: {link}>'.format(**self.__dict__)
# 
#     def __repr__(self):
#         return self.__str__()
# 
# SERP = SearchEngineResultsPage
# 
# Path: scrapcore/tools.py
# class CsvStreamWriter():
#     """
#     Writes consecutive objects to an csv output file.
#     """
#     def __init__(self, filename, csv_fieldnames):
#         self.csv_fieldnames = csv_fieldnames
#         self.file = open(filename, 'wt')
#         self.dict_writer = csv.DictWriter(
#             self.file,
#             fieldnames=csv_fieldnames,
#             delimiter=','
#         )
#         self.dict_writer.writeheader()
# 
#     def write(self, data, serp):
#         for row in data['results']:
#             d = serp
#             d.update(row)
#             d = ({k: v if type(v) is str else v for k, v in d.items() if k in self.csv_fieldnames})
#             self.dict_writer.writerow(d)
# 
#     def end(self):
#         self.file.close()
# 
# class JsonStreamWriter():
#     """Writes consecutive objects to an json output file."""
# 
#     def __init__(self, filename):
#         self.file = open(filename, 'wt')
#         self.file.write('[')
#         self.last_object = None
# 
#     def write(self, obj):
#         if self.last_object:
#             self.file.write(',')
#         json.dump(obj, self.file, indent=2, sort_keys=True)
#         self.last_object = id(obj)
# 
#     def end(self):
#         self.file.write(']')
#         self.file.close()


# Path: scrapcore/result_writer.py
import pprint
import sys
from scrapcore.database import Link, SERP
from scrapcore.tools import CsvStreamWriter, JsonStreamWriter

# -*- coding: utf-8 -*-
"""Stores SERP results in the appropriate output format."""


class ResultWriter():

    output_format = 'stdout'
    outfile = sys.stdout

    def init_outfile(self, config, force_reload=False):

        if not self.outfile or force_reload:

            output_file = config.get('output_filename', '')

            if output_file is None:
                self.output_format = None
            elif output_file.endswith('.json'):
                self.output_format = 'json'
            elif output_file.endswith('.csv'):
                self.output_format = 'csv'
            elif output_file is 'stdout':
                self.output_format = 'stdout'

            if self.output_format == 'json':

                self.outfile = JsonStreamWriter(output_file)