Пример #1
0
class DummyRoom:
    def __init__(self, id, message, exits):
        self.id = id
        self.is_outside = False
        self.set_message(message)
        # exits is a dict exits["east"] = "A"
        self.exits = exits
        self.encoder = JSONEncoder(indent=4)

    def __repr__(self):
        return "Room %s Exits %s" % (self.id, str(self.exits))

    def set_message(self, message):
        # Assign message
        self.message = message

        # Mark as outside if that's the case
        if ("location" in message) and (message["location"] == "outside the castle"):
            self.is_outside = True

    def get_message(self):
        return self.message

    def print_message(self):
        print self.encoder.encode(self.message)

    def get_exit_room_id(self, direction):
        return self.exits[direction]
Пример #2
0
 def default(self, obj):
     if isinstance(
             obj, (list, dict, str, unicode, int, float, bool, type(None))):
         return JSONEncoder.encode(self, obj)
     elif isinstance(obj, set):
         return JSONEncoder.encode(
             self, list(obj)
         )  #str(obj) #"set([{}])".format(",".join([ PythonObjectEncoder.default(self,i) for i in list(obj)]))
     return pickle.dumps(obj)
Пример #3
0
 def __init__(self, value):
     # Ensure the value is a serializable by trying to encode it.
     # WARNING: The value may change before it is encoded again, and may
     #          not be encodable after the change.
     try:
         e = JSONEncoder()
         e.encode(value)
     except TypeError:
         raise
     self.value = value
Пример #4
0
 def __init__(self, value):
     # Ensure the value is a serializable by trying to encode it.
     # WARNING: The value may change before it is encoded again, and may
     #          not be encodable after the change.
     try:
         e = JSONEncoder()
         e.encode(value)
     except TypeError:
         raise
     self.value = value
Пример #5
0
    def encode(self, obj):

        if isinstance(obj, TwitterMention):
            obj_as_dict = dict(ID=obj.id,
                               FROM_ID=obj.from_id,
                               FROM_SCREEN_NAME=str(obj.from_screen_name),
                               MESSAGE_TEXT=str(obj.message_text))

            return JSONEncoder.encode(self, obj_as_dict)

        return JSONEncoder.encode(self, obj)
Пример #6
0
class RestUtils(object):
    def __init__(self):
        """Initialization method
        """

        self.api_url = SDC_SERVER
        self.encoder = JSONEncoder()

    def _call_api(self,
                  pattern,
                  method,
                  body=None,
                  headers=None,
                  payload=None,
                  **kwargs):
        """Launch HTTP request to Policy Manager API with given arguments
        :param pattern: string pattern of API url with keyword arguments (format string syntax)
        :param method: HTTP method to execute (string)
        :param body: JSON body content (dict)
        :param headers: HTTP header request (dict)
        :param payload: Query parameters for the URL
        :param **kwargs: URL parameters (without url_root) to fill the patters
        :returns: REST API response
        """

        kwargs['url_root'] = self.api_url

        url = pattern.format(**kwargs)

        print 'METHOD: {}\nURL: {} \nHEADERS: {} \nBODY: {}'.format(
            method, url, headers, self.encoder.encode(body))

        try:
            if headers[CONTENT_TYPE] == CONTENT_TYPE_JSON:
                r = requests.request(method=method,
                                     url=url,
                                     data=self.encoder.encode(body),
                                     headers=headers,
                                     params=payload)
            else:
                r = requests.request(method=method,
                                     url=url,
                                     data=body,
                                     headers=headers,
                                     params=payload)

        except Exception, e:
            print "Request {} to {} crashed: {}".format(method, url, str(e))
            return None

        return r
Пример #7
0
class RestUtils(object):

    def __init__(self):
        """Initialization method
        """

        self.api_url = SERVER_ROOT
        print "Initialized API REST Utils"

        self.encoder = JSONEncoder()
        self.initialize_logging()

    def initialize_logging(self):
        self.logger = logging.getLogger('forum')
        hdlr = logging.FileHandler('forum_testing.log')
        formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
        hdlr.setFormatter(formatter)
        self.logger.addHandler(hdlr)
        self.logger.setLevel(logging.DEBUG)

    def _call_api(self, pattern, method, body=None, headers=HEADERS, payload=None, auth=None, **kwargs):

        """Launch HTTP request to Policy Manager API with given arguments
        :param pattern: string pattern of API url with keyword arguments (format string syntax)
        :param method: HTTP method to execute (string)
        :param body: JSON body content (dict)
        :param headers: HTTP header request (dict)
        :param payload: Query parameters for the URL
        :param **kwargs: URL parameters (without url_root) to fill the patters
        :returns: REST API response
        """

        kwargs['url_root'] = self.api_url

        url = pattern.format(**kwargs)
        self.logger.info('NEW REQUEST TO SEND')
        self.logger.info('\nMETHOD: {}\nURL: {} \nHEADERS: {} \nBODY: {}'.format(method, url, headers,
                                                                                 self.encoder.encode(body)))

        try:
            r = requests.request(method=method, url=url, data=self.encoder.encode(body), headers=headers,
                                 params=payload, auth=auth)

        except Exception, e:
            print "Request {} to {} crashed: {}".format(method, url, str(e))
            return None

        return r
Пример #8
0
class RestUtils(object):

    def __init__(self):
        """Initialization method
        """

        self.api_url = SERVER_ROOT
        print "Initialized API REST Utils"

        self.encoder = JSONEncoder()
        self.initialize_logging()

    def initialize_logging(self):
        self.logger = logging.getLogger('forum')
        hdlr = logging.FileHandler('forum_testing.log')
        formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
        hdlr.setFormatter(formatter)
        self.logger.addHandler(hdlr)
        self.logger.setLevel(logging.DEBUG)

    def _call_api(self, pattern, method, body=None, headers=HEADERS, payload=None, auth=None, **kwargs):

        """Launch HTTP request to Policy Manager API with given arguments
        :param pattern: string pattern of API url with keyword arguments (format string syntax)
        :param method: HTTP method to execute (string)
        :param body: JSON body content (dict)
        :param headers: HTTP header request (dict)
        :param payload: Query parameters for the URL
        :param **kwargs: URL parameters (without url_root) to fill the patters
        :returns: REST API response
        """

        kwargs['url_root'] = self.api_url

        url = pattern.format(**kwargs)
        self.logger.info('NEW REQUEST TO SEND')
        self.logger.info('\nMETHOD: {}\nURL: {} \nHEADERS: {} \nBODY: {}'.format(method, url, headers,
                                                                                 self.encoder.encode(body)))

        try:
            r = requests.request(method=method, url=url, data=self.encoder.encode(body), headers=headers,
                                 params=payload, auth=auth)

        except Exception, e:
            print "Request {} to {} crashed: {}".format(method, url, str(e))
            return None

        return r
