# Path: bamboo/lib/async.py
# def call_async(function, *args, **kwargs):
#     """Potentially asynchronously call `function` with the arguments.
# 
#     :param function: The function to call.
#     :param args: Arguments for the function.
#     :param kwargs: Keyword arguments for the function.
#     """
#     countdown = kwargs.pop('countdown', 0)
# 
#     if is_async():
#         function.__getattribute__('apply_async')(
#             countdown=countdown, args=args, kwargs=kwargs)
#     else:  # pragma: no cover
#         function(*args, **kwargs)
# 
# Path: bamboo/lib/datetools.py
# def recognize_dates(df, schema=None):
#     """Convert data columns to datetimes.
# 
#     Check if object columns in a dataframe can be parsed as dates.
#     If yes, rewrite column with values parsed as dates.
# 
#     If schema is passed, convert columes to datetime if column in *schema* is
#     of type datetime.
# 
#     :param df: The DataFrame to convert columns in.
#     :param schema: Schema to define columns of type datetime.
# 
#     :returns: A DataFrame with column values convert to datetime types.
#     """
#     return __parse_dates_schema(df, schema) if schema else __parse_dates(df)
# 
# Path: bamboo/lib/schema_builder.py
# def filter_schema(schema):
#     """Remove not settable columns."""
#     for column, column_schema in schema.iteritems():
#         if column_schema.get(CARDINALITY):
#             del column_schema[CARDINALITY]
#             schema[column] = column_schema
# 
#     return schema


from functools import partial
from celery.exceptions import RetryTaskError
from celery.task import task
from bamboo.lib.async import call_async
from bamboo.lib.datetools import recognize_dates
from bamboo.lib.schema_builder import filter_schema
import simplejson as json
import os
import tempfile
import pandas as pd
        # pandas needs a closed file for *read_csv*
        tmpfile.close()

        call_async(import_dataset, self, partial(
            csv_file_reader, tmpfile.name, na_values=na_values, delete=True))

        return self

    def import_from_json(self, json_file):
        """Impor data from a JSON file.

        :param json_file: JSON file to import.
        """
        content = json_file.file.read()
        call_async(import_dataset, self, partial(json_file_reader, content))

        return self

    def import_schema(self, schema):
        """Create a dataset from a SDF schema file (JSON).

        :param schema: The SDF (JSON) file to create a dataset from.

        :returns: The created dataset.
        """
        try:
            schema = json.loads(schema.file.read())
        except AttributeError:
            schema = json.loads(schema)


# Path: bamboo/lib/async.py
# def call_async(function, *args, **kwargs):
#     """Potentially asynchronously call `function` with the arguments.
# 
#     :param function: The function to call.
#     :param args: Arguments for the function.
#     :param kwargs: Keyword arguments for the function.
#     """
#     countdown = kwargs.pop('countdown', 0)
# 
#     if is_async():
#         function.__getattribute__('apply_async')(
#             countdown=countdown, args=args, kwargs=kwargs)
#     else:  # pragma: no cover
#         function(*args, **kwargs)
# 
# Path: bamboo/lib/datetools.py
# def recognize_dates(df, schema=None):
#     """Convert data columns to datetimes.
# 
#     Check if object columns in a dataframe can be parsed as dates.
#     If yes, rewrite column with values parsed as dates.
# 
#     If schema is passed, convert columes to datetime if column in *schema* is
#     of type datetime.
# 
#     :param df: The DataFrame to convert columns in.
#     :param schema: Schema to define columns of type datetime.
# 
#     :returns: A DataFrame with column values convert to datetime types.
#     """
#     return __parse_dates_schema(df, schema) if schema else __parse_dates(df)
# 
# Path: bamboo/lib/schema_builder.py
# def filter_schema(schema):
#     """Remove not settable columns."""
#     for column, column_schema in schema.iteritems():
#         if column_schema.get(CARDINALITY):
#             del column_schema[CARDINALITY]
#             schema[column] = column_schema
# 
#     return schema


# Path: bamboo/lib/readers.py
from functools import partial
from celery.exceptions import RetryTaskError
from celery.task import task
from bamboo.lib.async import call_async
from bamboo.lib.datetools import recognize_dates
from bamboo.lib.schema_builder import filter_schema
import simplejson as json
import os
import tempfile
import pandas as pd

        # pandas needs a closed file for *read_csv*
        tmpfile.close()

        call_async(import_dataset, self, partial(
            csv_file_reader, tmpfile.name, na_values=na_values, delete=True))

        return self

    def import_from_json(self, json_file):
        """Impor data from a JSON file.

        :param json_file: JSON file to import.
        """
        content = json_file.file.read()
        call_async(import_dataset, self, partial(json_file_reader, content))

        return self

    def import_schema(self, schema):
        """Create a dataset from a SDF schema file (JSON).

        :param schema: The SDF (JSON) file to create a dataset from.

        :returns: The created dataset.
        """
        try:
            schema = json.loads(schema.file.read())
        except AttributeError:
            schema = json.loads(schema)


        self.set_schema(filter_schema(schema))