text:
/*****************************************************************************
 * inventory.c
 *
 * DESCRIPTION
 *    This file contains the code needed to do a quick inventory of the GRIB2
 * file.  The intent is to enable one to figure out which message in a GRIB
 * file one is after without needing to call the FORTRAN library.
 *
 * HISTORY
 *   9/2002 Arthur Taylor (MDL / RSIS): Created.
 *  12/2002 Tim Kempisty, Ana Canizares, Tim Boyer, & Marc Saccucci
 *          (TK,AC,TB,&MS): Code Review 1.
 *
 * NOTES
 *****************************************************************************
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "clock.h"
#include "memendian.h"
#include "fileendian.h"
#include "degrib2.h"
#include "degrib1.h"
#include "tdlpack.h"
#include "myerror.h"
#include "myutil.h"
#include "myassert.h"
#include "inventory.h"
#include "metaname.h"
#include "filedatasource.h"

#define SECT0LEN_BYTE 16

typedef union {
   sInt4 li;
   char buffer[4];
} wordType;

/*****************************************************************************
 * GRIB2InventoryFree() -- Review 12/2002
 *
 * Arthur Taylor / MDL
 *
 * PURPOSE
 *   Free's any memory that was allocated for the inventory of a single grib
 * message
 *
 * ARGUMENTS
 * inv = Pointer to the inventory of a single grib message. (Input/Output)
 *
 * FILES/DATABASES: None
 *
 * RETURNS: void
 *
 * HISTORY
 *   9/2002 Arthur Taylor (MDL/RSIS): Created.
 *  12/2002 (TK,AC,TB,&MS): Code Review.
 *   7/2003 AAT: memwatch detected unfreed inv->unitName
 *
 * NOTES
 *****************************************************************************
 */
void GRIB2InventoryFree (inventoryType *inv)
{
   free (inv->element);
   inv->element = NULL;
   free (inv->comment);
   inv->comment = NULL;
   free (inv->unitName);
   inv->unitName = NULL;
   free (inv->shortFstLevel);
   inv->shortFstLevel = NULL;
   free (inv->longFstLevel);
   inv->longFstLevel = NULL;
}

/*****************************************************************************
 * GRIB2InventoryPrint() -- Review 12/2002
 *
 * Arthur Taylor / MDL
 *
 * PURPOSE
 *   Prints to standard out, an inventory of the file, assuming one has an
 * array of invenories of single grib messages.
 *
 * ARGUMENTS
 *    Inv = Pointer to an Array of inventories to print. (Input)
 * LenInv = Length of the Array Inv (Input)
 *
 * FILES/DATABASES: None
 *
 * RETURNS: void
 *
 * HISTORY
 *   9/2002 Arthur Taylor (MDL/RSIS): Created.
 *  12/2002 (TK,AC,TB,&MS): Code Review.
 *   1/2004 AAT: Added short form of First level to print out.
 *   3/2004 AAT: Switched from "#, Byte, ..." to "MsgNum, Byte, ..."
 *
 * NOTES
 *****************************************************************************
 */
void GRIB2InventoryPrint (inventoryType *Inv, uInt4 LenInv)
{
   uInt4 i;             /* Counter of which inventory we are printing. */
   double delta;        /* Difference between valid and reference time. */
   char refTime[25];    /* Used to store the formatted reference time. */
   char validTime[25];  /* Used to store the formatted valid time. */

   printf ("MsgNum, Byte, GRIB-Version, elem, level, reference(UTC),"
           " valid(UTC), Proj(hr)\n");
   fflush (stdout);
   for (i = 0; i < LenInv; i++) {
/*      strftime (refTime, 25, "%m/%d/%Y %H:%M", gmtime (&(Inv[i].refTime)));*/
      Clock_Print (refTime, 25, Inv[i].refTime, "%m/%d/%Y %H:%M", 0);
/*      strftime (validTime, 25, "%m/%d/%Y %H:%M",
                gmtime (&(Inv[i].validTime)));*/
      Clock_Print (validTime, 25, Inv[i].validTime, "%m/%d/%Y %H:%M", 0);
      delta = (Inv[i].validTime - Inv[i].refTime) / 3600.;
      delta = myRound (delta, 2);
      if (Inv[i].comment == NULL) {
         printf ("%d.%d, %d, %d, %s, %s, %s, %s, %.2f\n",
                 Inv[i].msgNum, Inv[i].subgNum, Inv[i].start,
                 Inv[i].GribVersion, Inv[i].element, Inv[i].shortFstLevel,
                 refTime, validTime, delta);
         fflush (stdout);
      } else {
         printf ("%d.%d, %d, %d, %s=\"%s\", %s, %s, %s, %.2f\n",
                 Inv[i].msgNum, Inv[i].subgNum, Inv[i].start,
                 Inv[i].GribVersion, Inv[i].element, Inv[i].comment,
                 Inv[i].shortFstLevel, refTime, validTime, delta);
         fflush (stdout);
      }
   }
}

/*****************************************************************************
 * InventoryParseTime() -- Review 12/2002
 *
 * Arthur Taylor / MDL
 *
 * PURPOSE
 *    To parse the time data from a grib2 char array to a time_t in UTC
 * seconds from the epoch.  This is very similar to metaparse.c:ParseTime
 * except using char * instead of sInt4
 *
 * ARGUMENTS
 *      is = The char array to read the time info from. (Input)
 * AnsTime = The time_t value to fill with the resulting time. (Output)
 *
 * FILES/DATABASES: None
 *
 * RETURNS: void
 *
 * HISTORY
 *  11/2002 Arthur Taylor (MDL/RSIS): Created.
 *  12/2002 (TK,AC,TB,&MS): Code Review.
 *
 * NOTES
 * 1) Couldn't use the default time_zone variable (concern over portability
 *    issues), so we print the hours, and compare them to the hours we had
 *    intended.  Then subtract the difference from the AnsTime.
 * 2) Similar to metaparse.c:ParseTime except using char * instead of sInt4
 *****************************************************************************
 */
static int InventoryParseTime (char *is, double *AnsTime)
{
   /* struct tm time; *//* A temporary variable to put the time info into. */
   /* char buffer[10]; *//* Used when printing the AnsTime's Hr. */
   /* int timeZone; *//* The adjustment in Hr needed to get the right UTC * time. */
   short int si_temp;   /* Temporarily stores the year as a short int to fix
                         * possible endian problems. */

/*   memset (&time, 0, sizeof (struct tm));*/
   MEMCPY_BIG (&si_temp, is + 0, sizeof (short int));
   if ((si_temp < 1900) || (si_temp > 2100)) {
      return -1;
   }
   if ((is[2] > 12) || (is[3] == 0) || (is[3] > 31) || (is[4] > 24) ||
       (is[5] > 60) || (is[6] > 61)) {
      return -1;
   }
   Clock_ScanDate (AnsTime, si_temp, is[2], is[3]);
   *AnsTime += is[4] * 3600. + is[5] * 60. + is[6];
/*
   time.tm_year = si_temp - 1900;
   time.tm_mon = is[2] - 1;
   time.tm_mday = is[3];
   time.tm_hour = is[4];
   time.tm_min = is[5];
   time.tm_sec = is[6];
   *AnsTime = mktime (&time) - (Clock_GetTimeZone () * 3600);
*/
   /* Cheap method of getting global time_zone variable. */
/*
   strftime (buffer, 10, "%H", gmtime (AnsTime));
   timeZone = atoi (buffer) - is[4];
   if (timeZone < 0) {
      timeZone += 24;
   }
   *AnsTime = *AnsTime - (timeZone * 3600);
*/
   return 0;
}

/*****************************************************************************
 * GRIB2SectToBuffer() -- Review 12/2002
 *
 * Arthur Taylor / MDL
 *
 * PURPOSE
 *    To read in a GRIB2 section into a buffer.  Reallocates space for the
 * section if buffLen < secLen.  Reads in secLen and checks that the section
 * is valid, and the file is large enough to hold the entire section.
 *
 * ARGUMENTS
 *      fp = Opened file pointing to the section in question. (Input/Output)
 * gribLen = The total length of the grib message. (Input)
 *    sect = Which section we think we are reading.
 *           If it is -1, then set it to the section the file says we are
 *           reading (useful for optional sect 2)) (Input/Output).
 *  secLen = The length of this section (Output)
 * buffLen = Allocated length of buff (Input/Output)
 *    buff = Stores the section (Output)
 *
 * FILES/DATABASES:
 *    An already opened GRIB2 file pointer, already at section in question.
 *
 * RETURNS: int (could use errSprintf())
 *  0 = Ok.
 * -1 = Ran out of file.
 * -2 = Section was miss-labeled.
 *
 * HISTORY
 *  11/2002 Arthur Taylor (MDL/RSIS): Created.
 *  12/2002 (TK,AC,TB,&MS): Code Review.
 *   8/2003 AAT: Removed dependence on curTot
 *
 * NOTES
 *   May want to put this in degrib2.c
 *****************************************************************************
 */
static int GRIB2SectToBuffer (DataSource &fp,
                              CPL_UNUSED uInt4 gribLen,
                              sChar *sect,
                              uInt4 *secLen, uInt4 *buffLen, char **buff)
{
   char *buffer = *buff; /* Local ptr to buff to reduce ptr confusion. */

   if (FREAD_BIG (secLen, sizeof (sInt4), 1, fp) != 1) {
      if (*sect != -1) {
         errSprintf ("ERROR: Ran out of file in Section %d\n", *sect);
      } else {
         errSprintf ("ERROR: Ran out of file in GRIB2SectToBuffer\n");
      }
      return -1;
   }
   if (*buffLen < *secLen) {
      *buffLen = *secLen;
      *buff = (char *) realloc ((void *) *buff, *buffLen * sizeof (char));
      buffer = *buff;
   }

   if (fp.DataSourceFread (buffer, sizeof (char), *secLen - sizeof (sInt4)) !=
       *secLen - sizeof (sInt4)) {
      if (*sect != -1) {
         errSprintf ("ERROR: Ran out of file in Section %d\n", *sect);
      } else {
         errSprintf ("ERROR: Ran out of file in GRIB2SectToBuffer\n");
      }
      return -1;
   }
   if (*sect == -1) {
      *sect = buffer[5 - 5];
   } else if (buffer[5 - 5] != *sect) {
      errSprintf ("ERROR: Section %d misslabeled\n", *sect);
      return -2;
   }
   return 0;
}

/*****************************************************************************
 * GRIB2SectJump() --
 *
 * Arthur Taylor / MDL
 *
 * PURPOSE
 *    To jump past a GRIB2 section.  Reads in secLen and checks that the
 * section is valid.
 *
 * ARGUMENTS
 *      fp = Opened file pointing to the section in question. (Input/Output)
 * gribLen = The total length of the grib message. (Input)
 *    sect = Which section we think we are reading.
 *           If it is -1, then set it to the section the file says we are
 *           reading (useful for optional sect 2)) (Input/Output).
 *  secLen = The length of this section (Output)
 *
 * FILES/DATABASES:
 *    An already opened GRIB2 file pointer, already at section in question.
 *
 * RETURNS: int (could use errSprintf())
 *  0 = Ok.
 * -1 = Ran out of file.
 * -2 = Section was miss-labeled.
 *
 * HISTORY
 *   3/2003 Arthur Taylor (MDL/RSIS): Created.
 *   8/2003 AAT: Removed dependence on curTot, which was used to compute if
 *          the file should be large enough for the fseek, but didn't check
 *          if it actually was.
 *
 * NOTES
 *   May want to put this in degrib2.c
 *****************************************************************************
 */