Пример #9
0
class Rest_Utils(object):

    def __init__(self, username='******', password='******'):
        """Initialization method
        :param domain_id: ID of the BE domain calling Sprayer
        """
        self.username = username
        self.password = password
        self.api_url = SERVER
        self.encoder = JSONEncoder()

    def _call_api(self, pattern, method='post', body=None, headers=HEADERS, payload=None,
                  **kwargs):
        """Launch HTTP request against Sprayer API with given arguments
        :param pattern: string pattern of API url with keyword arguments (format string syntax)
        :param method: HTTP method to execute
        :param body: dictionary with JSON body content
        :param **kwargs: url parameters (without url_root or domain_id) to fill url pattern
        :returns: REST API response Json body (dict)
        """
        kwargs['url_root'] = self.api_url

        url = pattern.format(**kwargs)

        try:
            r = requests.request(method=method, url=url, data=self.encoder.encode(body), headers=headers,
                                 params=payload, auth=(self.username, self.password))
        except Exception, e:
            return None
        return r
Пример #10
0
 def __call__(self):
     country = self.request.get('country')
     required = self.request.get('required', '').lower().strip() == 'true'
     utility = component.getUtility(ICountriesStates)
     jsonEncoder = JSONEncoder()
     return jsonEncoder.encode(
         utility.states(country=country, allow_no_values=not required))
Пример #11
0
    def _createPileupConfigFile(self, helper, fakeSites=None):
        """
        Stores pileup JSON configuration file in the working
        directory / sandbox.

        """

        if fakeSites is None:
            fakeSites = []

        if self._isCacheValid(helper):
            # we need to update the new sandbox json file in case TrustPUSitelists is on
            if fakeSites:
                self._updatePileupPNNs(helper, fakeSites)

            # if file already exist don't make a new dbs call and overwrite the file.
            # just return
            return

        encoder = JSONEncoder()
        # this should have been set in CMSSWStepHelper along with
        # the pileup configuration
        url = helper.data.dbsUrl

        dbsReader = DBSReader(url)

        configDict = self._queryDbsAndGetPileupConfig(helper, dbsReader,
                                                      fakeSites)

        # create JSON and save into a file
        jsonPU = encoder.encode(configDict)
        self._saveFile(helper, jsonPU)
Пример #12
0
    def optimizations(self, json_list, target_contexts):
        jsons = JSONEncoder().encode(json_list)
        print(jsons)

        with tempfile.NamedTemporaryFile() as fp:
            fp.write(jsons.encode('utf-8'))
            outputs = self.driver.decode(fp.name)
            print(outputs)

        for key in outputs:
            if 'success' in outputs[key]:
                newstr = ''
                for item in outputs[key][1][1:-1].split(','):
                    newstr = newstr + (chr(eval(item)))
                print(newstr)

                for item in target_contexts[key]:
                    old_body = item[0].body
                    target_context = item[1]
                    new_context = item[2] + '"' + newstr + '"'
                    item[0].body = old_body.replace(target_context,
                                                    new_context)
                    item[0].modified = True
                    self.make_changes = True

        self.smali_files_update()
Пример #13
0
    def encode(self, o):
        if self.check_circular:
            markers = {}
        else:
            markers = None

        def _encode(o):
            if isinstance(o, (list, tuple, dict)):
                if markers is not None:
                    marker_id = id(o)
                    if marker_id in markers:
                        raise ValueError("Circular reference detected")
                    markers[marker_id] = o
                try:
                    if isinstance(o, dict):
                        return {k: _encode(v) for k, v in o.items()}
                    else:
                        return [_encode(v) for v in o]
                finally:
                    if markers is not None:
                        del markers[marker_id]

            if isinstance(o, date):
                return {"$date": int(calendar.timegm(o.timetuple()) * 1000)}
            if isinstance(o, datetime):
                return {"$date": int(calendar.timegm(o.utctimetuple()) * 1000)}

            if isinstance(o, Reference):
                # FIXME if reference is not saved, save it first here
                return {"$ref": o._uri}

            return o

        return JSONEncoder.encode(self, _encode(o))
Пример #14
0
    def generate_link_from_dict(self, dct):
        """
        Creates a link by analyzing a view-dictionary and merging it to this view
        The incoming dictionary can be thought of as a diff that is added to the 
        existing view.
        Does not affect the View itself.
        """
        target = {}
        target['s'] = self.get_page()
        target['v'] = deepcopy(self.get_space_widget_mapping())
        target['b'] = deepcopy(self.get_box_mapping())
        target['c'] = deepcopy(self.get_widget_param_mapping())
        
        if dct.has_key('s'):
            target['s'] = dct['s']
        if dct.has_key('v'):
            target['v'].update(dct['v'])
        if dct.has_key('b'):
            target['b'].update(dct['b'])
        if dct.has_key('c'):
            for widget_id in dct['c'].keys():
                target['c'][widget_id] = dct['c'][widget_id]
        if dct.has_key('p'):
            target['p'] = dct['p']

        encoder = JSONEncoder()
        viewjsonstring = quote(encoder.encode(target))
        view_manager = self._core.get_view_manager()
        checkview = view_manager.get_from_json(viewjsonstring)
        existing_name = checkview.check_has_name()
        if existing_name == False:
            return "/web/?"+viewjsonstring
        else:
            return "/web/"+existing_name
