示例#1
0
    def test_sheet_query_params_passed(self):
        client = Fieldbook('fakebook')

        expected_value = [{
            "id": 12,
            "record_url": "https://fieldbook.com/records/fakesheet",
            "column1": "text",
            "column2": []
        }]

        client._get = MagicMock(return_value=expected_value)
        value = client.list('fakesheet', column1='text')

        self.assertIsNotNone(client.book_id)
        client._get.assert_called_with(sheet_name='fakesheet', column1='text')
        self.assertListEqual(value, expected_value)
    def test_sheet_query_params_passed(self):
        client = Fieldbook('fakebook')

        expected_value = [
            {
                "id": 12,
                "record_url": "https://fieldbook.com/records/fakesheet",
                "column1": "text",
                "column2": []
            }
        ]

        client._get = MagicMock(return_value=expected_value)
        value = client.list('fakesheet', column1='text')

        self.assertIsNotNone(client.book_id)
        client._get.assert_called_with(sheet_name='fakesheet', column1='text')
        self.assertListEqual(value, expected_value)
示例#3
0
    def handle(self, *args, **kwargs):
        self.book_id = kwargs.get('book_id')
        self.outdir = kwargs.get('outdir')
        self._key = kwargs.get('key', os.getenv('FIELDBOOK_KEY', None))
        self._secret = kwargs.get('secret', os.getenv('FIELDBOOK_SECRET',
                                                      None))
        self.verbosity = kwargs.get('verbosity')
        self.no_save = kwargs.get('no_save')

        if not (self._key and self._secret):
            raise CommandError("""
You must provide the Fieldbook book's API key and secret, either as option flags or by
setting FIELDBOOK_KEY and FIELDBOOK_SECRET environment variables
                """)

        if not os.path.exists(self.outdir):
            raise CommandError(
                "outdir does not exist. Either create the directory or choose an existing one."
            )

        outpath = os.path.abspath(self.outdir)

        book = Fieldbook(self.book_id, key=self._key, secret=self._secret)

        if self.verbosity > 1:
            self.stdout.write("Fetching sheet names for book {}".format(
                self.book_id))
        sheet_names = book.sheets()

        for sheet in sheet_names:
            if self.verbosity > 1:
                self.stdout.write("Fetching data for sheet {}".format(sheet))

            data = book.list(sheet)

            outtfile = os.path.join(outpath, "{}.json".format(sheet))
            if not self.no_save:
                if self.verbosity > 1:
                    self.stdout.write("Writing data to {}".format(outtfile))
                json.dump(data, open(outtfile, 'w'), indent=4)
            else:
                self.stdout.write(json.dumps(data))
示例#4
0
def download(args):
    friendly = getattr(args, 'friendly', False)
    verbose = getattr(args, 'verbose', 0)
    config = configparser.ConfigParser()
    config.read_file(args.configfile)

    for section_name, section_conf in config.items():
        if section_name != 'DEFAULT':
            if verbose > 2:
                print("Reading config for '{}'".format(section_name))
            book_id = section_conf.get('id')
            book_key = section_conf.get('key')
            book_secret = section_conf.get('secret')
            outdir = section_conf.get('outdir')
            if not os.path.exists(outdir):
                raise FileNotFoundError(outdir)
            outpath = os.path.abspath(os.path.join(outdir, section_name))
            if not os.path.exists(outpath):
                if verbose > 0:
                    print("Creating '{}'".format(outpath))
                os.mkdir(outpath)
            if book_id and book_key and book_secret:
                book = Fieldbook(book_id, key=book_key, secret=book_secret)
                sheet_names = book.sheets()
                if verbose > 2:
                    print("Processing fieldbook '{}'".format(book_id))

                for sheet in sheet_names:
                    if verbose > 1:
                        print("Downloading '{}'".format(sheet))
                    data = book.list(sheet)
                    outtfile = os.path.join(outpath, "{}.json".format(sheet))
                    if verbose > 2:
                        print("Saving data to '{}'".format(outtfile))
                    json.dump(data, open(outtfile, 'w'), indent=4)
                    if friendly:
                        time.sleep(0.05)
            else:
                print("'{}' section must include `id`, `key`, and `secret`.".
                      format(section_name))
示例#5
0
    def handle(self, *args, **kwargs):
        self.book_id = kwargs.get('book_id')
        self.outdir = kwargs.get('outdir')
        self._key = kwargs.get('key', os.getenv('FIELDBOOK_KEY', None))
        self._secret = kwargs.get('secret', os.getenv('FIELDBOOK_SECRET', None))
        self.verbosity = kwargs.get('verbosity')
        self.no_save = kwargs.get('no_save')

        if not (self._key and self._secret):
            raise CommandError(
                """
You must provide the Fieldbook book's API key and secret, either as option flags or by
setting FIELDBOOK_KEY and FIELDBOOK_SECRET environment variables
                """
            )

        if not os.path.exists(self.outdir):
            raise CommandError("outdir does not exist. Either create the directory or choose an existing one.")

        outpath = os.path.abspath(self.outdir)

        book = Fieldbook(self.book_id, key=self._key, secret=self._secret)

        if self.verbosity > 1:
            self.stdout.write("Fetching sheet names for book {}".format(self.book_id))
        sheet_names = book.sheets()

        for sheet in sheet_names:
            if self.verbosity > 1:
                self.stdout.write("Fetching data for sheet {}".format(sheet))

            data = book.list(sheet)

            outtfile = os.path.join(outpath, "{}.json".format(sheet))
            if not self.no_save:
                if self.verbosity > 1:
                    self.stdout.write("Writing data to {}".format(outtfile))
                json.dump(data, open(outtfile, 'w'), indent=4)
            else:
                self.stdout.write(json.dumps(data))
示例#6
0
def download(args):
    friendly = getattr(args, 'friendly', False)
    verbose = getattr(args, 'verbose', 0)
    config = configparser.ConfigParser()
    config.read_file(args.configfile)

    for section_name, section_conf in config.items():
        if section_name != 'DEFAULT':
            if verbose > 2:
                print("Reading config for '{}'".format(section_name))
            book_id = section_conf.get('id')
            book_key = section_conf.get('key')
            book_secret = section_conf.get('secret')
            outdir = section_conf.get('outdir')
            if not os.path.exists(outdir):
                raise FileNotFoundError(outdir)
            outpath = os.path.abspath(os.path.join(outdir, section_name))
            if not os.path.exists(outpath):
                if verbose > 0:
                    print("Creating '{}'".format(outpath))
                os.mkdir(outpath)
            if book_id and book_key and book_secret:
                book = Fieldbook(book_id, key=book_key, secret=book_secret)
                sheet_names = book.sheets()
                if verbose > 2:
                    print("Processing fieldbook '{}'".format(book_id))

                for sheet in sheet_names:
                    if verbose > 1:
                        print("Downloading '{}'".format(sheet))
                    data = book.list(sheet)
                    outtfile = os.path.join(outpath, "{}.json".format(sheet))
                    if verbose > 2:
                        print("Saving data to '{}'".format(outtfile))
                    json.dump(data, open(outtfile, 'w'), indent=4)
                    if friendly:
                        time.sleep(0.05)
            else:
                print("'{}' section must include `id`, `key`, and `secret`.".format(section_name))