static int GRIB2SectJump (DataSource &fp,
                          CPL_UNUSED sInt4 gribLen, sChar *sect, uInt4 *secLen)
{
   char sectNum;        /* Validates that we are on the correct section. */
   int c;               /* Check that the fseek is still inside the file. */

   if (FREAD_BIG (secLen, sizeof (sInt4), 1, fp) != 1) {
      if (*sect != -1) {
         errSprintf ("ERROR: Ran out of file in Section %d\n", *sect);
      } else {
         errSprintf ("ERROR: Ran out of file in GRIB2SectSkip\n");
      }
      return -1;
   }
   if (fp.DataSourceFread (&sectNum, sizeof (char), 1) != 1) {
      if (*sect != -1) {
         errSprintf ("ERROR: Ran out of file in Section %d\n", *sect);
      } else {
         errSprintf ("ERROR: Ran out of file in GRIB2SectSkip\n");
      }
      return -1;
   }
   if (*sect == -1) {
      *sect = sectNum;
   } else if (sectNum != *sect) {
      errSprintf ("ERROR: Section %d misslabeled\n", *sect);
      return -2;
   }
   /* Since fseek does not give an error if we jump outside the file, we test
    * it by using fgetc / ungetc. */
   fp.DataSourceFseek (*secLen - 5, SEEK_CUR);
   if ((c = fp.DataSourceFgetc()) == EOF) {
      errSprintf ("ERROR: Ran out of file in Section %d\n", *sect);
      return -1;
   } else {
		 fp.DataSourceUngetc(c);
   }
   return 0;
}

/*****************************************************************************
 * GRIB2Inventory2to7() --
 *
 * Arthur Taylor / MDL
 *
 * PURPOSE
 *   Inventories sections 3 to 7, filling out the inv record with the data in
 * section 4.  (Note: No Call to FORTRAN routines here).
 *
 * ARGUMENTS
 *   sectNum = Which section we are currently reading. (Input)
 *        fp = An opened file pointer to the file to the inventory of (In/Out)
 *   gribLen = The total length of the grib message. (Input)
 *   buffLen = length of buffer. (Input)
 *    buffer = Holds a given section. (Input)
 *       inv = The current inventory record to fill out. (Output)
 *  prodType = The GRIB2 type of product: 0 is meteo product, 1 is hydro,
 *             2 is land, 3 is space, 10 is oceanographic. (Input)
 *    center = Who produced it (Input)
 * subcenter = A sub group of center that actually produced it (Input)
 *
 * FILES/DATABASES:
 *
 * RETURNS: int (could use errSprintf())
 *  0 = "Ok"
 * -5 = Problems Reading in section 2 or 3
 * -6 = Problems Reading in section 3
 * -7 = Problems Reading in section 4
 * -8 = Problems Parsing section 4.
 * -9 = Problems Reading in section 5
 * -10 = Problems Reading in section 6
 * -11 = Problems Reading in section 7
 *
 * HISTORY
 *   3/2003 Arthur Taylor (MDL/RSIS): Created.
 *   4/2003 AAT: Modified to not have prodType, cat, subcat, templat in
 *          inventoryType structure.
 *   8/2003 AAT: curTot no longer serves a purpose.
 *   1/2004 AAT: Added center/subcenter.
 *
 * NOTES
 *****************************************************************************
 */
static int GRIB2Inventory2to7 (sChar sectNum, DataSource &fp, sInt4 gribLen,
                               uInt4 *buffLen, char **buffer,
                               inventoryType *inv, uChar prodType,
                               unsigned short int center,
                               unsigned short int subcenter)
{
   uInt4 secLen;        /* The length of the current section. */
   sInt4 foreTime;      /* forecast time (NDFD treats as "projection") */
   uChar foreTimeUnit;  /* The time unit of the "forecast time". */
   /* char *element; *//* Holds the name of the current variable. */
   /* char *comment; *//* Holds more comments about current variable. */
   /* char *unitName; *//* Holds the name of the unit [K] [%] .. etc */
   int convert;         /* Enum type of unit conversions (metaname.c),
                         * Conversion method for this variable's unit. */
   uChar cat;           /* General category of Meteo Product. */
   unsigned short int templat; /* The section 4 template number. */
   uChar subcat;        /* Specific subcategory of Product. */
   uChar fstSurfType;   /* Type of the first fixed surface. */
   double fstSurfValue; /* Value of first fixed surface. */
   sInt4 value;         /* The scaled value from GRIB2 file. */
   sChar factor;        /* The scaled factor from GRIB2 file */
   sChar scale;         /* Surface scale as opposed to probility factor. */
   uChar sndSurfType;   /* Type of the second fixed surface. */
   double sndSurfValue; /* Value of second fixed surface. */
   sChar f_sndValue;    /* flag if SndValue is valid. */
   uChar timeRangeUnit;
   sInt4 lenTime;       /* Used by parseTime to tell difference betweeen 8hr
                         * average and 1hr average ozone. */
   uChar genID;         /* The Generating process ID (used for GFS MOS) */
   uChar probType;      /* The probability type */
   double lowerProb;    /* The lower limit on probability forecast if
                         * template 4.5 or 4.9 */
   double upperProb;    /* The upper limit on probability forecast if
                         * template 4.5 or 4.9 */
   uChar timeIncrType;
   sChar percentile = 0;

   if ((sectNum == 2) || (sectNum == 3)) {
      /* Jump past section (2 or 3). */
      sectNum = -1;
      if (GRIB2SectJump (fp, gribLen, &sectNum, &secLen) != 0) {
         errSprintf ("ERROR: Problems Jumping past section 2 || 3\n");
         return -6;
      }
      if ((sectNum != 2) && (sectNum != 3)) {
         errSprintf ("ERROR: Section 2 or 3 misslabeled\n");
         return -5;
      } else if (sectNum == 2) {
         /* Jump past section 3. */
         sectNum = 3;
         if (GRIB2SectJump (fp, gribLen, &sectNum, &secLen) != 0) {
            errSprintf ("ERROR: Problems Jumping past section 3\n");
            return -6;
         }
      }
   }
   /* Read section 4 into buffer. */
   sectNum = 4;
   if (GRIB2SectToBuffer (fp, gribLen, &sectNum, &secLen, buffLen,
                          buffer) != 0) {
      errSprintf ("ERROR: Problems with section 4\n");
      return -7;
   }
/*
enum { GS4_ANALYSIS, GS4_ENSEMBLE, GS4_DERIVED, GS4_PROBABIL_PNT = 5,
   GS4_STATISTIC = 8, GS4_PROBABIL_TIME = 9, GS4_PERCENTILE = 10,
   GS4_RADAR = 20, GS4_SATELLITE = 30
};
*/
   /* Parse the interesting data out of sect 4. */
   MEMCPY_BIG (&templat, *buffer + 8 - 5, sizeof (short int));
   if ((templat != GS4_ANALYSIS) && (templat != GS4_ENSEMBLE)
       && (templat != GS4_DERIVED)
       && (templat != GS4_PROBABIL_PNT) && (templat != GS4_STATISTIC)
       && (templat != GS4_PROBABIL_TIME) && (templat != GS4_PERCENTILE)
       && (templat != GS4_ENSEMBLE_STAT)
       && (templat != GS4_RADAR) && (templat != GS4_SATELLITE)
       && (templat != GS4_DERIVED_INTERVAL)) {
      errSprintf ("This was only designed for templates 0, 1, 2, 5, 8, 9, "
                  "10, 11, 12, 20, 30\n");
      return -8;
   }
   cat = (*buffer)[10 - 5];
   subcat = (*buffer)[11 - 5];
   genID = 0;
   probType = 0;
   lowerProb = 0;
   upperProb = 0;
   if ((templat == GS4_RADAR) || (templat == GS4_SATELLITE) ||
       (templat == 254)) {
      inv->foreSec = 0;
      inv->validTime = inv->refTime;
      timeIncrType = 255;
      timeRangeUnit = 255;
      lenTime = 0;
   } else {
      genID = (*buffer)[14 - 5];
      /* Compute forecast time. */
      foreTimeUnit = (*buffer)[18 - 5];
      MEMCPY_BIG (&foreTime, *buffer + 19 - 5, sizeof (sInt4));
      if (ParseSect4Time2sec (foreTime, foreTimeUnit, &(inv->foreSec)) != 0) {
         errSprintf ("unable to convert TimeUnit: %d \n", foreTimeUnit);
         return -8;
      }
      /* Compute valid time. */
      inv->validTime = inv->refTime + inv->foreSec;
      timeIncrType = 255;
      timeRangeUnit = 1;
      lenTime = (sInt4) (inv->foreSec / 3600);
      switch (templat) {
         case GS4_PROBABIL_PNT: /* 4.5 */
            probType = (*buffer)[37 - 5];
            factor = (sChar) (*buffer)[38 - 5];
            MEMCPY_BIG (&value, *buffer + 39 - 5, sizeof (sInt4));
            lowerProb = value * pow (10.0, -1 * factor);
            factor = (sChar) (*buffer)[43 - 5];
            MEMCPY_BIG (&value, *buffer + 44 - 5, sizeof (sInt4));
            upperProb = value * pow (10.0, -1 * factor);
            break;
         case GS4_DERIVED_INTERVAL: /* 4.12 */
            if (InventoryParseTime (*buffer + 37 - 5, &(inv->validTime)) != 0) {
               printf ("Warning: Investigate Template 4.12 bytes 37-43\n");
               inv->validTime = inv->refTime + inv->foreSec;
            }
            timeIncrType = (*buffer)[50 - 5];
            timeRangeUnit = (*buffer)[51 - 5];
            MEMCPY_BIG (&lenTime, *buffer + 52 - 5, sizeof (sInt4));
/* If lenTime == missing (2^32 -1) we might do something, but not with 255.*/
/*
            if (lenTime == 255) {
               lenTime = (inv->validTime -
                          (inv->refTime + inv->foreSec)) / 3600;
            }
*/
            break;
         case GS4_PERCENTILE: /* 4.10 */
            percentile = (*buffer)[35 - 5];
            if (InventoryParseTime (*buffer + 36 - 5, &(inv->validTime)) != 0) {
               printf ("Warning: Investigate Template 4.10 bytes 36-42\n");
               inv->validTime = inv->refTime + inv->foreSec;
            }
            timeIncrType = (*buffer)[49 - 5];
            timeRangeUnit = (*buffer)[50 - 5];
            MEMCPY_BIG (&lenTime, *buffer + 51 - 5, sizeof (sInt4));
/* If lenTime == missing (2^32 -1) we might do something, but not with 255.*/
/*
            if (lenTime == 255) {
               lenTime = (inv->validTime -
                          (inv->refTime + inv->foreSec)) / 3600;
            }
*/
            break;
         case GS4_STATISTIC: /* 4.8 */
            if (InventoryParseTime (*buffer + 35 - 5, &(inv->validTime)) != 0) {
               printf ("Warning: Investigate Template 4.8 bytes 35-41\n");
               inv->validTime = inv->refTime + inv->foreSec;
            }
            timeIncrType = (*buffer)[48 - 5];
            timeRangeUnit = (*buffer)[49 - 5];
            MEMCPY_BIG (&lenTime, *buffer + 50 - 5, sizeof (sInt4));
/* If lenTime == missing (2^32 -1) we might do something, but not with 255.*/
/*
            if (lenTime == 255) {
               lenTime = (inv->validTime -
                          (inv->refTime + inv->foreSec)) / 3600;
            }
*/
            break;
         case GS4_ENSEMBLE_STAT: /* 4.11 */
            if (InventoryParseTime (*buffer + 38 - 5, &(inv->validTime)) != 0) {
               printf ("Warning: Investigate Template 4.11 bytes 38-44\n");
               inv->validTime = inv->refTime + inv->foreSec;
            }
            timeIncrType = (*buffer)[51 - 5];
            timeRangeUnit = (*buffer)[52 - 5];
            MEMCPY_BIG (&lenTime, *buffer + 53 - 5, sizeof (sInt4));
/* If lenTime == missing (2^32 -1) we might do something, but not with 255.*/
/*
            if (lenTime == 255) {
               lenTime = (inv->validTime -
                          (inv->refTime + inv->foreSec)) / 3600;
            }
*/
            break;
         case GS4_PROBABIL_TIME: /* 4.9 */
            probType = (*buffer)[37 - 5];
            if ((uChar) (*buffer)[38 - 5] > 128) {
               factor = 128 - (uChar) (*buffer)[38 - 5];
            } else {
               factor = (*buffer)[38 - 5];
            }
            MEMCPY_BIG (&value, *buffer + 39 - 5, sizeof (sInt4));
            lowerProb = value * pow (10.0, -1 * factor);

            if ((uChar) (*buffer)[43 - 5] > 128) {
               factor = 128 - (uChar) (*buffer)[43 - 5];
            } else {
               factor = (*buffer)[43 - 5];
            }
            MEMCPY_BIG (&value, *buffer + 44 - 5, sizeof (sInt4));
            upperProb = value * pow (10.0, -1 * factor);

            if (InventoryParseTime (*buffer + 48 - 5, &(inv->validTime)) != 0) {
               printf ("Warning: Investigate Template 4.9 bytes 48-54\n");
               inv->validTime = inv->refTime + inv->foreSec;
            }
            timeIncrType = (*buffer)[61 - 5];
            timeRangeUnit = (*buffer)[62 - 5];
            MEMCPY_BIG (&lenTime, *buffer + 63 - 5, sizeof (sInt4));
/* If lenTime == missing (2^32 -1) we might do something, but not with 255.*/
/*
            if (lenTime == 255) {
               lenTime = (inv->validTime -
                          (inv->refTime + inv->foreSec)) / 3600;
            }
*/
            break;
      }
   }

   if (timeRangeUnit == 255) {
      timeRangeUnit = 1;
      lenTime = (sInt4) ((inv->validTime - inv->foreSec - inv->refTime) /
                         3600);
   }
/*   myAssert (timeRangeUnit == 1);*/
   /* Try to convert lenTime to hourly. */
   if (timeRangeUnit == 0) {
      lenTime = (sInt4) (lenTime / 60.);
      timeRangeUnit = 1;
   } else if (timeRangeUnit == 1) {
   } else if (timeRangeUnit == 2) {
      lenTime = lenTime * 24;
      timeRangeUnit = 1;
   } else if (timeRangeUnit == 10) {
      lenTime = lenTime * 3;
      timeRangeUnit = 1;
   } else if (timeRangeUnit == 11) {
      lenTime = lenTime * 6;
      timeRangeUnit = 1;
   } else if (timeRangeUnit == 12) {
      lenTime = lenTime * 12;
      timeRangeUnit = 1;
   } else if (timeRangeUnit == 13) {
      lenTime = (sInt4) (lenTime / 3600.);
      timeRangeUnit = 1;
   } else {
      printf ("Can't handle this timeRangeUnit\n");
      myAssert (timeRangeUnit == 1);
   }
   if (lenTime == GRIB2MISSING_s4) {
      lenTime = 0;
   }
   /* Find out what the name of this variable is. */
   ParseElemName (center, subcenter, prodType, templat, cat, subcat,
                  lenTime, timeIncrType, genID, probType, lowerProb,
                  upperProb, &(inv->element), &(inv->comment),
                  &(inv->unitName), &convert, percentile);
/*
   if (strcmp (element, "") == 0) {
      mallocSprintf (&(inv->element), "unknown");
      mallocSprintf (&(inv->unitName), "[%s]", unitName);
      if (strcmp (comment, "unknown") == 0) {
         mallocSprintf (&(inv->comment), "(prodType %d, cat %d, subcat %d)"
                        " [%s]", prodType, cat, subcat, unitName);
      } else {
         mallocSprintf (&(inv->comment), "%s [%s]", comment, unitName);
      }
   } else {
      if (IsData_MOS (center, subcenter)) {
         * See : http://www.nco.ncep.noaa.gov/pmb/docs/on388/tablea.html *
         if (genID == 96) {
            inv->element = (char *) malloc ((1 + 7 + strlen (element))
                                            * sizeof (char));
            sprintf (inv->element, "MOSGFS-%s", element);
         } else {
            inv->element = (char *) malloc ((1 + 4 + strlen (element))
                                            * sizeof (char));
            sprintf (inv->element, "MOS-%s", element);
         }
      } else {
         inv->element = (char *) malloc ((1 + strlen (element))
                                         * sizeof (char));
         strcpy (inv->element, element);
      }
      mallocSprintf (&(inv->unitName), "[%s]", unitName);
      mallocSprintf (&(inv->comment), "%s [%s]", comment, unitName);
*
      inv->unitName = (char *) malloc ((1 + 2 + strlen (unitName))
                                       * sizeof (char));
      sprintf (inv->unitName, "[%s]", unitName);
      inv->comment = (char *) malloc ((1 + 3 + strlen (unitName) + strlen (comment))
                                      * sizeof (char));
      sprintf (inv->comment, "%s [%s]", comment, unitName);
*
   }
*/

   if ((templat == GS4_RADAR) || (templat == GS4_SATELLITE)
       || (templat == 254) || (templat == 1000) || (templat == 1001)
       || (templat == 1002)) {
      reallocSprintf (&(inv->shortFstLevel), "0 undefined");
      reallocSprintf (&(inv->longFstLevel), "0.000[-] undefined ()");
   } else {
      fstSurfType = (*buffer)[23 - 5];
      scale = (*buffer)[24 - 5];
      MEMCPY_BIG (&value, *buffer + 25 - 5, sizeof (sInt4));
      if ((value == GRIB2MISSING_s4) || (scale == GRIB2MISSING_s1)) {
         fstSurfValue = 0;
      } else {
         fstSurfValue = value * pow (10.0, (int) (-1 * scale));
      }
      sndSurfType = (*buffer)[29 - 5];
      scale = (*buffer)[30 - 5];
      MEMCPY_BIG (&value, *buffer + 31 - 5, sizeof (sInt4));
      if ((value == GRIB2MISSING_s4) || (scale == GRIB2MISSING_s1) ||
          (sndSurfType == GRIB2MISSING_u1)) {
         sndSurfValue = 0;
         f_sndValue = 0;
      } else {
         sndSurfValue = value * pow (10.0, -1 * scale);
         f_sndValue = 1;
      }

      ParseLevelName (center, subcenter, fstSurfType, fstSurfValue,
                      f_sndValue, sndSurfValue, &(inv->shortFstLevel),
                      &(inv->longFstLevel));
   }

   /* Jump past section 5. */
   sectNum = 5;
   if (GRIB2SectJump (fp, gribLen, &sectNum, &secLen) != 0) {
      errSprintf ("ERROR: Problems Jumping past section 5\n");
      return -9;
   }
   /* Jump past section 6. */
   sectNum = 6;
   if (GRIB2SectJump (fp, gribLen, &sectNum, &secLen) != 0) {
      errSprintf ("ERROR: Problems Jumping past section 6\n");
      return -10;
   }
   /* Jump past section 7. */
   sectNum = 7;
   if (GRIB2SectJump (fp, gribLen, &sectNum, &secLen) != 0) {
      errSprintf ("ERROR: Problems Jumping past section 7\n");
      return -11;
   }
   return 0;
}