Пример #15
0
 def get_tree_json(cls, story, user):    
     chapters = Chapter.objects.filter(story=story)
     if user.is_authenticated():
         chapters.select_related('readers', 'bookmarkers', 'likers')
     nodes = []
     for c in chapters:
         parent_pk = c.parent.pk if c.parent else None
         url = reverse('read_story', kwargs={'pk': story.pk})
         url += '?chapter-pk={}'.format(c.pk)
         node = {'pk': c.pk, 'parent': parent_pk, 'title': c.headline[:20],
                 'children': [], 'url': url}
         for n in nodes:
             if n['parent'] == c.pk: node['children'].append(n)
             elif parent_pk == n['pk']: n['children'].append(node)
         nodes.append(node)
         
         # TODO: pick color in template
         if user in c.bookmarkers.all(): node['color'] = 'blue'
         elif user in c.likers.all(): node['color'] = 'red'
         elif user in c.readers.all(): node['color'] = 'green'
         else: node['color'] = 'grey'
         
         if not parent_pk:
             root = [node]
     encoder = JSONEncoder()
     return encoder.encode(root)
Пример #16
0
class ExternalR2:
    
    def __init__(self, cmd, cwd, args=[]):
        self.cmd = cmd
        self.cwd = cwd
        self.args = args
        self.je = JSONEncoder(ensure_ascii=True)
        self.jd = JSONDecoder()
        self.proc = None
        self.output = None
        self.err = None

    def start(self, sendData=None):
        self.proc = Popen(args=[self.cmd] + self.args, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=self.cwd)
        self.output, self.err = self.proc.communicate(input=sendData)
        return self
    
    def start_fitness(self, x):
        j = self.je.encode(x.tolist())
        b = b64encode(j.encode())
        return self.start(sendData=b)
    
    def wait(self):
        pResult = self.proc.wait()
        if (pResult != 0):
            raise Exception("The process exited with an error: {}".format(self.err))

        # The process is expected to output a JSON.
        return self.jd.decode(b64decode(self.output).decode())
Пример #17
0
 def __call__(self):
     country = self.request.get('country')
     required = self.request.get('required','').lower().strip() == 'true'
     utility = component.getUtility(ICountriesStates)
     jsonEncoder = JSONEncoder()
     return jsonEncoder.encode(utility.states(country=country,
                                              allow_no_values=not required))
Пример #18
0
    def generate_link_from_actionlist(self, actionlist):
        """
        Generates an ajaxlink from an actionlist.
        ajaxlinks look like:
        javascript:SkdAjax.load_link([{'w':<widget_id>,'p':{params}}, ... ])

        the corresponding js on clientside should generate something like:
        /ajax/{'w':<widget_id>,'p':{params}}
        """
        link = "javascript:window.SkdAJAX.execute_action(%s);"
        linkjson = []
        for action in actionlist.get_actions():
            if action.get_url() is not None:
                return action.get_url()
            elif action.get_view_id() is not None:
                view = View.get_from_id(action.get_view_id())
                name = view.get_name()
                return "/web/"+quote(name)
            elif action.get_space() is not None and action.get_widget_id() is not None:
                page = Page.get_page(self.get_page())
                space_names = page.get_space_names()
                space_name = space_names[action.get_space()]
                linkjson.append({"w":action.get_widget_id(), "s":space_name, "p":{}})
        encoder = JSONEncoder()
        ajaxdata = encoder.encode(linkjson)
        ajaxdata = ajaxdata.replace('"',"'")
        return link%ajaxdata
Пример #19
0
def main():
    girl = []
    num_girl = 0
    girl_history = []
    for num, user in enumerate(users.iterrows()):
        user = user[1]
        filter_girl = []
        if num > -1:
            user_sub = subs[subs['user_id'] == user['user_id']]

            filter_girl.append(uf.sex_filter(user, 1))
            filter_girl.append(
                uf.age_filter(user, list(range(1988, 2001)), True))
            filter_girl.append(uf.top_univer_filter(user, user_sub))
            filter_girl.append(uf.travel_filter(user, user_sub, threshold=0))

            if all(filter_girl):
                girl.append(user['user_id'])
                num_girl += 1
            girl_history.append(filter_girl)
            if num % 500 == 0 and num > 0:
                print(f"girl: {num_girl}")
                print(f"random_id girl: {girl[-1]}")

    j = JSONEncoder()
    with open(f'./filtered_users/girl_{FILE_NAME}.json', 'w') as fw:
        fw.write(j.encode(girl))
Пример #20
0
    def encode(self, o):
        if self.check_circular:
            markers = {}
        else:
            markers = None

        def _encode(o):
            if isinstance(o, (list, tuple, dict)):
                if markers is not None:
                    marker_id = id(o)
                    if marker_id in markers:
                        raise ValueError("Circular reference detected")
                    markers[marker_id] = o
                try:
                    if isinstance(o, dict):
                        return {k: _encode(v) for k, v in o.items()}
                    else:
                        return [_encode(v) for v in o]
                finally:
                    if markers is not None:
                        del markers[marker_id]

            if isinstance(o, date):
                return {"$date": int(calendar.timegm(o.timetuple()) * 1000)}
            if isinstance(o, datetime):
                return {"$date": int(calendar.timegm(o.utctimetuple()) * 1000)}

            if isinstance(o, Reference):
                # FIXME if reference is not saved, save it first here
                return {"$ref": o._uri}

            return o

        return JSONEncoder.encode(self, _encode(o))
Пример #21
0
class GithubRequest(object):
    def __init__(self, username, password):
        self._username = username
        self._password = password
        self._create_connection()
        self._create_auth_header()
        self._decoder = JSONDecoder()
        self._encoder = JSONEncoder()

    def _create_auth_header(self):
        userpass = '******' % (self._username, self._password)
        authkey = base64.b64encode(userpass).replace('\n','')
        self._auth_header = {}
        self._auth_header['Authorization'] = 'Basic %s' % authkey

        
    def _create_connection(self):
        self._connection = HTTPSConnection(GITHUBAPI)
        self._connection.connect()

        
    def request(self, method, url, params = None):
        if params != None:
            jsondata = self._encoder.encode(params)
        else:
            jsondata = None
        
        self._connection.request(method,url,jsondata,self._auth_header)
        response = self._connection.getresponse()
        jsonresponse = response.read()
        textresponse = self._decoder.decode(jsonresponse)
        return textresponse
