Exemplo n.º 1
0
def to_json(params):
    jsonpickle.load_backend('json', 'dumps', 'loads', ValueError)
    jsonpickle.set_preferred_backend('json')
    jsonpickle.set_encoder_options('json', ensure_ascii=False)
    out = jsonpickle.encode(params, unpicklable=False)
    out = out.replace(': None', ': null')
    return out
Exemplo n.º 2
0
    def set_backend(self, *args):
        backend = args[0]

        self._is_installed(backend)

        jsonpickle.load_backend(*args)
        jsonpickle.set_preferred_backend(backend)
Exemplo n.º 3
0
def to_json(params):
    jsonpickle.load_backend('json', 'dumps', 'loads', ValueError)
    jsonpickle.set_preferred_backend('json')
    jsonpickle.set_encoder_options('json', ensure_ascii=False)
    out = jsonpickle.encode(params, unpicklable=False)
    out = out.replace(': None', ': null')
    return out
    def test_load_backend(self):
        """Test that we can call jsonpickle.load_backend()

        """
        if PY32:
            return self.skip('no simplejson for python 3.2')
        jsonpickle.load_backend('simplejson', 'dumps', 'loads', ValueError)
        self.assertTrue(True)
Exemplo n.º 5
0
    def test_load_backend(self):
        """Test that we can call jsonpickle.load_backend()

        """
        if PY32:
            return self.skip('no simplejson for python 3.2')
        jsonpickle.load_backend('simplejson', 'dumps', 'loads', ValueError)
        self.assertTrue(True)
Exemplo n.º 6
0
    def test_load_backend(self):
        """Test that we can call jsonpickle.load_backend()

        """
        if PY32:
            self.skipTest("no simplejson for python 3.2")
            return
        jsonpickle.load_backend("simplejson", "dumps", "loads", ValueError)
        self.assertTrue(True)
Exemplo n.º 7
0
    def test_load_backend_submodule(self):
        """Test that we can load a submodule as a backend

        """
        jsonpickle.load_backend('os.path', 'split', 'join', AttributeError)
        self.assertTrue('os.path' in jsonpickle.json._backend_names and
                        'os.path' in jsonpickle.json._encoders and
                        'os.path' in jsonpickle.json._decoders and
                        'os.path' in jsonpickle.json._encoder_options and
                        'os.path' in jsonpickle.json._decoder_exceptions)
Exemplo n.º 8
0
    def test_load_backend_submodule(self):
        """Test that we can load a submodule as a backend

        """
        jsonpickle.load_backend('os.path', 'split', 'join', AttributeError)
        self.assertTrue('os.path' in jsonpickle.json._backend_names and
                        'os.path' in jsonpickle.json._encoders and
                        'os.path' in jsonpickle.json._decoders and
                        'os.path' in jsonpickle.json._encoder_options and
                        'os.path' in jsonpickle.json._decoder_exceptions)
Exemplo n.º 9
0
    def test_set_preferred_backend_allows_magic(self):
        """Tests that we can use the pluggable backends magically
        """
        backend = "os.path"
        jsonpickle.load_backend(backend, "split", "join", AttributeError)
        jsonpickle.set_preferred_backend(backend)

        slash_hello, world = jsonpickle.encode("/hello/world")
        jsonpickle.remove_backend(backend)

        self.assertEqual(slash_hello, "/hello")
        self.assertEqual(world, "world")
Exemplo n.º 10
0
    def test_set_preferred_backend_allows_magic(self):
        """Tests that we can use the pluggable backends magically
        """
        backend = 'os.path'
        jsonpickle.load_backend(backend, 'split', 'join', AttributeError)
        jsonpickle.set_preferred_backend(backend)

        slash_hello, world = jsonpickle.encode('/hello/world')
        jsonpickle.remove_backend(backend)

        self.assertEqual(slash_hello, '/hello')
        self.assertEqual(world, 'world')
Exemplo n.º 11
0
    def test_set_preferred_backend_allows_magic(self):
        """Tests that we can use the pluggable backends magically
        """
        backend = 'os.path'
        jsonpickle.load_backend(backend, 'split', 'join', AttributeError)
        jsonpickle.set_preferred_backend(backend)

        slash_hello, world = jsonpickle.encode('/hello/world')
        jsonpickle.remove_backend(backend)

        self.assertEqual(slash_hello, '/hello')
        self.assertEqual(world, 'world')