/*****************************************************************************
 * GRIB2Inventory() -- Review 12/2002
 *
 * Arthur Taylor / MDL
 *
 * PURPOSE
 *   Fills out an inventory structure for each GRIB message in a GRIB file,
 * without calling the FORTRAN routines to unpack the message.  It returns
 * the number of messages it found, or a negative number signifying an error.
 *
 * ARGUMENTS
 * filename = File to do the inventory of. (Input)
 *      Inv = The resultant array of inventories. (Output)
 *   LenInv = Length of the Array Inv (Output)
 *   numMsg = # of messages to inventory (0 = all, 1 = just first) (In)
 *   msgNum = MsgNum to start with, MsgNum of last message (Input/Output)
 *
 * FILES/DATABASES:
 *    Opens a GRIB2 file for reading given its filename.
 *
 * RETURNS: int (could use errSprintf())
 * +# = number of GRIB2 messages in the file.
 * -1 = Problems opening file for read.
 * -2 = Problems in section 0
 * -3 = Ran out of file.
 * -4 = Problems Reading in section 1
 * -5 = Problems Reading in section 2 or 3
 * -6 = Problems Reading in section 3
 * -7 = Problems Reading in section 4
 * -8 = Problems Parsing section 4.
 * -9 = Problems Reading in section 5
 * -10 = Problems Reading in section 6
 * -11 = Problems Reading in section 7
 * -12 = Problems inventory'ing a GRIB1 record
 * -13 = Problems inventory'ing a TDLP record
 *
 * HISTORY
 *   9/2002 Arthur Taylor (MDL/RSIS): Created.
 *  11/2002 AAT: Revised.
 *  12/2002 (TK,AC,TB,&MS): Code Review.
 *   3/2003 AAT: Corrected some satelite type mistakes.
 *   3/2003 AAT: Implemented multiple grid inventories in the same GRIB2
 *          message.
 *   4/2003 AAT: Started adding GRIB1 support
 *   6/2003 Matthew T. Kallio (matt@wunderground.com):
 *          "wmo" dimension increased to WMO_HEADER_LEN + 1 (for '\0' char)
 *   7/2003 AAT: Added numMsg so we can quickly find the reference time for
 *          a file by inventorying just the first message.
 *   8/2003 AAT: Adjusted use of GRIB_LIMIT to only affect the first message
 *          after we know we have a GRIB file, we don't want "trailing" bytes
 *          to break the program.
 *   8/2003 AAT: switched fileLen to only be computed for an error message.
 *   8/2003 AAT: curTot no longer serves a purpse.
 *   5/2004 AAT: Added a check for section number 2..8 for the repeated
 *          section (otherwise error)
 *  10/2004 AAT: Added ability to inventory TDLP records.
 *
 * NOTES
 *****************************************************************************
 */