Пример #22
0
    def generate_link_from_actionlist(self,actionlist):
        """
        returns a link that describes a call to a view that results of the actionlist
        """
        target = {}
        target['s'] = self.get_page()
        target['v'] = deepcopy(self.get_space_widget_mapping())
        target['b'] = deepcopy(self.get_box_mapping())
        target['c'] = deepcopy(self.get_widget_param_mapping())
        
        for action in actionlist.get_actions():
            if action.get_url() is not None:
                return action.get_url()
            elif action.get_view_id() is not None:
                view = actionlist._core.get_view_manager().get_from_id(action.get_view_id())
                name = view.get_name()
                return "/web/"+quote(name)
            elif action.get_space() is not None and action.get_widget_id() is not None:
                target['v'][action.get_space()] = action.get_widget_id()
                #delete any parameters of this widget. otherwise link will only
                #load current state of that widget again
                if target['c'].has_key(action.get_widget_id()):
                    del(target['c'][action.get_widget_id()])

        encoder = JSONEncoder()
        viewjsonstring = quote(encoder.encode(target))
        view_manager = self._core.get_view_manager()
        checkview = view_manager.get_from_json(viewjsonstring)
        existing_name = checkview.check_has_name()
        if existing_name == False:
            return "/web/?"+viewjsonstring
        else:
            return "/web/"+existing_name
    def test_command_parser(self):

        parser = search_command_internals.SearchCommandParser()
        encoder = JSONEncoder()
        file_path = os.path.abspath(os.path.join(TestSearchCommandInternals._package_path, 'data', 'input', '_empty.csv'))

        options = [
            'boolean=true',
            'duration=00:00:10',
            'fieldname=word_count',
            'file=%s' % encoder.encode(file_path),
            'integer=10',
            'optionname=foo_bar',
            'regularexpression="\\\\w+"',
            'set=foo']

        fields = ['field_1', 'field_2', 'field_3']

        command = StubbedStreamingCommand()  # All options except for the builtin options are required
        parser.parse(options + fields, command)
        command_line = str(command)

        self.assertEqual(
            'stubbedstreaming boolean=true duration=10 fieldname="word_count" file=%s integer=10 optionname="foo_bar" regularexpression="\\\\w+" set="foo" field_1 field_2 field_3' % encoder.encode(file_path),
            command_line)

        for option in options:
            self.assertRaises(ValueError, parser.parse, [x for x in options if x != option] + ['field_1', 'field_2', 'field_3'], command)

        command = StubbedReportingCommand()  # No options are required
        parser.parse(options + fields, command)

        for option in options:
            try:
                parser.parse([x for x in options if x != option] + ['field_1', 'field_2', 'field_3'], command)
            except Exception as e:
                self.assertFalse("Unexpected exception: %s" % e)

        try:
            parser.parse(options, command)
        except Exception as e:
            self.assertFalse("Unexpected exception: %s" % e)

        for option in command.options.itervalues():
            if option.name in ['show_configuration', 'logging_configuration', 'logging_level']:
                continue
            self.assertTrue(option.is_set)

        self.assertEqual(len(command.fieldnames), 0)

        try:
            parser.parse(fields, command)
        except Exception as e:
            self.assertFalse("Unexpected exception: %s" % e)

        for option in command.options.itervalues():
            self.assertFalse(option.is_set)

        self.assertListEqual(fields, command.fieldnames)
        return
Пример #24
0
    def _createPileupConfigFile(self, helper, fakeSites=None):
        """
        Stores pileup JSON configuration file in the working
        directory / sandbox.

        """

        if fakeSites is None:
            fakeSites = []

        if self._isCacheValid(helper):
            # we need to update the new sandbox json file in case TrustPUSitelists is on
            if fakeSites:
                self._updatePileupPNNs(helper, fakeSites)

            # if file already exist don't make a new dbs call and overwrite the file.
            # just return
            return

        encoder = JSONEncoder()
        # this should have been set in CMSSWStepHelper along with
        # the pileup configuration
        url = helper.data.dbsUrl

        dbsReader = DBSReader(url)

        configDict = self._queryDbsAndGetPileupConfig(helper, dbsReader, fakeSites)

        # create JSON and save into a file
        jsonPU = encoder.encode(configDict)
        self._saveFile(helper, jsonPU)
Пример #25
0
    def generate_link_from_dict(self, dct):
        """
        Creates a link by analyzing a view-dictionary and merging it to this view
        The incoming dictionary can be thought of as a diff that is added to the 
        existing view.
        Does not affect the View itself.
        """
        target = {}
        target['s'] = self.get_page()
        target['v'] = deepcopy(self.get_space_widget_mapping())
        target['b'] = deepcopy(self.get_box_mapping())
        target['c'] = deepcopy(self.get_widget_param_mapping())

        if dct.has_key('s'):
            target['s'] = dct['s']
        if dct.has_key('v'):
            target['v'].update(dct['v'])
        if dct.has_key('b'):
            target['b'].update(dct['b'])
        if dct.has_key('c'):
            for widget_id in dct['c'].keys():
                target['c'][widget_id] = dct['c'][widget_id]
        if dct.has_key('p'):
            target['p'] = dct['p']

        encoder = JSONEncoder()
        viewjsonstring = quote(encoder.encode(target))
        view_manager = self._core.get_view_manager()
        checkview = view_manager.get_from_json(viewjsonstring)
        existing_name = checkview.check_has_name()
        if existing_name == False:
            return "/web/?" + viewjsonstring
        else:
            return "/web/" + existing_name
Пример #26
0
    def generate_link_from_actionlist(self, actionlist):
        """
        returns a link that describes a call to a view that results of the actionlist
        """
        target = {}
        target['s'] = self.get_page()
        target['v'] = deepcopy(self.get_space_widget_mapping())
        target['b'] = deepcopy(self.get_box_mapping())
        target['c'] = deepcopy(self.get_widget_param_mapping())

        for action in actionlist.get_actions():
            if action.get_url() is not None:
                return action.get_url()
            elif action.get_view_id() is not None:
                view = actionlist._core.get_view_manager().get_from_id(
                    action.get_view_id())
                name = view.get_name()
                return "/web/" + quote(name)
            elif action.get_space() is not None and action.get_widget_id(
            ) is not None:
                target['v'][action.get_space()] = action.get_widget_id()
                #delete any parameters of this widget. otherwise link will only
                #load current state of that widget again
                if target['c'].has_key(action.get_widget_id()):
                    del (target['c'][action.get_widget_id()])

        encoder = JSONEncoder()
        viewjsonstring = quote(encoder.encode(target))
        view_manager = self._core.get_view_manager()
        checkview = view_manager.get_from_json(viewjsonstring)
        existing_name = checkview.check_has_name()
        if existing_name == False:
            return "/web/?" + viewjsonstring
        else:
            return "/web/" + existing_name
