pyresource.py

This tool will recursively scan the contents of a folder, and convert any files found into 'HTTPResource' entries that can be used with modules like the AdafruitHTTPServer.

Location: /tools/pyresource/pyresource.py

On Windows, the BSP package that contains the tools folder is normally found in the '%APPDATA%\Arduino15\packages\adafruit\hardware\wiced\0.6.0' folder. On OS X it can usually be found in the '~/Library/Arduino15/packages/adafruit/hardware/wiced/0.6.0' folder.
This tool was added in version 0.6.0 of FeatherLib

Usage

This tool accepts a single argument: the path to the folder where the files you wish to convert (recursively) are stored, relative to the current directory.

All HTTPResource header files will be written to the folder that the script is executed from.

Note that when using this tool folder separators ('/' or '\') will be converted to '_dir_' and spaces and periods will be converted to '_'.
Usage: pyresource.py [OPTIONS] DIR

  Adafruit Python HTTP Resource Tool

  This tool recursively converts the folder contents into HTTP server
  resources in a C header format. These headers can then be imported into
  WICED Feather HTTP server sketches.

  Example of recursively converting the contents of the 'resources' folder:

    $ python pyresource.py resources

Options:
  --help  Show this message and exit.

As an example, if you place all of your static files in the 'resources' folder of your Arduino sketch, and you wish to generate a set of HTTPResource records in the main sketch folder (one level higher than resources) you would run the tool as follows:

# Run from 'libraries/AdafruitWicedExamples/HTTPServer/D3Graphic'
$ python ../../../../tools/pyresource/pyresource.py resources

Assuming the same D3Graphic example mentionned above, this would generate the following output:

Looking for files in 'resources'
Converted 'resources/d3.min.js' to '_d3_min_js.h'
Converted 'resources/favicon.ico' to 'favicon_ico.h'
Converted 'resources/index.html' to 'index_html.h'
Wrote resource index to 'resources.h'

HTTPResource Records

Looking at the example above, we can see that three static files were converted to headers and HTTPResource records ('d3.min.js', 'favicon.ico', and 'index.html').

Each output header file contains a single HTTPResource, which has the binary equivalent of the file encoded inside it.

For example, for favicon_ico.h we get a 10990 byte long HTTPResource named favicon_ico, shown below:

/* Auto-generated by pyresource. Do not edit this file. */
const uint8_t favicon_ico_data[10990] = {
  0x00, 0x00, 0x01, 0x00, 0x03, 0x00, 0x10, 0x10, 0x00, 0x00, 0x01, 0x00, 0x08,
  0x00, 0x68, 0x05, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x20, 0x20, 0x00, 0x00,
  // ... data removed for brevity ...
  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  0x00, 0x00, 0x00, 0x00, 0x00
};

const HTTPResource favicon_ico(favicon_ico_data, 10990);

HTTPResource Collection: resources.h

The tool will also generate a single header file named resources.h, which is the only file that you need to reference in your sketch.

The resources.h file lists all of the HTTPResource records available, and you can then insert these resources into a page collection for your sketch, adding them to the AdafruitHTTPServer individually or as a list.

The tool will attempt to automatically determine the MIME type for the file based on the file extension, selecting from the list of MIME types supported by FeatherLib.

Using the example from above, we would get the following content in resources.h from D3Graphic:

#ifndef _RESOURCE_H_
#define _RESOURCE_H_

/* Auto-generated by pyresource. Do not edit this file. */

#include "http_common.h"
#include "_d3_min_js.h"
#include "favicon_ico.h"
#include "index_html.h"


/* HTTPPage collection from generated headers

HTTPPage("/d3.min.js", HTTP_MIME_JAVASCRIPT, &_d3_min_js),
HTTPPage("/favicon.ico", HTTP_MIME_IMAGE_MICROSOFT, &favicon_ico),
HTTPPage("/index.html", HTTP_MIME_TEXT_HTML, &index_html),


*/

#endif /* ifndef _RESOURCE_H_ */

For details on using the static content references in resources.h, see the appropriate section in the AdafruitHTTPServer classes documentation.

Last updated on 2016-09-17 at 11.05.16 AM Published on 2016-03-23 at 01.29.37 PM