int GRIB2Inventory (DataSource &fp, inventoryType **Inv, uInt4 *LenInv,
                    int numMsg, int *MsgNum)
{
   //FileDataSource fp (filename);            /* The opened GRIB2 file. */
   sInt4 offset = 0;    /* Where we are in the file. */
   sInt4 msgNum;        /* Which GRIB2 message we are on. */
   uInt4 gribLen;       /* Length of the current GRIB message. */
   uInt4 secLen;        /* Length of current section. */
   sChar sectNum;       /* Which section we are reading. */
   char *buff;          /* Holds the info between records. */
   uInt4 buffLen;       /* Length of info between records. */
   sInt4 sect0[SECT0LEN_WORD]; /* Holds the current Section 0. */
   char *buffer = NULL; /* Holds a given section. */
   uInt4 bufferLen = 0; /* Size of buffer. */
   inventoryType *inv;  /* Local ptr to Inv to reduce ptr confusion. */
   inventoryType *lastInv; /* Used to point to last inventory record when
                            * there are multiple grids in the same message. */
   wordType word;       /* Used to parse the prodType out of Sect 0. */
   int ans;             /* The return error code of ReadSect0. */
   char *msg;           /* Used to pop messages off the error Stack. */
   int version;         /* Which version of GRIB is in this message. */
   uChar prodType;      /* Which GRIB2 type of product, 0 is meteo, 1 is
                         * hydro, 2 is land, 3 is space, 10 is oceanographic.
                         */
   int grib_limit;      /* How many bytes to look for before the first "GRIB"
                         * in the file.  If not found, is not a GRIB file. */
   int c;               /* Determine if end of the file without fileLen. */
   sInt4 fileLen;       /* Length of the GRIB2 file. */
   unsigned short int center, subcenter; /* Who produced it. */
   // char *ptr;           /* used to find the file extension. */

   grib_limit = GRIB_LIMIT;
	 /*
   if (filename != NULL) {
      //if ((fp = fopen (filename, "rb")) == NULL) {
      //   errSprintf ("ERROR: Problems opening %s for read.", filename);
      //   return -1;
      //}
		  //fp = FileDataSource(filename);
      ptr = strrchr (filename, '.');
      if (ptr != NULL) {
         if (strcmp (ptr, ".tar") == 0) {
            grib_limit = 5000;
         }
      }
   } else {
      //fp = stdin; // TODO!!
   }
	 */
   msgNum = *MsgNum;

   buff = NULL;
   buffLen = 0;
   while ((c = fp.DataSourceFgetc()) != EOF) {
		 fp.DataSourceUngetc(c);
      // ungetc (c, fp);
      /* msgNum++ done first so any error messages range from 1..n, instead
       * of 0.. n-1. Note msgNum should end up as n not (n-1) */
      msgNum++;
/* Used when testing inventory of large TDLPack files. */
/*
#ifdef DEBUG
      myAssert (msgNum < 32500L);
      if (msgNum % 10 == 0) {
         printf ("%ld :: %f\n", msgNum, clock () / (double) CLOCKS_PER_SEC);
      }
#endif
*/
      /* Make it so the second, third, etc messages have no limit to finding
       * the "GRIB" keyword. */
      if (msgNum > 1) {
         grib_limit = -1;
      }
      /* Read in the wmo header and sect0. */
      if (ReadSECT0 (fp, &buff, &buffLen, grib_limit, sect0, &gribLen,
                     &version) < 0) {
         if (msgNum == 1) {
            /* Handle case where we couldn't find 'GRIB' in the message. */
            preErrSprintf ("Inside GRIB2Inventory, Message # %d\n", msgNum);
            free (buffer);
            free (buff);
            //fclose (fp);
            return -2;
         } else {
            /* Handle case where there are trailing bytes. */
            msg = errSprintf (NULL);
            printf ("Warning: Inside GRIB2Inventory, Message # %d\n",
                    msgNum);
            printf ("%s", msg);
            free (msg);
            /* find out how big the file is. */
            fp.DataSourceFseek (0L, SEEK_END);
            fileLen = fp.DataSourceFtell();
            /* fseek (fp, 0L, SEEK_SET); */
            printf ("There were %d trailing bytes in the file.\n",
                    fileLen - offset);
            free (buffer);
            free (buff);
            //fclose (fp);
            return msgNum;
         }
      }

      /* Make room for this GRIB message in the inventory list. */
      *LenInv = *LenInv + 1;
      *Inv = (inventoryType *) realloc ((void *) *Inv,
                                        *LenInv * sizeof (inventoryType));
      inv = *Inv + (*LenInv - 1);

      /* Start parsing the message. */
      inv->GribVersion = version;
      inv->msgNum = msgNum;
      inv->subgNum = 0;
      inv->start = offset;
      inv->element = NULL;
      inv->comment = NULL;
      inv->unitName = NULL;
      inv->shortFstLevel = NULL;
      inv->longFstLevel = NULL;

      if (version == 1) {
         if (GRIB1_Inventory (fp, gribLen, inv) != 0) {
            preErrSprintf ("Inside GRIB2Inventory \n");
            free (buffer);
            free (buff);
            //fclose (fp);
            return -12;
         }
      } else if (version == -1) {
         if (TDLP_Inventory (fp, gribLen, inv) != 0) {
            preErrSprintf ("Inside GRIB2Inventory \n");
            free (buffer);
            free (buff);
            //fclose (fp);
            return -13;
         }
      } else {
         word.li = sect0[1];
         prodType = word.buffer[2];

         /* Read section 1 into buffer. */
         sectNum = 1;
         if (GRIB2SectToBuffer (fp, gribLen, &sectNum, &secLen, &bufferLen,
                                &buffer) != 0) {
            errSprintf ("ERROR: Problems with section 1\n");
            free (buffer);
            free (buff);
            //fclose (fp);
            return -4;
         }
         /* Parse the interesting data out of sect 1. */
         InventoryParseTime (buffer + 13 - 5, &(inv->refTime));
         MEMCPY_BIG (&center, buffer + 6 - 5, sizeof (short int));
         MEMCPY_BIG (&subcenter, buffer + 8 - 5, sizeof (short int));

         sectNum = 2;
         do {
            /* Look at sections 2 to 7 */
            if ((ans = GRIB2Inventory2to7 (sectNum, fp, gribLen, &bufferLen,
                                           &buffer, inv, prodType, center,
                                           subcenter)) != 0) {
               //fclose (fp);
               free (buffer);
               free (buff);
               return ans;
            }
            /* Try to read section 8. If it is "7777" = 926365495 regardless
             * of endian'ness then we have a simple message, otherwise it is
             * complex, and we need to read more. */
            if (FREAD_BIG (&secLen, sizeof (sInt4), 1, fp) != 1) {
               errSprintf ("ERROR: Ran out of file looking for Sect 8.\n");
               free (buffer);
               free (buff);
               // fclose (fp);
               return -4;
            }
            if (secLen == 926365495L) {
               sectNum = 8;
            } else {
               if (fp.DataSourceFread (&sectNum, sizeof (char), 1) != 1) {
                  errSprintf ("ERROR: Ran out of file looking for "
                              "subMessage.\n");
                  free (buffer);
                  free (buff);
                  //fclose (fp);
                  return -4;
               }
               if ((sectNum < 2) || (sectNum > 7)) {
                  errSprintf ("ERROR (GRIB2Inventory): Couldn't find the end"
                              " of message\n");
                  errSprintf ("and it doesn't appear to repeat sections.\n");
                  errSprintf ("so it is probably an ASCII / binary bug\n");
                  free (buffer);
                  free (buff);
                  //fclose (fp);
                  return -4;
               }
               fp.DataSourceFseek (-5, SEEK_CUR);
               /* Make room for the next part of this GRIB message in the
                * inventory list.  This is for when we have sub-grids. */
               *LenInv = *LenInv + 1;
               *Inv = (inventoryType *) realloc ((void *) *Inv,
                                                 *LenInv *
                                                 sizeof (inventoryType));
               inv = *Inv + (*LenInv - 1);
               lastInv = *Inv + (*LenInv - 2);

               inv->GribVersion = version;
               inv->msgNum = msgNum;
               inv->subgNum = lastInv->subgNum + 1;
               inv->start = offset;
               inv->element = NULL;
               inv->comment = NULL;
               inv->unitName = NULL;
               inv->shortFstLevel = NULL;
               inv->longFstLevel = NULL;

               word.li = sect0[1];
               prodType = word.buffer[2];
               inv->refTime = lastInv->refTime;
            }
         } while (sectNum != 8);
      }

      /* added to inventory either first msgNum messages, or all messages */
      if (numMsg == msgNum) {
         break;
      }
      /* Continue on to the next GRIB2 message. */
      if (version == -1) {
         /* TDLPack uses 4 bytes for FORTRAN record size, then another 8
          * bytes for the size of the record (so FORTRAN can see it), then
          * the data rounded up to an 8 byte boundary, then a trailing 4
          * bytes for a final FORTRAN record size.  However it only stores
          * in_ the gribLen the non-rounded amount, so we need to take care
          * of the rounding, and the trailing 4 bytes here. */
         offset += buffLen + ((sInt4) ceil (gribLen / 8.0)) * 8 + 4;
      } else {
         offset += buffLen + gribLen;
      }
      fp.DataSourceFseek (offset, SEEK_SET);
   }
   free (buffer);
   free (buff);
   //fclose (fp);
   *MsgNum = msgNum;
   return msgNum;
}

int GRIB2RefTime (char *filename, double *refTime)
{
   FileDataSource fp (filename);            /* The opened GRIB2 file. */
   sInt4 offset = 0;    /* Where we are in the file. */
   sInt4 msgNum;        /* Which GRIB2 message we are on. */
   uInt4 gribLen;       /* Length of the current GRIB message. */
   uInt4 secLen;        /* Length of current section. */
   sChar sectNum;       /* Which section we are reading. */
   char *buff;          /* Holds the info between records. */
   uInt4 buffLen;       /* Length of info between records. */
   sInt4 sect0[SECT0LEN_WORD]; /* Holds the current Section 0. */
   char *buffer = NULL; /* Holds a given section. */
   uInt4 bufferLen = 0; /* Size of buffer. */
   /* wordType word; */       /* Used to parse the prodType out of Sect 0. */
   int ans;             /* The return error code of ReadSect0. */
   char *msg;           /* Used to pop messages off the error Stack. */
   int version;         /* Which version of GRIB is in this message. */
   /* uChar prodType; */      /* Which GRIB2 type of product, 0 is meteo, 1 is
                         * hydro, 2 is land, 3 is space, 10 is oceanographic.
                         */
   int grib_limit;      /* How many bytes to look for before the first "GRIB"
                         * in the file.  If not found, is not a GRIB file. */
   int c;               /* Determine if end of the file without fileLen. */
   sInt4 fileLen;       /* Length of the GRIB2 file. */
   char *ptr;           /* used to find the file extension. */
   double refTime1;

   grib_limit = GRIB_LIMIT;
   if (filename != NULL) {
      //if ((fp = fopen (filename, "rb")) == NULL) {
      //   errSprintf ("ERROR: Problems opening %s for read.", filename);
      //   return -1;
      //}
		  //fp = DataSource(filename);
      ptr = strrchr (filename, '.');
      if (ptr != NULL) {
         if (strcmp (ptr, ".tar") == 0) {
            grib_limit = 5000;
         }
      }
   } else {
      // fp = stdin; // TODO!!
   }
   msgNum = 0;

   buff = NULL;
   buffLen = 0;
   while ((c = fp.DataSourceFgetc()) != EOF) {
		 fp.DataSourceUngetc(c);
      /* msgNum++ done first so any error messages range from 1..n, instead
       * of 0.. n-1. Note msgNum should end up as n not (n-1) */
      msgNum++;
      /* Make it so the second, third, etc messages have no limit to finding
       * the "GRIB" keyword. */
      if (msgNum > 1) {
         grib_limit = -1;
      }
      /* Read in the wmo header and sect0. */
      if ((ans = ReadSECT0 (fp, &buff, &buffLen, grib_limit, sect0, &gribLen,
                            &version)) < 0) {
         if (msgNum == 1) {
            /* Handle case where we couldn't find 'GRIB' in the message. */
            preErrSprintf ("Inside GRIB2RefTime, Message # %d\n", msgNum);
            free (buffer);
            free (buff);
            //fclose (fp);
            return -2;
         } else {
            /* Handle case where there are trailing bytes. */
            msg = errSprintf (NULL);
            printf ("Warning: Inside GRIB2RefTime, Message # %d\n", msgNum);
            printf ("%s", msg);
            free (msg);
            /* find out how big the file is. */
            fp.DataSourceFseek (0L, SEEK_END);
            fileLen = fp.DataSourceFtell();
            /* fseek (fp, 0L, SEEK_SET); */
            printf ("There were %d trailing bytes in the file.\n",
                    fileLen - offset);
            free (buffer);
            free (buff);
            //fclose (fp);
            return msgNum;
         }
      }

      if (version == 1) {
         if (GRIB1_RefTime (fp, gribLen, &(refTime1)) != 0) {
            preErrSprintf ("Inside GRIB1_RefTime\n");
            free (buffer);
            free (buff);
            //fclose (fp);
            return -12;
         }
      } else if (version == -1) {
         if (TDLP_RefTime (fp, gribLen, &(refTime1)) != 0) {
            preErrSprintf ("Inside TDLP_RefTime\n");
            free (buffer);
            free (buff);
            //fclose (fp);
            return -13;
         }
      } else {
          /* word.li = sect0[1]; */
         /* prodType = word.buffer[2]; */

         /* Read section 1 into buffer. */
         sectNum = 1;
         if (GRIB2SectToBuffer (fp, gribLen, &sectNum, &secLen, &bufferLen,
                                &buffer) != 0) {
            errSprintf ("ERROR: Problems with section 1\n");
            free (buffer);
            //fclose (fp);
            return -4;
         }
         /* Parse the interesting data out of sect 1. */
         InventoryParseTime (buffer + 13 - 5, &(refTime1));
      }
      if (msgNum == 1) {
         *refTime = refTime1;
      } else {
         if (*refTime > refTime1) {
            *refTime = refTime1;
         }
      }

      /* Continue on to the next GRIB2 message. */
      offset += gribLen + buffLen;
      fp.DataSourceFseek (offset, SEEK_SET);
   }
   free (buffer);
   free (buff);
   //fclose (fp);
   return 0;
}


text:
package com.google.api.codegen.transformer.py;