Пример #27
0
class RestUtils(object):

    def __init__(self):
        """Initialization method
        """

        self.api_url = PUPPET_WRAPPER_SERVER
        self.encoder = JSONEncoder()

    def _call_api(self, pattern, method, body=None, headers=HEADERS, payload=None, **kwargs):

        """Launch HTTP request to Policy Manager API with given arguments
        :param pattern: string pattern of API url with keyword arguments (format string syntax)
        :param method: HTTP method to execute (string)
        :param body: JSON body content (dict)
        :param headers: HTTP header request (dict)
        :param payload: Query parameters for the URL
        :param **kwargs: URL parameters (without url_root) to fill the patters
        :returns: REST API response
        """

        kwargs['url_root'] = self.api_url

        url = pattern.format(**kwargs)

        try:
            r = requests.request(method=method, url=url, data=self.encoder.encode(body), headers=headers,
                                 params=payload)

        except Exception, e:
            print "Request {} to {} crashed: {}".format(method, url, str(e))
            return None

        return r
Пример #28
0
def writeMaterial(f, matName, matIndex):
    mat = bpy.data.materials[matName]

    diffuse = mat.diffuse_color
    specular = mat.specular_color
    shadeless = mat.use_shadeless

    json = {
        "diffuseColor": [diffuse.r, diffuse.g, diffuse.b, 1.0],
        "specularColor": [specular.r, specular.g, specular.b, 1.0],
        "shadeless": int(shadeless)
    }

    # save texture slots

    # get number of active slots
    texSlotsNum = 0
    for texSloti, texSlot in enumerate(mat.texture_slots):
        if texSloti < 8:
            if not texSlot is None:
                if texSlot.texture.type == 'IMAGE':
                    if texSlot.texture.image:
                        texSlotsNum = texSlotsNum + 1

    json["texturesNum"] = texSlotsNum
    json["textures"] = []

    for texSloti, texSlot in enumerate(mat.texture_slots):
        # write maximum of 8 texture slots to maintain compatibility with legacy OpenGL
        if texSloti < 8:
            if not texSlot is None:
                tex = texSlot.texture
                if tex.type == 'IMAGE':
                    if tex.image:
                        imgFilename = os.path.basename(tex.image.filepath)
                        print(imgFilename)
                        print(texSlot.blend_type)
                        blend_type = 0
                        if texSlot.blend_type == 'MIX':
                            blend_type = 0
                        elif texSlot.blend_type == 'MULTIPLY':
                            blend_type = 1
                        else:
                            blend_type = 0
                        json["textures"] += [{
                            "filename": imgFilename,
                            "blendType": blend_type
                        }]

    jsonStr = JSONEncoder().encode(json)
    print(jsonStr)

    jsonASCII = jsonStr.encode('ascii')
    jsonSize = len(jsonASCII)

    matData = bytearray()
    matData = matData + struct.pack('<I', jsonSize)
    matData = matData + bytearray(jsonASCII)

    f.write(packChunk(ChunkType.MATERIAL, matIndex, matName, matData))
Пример #29
0
class JSON(AbstractCodec):
    def __init__(self, types=(), converters=(), custom_typeid='__type__'):
        self.types = AbstractCodec.expand_datatype_list(types)
        self.converters = AbstractCodec.expand_converter_list(converters)
        self.custom_typeid = custom_typeid

        self.encoder = JSONEncoder(default=self._box_object)
        self.decoder = JSONDecoder(object_hook=self._unbox_object)

        self.encoders = dict(
            (datatype, (idx, encode))
            for idx, (datatype, encode, decode) in enumerate(self.types))
        self.inflaters = tuple(
            (idx + len(self.types), inflate)
            for idx, (inflate, deflate) in enumerate(self.converters))

        self.decoders = tuple(decode
                              for datatype, encode, decode in self.types)
        self.decoders += tuple(deflate for inflate, deflate in self.converters)

    def _box_object(self, obj):
        try:
            typeid, encode = self.encoders[type(obj)]
            return {self.custom_typeid: (typeid, encode(obj))}
        except KeyError:
            for typeid, inflate in self.inflaters:
                result = inflate(obj)
                if result is not None:
                    break
            else:
                raise TypeError('Cannot encode objects of type %s' %
                                type(obj).__name__)

            return {self.custom_typeid: (typeid, result)}

    def _unbox_object(self, obj):
        try:
            typeid, data = obj[self.custom_typeid]
        except (KeyError, ValueError):
            return obj

        try:
            decode = self.decoders[typeid]
        except IndexError:
            raise TypeError('Invalid type identifier %s' % typeid)

        return decode(data)

    def decode(self, data):
        try:
            return self.decoder.decode(data)
        except BaseException as e:
            raise ValueError('Failed to decode "%s"' % str(data))

    def encode(self, obj):
        try:
            return self.encoder.encode(obj)
        except BaseException as e:
            raise ValueError('Failed to encode "%s"' % str(obj))
Пример #30
0
def inform_sns(arns: list, message: str, region):
    jsonizer = JSONEncoder()
    sns_topics = set(arns)
    sns = BotoClientProxy('sns', region_name=region)
    for sns_topic in sns_topics:
        sns.publish(TopicArn=sns_topic,
                    Subject="SenzaTrafficRedirect",
                    Message=jsonizer.encode(message))
Пример #31
0
def reptree(node, charset = 'utf-8'):
	from json import JSONEncoder
	enc = JSONEncoder(encoding = charset)

	if node.symbol is not None:
		return enc.encode(node.symbol)
	else:
		return "c()?" + reptree(node.right) + ":" + reptree(node.left)
Пример #32
0
 def default(self, o):
     if isinstance(o, datetime.datetime):
         return o.isoformat()
     if isinstance(o, datetime.date):
         return o.isoformat()
     if isinstance(o, users.User):
         return unicode(o)
     return JSONEncoder.encode(self, o)
Пример #33
0
def inform_sns(arns: list, message: str, region):
    jsonizer = JSONEncoder()
    sns_topics = set(arns)
    sns = BotoClientProxy('sns', region_name=region)
    for sns_topic in sns_topics:
        sns.publish(TopicArn=sns_topic,
                    Subject="SenzaTrafficRedirect",
                    Message=jsonizer.encode(message))