Exemplo n.º 12
0
def download_story(url: str, pages_file: str):
    print("Downloading story at", url, "and saving as", pages_file, flush=True)

    # configure jsonpickle to preserve utf-8 strings instead of \u escape sequences
    jsonpickle.load_backend('simplejson', 'dumps', 'loads', ValueError)
    jsonpickle.set_preferred_backend('simplejson')
    jsonpickle.set_encoder_options('simplejson', ensure_ascii=False)

    # configure Selenium
    chrome_options = Options()
    chrome_options.headless = True
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    driver = webdriver.Chrome(options=chrome_options)
    driver.implicitly_wait(1)
    driver.request_interceptor = request_interceptor

    print("Opening", url)
    driver.get(url)
    page_driver = PageDriver(driver)

    print("Opening", pages_file, "for writing")
    with open(pages_file, 'w') as f:
        print("Starting main loop", flush=True)

        queue: "Queue[PagePath]" = Queue()
        queue.put(())

        while not queue.empty():
            page_path = queue.get()

            # open page
            print(page_path, end=' ', flush=True)
            page_driver = page_driver.navigate(*page_path)

            # save page to file
            page = page_driver.serialize(page_path)
            page_json = jsonpickle.encode(page,
                                          unpicklable=True,
                                          indent=2 * ' ')
            f.write(page_json)
            f.write('\n')

            # add answer pages to queue
            for answer_id in range(len(page_driver.answers())):
                new_page_path = (*page_path, answer_id)
                queue.put(new_page_path)

            # return to root page
            print(page_driver.article[:50], flush=True)
            page_driver = page_driver.restart()
Exemplo n.º 13
0
    def write_json(self, json_file_name):
        r"""Export to JSON format
        
        Parameters
        ----------
        json_file_name : str
            Path to the JSON file
    
        """
        jsonpickle.load_backend('json')
        jsonpickle.set_encoder_options('json', sort_keys=False, indent=4)

        with open(json_file_name, "w") as f:
            f.write(jsonpickle.encode(self))
Exemplo n.º 14
0
    def test_load_backend_skips_bad_decoder_exceptions(self):
        """Test that we ignore bad decoder exceptions"""

        jsonpickle.load_backend("os.path", "join", "split", "bad!")
        self.failIf(self._backend_is_partially_loaded("os.path"))
Exemplo n.º 15
0
    def test_load_backend_skips_bad_inputs(self):
        """Test that we ignore bad decoders"""

        jsonpickle.load_backend("os.path", "join", "bad!", AttributeError)
        self.failIf(self._backend_is_partially_loaded("os.path"))
Exemplo n.º 16
0
    def test_load_backend(self):
        """Test that we can call jsonpickle.load_backend()

        """
        jsonpickle.load_backend("simplejson", "dumps", "loads", ValueError)
Exemplo n.º 17
0
    def test_load_backend_skips_bad_decoder_exceptions(self):
        """Test that we ignore bad decoder exceptions"""

        jsonpickle.load_backend('os.path', 'join', 'split', 'bad!')
        self.failIf(self._backend_is_partially_loaded('os.path'))
Exemplo n.º 18
0
            else:
                it[i] = formatAll(it[i],formatDic,{},formatter)
    elif isinstance(it,str):
        if formatter is not None:
            it = formatter(it).evaluate(formatDic)
        else:
            it = it.format(**formatDic)
        
    return it
   
# register common extension and backend demjson
import jsonpickle.ext.numpy
import demjson
jsonBackend = "demjson"
jsonpickle.ext.numpy.register_handlers()
jsonpickle.load_backend(jsonBackend,"encode","decode",CE.MyValueError)
jsonpickle.set_preferred_backend(jsonBackend)
jsonpickle.set_decoder_options(jsonBackend,decode_float=float)

def jsonDump(obj,file,compact=False,*args,**kargs):
    global jsonBackend
    jsonpickle.set_encoder_options(jsonBackend,compactly=compact,*args,**kargs)
    file.write(jsonpickle.encode(obj))
    
def jsonParse(s):
    return jsonpickle.decode(s)
    
def jsonLoad(filePath):
    f = open(filePath)
    return jsonParse(f.read())
        
Exemplo n.º 19
0
    def test_load_backend_skips_bad_decode(self):
        """Test that we ignore bad decoders"""

        jsonpickle.load_backend('os.path', 'join', 'bad!', AttributeError)
        self.failIf(self._backend_is_partially_loaded('os.path'))
Exemplo n.º 20
0