import com.google.api.codegen.config.FieldConfig;
import com.google.api.codegen.transformer.ModelTypeNameConverter;
import com.google.api.codegen.util.EscaperFactory;
import com.google.api.codegen.util.Name;
import com.google.api.codegen.util.TypeName;
import com.google.api.codegen.util.TypeNameConverter;
import com.google.api.codegen.util.TypedValue;
import com.google.api.codegen.util.py.PythonTypeTable;
import com.google.api.tools.framework.model.EnumType;
import com.google.api.tools.framework.model.EnumValue;
import com.google.api.tools.framework.model.Interface;
import com.google.api.tools.framework.model.Method;
import com.google.api.tools.framework.model.ProtoElement;
import com.google.api.tools.framework.model.TypeRef;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.protobuf.DescriptorProtos.FieldDescriptorProto.Type;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PythonModelTypeNameConverter extends ModelTypeNameConverter {
  private static final String GOOGLE_PREFIX = "google";

  private static final String GOOGLE_CLOUD_PREFIX = GOOGLE_PREFIX + ".cloud";

  /** A map from primitive type to its corresponding Python types */
  private static final Map<Type, String> PRIMITIVE_TYPE_MAP =
      ImmutableMap.<Type, String>builder()
          .put(Type.TYPE_DOUBLE, "float")
          .put(Type.TYPE_FLOAT, "float")
          .put(Type.TYPE_INT32, "int")
          .put(Type.TYPE_INT64, "long")
          .put(Type.TYPE_UINT32, "int")
          .put(Type.TYPE_UINT64, "long")
          .put(Type.TYPE_SINT32, "int")
          .put(Type.TYPE_SINT64, "long")
          .put(Type.TYPE_FIXED32, "int")
          .put(Type.TYPE_FIXED64, "long")
          .put(Type.TYPE_SFIXED32, "int")
          .put(Type.TYPE_SFIXED64, "long")
          .put(Type.TYPE_BOOL, "bool")
          .put(Type.TYPE_STRING, "str")
          .put(Type.TYPE_BYTES, "bytes")
          .build();

  /** A map from primitive types to its default value. */
  private static final Map<Type, String> PRIMITIVE_ZERO_VALUE =
      ImmutableMap.<Type, String>builder()
          .put(Type.TYPE_BOOL, "False")
          .put(Type.TYPE_DOUBLE, "0.0")
          .put(Type.TYPE_FLOAT, "0.0")
          .put(Type.TYPE_INT64, "0")
          .put(Type.TYPE_UINT64, "0")
          .put(Type.TYPE_SINT64, "0")
          .put(Type.TYPE_FIXED64, "0")
          .put(Type.TYPE_SFIXED64, "0")
          .put(Type.TYPE_INT32, "0")
          .put(Type.TYPE_UINT32, "0")
          .put(Type.TYPE_SINT32, "0")
          .put(Type.TYPE_FIXED32, "0")
          .put(Type.TYPE_SFIXED32, "0")
          .put(Type.TYPE_STRING, "\'\'")
          .put(Type.TYPE_BYTES, "b\'\'")
          .build();

  // TODO (geigerj): Read this from configuration?
  private final Iterable<String> COMMON_PROTOS =
      ImmutableSet.of(
          "google.iam",
          "google.protobuf",
          "google.api",
          "google.longrunning",
          "google.rpc",
          "google.type",
          "google.logging.type");

  private final TypeNameConverter typeNameConverter;
  private String protoNamespace;
  private String parentProtoPackage;

  public PythonModelTypeNameConverter(String implicitPackageName) {
    typeNameConverter = new PythonTypeTable(implicitPackageName);
    List<String> protoNames = new ArrayList<>();
    for (String n : implicitPackageName.split("\\.")) {
      if (n.equals("gapic")) {
        break;
      }
      protoNames.add(n);
    }

    List<String> parentProtoPackageNames = new ArrayList<>(protoNames);
    Pattern versionNamespacePattern =
        Pattern.compile("(.+?)_(([vV]\\d+)([pP_]\\d+)?(([aA]lpha|[bB]eta)\\d*)?)");
    Matcher matcher = versionNamespacePattern.matcher(protoNames.get(protoNames.size() - 1));
    if (matcher.matches()) {
      parentProtoPackageNames.remove(protoNames.get(protoNames.size() - 1));
      parentProtoPackageNames.add(matcher.group(1));
      parentProtoPackageNames.add(matcher.group(2));
    }
    parentProtoPackage = Joiner.on(".").join(parentProtoPackageNames);

    protoNames.add("proto");
    protoNamespace = Joiner.on(".").join(protoNames);
  }

  @Override
  public TypeName getTypeNameInImplicitPackage(String shortName) {
    return typeNameConverter.getTypeNameInImplicitPackage(shortName);
  }

  @Override
  public TypeName getTypeName(TypeRef type) {
    if (type.isMap()) {
      return new TypeName("dict");
    } else if (type.isRepeated()) {
      return new TypeName("list");
    } else {
      return getTypeNameForElementType(type);
    }
  }

  @Override
  public TypeName getTypeNameForElementType(TypeRef type) {
    String primitiveTypeName = PRIMITIVE_TYPE_MAP.get(type.getKind());
    if (primitiveTypeName != null) {
      return new TypeName(primitiveTypeName);
    }
    switch (type.getKind()) {
      case TYPE_MESSAGE:
        return getTypeName(type.getMessageType());
      case TYPE_ENUM:
        return getTypeName(type.getEnumType());
      default:
        throw new IllegalArgumentException("unknown type kind: " + type.getKind());
    }
  }

  @Override
  public TypeName getTypeName(ProtoElement elem) {
    List<String> path = getClassNamePath(elem);

    if (elem instanceof EnumType) {
      path.add(0, "enums");
      String shortName = Joiner.on(".").join(path);
      return getTypeNameInImplicitPackage(shortName);
    }

    boolean useGrpcName = elem instanceof Interface || elem instanceof Method;

    if (useGrpcName) {
      path.add(0, getGrpcPbFileName(elem.getFile().getSimpleName()));
    } else {
      path.add(0, getPbFileName(elem.getFile().getSimpleName()));
    }
    path.add(0, protoPackageToPythonPackage(elem.getFile().getProto().getPackage()));
    String fullName = Joiner.on(".").join(path);
    return typeNameConverter.getTypeName(fullName);
  }

  private List<String> getClassNamePath(ProtoElement elem) {
    List<String> path = new LinkedList<>();
    for (ProtoElement elt = elem; elt.getParent() != null; elt = elt.getParent()) {
      path.add(0, elt.getSimpleName());
    }
    return path;
  }

  @Override
  public TypedValue getEnumValue(TypeRef type, EnumValue value) {
    return TypedValue.create(getTypeName(type), "%s." + value.getSimpleName());
  }

  @Override
  public TypedValue getSnippetZeroValue(TypeRef type) {
    // Don't call getTypeName; we don't need to import these.
    if (type.isMap()) {
      return TypedValue.create(new TypeName("dict"), "{}");
    }
    if (type.isRepeated()) {
      return TypedValue.create(new TypeName("list"), "[]");
    }
    if (PRIMITIVE_ZERO_VALUE.containsKey(type.getKind())) {
      return TypedValue.create(getTypeName(type), PRIMITIVE_ZERO_VALUE.get(type.getKind()));
    }
    if (type.isMessage()) {
      return TypedValue.create(getTypeName(type), "{}");
    }
    if (type.isEnum()) {
      return getEnumValue(type, type.getEnumType().getValues().get(0));
    }
    return TypedValue.create(new TypeName(""), "None");
  }

  @Override
  public TypedValue getImplZeroValue(TypeRef type) {
    return getSnippetZeroValue(type);
  }

  @Override
  public String renderPrimitiveValue(TypeRef type, String value) {
    Type primitiveType = type.getKind();
    if (!PRIMITIVE_TYPE_MAP.containsKey(primitiveType)) {
      throw new IllegalArgumentException(
          "Initial values are only supported for primitive types, got type "
              + type
              + ", with value "
              + value);
    }
    switch (primitiveType) {
      case TYPE_BOOL:
        return Name.from(value.toLowerCase()).toUpperCamel();
      case TYPE_STRING:
        return "'" + EscaperFactory.getSingleQuoteEscaper().escape(value) + "'";
      case TYPE_BYTES:
        return "b'" + EscaperFactory.getSingleQuoteEscaper().escape(value) + "'";
      default:
        // Types that do not need to be modified (e.g. TYPE_INT32) are handled
        // here
        return value;
    }
  }

  @Override
  public TypeName getTypeNameForTypedResourceName(
      FieldConfig fieldConfig, String typedResourceShortName) {
    throw new UnsupportedOperationException(
        "getTypeNameForTypedResourceName not supported by Python");
  }

  @Override
  public TypeName getTypeNameForResourceNameElementType(
      FieldConfig fieldConfig, String typedResourceShortName) {
    throw new UnsupportedOperationException(
        "getTypeNameForResourceNameElementType not supported by Python");
  }

  private String protoPackageToPythonPackage(String protoPackage) {
    if (!protoPackage.startsWith(GOOGLE_PREFIX)) {
      return protoPackage;
    }

    for (String commonProto : COMMON_PROTOS) {
      if (protoPackage.startsWith(commonProto)) {
        return protoPackage;
      }
    }

    // This bit of logic assumes that the package of the element will have the same parent package
    // as the service protos for the client. For instance if the proto package of the element is
    // `a.b.v1.c` and the proto package of the service is `a.b.v1.d` the correct python package
    // for the element should be `a.b_v1.proto.c` and the correct python package for the service
    // is `a.b_v1.proto.d`.
    if (protoPackage.startsWith(parentProtoPackage)) {
      return protoNamespace + protoPackage.substring(parentProtoPackage.length());
    }
    return protoNamespace;
  }

  private String getPbFileName(String filename) {
    return filename.substring(filename.lastIndexOf("/") + 1, filename.length() - ".proto".length())
        + "_pb2";
  }

  private String getGrpcPbFileName(String filename) {
    return filename.substring(filename.lastIndexOf("/") + 1, filename.length() - ".proto".length())
        + "_pb2_grpc";
  }
}

redpajama_set_name: RedPajamaGithub


text:
from d3status.libs.loader import load

load_model = load("d3status.db", "Model")

class Model(object):
    _dbs = {}

    @classmethod
    def setup_dbs(cls, dbs):
        cls._dbs = dbs

    @property
    def dbs(self):
        return self.dbs

    # legacy support
    @property
    def db(self):
        return self._dbs.get("db", None)


text:
using CsvHelper;
using CsvHelper.Configuration;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using NominateAndVote.DataModel.Poco;
using NominateAndVote.DataModel.Tests;
using NominateAndVote.DataTableStorage;
using NominateAndVote.DataTableStorage.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Threading;
using System.Windows;
using System.Windows.Forms;
using MessageBox = System.Windows.MessageBox;