Пример #34
0
def reptree(node, charset='utf-8'):
    from json import JSONEncoder
    enc = JSONEncoder(encoding=charset)

    if node.symbol is not None:
        return enc.encode(node.symbol)
    else:
        return "c()?" + reptree(node.right) + ":" + reptree(node.left)
Пример #35
0
 def encode(self, data):
     """
     encode data as json
     """
     encoder = JSONEncoder()
     thunker = JSONThunker()
     thunked = thunker.thunk(data)
     return encoder.encode(thunked)
Пример #36
0
 def encode(self, data):
     """
     encode data as json
     """
     encoder = JSONEncoder()
     thunker = JSONThunker()
     thunked = thunker.thunk(data)
     return encoder.encode(thunked)
    def test_command_parser(self):
        from splunklib.searchcommands.search_command_internals import \
            SearchCommandParser
        parser = SearchCommandParser()
        encoder = JSONEncoder()
        file_path = TestSearchCommandsApp._data_file(os.path.join('input', 'counts.csv'))

        options = [
            'boolean=true',
            'duration=00:00:10',
            'fieldname=word_count',
            'file=%s' % encoder.encode(file_path),
            'integer=10',
            'optionname=foo_bar',
            'regularexpression="\\\\w+"',
            'set=foo']
        fields = ['field_1', 'field_2', 'field_3']

        command = StubbedStreamingCommand()  # All options are required
        parser.parse(options + fields, command)
        command_line = str(command)

        self.assertEqual(
            'stubbedstreaming boolean=true duration=10 fieldname="word_count" file=%s integer=10 optionname="foo_bar" regularexpression="\\\\w+" set="foo" field_1 field_2 field_3' % encoder.encode(file_path),
            command_line)

        for option in options:
            self.assertRaises(ValueError, parser.parse, [x for x in options if x != option] + ['field_1', 'field_2', 'field_3'], command)

        command = StubbedReportingCommand()  # No options are required
        parser.parse(options + fields, command)

        for option in options:
            try:
                parser.parse([x for x in options if x != option] + ['field_1', 'field_2', 'field_3'], command)
            except Exception as e:
                self.assertFalse("Unexpected exception: %s" % e)

        try:
            parser.parse(options, command)
        except Exception as e:
            self.assertFalse("Unexpected exception: %s" % e)

        for option in command.options.itervalues():
            self.assertTrue(option.is_set)

        self.assertEqual(len(command.fieldnames), 0)

        try:
            parser.parse(fields, command)
        except Exception as e:
            self.assertFalse("Unexpected exception: %s" % e)

        for option in command.options.itervalues():
            self.assertFalse(option.is_set)

        self.assertListEqual(fields, command.fieldnames)
        return
Пример #38
0
class CrabWebQuery:
    """CherryPy handler class for the JSON query part of the crab web
    interface."""

    def __init__(self, store, monitor, service):
        """Constructor: saves the given storage backend and reference
        to the monitor thread."""

        self.store = store
        self.monitor = monitor
        self.service = service

        def to_json(obj):
            if isinstance(obj, datetime):
                return format_datetime(obj)
            raise TypeError('Cannot JSON-encode object')

        self.json_encoder = JSONEncoder(default=to_json)

    @cherrypy.expose
    def jobstatus(self, startid, alarmid, finishid):
        """CherryPy handler returning the job status dict fetched
        from the monitor thread."""

        try:
            s = self.monitor.wait_for_event_since(int(startid),
                                                  int(alarmid), int(finishid))
            s['service'] = dict((s, self.service[s].is_alive())
                                for s in self.service)
            return self.json_encoder.encode(s)
        except ValueError:
            raise HTTPError(400, 'Query parameter not an integer')

    @cherrypy.expose
    def jobinfo(self, id_):
        """CherryPy handler returning the job information for the given job."""
        try:
            info = self.store.get_job_info(int(id_))
        except ValueError:
            raise HTTPError(400, 'Job ID not a number')
        if info is None:
            raise HTTPError(404, 'Job not found')

        info["id"] = id_
        return self.json_encoder.encode(info)
Пример #39
0
class Renderer(abc.Renderer):

    encoder = None

    def __init__(self):
        self.encoder = JSONEncoder()

    def __call__(self, view_model, context=None):
        return self.encoder.encode(view_model.data)
Пример #40
0
 def __call__(self, instructions):
     jd = JSONDecoder()
     je = JSONEncoder()
     answer = {}
     try:
         instructions = jd.decode(instructions)
     except ValueError, e:
         answer['error'] = "could not decode instructions"
         self._core.response_body.append(je.encode(answer))
         return
Пример #41
0
def wrap2dasjson(data):
    """DAS JSON wrapper"""
    encoder = JSONEncoder()
    cherrypy.response.headers['Content-Type'] = "text/json"
    try:
        jsondata = encoder.encode(data)
        return jsondata
    except:
        return dict(error="Failed to JSONtify obj '%s' type '%s'" \
            % (data, type(data)))
Пример #42
0
 def __call__(self, instructions):
     jd = JSONDecoder()
     je = JSONEncoder()
     answer = {}
     try:
         instructions = jd.decode(instructions)
     except ValueError, e:
         answer['error'] = "could not decode instructions"
         self._core.response_body.append(je.encode(answer))
         return
Пример #43
0
def wrap2dasjson(data):
    """DAS JSON wrapper"""
    encoder = JSONEncoder()
    cherrypy.response.headers['Content-Type'] = "text/json"
    try:
        jsondata = encoder.encode(data)
        return jsondata
    except:
        return dict(error="Failed to JSONtify obj '%s' type '%s'" \
            % (data, type(data)))