#little wrapper functions to set defaults for dumps/loads behavior
def jsonencode(obj):
#    import pdb; pdb.set_trace()
    return jsonpickle.encode(obj, unpicklable=False)

def jsondecode(data):
    return json.loads(data)
        
   


try:
    import ujson
    jsonpickle.load_backend("ujson", "dumps", "loads", ValueError)
    jsonpickle.set_preferred_backend("ujson")

except ImportError:
    import json


#note these handlers are not ok for "picklable" stuff
class BSONObjectIDHandler(jsonpickle.handlers.BaseHandler):
    def flatten(self, obj, data={}):
        return str(obj)

    
class MongoEngineFileFieldHandler(jsonpickle.handlers.BaseHandler):
    def flatten(self, obj, data={}):
        if not obj.grid_id:
Exemplo n.º 21
0
    def test_load_backend_skips_bad_decoder_exceptions(self):
        """Test that we ignore bad decoder exceptions"""

        jsonpickle.load_backend('os.path', 'join', 'split', 'bad!')
        self.failIf(self._backend_is_partially_loaded('os.path'))
Exemplo n.º 22
0
    def test_load_backend_submodule(self):
        """Test that we can load a submodule as a backend

        """
        jsonpickle.load_backend('os.path', 'join', 'split', AttributeError)
Exemplo n.º 23
0
    def test_load_backend(self):
        """Test that we can call jsonpickle.load_backend()

        """
        jsonpickle.load_backend('simplejson', 'dumps', 'loads', ValueError)
Exemplo n.º 24
0
import sys,os, subprocess, ctypes
import graphviz as gv

if sys.version_info[0] != 3:
    print("This script is only python3 compatible!")
    exit(1)

from argparse import ArgumentParser

from attrdict import AttrMap
import xml.etree.ElementTree as ET

import commentjson
import jsonpickle
jsonpickle.load_backend('commentjson', 'dumps', 'loads', ValueError)

def jsonParse(s):
  return jsonpickle.decode(s) 
        
def jsonLoad(filePath):
  f = open(filePath)
  return jsonParse(f.read())
  

def main():
    parser = ArgumentParser()
    
    parser.add_argument("-f", "--files", dest="files", 
        help="The converter logic xml to visualize", metavar="<path>", action="append", required=True)
    
Exemplo n.º 25
0
    def test_load_backend_skips_bad_inputs(self):
        """Test that we ignore bad decoders"""

        jsonpickle.load_backend('os.path', 'join', 'bad!', AttributeError)
        self.failIf(self._backend_is_partially_loaded('os.path'))
Exemplo n.º 26
0
import firepython.utils
import firepython._const as CONST
from firepython.handlers import ThreadBufferedHandler

__all__ = [
    'FirePythonBase',
    'FirePythonDjango',
    'FirePythonWSGI',
    'paste_filter_factory',
]


# add a new backed jsonpickle for Django
# jsonpickle will attempt to import this if default
# jsonpickle libraries are not present
jsonpickle.load_backend('django.utils.simplejson', 'dumps',
                        'loads', ValueError)

class FirePythonBase(object):

    def __init__(self):
        raise NotImplementedError("Must be subclassed")

    def install_handler(self):
        logger = logging.getLogger(self._logger_name)
        self._handler = ThreadBufferedHandler()
        logger.addHandler(self._handler)

    def uninstall_handler(self):
        if self._handler is None:
            return
        logger = logging.getLogger(self._logger_name)
Exemplo n.º 27
0
import configparser


import jsonpickle
import jsonpickle.ext.numpy
import demjson

jsonBackend = "demjson"
jsonpickle.ext.numpy.register_handlers()
jsonpickle.load_backend(jsonBackend,"encode","decode", ValueError)
jsonpickle.set_preferred_backend(jsonBackend)
jsonpickle.set_decoder_options(jsonBackend,decode_float=float)

def jsonDump(obj,file,compact=False,*args,**kargs):
    global jsonBackend
    jsonpickle.set_encoder_options(jsonBackend,compactly=compact,*args,**kargs)
    file.write(jsonpickle.encode(obj))
    
def jsonParse(s):
    return jsonpickle.decode(s)
    
def jsonLoad(filePath):
    f = open(filePath)
    return jsonParse(f.read())
    


def loadIniFile(filePath):
    config = configparser.ConfigParser()
    config.read(filePath)
    return config
Exemplo n.º 28
0
    import gprof2dot