namespace NominateAndVote.DataTableStorageHelper
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Load_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("It should be quite fast");

            // create data manager
            var dataManager = CreateDataManager();
            dataManager.CreateTablesIfNeeded();

            // add data
            var dataModel = new SampleDataModel();
            foreach (var poco in dataModel.Administrators)
            {
                dataManager.SaveEntity(new AdministratorEntity(poco));
            }
            foreach (var poco in dataModel.News)
            {
                dataManager.SaveEntity(new NewsEntity(poco));
            }
            foreach (var poco in dataModel.Nominations)
            {
                dataManager.SaveEntity(new NominationEntity(poco));
            }
            foreach (var poco in dataModel.Polls)
            {
                dataManager.SaveEntity(new PollEntity(poco));
            }
            // skip poll subjects
            foreach (var poco in dataModel.Users)
            {
                dataManager.SaveEntity(new UserEntity(poco));
            }
            foreach (var poco in dataModel.Votes)
            {
                dataManager.SaveEntity(new VoteEntity(poco));
            }
            MessageBox.Show("Done!");
        }

        private void Button_Clear_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(
                "It can take even one minute! Be not afraid if the GUI freezes (sorry, it is only a helper app)");

            // create data manager
            var dataManager = CreateDataManager();

            // delete tables
            foreach (var entityType in TableNames.GetEntityTypes())
            {
                if (entityType != typeof(PollSubjectEntity))
                {
                    dataManager.GetTableReference(entityType).Delete();
                }
            }

            Thread.Sleep(60000);

            // create tables
            dataManager.CreateTablesIfNeeded();

            MessageBox.Show("Done!");
        }

        private void Button_Upload_Click(object sender, RoutedEventArgs e)
        {
            // browse file
            var fileDialog = new OpenFileDialog();

            // optional filter to restrict file types
            fileDialog.Filter = "CSV Files|*.csv";

            if (fileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }

            var csv = new CsvReader(new StreamReader(fileDialog.FileName));

            var pollSubjects = new List<PollSubject>();
            while (csv.Read())
            {
                var ps = new PollSubject
                {
                    Id = csv.GetField<long>(0),
                    Title = csv.GetField<string>(2),
                    Year = csv.GetField<int>(3)
                };
                pollSubjects.Add(ps);
            }

            MessageBox.Show("Read " + pollSubjects.Count + " poll subjects, saving them will take a while");
            MessageBox.Show("Make sure you have no poll subjects in the table before continuing");

            // create data manager
            var dataManager = CreateDataManager();
            dataManager.CreateTablesIfNeeded();

            // save poll subjects
            var total = pollSubjects.Count;
            var remaining = total;

            // tactic: remove saved items from list
            while (remaining > 0)
            {
                var n = Math.Min(500, remaining);
                var somePs = pollSubjects.GetRange(0, n);

                dataManager.SavePollSubjectsBatch(somePs);

                pollSubjects.RemoveRange(0, n);
                remaining -= n;
            }
        }

        private TableStorageDataManager CreateDataManager()
        {
            // set table names
            TableNames.ResetToDefault();

            // connect and create tables
            var storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            return new TableStorageDataManager(storageAccount);
        }
    }
}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Uses of Class com.fasterxml.jackson.core.JsonLocation (Jackson-core 2.13.0 API)</title>
<meta name="date" content="2021-09-30">
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">

JavaScript is disabled on your browser.

Skip navigation links

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes

Uses of Class
com.fasterxml.jackson.core.JsonLocation

Packages that use JsonLocation

Package
Description

com.fasterxml.jackson.core

Main public API classes of the core streaming JSON
 processor: most importantly JsonFactory
 used for constructing
 JSON parser (JsonParser)
 and generator
 (JsonGenerator)
 instances.

com.fasterxml.jackson.core.base

Base classes used by concrete Parser and Generator implementations;
 contain functionality that is not specific to JSON or input
 abstraction (byte vs char).

com.fasterxml.jackson.core.exc

Package for some of JsonProcessingException
 subtypes contained by streaming API.

com.fasterxml.jackson.core.filter

com.fasterxml.jackson.core.json

JSON-specific parser and generator implementation classes that
 Jackson defines and uses.

com.fasterxml.jackson.core.json.async

Non-blocking ("async") JSON parser implementation.

com.fasterxml.jackson.core.util

Utility classes used by Jackson Core functionality.

Uses of JsonLocation in com.fasterxml.jackson.core

Fields in com.fasterxml.jackson.core declared as JsonLocation

Modifier and Type
Field and Description

protected JsonLocation
JsonProcessingException._location

static JsonLocation
JsonLocation.NA

Shared immutable "N/A location" that can be returned to indicate
 that no location information is available.

Methods in com.fasterxml.jackson.core that return JsonLocation

Modifier and Type
Method and Description

JsonLocation
JsonParser.currentLocation()

Method that returns location of the last processed input unit (character
 or byte) from the input;
 usually for error reporting purposes.

JsonLocation
JsonParser.currentTokenLocation()

Method that return the starting location of the current
 (most recently returned)
 token; that is, the position of the first input unit (character or byte) from input
 that starts the current token.

abstract JsonLocation
JsonParser.getCurrentLocation()

Alias for JsonParser.currentLocation(), to be deprecated in later
 Jackson 2.x versions (and removed from Jackson 3.0).

abstract JsonLocation
JacksonException.getLocation()

Accessor for location information related to position within input
 or output (depending on operation), if available; if not available
 may return null.

JsonLocation
JsonProcessingException.getLocation()

JsonLocation
JsonStreamContext.getStartLocation(Object srcRef)

Deprecated.

Since 2.13 use JsonStreamContext.startLocation(com.fasterxml.jackson.core.io.ContentReference) instead

abstract JsonLocation
JsonParser.getTokenLocation()

Alias for JsonParser.currentTokenLocation(), to be deprecated in later
 Jackson 2.x versions (and removed from Jackson 3.0).

JsonLocation
JsonStreamContext.startLocation(ContentReference srcRef)

Optional method that may be used to access starting location of this context:
 for example, in case of JSON `Object` context, offset at which `[` token was
 read or written.

Constructors in com.fasterxml.jackson.core with parameters of type JsonLocation

Constructor and Description

JsonParseException(JsonParser p,
                  String msg,
                  JsonLocation loc)

JsonParseException(JsonParser p,
                  String msg,
                  JsonLocation loc,
                  Throwable root)

JsonParseException(String msg,
                  JsonLocation loc)

Deprecated.

JsonParseException(String msg,
                  JsonLocation loc,
                  Throwable root)

Deprecated.

JsonProcessingException(String msg,
                       JsonLocation loc)

JsonProcessingException(String msg,
                       JsonLocation loc,
                       Throwable rootCause)

Uses of JsonLocation in com.fasterxml.jackson.core.base

Methods in com.fasterxml.jackson.core.base that return JsonLocation

Modifier and Type
Method and Description

JsonLocation
ParserBase.getCurrentLocation()

Method that returns location of the last processed character;
 usually for error reporting purposes

JsonLocation
ParserBase.getTokenLocation()

Method that return the starting location of the current
 token; that is, position of the first character from input
 that starts the current token.

Uses of JsonLocation in com.fasterxml.jackson.core.exc

Constructors in com.fasterxml.jackson.core.exc with parameters of type JsonLocation

Constructor and Description

StreamReadException(JsonParser p,
                   String msg,
                   JsonLocation loc)

StreamReadException(JsonParser p,
                   String msg,
                   JsonLocation loc,
                   Throwable rootCause)

StreamReadException(String msg,
                   JsonLocation loc,
                   Throwable rootCause)

Uses of JsonLocation in com.fasterxml.jackson.core.filter

Methods in com.fasterxml.jackson.core.filter that return JsonLocation

Modifier and Type
Method and Description

JsonLocation
FilteringParserDelegate.getCurrentLocation()

JsonLocation
FilteringParserDelegate.getTokenLocation()

Uses of JsonLocation in com.fasterxml.jackson.core.json

Methods in com.fasterxml.jackson.core.json that return JsonLocation

Modifier and Type
Method and Description

JsonLocation
DupDetector.findLocation()

JsonLocation
ReaderBasedJsonParser.getCurrentLocation()

JsonLocation
UTF8DataInputJsonParser.getCurrentLocation()

JsonLocation
UTF8StreamJsonParser.getCurrentLocation()

JsonLocation
JsonReadContext.getStartLocation(Object rawSrc)

Deprecated.

JsonLocation
ReaderBasedJsonParser.getTokenLocation()

JsonLocation
UTF8DataInputJsonParser.getTokenLocation()

JsonLocation
UTF8StreamJsonParser.getTokenLocation()

JsonLocation
JsonReadContext.startLocation(ContentReference srcRef)

Uses of JsonLocation in com.fasterxml.jackson.core.json.async

Methods in com.fasterxml.jackson.core.json.async that return JsonLocation

Modifier and Type
Method and Description

JsonLocation
NonBlockingJsonParserBase.getCurrentLocation()

JsonLocation
NonBlockingJsonParserBase.getTokenLocation()

Uses of JsonLocation in com.fasterxml.jackson.core.util

Methods in com.fasterxml.jackson.core.util that return JsonLocation

Modifier and Type
Method and Description

JsonLocation
JsonParserDelegate.currentLocation()

JsonLocation
JsonParserDelegate.currentTokenLocation()

JsonLocation
JsonParserDelegate.getCurrentLocation()

JsonLocation
JsonParserDelegate.getTokenLocation()

Skip navigation links

Overview

Package

Class

Use

Tree

Deprecated

Index

Help

Prev

Next

Frames

No Frames

All Classes

Copyright © 2008–2021 FasterXML. All rights reserved.


text:
import pytest

from choo.models import Model
from choo.types import Coordinates, LineType

class TestCoordinates:
    def test_distance(self):
        assert round(Coordinates(51.451379, 7.013569).distance_to(Coordinates(51.454726, 6.979494)), 3) == 2390.268
        with pytest.raises(TypeError):
            Coordinates(51.451379, 7.013569).distance_to(7)

    def test_serialize(self):
        assert tuple(Coordinates(51.451379, 7.013569).serialize()) == (51.451379, 7.013569)

    def test_unserialize(self):
        assert Coordinates(51.451379, 7.013569) == Coordinates.unserialize((51.451379, 7.013569))

    def test_reversed(self):
        assert reversed(Coordinates(51.451379, 7.013569)) == (7.013569, 51.451379)

class TestSerializable:
    def test_no_serialize_supported(self):
        model = Model()
        with pytest.raises(TypeError):
            model.serialize()

    def test_wrong_type(self):
        with pytest.raises(ValueError):
            Model.unserialize({'@type': 'coordinates'})

class TestSimpleSerializable:
    def test_unsimple_serialize(self):
        assert LineType.train.serialize(simple=False) == {'@type': 'linetype', 'value': 'train'}

    def test_unsimple_unserialize(self):
        assert LineType.unserialize({'@type': 'linetype', 'value': 'train'}) == LineType.train
        with pytest.raises(ValueError):
            LineType.unserialize({'@type': 'stop'})


text:
require 'carrierwave'

module Gamification
  module Concerns::Models::Medal
    extend ActiveSupport::Concern

    included do
      belongs_to :goal

      mount_uploader :image, ::Gamification::ImageUploader
    end
  end
end


text:
import java.io.InputStreamReader;
import java.io.IOException;
import java_cup.runtime.Symbol;

/** Static semantics driver class */
class Semant {

    /** Reads AST from from consosle, and outputs the new AST */
    public static void main(String[] args) {
	args = Flags.handleFlags(args);
	try {
	    ASTLexer lexer = new ASTLexer(new InputStreamReader(System.in));
	    ASTParser parser = new ASTParser(lexer);
	    Object result = parser.parse().value;
	    ((Program)result).semant();
	    ((Program)result).dump_with_types(System.out, 0);
	} catch (Exception ex) {
	    ex.printStackTrace(System.err);
	}
    }
}


text:
#define KL_LOG KL_USER
#include <klog.h>
#include <stdc.h>
#include <exec.h>
#include <ktest.h>
#include <malloc.h>

/* Borrowed from mips/malta.c */
char *kenv_get(const char *key);

int main(void) {
  const char *init = kenv_get("init");
  const char *test = kenv_get("test");

  if (init) {
    exec_args_t init_args = {
      .prog_name = init, .argc = 1, .argv = (const char *[]){init}};

    run_program(&init_args);
  } else if (test) {
    ktest_main(test);
  } else {
    /* This is a message to the user, so I intentionally use kprintf instead of
     * log. */
    kprintf("============\n");
    kprintf("No init specified!\n");
    kprintf("Use init=PROGRAM to start a user-space init program or test=TEST "
            "to run a kernel test.\n");
    kprintf("============\n");
    return 1;
  }

  return 0;
}


text:
<?php
/*
 * Category Bundle
 * This file is part of the BardisCMS.
 *
 * (c) George Bardis <george@bardis.info>
 *
 */

namespace BardisCMS\CategoryBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;

class CategoryBundle extends Bundle
{
}


text:
<?php
namespace P\lib\framework\helpers;
use P\lib\framework\core\system as  system;
use P\lib\framework\core\utils as   utils;

/**
 * Description of FacebookOAuth
 *
 * @author Johan
 */