Пример #44
0
    def _create_secured_jwt(body, signer):
        # type: (Any, AttestationSigningKey) -> str
        """ Return a secured JWT expressing the body, secured with the specified signing key.
        :param Any body: The body of the token to be serialized.
        :param signer: the certificate and key to sign the token.
        :type signer: AttestationSigningKey
        """
        header = {
            "alg": "RSA256" if isinstance(signer._signing_key, RSAPrivateKey) else "ECDH256",
            "jwk": {
                "x5c": [
                    base64.b64encode(signer._certificate.public_bytes(
                        Encoding.DER)).decode('utf-8')
                ]
            }
        }
        json_header = JSONEncoder().encode(header)
        return_value = Base64Url.encode(json_header.encode('utf-8'))

        try:
            body = body.serialize()
        except AttributeError:
            pass
        json_body = ''
        if body is not None:
            json_body = JSONEncoder().encode(body)
        return_value += '.'
        return_value += Base64Url.encode(json_body.encode('utf-8'))

        # Now we want to sign the return_value.
        if isinstance(signer._signing_key, RSAPrivateKey):
            signature = signer._signing_key.sign(
                return_value.encode('utf-8'),
                algorithm=SHA256(),
                padding=padding.PKCS1v15())
        else:
            signature = signer._signing_key.sign(
                return_value.encode('utf-8'),
                algorithm=SHA256())
        # And finally append the base64url encoded signature.
        return_value += '.'
        return_value += Base64Url.encode(signature)
        return return_value
Пример #45
0
class JsonLinesItemExporter(BaseItemExporter):
    def __init__(self, file, **kwargs):
        self._configure(kwargs, dont_fail=True)
        self.file = file
        kwargs.setdefault('ensure_ascii', not self.encoding)
        self.encoder = JSONEncoder(**kwargs)

    def export_item(self, item):
        itemdict = dict(self._get_serialized_fields(item))
        data = self.encoder.encode(itemdict) + '\n'
        self.file.write(to_bytes(data, self.encoding))
Пример #46
0
class FrameConverter(object):
    def __init__(self):
        self.debug = True
        self.encoder = JSONEncoder()
        self.decoder = JSONDecoder()

    def setDebug(self, debug):
        self.debug = debug

    def encode(self, depthFrame, colorFrame, labelFrame, skeletonFrame):
        encodedObject = {
            'depth': self.encode_image(depthFrame),
            'color': self.encode_image(colorFrame),
            'label': labelFrame,
            'skeleton': skeletonFrame
        }
        encodedJSON = self.encoder.encode(encodedObject)
        if self.debug:
            decodedFrame = self.decode(encodedJSON)
            assert np.array_equal(decodedFrame['depth_image'], depthFrame)
            assert np.array_equal(decodedFrame['color_image'], colorFrame)
            assert np.array_equal(decodedFrame['label'], labelFrame)
            assert np.array_equal(decodedFrame['skeleton'], skeletonFrame)

        return encodedJSON

    def decode(self, json):
        decodedDict = self.decoder.decode(json)
        depthFrame = self.decode_image(decodedDict['depth'])
        colorFrame = self.decode_image(decodedDict['color'])
        labelFrame = decodedDict['label']
        skeletonFrame = decodedDict['skeleton']

        return {
            'depth_image': depthFrame,
            'color_image': colorFrame,
            'label': labelFrame,
            'skeleton': skeletonFrame
        }

    def encode_image(self, original_image):
        encoded_image = base64.b64encode(original_image)
        image_shape = original_image.shape
        return {'image': encoded_image, 'shape': image_shape}

    def decode_image(self, encoded_image_frame):
        try:
            depthFrame = base64.decodestring(encoded_image_frame)
            bytes = bytearray(depthFrame)
            image = Image.open(io.BytesIO(bytes))
            encoded_image = array(image)
        except:
            return ""
        return encoded_image
Пример #47
0
class JsonFormatter(Formatter):
    def begin(self, fields):
        self.encoder = JSONEncoder(indent=2, separators=(', ', ': '))

    def iterout(self, iterable):
        self.begin([])
        for chunk in self.encoder.iterencode(list(iterable)):
            self.fp.write(chunk)

    def out(self, d):
        self.fp.write(self.encoder.encode(d))
Пример #48
0
 def wrapper (self, *args, **kwds):
     """Decorator wrapper"""
     encoder = JSONEncoder()
     data = func(self, *args, **kwds)
     cherrypy.response.headers['Content-Type'] = "text/json"
     try:
         jsondata = encoder.encode(data)
         return jsondata
     except:
         Exception("Failed to JSONtify obj '%s' type '%s'" \
             % (data, type(data)))
Пример #49
0
def hash_object(json_encoder: JSONEncoder, obj: dict) -> str:
    """
    Calculates SHA-256 hash of JSON encoded 'obj'.

    :param json_encoder: JSON encoder object.
    :param obj: Object to be hashed.
    :return: SHA-256 as hexadecimal string.
    :rtype str
    """
    s = json_encoder.encode((obj, SALT_FOR_DATA_INTEGRITY))
    return hashlib.sha256(s.encode()).hexdigest()
Пример #50
0
def get_trash_routine(request, resource_id):
    j_encoder = JEncoder()
    dydb = get_dynamodb_west()
    trash_table = dydb.Table('Trash')
    key = {'id': resource_id}
    response = trash_table.get_item(Key=key)
    try:
        response['Item']
    except KeyError:
        return '{} is not a valid resource_id'.format(resource_id)
    else:
        return j_encoder.encode(response['Item'])
Пример #51
0
class FloydhubKerasCallback(BaseLogger):
    '''
    This class can be used as a callback object that can be passed to the method fit()
    when training your model (inside 'callbacks' argument)
    If it is used while your model is running on a floydhub server, training metrics
    will be plotted at real time under the 'Training metrics' panel.
    '''
    def __init__(self, mode='epoch', metrics=None, stateful_metrics=None):
        super().__init__(stateful_metrics)

        if mode not in ('epoch', 'batch'):
            raise ValueError('Mode parameter should be "epoch" or "batch"')

        if metrics is not None and not isinstance(metrics, (list, tuple)):
            raise ValueError(
                'Metrics parameter should be a list of training metric names to track'
            )

        if stateful_metrics is not None and not isinstance(
                metrics, (list, tuple)):
            raise ValueError(
                'Stateful metrics parameter should be a list of training metric names to track'
            )

        self.mode = mode
        self.metrics = frozenset(metrics) if metrics is not None else None
        self.encoder = JSONEncoder()

    def report(self, metric, value, **kwargs):
        info = {'metric': metric, 'value': value}
        info.update(kwargs)
        print(self.encoder.encode(info))

    def on_batch_end(self, batch, logs):
        if not self.mode == 'batch':
            return

        metrics = frozenset(logs.keys()) - frozenset(['batch', 'size'])
        if self.metrics:
            metrics &= self.metrics
        for metric in metrics:
            self.report(metric, round(logs[metric].item(), 5), step=batch)

    def on_epoch_end(self, epoch, logs):
        if not self.mode == 'epoch':
            return

        metrics = frozenset(logs.keys())
        if self.metrics:
            metrics &= self.metrics
        for metric in metrics:
            self.report(metric, round(logs[metric].item(), 5), step=epoch)
