Exemple #1
0
class WorkingDataCache(with_metaclass( 
        type("",(type,),{
            "workingDataCache":{},
            "__getitem__":lambda cls, key: cls.workingDataCache.get(key),
            "__setitem__":lambda cls, key,val: cls.workingDataCache.update({key:val}),
            "__getattr__":lambda cls, key: cls.workingDataCache.get(key),
            "__setattr__":lambda cls, key, val: cls.workingDataCache.update({key:val})
        }), object
    )):

    myLogger = pixiedust.getLogger(__name__)

    @staticmethod
    def onNewDisplayRun(entity, options):
        if "cell_id" in options and "showchrome" in options:
            #User is doing a new display run from the cell, clear the cache as we don't know if the entity has changed
            WorkingDataCache.removeEntry(options["cell_id"])

    @staticmethod
    def removeEntry(key):
        WorkingDataCache.workingDataCache.pop(key, None)

    @staticmethod
    def putInCache(options, data, constraints):
        if "cell_id" not in options or "noChartCache" in options:
            return
        constraints.pop("self",None)
        WorkingDataCache[options["cell_id"]] = {
            "data": data,
            "constraints": constraints
        }

    @staticmethod
    def getFromCache(options, constraints ):
        if "cell_id" not in options:
            return None
        constraints.pop("self",None)
        cellId = options["cell_id"]
        value = WorkingDataCache[cellId]
        if value:
            WorkingDataCache.myLogger.debug("Found cache data for {}. Validating integrity...".format(cellId))
            for item in list(value["constraints"].items()):
                if item[0] not in constraints or item[1] != constraints[item[0]]:
                    WorkingDataCache.myLogger.debug("Cache data not validated for key {0}. Expected Value is {1}. Got {2}. Destroying it!...".format(item[0], item[1], constraints[item[0]]))
                    WorkingDataCache.removeEntry(cellId)
                    return None
            WorkingDataCache.myLogger.debug("Cache data validated for {}. Using it!...".format(cellId))
            return value["data"]
        WorkingDataCache.myLogger.debug("No Cache Entry found for {}".format(cellId))
Exemple #2
0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------

from ..display import *
from pixiedust.utils.dataFrameAdapter import *
from pixiedust.display.chart.renderers import PixiedustRenderer
from pixiedust.display.streaming import StreamingDataAdapter
import pixiedust

myLogger = pixiedust.getLogger(__name__)

#bootstrap all the renderers
renderers = ["matplotlib", "bokeh", "seaborn", "mapbox", "google", "brunel"]

for renderer in renderers:
    try:
        __import__("pixiedust.display.chart.renderers." + renderer)
    except Exception as exc:
        myLogger.warn("Unable to import renderer {0}: {1}".format(
            renderer, str(exc)))


@PixiedustDisplayMeta()
class ChartDisplayMeta(DisplayHandlerMeta):
    @addId
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -------------------------------------------------------------------------------

import requests
import os
import pixiedust
import pytz
from dateutil.parser import parse
import datetime
from pixiedust_flightpredict import Configuration

myLogger=pixiedust.getLogger(__name__)

flexBaseUrl = "https://api.flightstats.com/flex/"

def buildUrl(path, **kwargs):
    appId=os.environ.get("appId") or Configuration["appId"]
    appKey=os.environ.get("appKey") or Configuration["appKey"]

    if appId is None or appKey is None:
        raise ValueError("appId or appKey is not defined")
    return (flexBaseUrl+path).format(**kwargs) + "?appId={0}&appKey={1}".format(appId, appKey)

def toUTC(sDateTime, timeZoneRegionName):
    return pytz.timezone( timeZoneRegionName ).localize(parse(sDateTime)).astimezone (pytz.utc)

airportCache={}