class FacebookOAuth
{
    const APP_ID        = 364185360326491;
    const APP_SECRET    = '52df1aaa2812d7860b4b429af615c788';
    const URL           = 'http://www.alpes-passion.fr/';
    static $token;

    public static function getCurentUrl()
    {
        return urlencode(self::URL);
    }

    public static function getAuthLink()
    {
        $sSeed = system\Session::get('facebook_state');

        system\Session::set('login_redirection', \P\url());

        if (empty($sSeed))
        {
            $sSeed =  rand(0,  time()).'_seed';
            system\Session::set('facebook_state', $sSeed);
        }

        $sFaceBookUrl = 'https://www.facebook.com/dialog/oauth?';
        $sFaceBookUrl .= 'client_id='.self::APP_ID;
        $sFaceBookUrl .= '&redirect_uri='.self::getCurentUrl();
        $sFaceBookUrl .= '&scope=publish_stream,read_stream,read_friendlists,manage_pages,user_birthday,email,publish_actions,friends_photos,user_photos,user_likes';
        $sFaceBookUrl .= '&state='.$sSeed;

        return $sFaceBookUrl;
    }

    public static function handleOAuth()
    {
        system\Session::destroy('facebook_state');
        system\Session::destroy('my_url');

        $sCode      = utils\Http::getParam('code');
        $sState     = utils\Http::getParam('state');

        if (!empty($sCode) && !empty($sState) )
        {
            return self::processOAuth($sCode, $sState);
        }

        return false;
    }

    public static function processOAuth($sCode, $sState)
    {
        $sState = str_replace('#_=_', '', $sState);

        if (empty($sState) || empty($sCode))
            return false;

        // Access token
        $sAccessUrl = 'https://graph.facebook.com/oauth/access_token?';
        $sAccessUrl .= 'client_id='.self::APP_ID;
        $sAccessUrl .= '&redirect_uri='.self::getCurentUrl();
        $sAccessUrl .= '&client_secret='.self::APP_SECRET;
        $sAccessUrl .= '&code='.$sCode;

        $sResponse = file_get_contents($sAccessUrl);

        if (preg_match('/access_token=([0-9a-zA-Z]*)(&expires=([0-9]*))*/', $sResponse, $asReq))
        {
            $sAccessToken   = $asReq[1];
            $nExpire        = (int) $asReq[2];

            if (empty($nExpire))
                $nExpire = 60 * 24 * 3600;

            // get basic information
            $sInfo = file_get_contents('https://graph.facebook.com/me?access_token='.$sAccessToken);

            $asInfo = json_decode($sInfo);

            $asData = array(
                'loginpk'       => 0,
                'facebook_id'   => $asInfo->id,
                'name'          => $asInfo->name,
                'firstname'     => $asInfo->first_name,
                'middlename'    => $asInfo->middle_name,
                'lastname'      => $asInfo->last_name,
                'profile'       => $asInfo->link,
                'username'      => $asInfo->username,
                'birthday'      => $asInfo->birthday,
                'gender'        => $asInfo->gender,
                'email'         => $asInfo->email,
                'updated_time'  => $asInfo->updated_time,
                'expire'        => $nExpire,
                'token'         => $sAccessToken
            );

            return $asData;
        }
        else
        {
            utils\Debug::dump('pas de match ');
            utils\Debug::dump($sResponse);
        }

        return false;
    }

    public static function logout()
    {
        $_SESSION = array();

        if (ini_get("session.use_cookies"))
        {
            $params         = session_get_cookie_params();

            setcookie(
                            session_name(), '', time() - 42000,
                            $params["path"], $params["domain"],
                            $params["secure"], $params["httponly"]
                            );
        }

        session_destroy();

        system\Session::destroy('userid');
        system\Session::destroy('token');
    }

    public static function getAppToken()
    {
        if (empty(self::$token))
        {
            $sUrl = 'https://graph.facebook.com/oauth/access_token?';
            $sUrl .= 'client_id='.self::APP_ID;
            $sUrl .= '&client_secret='.self::APP_SECRET;
            $sUrl .= '&grant_type=client_credentials';

            $sQueryString = file_get_contents($sUrl);

            self::$token = str_replace('access_token=', '', $sQueryString);
        }

        return self::$token;
    }
}

?>


text:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>JS Console - Example</title>
	<link href="styles/js-console.css" rel="stylesheet" />
</head>
<body>
	<div id="js-console">
	</div>
	<script src="scripts/js-console.js">
	</script>
	<script>
		jsConsole.writeLine('Using the dot-notation');
		jsConsole['writeLine']('Using the brackets');
	</script>
</body>
</html>


text: google-site-verification: google32fd52b6c900160b.html


text:
# logparser Input Plugin

The logparser plugin streams and parses the given logfiles. Currently it only
has the capability of parsing "grok" patterns from logfiles, which also supports
regex patterns.

### Configuration:

```toml
[[inputs.logparser]]
  ## Log files to parse.
  ## These accept standard unix glob matching rules, but with the addition of
  ## ** as a "super asterisk". ie:
  ##   /var/log/**.log     -> recursively find all .log files in /var/log
  ##   /var/log/*/*.log    -> find all .log files with a parent dir in /var/log
  ##   /var/log/apache.log -> only tail the apache log file
  files = ["/var/log/apache/access.log"]
  ## Read file from beginning.
  from_beginning = false

  ## Parse logstash-style "grok" patterns:
  ##   Telegraf built-in parsing patterns: https://goo.gl/dkay10
  [inputs.logparser.grok]
    ## This is a list of patterns to check the given log file(s) for.
    ## Note that adding patterns here increases processing time. The most
    ## efficient configuration is to have one pattern per logparser.
    ## Other common built-in patterns are:
    ##   %{COMMON_LOG_FORMAT}   (plain apache & nginx access logs)
    ##   %{COMBINED_LOG_FORMAT} (access logs + referrer & agent)
    patterns = ["%{COMBINED_LOG_FORMAT}"]
    ## Name of the outputted measurement name.
    measurement = "apache_access_log"
    ## Full path(s) to custom pattern files.
    custom_pattern_files = []
    ## Custom patterns can also be defined here. Put one pattern per line.
    custom_patterns = '''
    '''
```

## Grok Parser

The grok parser uses a slightly modified version of logstash "grok" patterns,
with the format `%{<capture_syntax>[:<semantic_name>][:<modifier>]}`