Пример #52
0
 def _build_query_params(self):
     encoder = JSONEncoder()
     # Prepare aprams from attributes
     params = copy.deepcopy(self.__dict__)
     params.pop('original_url')  # Remove param
     params['format'] = params.pop('response_format')  # rename param
     # Convert values proxy crawl compatible values (json-like, i.e True -> 'true')
     # and ignore params with None or False value
     params = [(k, encoder.encode(v).strip('"')) for k, v in params.items()
               if v]
     # Make query string
     query_params = urllib.parse.urlencode(params)
     return query_params
class FrameConverter(object):
    def __init__(self):
        self.debug = True
        self.encoder = JSONEncoder()
        self.decoder = JSONDecoder()

    def setDebug(self, debug):
        self.debug = debug

    def encode(self, depthFrame, colorFrame, labelFrame, skeletonFrame):
        encodedObject = {
            "depth": self.encode_image(depthFrame),
            "color": self.encode_image(colorFrame),
            "label": labelFrame,
            "skeleton": skeletonFrame,
        }
        encodedJSON = self.encoder.encode(encodedObject)
        if self.debug:
            decodedFrame = self.decode(encodedJSON)
            assert np.array_equal(decodedFrame["depth_image"], depthFrame)
            assert np.array_equal(decodedFrame["color_image"], colorFrame)
            assert np.array_equal(decodedFrame["label"], labelFrame)
            assert np.array_equal(decodedFrame["skeleton"], skeletonFrame)

        return encodedJSON

    def decode(self, json):
        decodedDict = self.decoder.decode(json)
        depthFrame = self.decode_image(decodedDict["depth"])
        colorFrame = self.decode_image(decodedDict["color"])
        labelFrame = decodedDict["label"]
        skeletonFrame = decodedDict["skeleton"]

        return {"depth_image": depthFrame, "color_image": colorFrame, "label": labelFrame, "skeleton": skeletonFrame}

    def encode_image(self, original_image):
        encoded_image = base64.b64encode(original_image)
        image_shape = original_image.shape
        return {"image": encoded_image, "shape": image_shape}

    def decode_image(self, encoded_image_frame):
        try:
            depthFrame = base64.decodestring(encoded_image_frame)
            bytes = bytearray(depthFrame)
            image = Image.open(io.BytesIO(bytes))
            encoded_image = array(image)
        except:
            return ""
        return encoded_image
Пример #54
0
class TacoTransport():
    """Taco transport class.

    Implements the communication between Taco clients and servers.
    """

    def __init__(self, in_, out, from_obj=None, to_obj=None):
        """Constructs new TacoTransport object.

        Stores the input and output streams.  The "from_obj" and "to_obj"
        functions are used for the JSON encoder's "default" function
        and the JSON decoder's "object_hook" parameters respectively.
        """

        self.in_ = in_
        self.out = out

        self.encoder = JSONEncoder(default=from_obj)
        self.decoder = JSONDecoder(object_hook=to_obj)

    def read(self):
        """Read a message from the input stream.

        The decoded message is returned as a data structure, or
        None is returned if nothing was read.
        """

        text = ''
        while True:
            line = self.in_.readline()
            line = utf_8_decode(line)[0]

            if line == '' or line.startswith('// END'):
                break

            text += line

        if text == '':
            return None

        return self.decoder.decode(text)

    def write(self, message):
        """Write a message to the output stream."""

        message = self.encoder.encode(message)
        self.out.write(utf_8_encode(message)[0])
        self.out.write(utf_8_encode('\n// END\n')[0])
        self.out.flush()
Пример #55
0
    def _updatePileupPNNs(self, stepHelper, fakeSites):
        """
        Update the workflow copy of the cached pileup file with PNNs
        forced by TrustPUSitelists flag
        """
        fileName = self._getStepFilePath(stepHelper)
        fakePNNs = mapSitetoPNN(fakeSites)
        with open(fileName, 'r') as puO:
            pileupData = json.load(puO)

        for dummyPUType, blockDict in pileupData.iteritems():
            for dummyBlockName, blockInfo in blockDict.iteritems():
                blockInfo['PhEDExNodeNames'].extend([x for x in fakePNNs if x not in blockInfo])

        encoder = JSONEncoder()
        jsonPU = encoder.encode(pileupData)
        self._writeFile(fileName, jsonPU)
Пример #56
0
class RestUtils(object):

    def __init__(self):
        """Initialization method
        """

        self.api_url = SDC_SERVER
        self.encoder = JSONEncoder()

    def _call_api(self, pattern, method, body=None, headers=None, payload=None, **kwargs):

        """Launch HTTP request to Policy Manager API with given arguments
        :param pattern: string pattern of API url with keyword arguments (format string syntax)
        :param method: HTTP method to execute (string)
        :param body: JSON body content (dict)
        :param headers: HTTP header request (dict)
        :param payload: Query parameters for the URL
        :param **kwargs: URL parameters (without url_root) to fill the patters
        :returns: REST API response
        """

        kwargs['url_root'] = self.api_url

        url = pattern.format(**kwargs)

        #print "==============="
        #print "### REQUEST ###"
        #print 'METHOD: {}\nURL: {} \nHEADERS: {} \nBODY: {}'.format(method, url, headers, self.encoder.encode(body))

        try:
            if headers[CONTENT_TYPE] == CONTENT_TYPE_JSON:
                r = requests.request(method=method, url=url, data=self.encoder.encode(body), headers=headers,
                                     params=payload, verify=False)
            else:
                r = requests.request(method=method, url=url, data=body, headers=headers, params=payload, verify=False)

        except Exception, e:
            print "Request {} to {} crashed: {}".format(method, url, str(e))
            return None

        #print "### RESPONSE ###"
        #print "HTTP RESPONSE CODE:", r.status_code
        #print 'HEADERS: {} \nBODY: {}'.format(r.headers, r.content)

        return r