Exemplo n.º 1
0
def _json_equal(a, b):
    """Compare the JSON strings generated by two objects."""
    # Comparing the JSON strings automatically takes into account the automatic
    # Python conversions that happen, such as tuple to list, when encoding to
    # JSON. We try to make the strings deterministic and small with the options
    # we use.
    return (jsondumps(a, separators=(',',':'), sort_keys=True, allow_nan=False)
            == jsondumps(b, separators=(',',':'), sort_keys=True, allow_nan=False))
Exemplo n.º 2
0
 def fix_note_chars(note):
     if isinstance(note, bool):
         note2 = 'False'
         if note:
             note2 = 'True'
         note = note2
     else:
         import sys
         from json import dumps as jsondumps
         if note not in [None,'']:
             if sys.stdout.encoding:
                 note = note.decode(sys.stdout.encoding)
         note = jsondumps(note)
         note = note.replace('||t','        ')
         note = note.replace('\\\\t','        ')
         note = note.replace('\\\t','        ')
         note = note.replace('\\t','        ')
         note = note.replace('\t','        ')
         note = note.replace('\\"','"')
         note = note.replace('\"','"')
         note = note.replace('||n','<br/>')
         note = note.replace('\\\\n','<br/>')
         note = note.replace('\\\n','<br/>')
         note = note.replace('\\n','<br/>')
         note = note.replace('\n','<br/>')
     return note
        def fix_note_chars(note):
            if isinstance(note, bool):
                note2 = "False"
                if note:
                    note2 = "True"
                note = note2
            else:
                import sys
                from json import dumps as jsondumps

                if note not in [None, ""]:
                    if sys.stdout.encoding:
                        note = note.decode(sys.stdout.encoding)
                note = jsondumps(note)
                note = note.replace("||t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
                note = note.replace("\\\\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
                note = note.replace("\\\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
                note = note.replace("\\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
                note = note.replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
                note = note.replace('\\"', '"')
                note = note.replace('"', '"')
                note = note.replace("||n", "<br/>")
                note = note.replace("\\\\n", "<br/>")
                note = note.replace("\\\n", "<br/>")
                note = note.replace("\\n", "<br/>")
                note = note.replace("\n", "<br/>")
            return note
Exemplo n.º 4
0
def fix_message_characters(message):
    """Fixes the escaped characters and replaces them with equivalents
    that are formatted for html.

    :param message: the message as a string
    :return: the html-formatted string
    """
    if isinstance(message, bool):
        return str(message)

    import sys
    from json import dumps as jsondumps
    if message not in [None, '']:
        if sys.stdout.encoding:
            message = message.decode(sys.stdout.encoding)
    message = jsondumps(message)

    tab = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
    newline = '<br/>'
    # OrderedDict does not exist in python 2.6, so do this the long way for now
    message = message.replace('||t', tab)
    message = message.replace('\\\\t', tab)
    message = message.replace('\\\t', tab)
    message = message.replace('\\t', tab)
    message = message.replace('\t', tab)
    message = message.replace('||n', newline)
    message = message.replace('\\\\n', newline)
    message = message.replace('\\\n', newline)
    message = message.replace('\\n', newline)
    message = message.replace('\n', newline)
    message = message.replace('\\"', '"')
    message = message.replace('\"', '"')

    return message
Exemplo n.º 5
0
def fix_message_characters(message):
    """Fixes the escaped characters and replaces them with equivalents
    that are formatted for html.

    :param message: the message as a string
    :return: the html-formatted string
    """
    import sys
    from json import dumps as jsondumps
    if message not in [None, '']:
        if sys.stdout.encoding:
            message = message.decode(sys.stdout.encoding)
    message = jsondumps(message)

    # OrderedDict does not exist in python 2.6, so do this the long way for now
    message = message.replace('||t', TAB)
    message = message.replace('\\\\t', TAB)
    message = message.replace('\\\t', TAB)
    message = message.replace('\\t', TAB)
    message = message.replace('\t', TAB)
    message = message.replace('||n', NEWLINE)
    message = message.replace('\\\\n', NEWLINE)
    message = message.replace('\\\n', NEWLINE)
    message = message.replace('\\n', NEWLINE)
    message = message.replace('\n', NEWLINE)
    message = message.replace('\\"', '"')
    message = message.replace('\"', '"')

    return message
    def add(self, data):
        if self.conn == None:
            self.connect()
        cur = self.conn.cursor()

        # no escaping needed, psycopg and jinja takes care of that.
        cur.execute(
            """INSERT INTO supporters (created, firstname, lastname, town, 
                emailaddress, publicvisible, campaign_info, post_data )
        VALUES (NOW(), %s, %s, %s, %s, %s, %s, %s, %s, %s)""",
            (
                data["firstname"],
                data["lastname"],
                data["town"],
                data["emailaddress"],
                data["publicvisible"],
                data["campaign_info"],
                jsondumps(data["postdata"]),
            ),
        )
        self.conn.commit()
        cur.close()
        # delete cached data
        cache.delete_memoized(self.count)
        cache.delete_memoized(self.public_supporters)
Exemplo n.º 7
0
    def execute(self, path):
        assert self.server_url
        #f = open(path, 'rb')
        import codecs
        f = codecs.open(path, 'rb')

        count = 0
        while 1:
            buffer = f.read(self.chunk_size)
            if not buffer:
                break

            if count == 0:
                action = "create"
            else:
                action = "append"

            fields = [
                ("ajax", "true"),
                ("action", action),
            ]
            if self.ticket:
                fields.append( ("ticket", self.ticket) )
                fields.append( ("login_ticket", self.ticket) )
                basename = os.path.basename(path)
                from json import dumps as jsondumps
                if sys.stdout.encoding:
                    basename = basename.decode(sys.stdout.encoding)
                basename = jsondumps(basename)
                basename = basename.strip('"')
                # the first index begins at 0
                fields.append( ("file_name0", basename) )

            if self.subdir:
                fields.append( ("subdir", self.subdir) )
	    
            files = [("file", path, buffer)]
            (status, reason, content) = self.upload(self.server_url,fields,files)

            
            if reason != "OK":
                raise TacticUploadException("Upload of '%s' failed: %s %s" % (path, status, reason) )

            count += 1


        f.close()
Exemplo n.º 8
0
 def _should_send_property(self, key, value):
     """Check the property lock (property_lock)"""
     to_json = self.trait_metadata(key, 'to_json', self._trait_to_json)
     if key in self._property_lock:
         # model_state, buffer_paths, buffers
         split_value = _remove_buffers({ key: to_json(value, self)})
         split_lock = _remove_buffers({ key: self._property_lock[key]})
         # A roundtrip conversion through json in the comparison takes care of
         # idiosyncracies of how python data structures map to json, for example
         # tuples get converted to lists.
         if (jsonloads(jsondumps(split_value[0])) == split_lock[0]
             and split_value[1] == split_lock[1]
             and _buffer_list_equal(split_value[2], split_lock[2])):
             return False
     if self._holding_sync:
         self._states_to_send.add(key)
         return False
     else:
         return True
Exemplo n.º 9
0
 def fix_note_chars(note):
     import sys
     from json import dumps as jsondumps
     if sys.stdout.encoding:
         note = note.decode(sys.stdout.encoding)
     note = jsondumps(note)
     note = note.replace('||t','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
     note = note.replace('\\\\t','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
     note = note.replace('\\\t','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
     note = note.replace('\\t','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
     note = note.replace('\t','&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')
     note = note.replace('\\"','"')
     note = note.replace('\"','"')
     note = note.replace('||n','<br/>')
     note = note.replace('\\\\n','<br/>')
     note = note.replace('\\\n','<br/>')
     note = note.replace('\\n','<br/>')
     note = note.replace('\n','<br/>')
     return note
Exemplo n.º 10
0
	def dumps(self,*p):
		web.header('Content-type', 'application/json')
		return jsondumps(*p)
Exemplo n.º 11
0
    def test_user_calendar02(self):
        user = self.login()
        other_user = self.other_user

        create_cal = Calendar.objects.create
        cal1 = create_cal(user=user, is_default=True, name='Cal #1')
        cal2 = create_cal(user=other_user,
                          is_default=True,
                          name='Cal #2',
                          is_public=True)
        cal3 = create_cal(user=other_user, name='Cal #3', is_public=False)

        create_act = partial(
            Activity.objects.create,
            user=user,
            type_id=ACTIVITYTYPE_TASK,
            floating_type=FLOATING,
        )
        act1 = create_act(title='Act#1')
        act2 = create_act(
            title='Act#2',
            type_id=ACTIVITYTYPE_MEETING,
            sub_type_id=ACTIVITYSUBTYPE_MEETING_QUALIFICATION,
        )
        act3 = create_act(title='Act#3', is_deleted=True)
        act4 = create_act(title='Act#4', user=other_user)
        act5 = create_act(title='Act#5', floating_type=NARROW)

        create_rel = partial(
            Relation.objects.create,
            user=user,
            subject_entity=user.linked_contact,
            type_id=REL_SUB_PART_2_ACTIVITY,
        )
        create_rel(object_entity=act1)
        act1.calendars.add(cal1)
        create_rel(object_entity=act2)
        act2.calendars.add(cal1)
        create_rel(object_entity=act3)
        act3.calendars.add(cal1)
        create_rel(object_entity=act4)
        act4.calendars.add(cal1)
        create_rel(object_entity=act5)
        act5.calendars.add(cal1)

        response = self.assertPOST200(
            self.CALENDAR_URL,
            data={'selected_calendars': [cal1.id, cal2.id, cal3.id]})

        with self.assertNoException():
            ctxt = response.context
            cal_ids = ctxt['current_calendars']
            floating_acts = ctxt['floating_activities']
            my_cals = ctxt['my_calendars']
            user_name = ctxt['user_username']
            event_url = ctxt['events_url']
            others_calendars = ctxt['others_calendars']
            n_others_calendars = ctxt['n_others_calendars']
            creme_calendars_by_user = ctxt['creme_calendars_by_user']
            creation_perm = ctxt['creation_perm']

        self.assertEqual(user.username, user_name)
        self.assertEqual({str(cal1.id), str(cal2.id)}, set(cal_ids))
        self.assertEqual({act1, act2, act4}, set(floating_acts))
        self.assertEqual({cal1}, set(my_cals))
        # self.assertEqual(reverse('activities__calendars_activities', args=('',)), event_url)
        self.assertEqual(reverse('activities__calendars_activities'),
                         event_url)
        self.assertEqual({other_user: [cal2]}, others_calendars)
        self.assertEqual(1, n_others_calendars)
        filter_key = '{} {} {}'.format(
            other_user.username,
            other_user.first_name,
            other_user.last_name,
        )
        self.assertEqual(
            jsondumps({filter_key: [{
                'name': cal2.name,
                'id': cal2.id
            }]}), creme_calendars_by_user)
        self.assertIs(creation_perm, True)
Exemplo n.º 12
0
def collection(collection_id):
    collections = current_app.site.collections
    collection_id = collections.default_id if collection_id is None else collection_id
    try:
        collection = collections[collection_id]
    except KeyError:
        flask.abort(404)

    try:
        filters = parse_filters(collection.filter_specs,
                                request.args.get('filter', '{}'))
    except (KeyError, ValueError):
        current_app.logger.warn('Bad request', exc_info=True)
        return flask.abort(400)

    try:
        stmt = collection.filtered_listing(filters)
    except UnknownOperator:
        current_app.logger.warn('Bad request', exc_info=True)
        flask.abort(400)

    fspecs = collection.filter_specs

    all_known = {
        name: [
            x[0] for x in db.session.execute(
                select([rel.value]).order_by(rel.value)).fetchall()
        ]
        for name, rel in collection.model.relations.items()
        if {'any', 'all'}.intersection(fspecs[name].operators)
    }
    all_known['status'] = STATUS.keys()
    all_known['tags'] = [
        x[0] for x in db.session.execute(
            select([Tag.value]).where(Tag.collection == collection.name).
            order_by(Tag.value)).fetchall()
    ]
    filters = [{
        'key': x.name,
        'constraints': all_known.get(x.name),
        'title': x.title,
        'operators': x.operators,
        'value_type': VALUE_TYPE_MAP.get(x.value_type, x.value_type)
    } for x in fspecs.values()]

    rows = db.session.execute(stmt).fetchall()
    rowdata = ',\n'.join([
        x.replace('["#TAGS#"]', tags if tags != '[null]' else '[]',
                  2).replace('"#STATUS#"', STATUS_STRS[status])
        for x, status, tags in rows
    ])
    dct = {
        'all_known': all_known,
        'compare': bool(collection.compare),
        'filters': {
            'title': 'Filter',
            'widget': {
                'type': 'filter',
                'filters': filters
            }
        },
        # TODO: summary non-functional, remove once frontend is switched
        'summary': {
            'title': 'Summary',
            'widget': {
                'data': {
                    'items': [
                        x.__setitem__('value', 0) or x
                        for x in collection.summary_items
                    ]
                },
                'type': 'keyval'
            }
        },
        'summary_config': {
            'title': 'Summary',
            'items': collection.summary_items
        },
        'listing': {
            'title': 'Data sets ({} found)'.format(len(rows)),
            'widget': {
                'data': {
                    'columns': [{
                        'align':
                        ALIGN.get(x.formatter, 'left'),
                        'formatter':
                        x.formatter,
                        'sortkey':
                        'title' if x.formatter == 'route' else None,
                        'id':
                        x.name,
                        'title':
                        x.heading,
                        'list':
                        x.islist,
                    } for x in collection.listing_columns],
                    'rows': ['#ROWS#'],
                    'sortcolumn':
                    collection.sortcolumn,
                    'sortorder':
                    collection.sortorder
                },
                'type': 'table',
            }
        },
    }
    indent = None
    separators = (',', ':')
    if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and not request.is_xhr:
        indent = 2
        separators = (', ', ': ')
    jsondata = jsondumps(dct,
                         indent=indent,
                         separators=separators,
                         sort_keys=True)
    jsondata = jsondata.replace('"#ROWS#"', rowdata)
    return current_app.response_class(
        (jsondata, '\n'), mimetype=current_app.config['JSONIFY_MIMETYPE'])
Exemplo n.º 13
0
    'string[]': str,
    'subset': lambda x: x,
    'timedelta': lambda x: x,
    'words': lambda x: x.split(),
}

VALUE_TYPE_MAP = {
    'string[]': 'string',
    'words': 'string',
}

# Order corresponds to marv.model.STATUS OrderedDict
STATUS_ICON = ['fire', 'eye-close', 'warning-sign', 'time']
STATUS_JSON = [
    jsondumps({
        'icon': STATUS_ICON[i],
        'title': x
    }, separators=(',', ':')) for i, x in enumerate(STATUS.values())
]
# TODO: reconsider in case we get a couple of more states
STATUS_STRS = {
    bitmask: ','.join(
        filter(None, [
            STATUS_JSON[i] if bitmask & 2**i else None
            for i in range(len(STATUS_JSON))
        ]))
    for bitmask in range(2**(len(STATUS_JSON)))
}


class FilterParseError(Exception):
    pass
Exemplo n.º 14
0
def handler(event, context):
    getLogger().info('Processing event {}'.format(jsondumps(event)))
    return __ACTIONS.get(event['action'],
                         __unsupported_action)(event['arguments'])
Exemplo n.º 15
0
 def data(self, value):
     self.raw_data = jsondumps(value)
Exemplo n.º 16
0
def dict_to_json(in_dict):
    return jsondumps(in_dict)
Exemplo n.º 17
0
            value = None
        print(url, value)
        return value

    def getall(self):
        lv0 = 1
        while lv0:
            arr0 = []
            lv1 = 1
            while lv1:
                jsk = self.get(lv0, lv1, 0)
                if jsk is not None: arr0.append(jsk)
                else:
                    arr1 = []
                    lv2 = 1
                    while True:
                        jsk = self.get(lv0, lv1, lv2)
                        if jsk is None: break
                        arr1.append(jsk)
                        lv2 = lv2 + 1
                    if arr1 != []: arr0.append(arr1)
                    else: break
                lv1 = lv1 + 1
            if arr0: self.data.append(arr0)
            else: break
            lv0 = lv0 + 1

stgetobj = stget()
stgetobj.getall()
open('dump.json', 'wt').write(jsondumps(stgetobj.data))
Exemplo n.º 18
0
 def line(self, value):
     "@param value: List of strings"
     self.raw_line = jsondumps(value)
Exemplo n.º 19
0
 def messages(self, value):
     # TODO: None if not value
     self.raw_messages = jsondumps(value)
Exemplo n.º 20
0
def dict_to_json(in_dict):
    return jsondumps(in_dict)