Telegraf has many of it's own
[built-in patterns](https://github.com/influxdata/telegraf/blob/master/plugins/inputs/logparser/grok/patterns/influx-patterns),
as well as supporting
[logstash's builtin patterns](https://github.com/logstash-plugins/logstash-patterns-core/blob/master/patterns/grok-patterns).

The best way to get acquainted with grok patterns is to read the logstash docs,
which are available here:
  https://www.elastic.co/guide/en/logstash/current/plugins-filters-grok.html

If you need help building patterns to match your logs,
you will find the http://grokdebug.herokuapp.com application quite useful!

By default all named captures are converted into string fields.
Modifiers can be used to convert captures to other types or tags.
Timestamp modifiers can be used to convert captures to the timestamp of the
 parsed metric.

- Available modifiers:
  - string   (default if nothing is specified)
  - int
  - float
  - duration (ie, 5.23ms gets converted to int nanoseconds)
  - tag      (converts the field into a tag)
  - drop     (drops the field completely)
- Timestamp modifiers:
  - ts               (This will auto-learn the timestamp format)
  - ts-ansic         ("Mon Jan _2 15:04:05 2006")
  - ts-unix          ("Mon Jan _2 15:04:05 MST 2006")
  - ts-ruby          ("Mon Jan 02 15:04:05 -0700 2006")
  - ts-rfc822        ("02 Jan 06 15:04 MST")
  - ts-rfc822z       ("02 Jan 06 15:04 -0700")
  - ts-rfc850        ("Monday, 02-Jan-06 15:04:05 MST")
  - ts-rfc1123       ("Mon, 02 Jan 2006 15:04:05 MST")
  - ts-rfc1123z      ("Mon, 02 Jan 2006 15:04:05 -0700")
  - ts-rfc3339       ("2006-01-02T15:04:05Z07:00")
  - ts-rfc3339nano   ("2006-01-02T15:04:05.999999999Z07:00")
  - ts-httpd         ("02/Jan/2006:15:04:05 -0700")
  - ts-epoch         (seconds since unix epoch)
  - ts-epochnano     (nanoseconds since unix epoch)
  - ts-"CUSTOM"

CUSTOM time layouts must be within quotes and be the representation of the
"reference time", which is `Mon Jan 2 15:04:05 -0700 MST 2006`
See https://golang.org/pkg/time/#Parse for more details.


text:
Please consider for approval funding for this PD project. Included in the costs are electronics components, reference books, and some basic materials. All of the software and most of the training and references for this project are open-source and freely available on the web.

## Summary
The [Raspberry Pi](http://raspberrypi.org) is a single-board computer [developed in the the UK](http://www.wired.co.uk/magazine/archive/2013/07/features/the-life-of-pi) with the intention of promoting the teaching of basic computer science in schools. The small form factor and low cost of the Raspberry Pi have made it ideal for more advanced research projects as well.

By building a cluster of Raspberry Pi computers, one can design and test complicated network configurations and software solutions while emulating a larger scale network. This keeps such development off of the production servers, and allows for rapid prototyping in Linux systems without using additional IT resources. Building a similar system with full-size computer hardware would be extremely expensive and require a large amount of space and electricity to operate. Some examples of Raspberry Pi clusters built at different Universities are included in the references at the bottom of this page.

## Objectives
I intend to design and build a small cluster of Raspberry Pi computers and use the device for researching and developing advanced web serving solutions. The goal of this training and research is to develop systems and processes that may directly improve performance and reliability of the College's website for all campuses.

### Advanced hardware design / build
To save money, and provide more opportunity for creativity and learning, the case for the Pi cluster will be built using tools on campus. This will be a fraction of the price of commercially available Raspberry Pi cases, and will allow me to include the design templates I create so that the *entire project* can be open source – hardware and all.

Using equipment at Tenth St Campus, a custom case will be designed and built to contain the computing hardware. Innovative design and production techniques will be explored and documented through this process.

- **Case** to be designed with CAD tools, cut from acrylic on laser cutter
  - capacity for six Raspberry Pi 2 boards
  - appropriate ventilation / cooling
  - contain all wiring and accessories
    - usb charging device to power Pis
    - network cables, switch
  - each Pi gets a small protoboard with an RGB LED status light. This allows the device to *visualize* server activity while load balancing and performing other tasks.

See the [Bill of Materials](bom.md) for details on materials required for this project.

### Software testing, development
Once the Madmartigan device is built, it can be used to test and develop various types of networking software and solutions. I plan to complete and document self-directed study on the following topics:

#### Configuration Deployment with Ansible
Ansible is an open-source software platform for configuring and managing computers.

#### Load balancing with Drupal on Nginx
![architecture diagram](lib/img/architecture.png "Network architecture of the Madmartigan device")
> Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault-tolerant configurations.

>It is possible to use nginx as a very efficient HTTP load balancer to distribute traffic to several application servers and to improve performance, scalability and reliability of web applications with nginx.

#### Video Wall multi panel displays
![video wall](lib/img/videowall.jpg "video wall - matrix of video displays")
Using a number of video displays, the cluster could be used to develop and demonstrate large scale display and technology. Dividing a single image source into several displays is possible with the six Raspberry Pis.

#### Distributed digital signage systems
The device can be used to demonstrate and tune digital signage software for Raspberry Pi hardware. A distributed system wherein the signage assets are sourced from one server then distributed and stored on remote devices, reducing server and network load.

#### Data Structure Servers - Redis
One of the components of the load balancing server setup is a Redis server, which is an open source _data structure server_. Learning how to use and get the most of Redis will be a major component of the project.

From WikiPedia:
>Redis is a data structure server. It is open-source, networked, in-memory, and stores keys with optional durability. The development of Redis has been sponsored by Pivotal Software since May 2013; before that, it was sponsored by VMware. According to the monthly ranking by DB-Engines.com, Redis is the most popular key-value store.

## References
- http://www.wired.co.uk/magazine/archive/2013/07/features/the-life-of-pi
- http://www.midwesternmac.com/blogs/jeff-geerling/introducing-dramble-raspberry
- https://github.com/geerlingguy/raspberry-pi-dramble
- https://servercheck.in/blog/lessons-learned-building-raspberry-pi-dramble
- http://nginx.org/en/docs/http/load_balancing.html
- http://coen.boisestate.edu/ece/files/2013/05/Creating.a.Raspberry.Pi-Based.Beowulf.Cluster_v2.pdf
- http://www.southampton.ac.uk/~sjc/raspberrypi/pi_supercomputer_southampton.htm
- http://www.raspberrypi.org/forums/viewtopic.php?f=24&t=41449
- http://www.raspberrypi.org/video-wall/


text:
using System;
using System.Linq;
using Microsoft.Azure.Management.SiteRecovery.Models;

namespace Microsoft.Azure.Management.SiteRecovery.Models
{
    /// <summary>
    /// A standard service response for long running operations.
    /// </summary>
    public partial class ReprotectOperationResponse : FailoverOperationResponse
    {
        /// <summary>
        /// Initializes a new instance of the ReprotectOperationResponse class.
        /// </summary>
        public ReprotectOperationResponse()
        {
        }
    }
}


text:
IECORE_PUSH_DEFAULT_VISIBILITY
#include "OpenEXR/ImathMatrix.h"
#include "OpenEXR/ImathVec.h"
IECORE_POP_DEFAULT_VISIBILITY

#include "OpenEXR/ImathMatrixAlgo.h"

IECORE_PUSH_DEFAULT_VISIBILITY
#include "boost/test/unit_test.hpp"
IECORE_POP_DEFAULT_VISIBILITY

#include "boost/test/floating_point_comparison.hpp"

namespace IECore
{
void addInterpolatorTest(boost::unit_test::test_suite* test);

template<typename T>
class LinearInterpolatorTest
{
	public:
		void testSimple();
		void testTyped();
		void testVector();
};

template<typename T>
class CubicInterpolatorTest
{
	public:

		void testSimple();
		void testTyped();
		void testVector();
};

template<typename T>
class MatrixLinearInterpolatorTest
{
	public:
		void testSimple();
		void testTyped();
		void testVector();
};

template<typename T>
class MatrixCubicInterpolatorTest
{
	public:

		void testSimple();
		void testTyped();
		void testVector();
};

struct InterpolatorTestSuite : public boost::unit_test::test_suite
{

	InterpolatorTestSuite() : boost::unit_test::test_suite("InterpolatorTestSuite")
	{
		addLinearTest<float>();
		addLinearTest<double>();
		addLinearTest<Imath::V3f>();
		addLinearTest<Imath::V3d>();

		addLinearMatrixTest<float>();
		addLinearMatrixTest<double>();

		addCubicTest<float>();
		addCubicTest<double>();
		addCubicTest<Imath::V3f>();
		addCubicTest<Imath::V3d>();

		addCubicMatrixTest<float>();
		addCubicMatrixTest<double>();
	}

	template<typename T>
	void addLinearTest()
	{
		static boost::shared_ptr<LinearInterpolatorTest<T> > instance(new LinearInterpolatorTest<T>());

		add( BOOST_CLASS_TEST_CASE( &LinearInterpolatorTest<T>::testSimple, instance ) );
		add( BOOST_CLASS_TEST_CASE( &LinearInterpolatorTest<T>::testTyped, instance ) );
		add( BOOST_CLASS_TEST_CASE( &LinearInterpolatorTest<T>::testVector, instance ) );
	}

	template<typename T>
	void addCubicTest()
	{
		static boost::shared_ptr<CubicInterpolatorTest<T> > instance(new CubicInterpolatorTest<T>());

		add( BOOST_CLASS_TEST_CASE( &CubicInterpolatorTest<T>::testSimple, instance ) );
		add( BOOST_CLASS_TEST_CASE( &CubicInterpolatorTest<T>::testTyped, instance ) );
		add( BOOST_CLASS_TEST_CASE( &CubicInterpolatorTest<T>::testVector, instance ) );
	}

	template<typename T>
	void addLinearMatrixTest()
	{
		static boost::shared_ptr<MatrixLinearInterpolatorTest<T> > instance(new MatrixLinearInterpolatorTest<T>());

		add( BOOST_CLASS_TEST_CASE( &MatrixLinearInterpolatorTest<T>::testSimple, instance ) );
		add( BOOST_CLASS_TEST_CASE( &MatrixLinearInterpolatorTest<T>::testTyped, instance ) );
		add( BOOST_CLASS_TEST_CASE( &MatrixLinearInterpolatorTest<T>::testVector, instance ) );
	}

	template<typename T>
	void addCubicMatrixTest()
	{
		static boost::shared_ptr<MatrixCubicInterpolatorTest<T> > instance(new MatrixCubicInterpolatorTest<T>());

		add( BOOST_CLASS_TEST_CASE( &MatrixCubicInterpolatorTest<T>::testSimple, instance ) );
		add( BOOST_CLASS_TEST_CASE( &MatrixCubicInterpolatorTest<T>::testTyped, instance ) );
		add( BOOST_CLASS_TEST_CASE( &MatrixCubicInterpolatorTest<T>::testVector, instance ) );
	}
};
}

#include "InterpolatorTest.inl"

#endif


text:
package uk.co.bsol.trezorj.core.usb;

import com.codeminders.hidapi.HIDDevice;
import org.junit.Test;

import java.io.IOException;

import static org.mockito.Mockito.mock;

public class HIDOutputStreamTest {

  // Note that Mockito cannot mock native methods
  // This is just to avoid a null reference and remove USB dependency from build environments
  private HIDDevice device = mock(HIDDevice.class);

  @Test
  public void verifySingleFrameUnbuffered() throws IOException {

    final byte[] expected = new byte[]{1, 2};

    HIDOutputStream testObject = HIDStreams.newSingleFrameHIDOutputStream(device, expected);

    testObject.write(new byte[]{2}, 0, 1);

    testObject.close();

  }

  @Test
  public void verifySingleFrameBuffered() throws IOException {

    final byte[] expected = HIDStreams.newHIDFrame(4);

    HIDOutputStream testObject = HIDStreams.newSingleFrameHIDOutputStream(device, expected);

    // Create the payload ignoring the initial length byte
    for (int i = 1; i < expected.length; i++) {
      testObject.write(expected[i]);
    }

    // This should perform the write with the length byte calculated
    testObject.flush();

    testObject.close();

  }

  @Test
  public void verifyMultiFrameUnbuffered() throws IOException {

    // Create a multi-frame HID
    final byte[][] expected = new byte[][]{
      HIDStreams.newHIDFrame(63),
      HIDStreams.newHIDFrame(3)
    };

    HIDOutputStream testObject = HIDStreams.newMultiFrameHIDOutputStream(device, expected);

    byte[] payload = new byte[66];
    System.arraycopy(HIDStreams.newHIDPayload(63), 0, payload, 0, 63);
    System.arraycopy(HIDStreams.newHIDPayload(3), 0, payload, 63, 3);

    testObject.write(payload, 0, payload.length);

    testObject.close();

  }

  @Test
  public void verifyMultiFrameBuffered() throws IOException {

    // Create a multi-frame HID
    final byte[][] expected = new byte[][]{
      HIDStreams.newHIDFrame(63),
      HIDStreams.newHIDFrame(3)
    };

    HIDOutputStream testObject = HIDStreams.newMultiFrameHIDOutputStream(device, expected);

    byte[] payload = new byte[66];
    System.arraycopy(HIDStreams.newHIDPayload(63), 0, payload, 0, 63);
    System.arraycopy(HIDStreams.newHIDPayload(3), 0, payload, 63, 3);

    for (byte b : payload) {
      testObject.write(b);
    }

    testObject.flush();

    testObject.close();

  }

}


text:
package net.i2p.client;

/*
 * free (adj.): unencumbered; not under the control of others
 * Written by jrandom in 2003 and released into the public domain
 * with no warranty of any kind, either expressed or implied.
 * It probably won't  make your computer catch on fire, or eat
 * your children, but it might.  Use at your own risk.
 *
 */

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import net.i2p.I2PException;
import net.i2p.crypto.SigType;
import net.i2p.data.Certificate;
import net.i2p.data.Destination;

/**
 * Define the standard means of interacting with the I2P system
 *
 * An I2PClient contains no state, it is just a facility for creating private key files
 * and generating sesssions from existing private key files.
 *
 * @author jrandom
 */
public interface I2PClient {
    /** Standard host property, defaulting to localhost if not specified */
    public final static String PROP_TCP_HOST = "i2cp.tcp.host";
    /** Standard port number property */
    public final static String PROP_TCP_PORT = "i2cp.tcp.port";
    /** Reliability property */
    public final static String PROP_RELIABILITY = "i2cp.messageReliability";
    /** Reliability value: best effort */
    public final static String PROP_RELIABILITY_BEST_EFFORT = "BestEffort";
    /** Reliability value: guaranteed */
    public final static String PROP_RELIABILITY_GUARANTEED = "Guaranteed";
    /** @since 0.8.1 */
    public final static String PROP_RELIABILITY_NONE = "none";

    /** @since 0.9.12 */
    public static final String PROP_SIGTYPE = "i2cp.destination.sigType";
    /** @since 0.9.12 */
    public static final SigType DEFAULT_SIGTYPE = SigType.DSA_SHA1;

    /**
     * For router->client payloads.
     *
     * If false, the router will send the MessageStatus,
     * the client must respond with a ReceiveMessageBegin,
     * the router will send the MessagePayload,
     * and the client respond with a ReceiveMessageEnd.
     *
     * If true, the router will send the MessagePayload immediately,
     * and will not send a MessageStatus.
     * The client will not send ReceiveMessageBegin or ReceiveMessageEnd.
     *
     * Default false, but the implementation in this package sets to true.
     *
     * @since 0.9.4
     */
    public final static String PROP_FAST_RECEIVE = "i2cp.fastReceive";

    /** protocol flag that must be sent when opening the i2cp connection to the router */
    public final static int PROTOCOL_BYTE = 0x2A;

    /** Create a new client session for the Destination stored at the destKeyStream
     * using the specified options to both connect to the router, to instruct
     * the router how to handle the new session, and to configure the end to end
     * encryption.
     *
     * As of 0.9.19, defaults in options are honored.
     *
     * @param destKeyStream location from which to read the Destination, PrivateKey, and SigningPrivateKey from,
     *                      format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
     * @param options set of options to configure the router with, if null will use System properties
     * @return new session allowing a Destination to recieve all of its messages and send messages to any other Destination.
     */
    public I2PSession createSession(InputStream destKeyStream, Properties options) throws I2PSessionException;

    /** Create a new destination with the default certificate creation properties and store
     * it, along with the private encryption and signing keys at the specified location
     *
     * @param destKeyStream create a new destination and write out the object to the given stream,
     *			    formatted as Destination, PrivateKey, and SigningPrivateKey
     *                      format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
     * @return new destination
     */
    public Destination createDestination(OutputStream destKeyStream) throws I2PException, IOException;

    /**
     * Create a destination with the given signature type.
     * It will have a null certificate for DSA 1024/160 and KeyCertificate otherwise.
     * This is not bound to the I2PClient, you must supply the data back again
     * in createSession().
     *
     * @param destKeyStream location to write out the destination, PrivateKey, and SigningPrivateKey,
     *                      format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
     * @since 0.9.12
     */
    public Destination createDestination(OutputStream destKeyStream, SigType type) throws I2PException, IOException;

    /** Create a new destination with the given certificate and store it, along with the private
     * encryption and signing keys at the specified location
     *
     * @param destKeyStream location to write out the destination, PrivateKey, and SigningPrivateKey,
     *                      format is specified in {@link net.i2p.data.PrivateKeyFile PrivateKeyFile}
     * @param cert certificate to tie to the destination
     * @return newly created destination
     */
    public Destination createDestination(OutputStream destKeyStream, Certificate cert) throws I2PException, IOException;
}