except ImportError:
    gprof2dot = None

import firepython
import firepython.utils
import firepython._const as CONST
from firepython.handlers import ThreadBufferedHandler

__all__ = ["FirePythonBase", "FirePythonDjango", "FirePythonWSGI", "paste_filter_factory"]


# add a new backed jsonpickle for Django
# jsonpickle will attempt to import this if default
# jsonpickle libraries are not present
jsonpickle.load_backend("django.utils.simplejson", "dumps", "loads", ValueError)


class FirePythonBase(object):
    def __init__(self):
        raise NotImplementedError("Must be subclassed")

    def install_handler(self):
        logger = logging.getLogger(self._logger_name)
        self._handler = ThreadBufferedHandler()
        logger.addHandler(self._handler)

    def uninstall_handler(self):
        if self._handler is None:
            return
        logger = logging.getLogger(self._logger_name)
Exemplo n.º 29
0
    def test_load_backend(self):
        """Test that we can call jsonpickle.load_backend()

        """
        jsonpickle.load_backend('simplejson', 'dumps', 'loads', ValueError)
        self.assertTrue(True)
Exemplo n.º 30
0
    def test_load_backend_submodule(self):
        """Test that we can load a submodule as a backend

        """
        jsonpickle.load_backend('os.path', 'join', 'split', AttributeError)
Exemplo n.º 31
0
import pickle
from io import BytesIO

import jsonpickle
import numpy

from common.scipy_helper import pdDF
from common.web_helper import firefox_quick_get_url

jsonpickle.load_backend('simplejson')
jsonpickle.set_encoder_options('simplejson',
                               sort_keys=True,
                               ensure_ascii=False)

stock_server_address = 'http://127.0.0.1:9966'


def _visit_server(oper_str, headers=None):
    url = stock_server_address + '/' + oper_str
    resp = firefox_quick_get_url(url, timeout=0.5, headers=headers)
    byteio = BytesIO()
    for chunk in resp.iter_content(1024):
        byteio.write(chunk)
    val = pickle.loads(byteio.getvalue())
    assert resp.status_code == 200
    return val


def _np_compound_arr_to_df(arr):
    val = pdDF(arr)
    val['datetime'] = (val['datetime'] / 1000000).astype(numpy.int64)
Exemplo n.º 32
0
import firepython
import firepython.utils
import firepython._const as CONST
from firepython.handlers import ThreadBufferedHandler

__all__ = [
    'FirePythonBase',
    'FirePythonDjango',
    'FirePythonWSGI',
    'paste_filter_factory',
]

# add a new backed jsonpickle for Django
# jsonpickle will attempt to import this if default
# jsonpickle libraries are not present
jsonpickle.load_backend('django.utils.simplejson', 'dumps', 'loads',
                        ValueError)


class FirePythonBase(object):
    def __init__(self):
        raise NotImplementedError("Must be subclassed")

    def install_handler(self):
        logger = logging.getLogger(self._logger_name)
        self._handler = ThreadBufferedHandler()
        logger.addHandler(self._handler)

    def uninstall_handler(self):
        if self._handler is None:
            return
        logger = logging.getLogger(self._logger_name)
Exemplo n.º 33
0
from flask import Response
import jsonpickle
from app.libs.errorhandler import compose_error
from app.consts.errors import ERROR_MAP


jsonpickle.load_backend('json', 'dumps', 'loads', ValueError)
jsonpickle.set_preferred_backend('json')
jsonpickle.set_encoder_options('json', ensure_ascii=False)


def jsonify(raw=None, status_code=200):
    resp = Response(jsonpickle.encode(raw), mimetype='application/json')
    resp.status_code = status_code
    return resp


def error_jsonify(error_code, specifiy_error="", status_code=400):
    error_resp = compose_error(specifiy_error if specifiy_error else ERROR_MAP[error_code], error_code)
    return jsonify(error_resp, status_code)


def jsonify_cors(raw=None, status_code=200):
    res = jsonify(raw, status_code)
    res.headers['Access-Control-Allow-Origin'] = '*'
    res.headers['Access-Control-Allow-Method'] = '*'
    res.headers['Access-Control-Allow-Headers'] = '*'
    res.headers['Content-Type'] = 'text/plain;charset=UTF-8'
    return res

Exemplo n.º 34
0
 def add_backend(self, backend_string):
     jsonpickle.load_backend(backend_string)
     jsonpickle.set_preferred_backend(backend_string)