Example #1
0
def main():
    try:
        fp = open(args.file)
        js = json.load(fp)
        fp.close()
    
        js['__type__'] = args.type
        for key in ['StartTime','Time','EndTime']:
            if key in js.keys():
                js[key]= js[key].replace('Z','000')
        
        if args.verbose:
            sys.stderr.write('json:')
            json.dump(js, sys.stderr, indent=4)
            sys.stderr.write('\n')

        object = loader(json.dumps(js))
        if args.type == 'Message':
            print '{0:<12} {1:<20} {2:<.40} {3:<.40}'.format(
                object.EC2InstanceId, 
                object.Time.strftime(format="%Y-%m-%d %H:%M:%S"),
                object.AutoScalingGroupName, 
                object.Description
            )
        else:
            print dumper(object,indent=4)
    except:
        None
    return
Example #2
0
def process(line,pattern,mapping,keys,session,server,file,dts,errors=False,regex=None,replace=None,verbose=False):
    if regex and replace:
        m = regex.match(line)
        if m:
            line = regex.sub(replace,line)
        #print line
    match = pattern.match(line)
    if not match:
        if errors:
            sys.stderr.write('%s\n'%line)
        return
    data = dict(server=byName(Server,server,session),file=file)
    for k in range(len(mapping)):
        value=match.group(k+1)
        if mapping[k] == 'when':
            data[mapping[k]] = datetime.strptime(value,dts)
        elif mapping[k] == 'thread':
            data[mapping[k]] = byName(Thread,value,session)
        elif mapping[k] == 'description':
            data[mapping[k]] = value[:4095]
            for p in keys:
                m = p.match(value)
                if m:
                    v = m.group(1)
                    data['key'] = byName(Key,v[:4096],session)
                    continue
        elif mapping[k] == 'level':
            data[mapping[k]] = byName(Level,value,session)
        else:
            data[mapping[k]] = value
    m = Message(**data)
    if verbose:
        print dumper(m)
    session.add(m)
    return
Example #3
0
 def test_stacked_decorators(self):
     from jsonweb.encode import to_object, dumper
     from jsonweb.decode import from_object, loader
             
     def person_handler(cls, obj):
         return cls(
             obj['first_name'],
             obj['last_name']
         )                
             
     @to_object(suppress=["foo"])
     @from_object(person_handler)
     class Person(object):
         def __init__(self, first_name, last_name):
             self.foo = "bar"
             self.first_name = first_name
             self.last_name = last_name
             
     person = Person("shawn", "adams")
     json_str = dumper(person)
     del person
     person = loader(json_str)
     self.assertTrue(isinstance(person, Person))
              
     
Example #4
0
    def generate_metadata(self, page_list, display_name):
        """
        Method outputs metadata file for a Dashboard based on a list of Pages and their content
        :param page_list:
        :param display_name:
        :return:
        """
        dashboard_pages = []
        for page in page_list:
            text_page = Page(page.get_name(), page.get_display_name(),
                             page.get_template())
            dashboard_pages.append(text_page)
            for container in page.get_containers():
                #print (page.get_name())
                #print (container.get_name())
                text_container = Container(container.get_name(),
                                           container.get_display_name(),
                                           container.get_template(),
                                           container.get_colspan(),
                                           container.get_column(),
                                           container.get_row(),
                                           container.get_rowspan())
                text_page.add_container(text_container)

                for widget in container.get_widgets():
                    text_widget = Widget(widget.get_name(),
                                         widget.get_display_name(),
                                         widget.get_font_size(),
                                         widget.get_template(),
                                         widget.get_type(),
                                         widget.get_colspan(),
                                         widget.get_column(), widget.get_row(),
                                         widget.get_rowspan())
                    text_container.add_widget(text_widget)

                    for property in widget.get_properties():
                        text_prop = Property(property.get_id(),
                                             property.get_key(),
                                             property.get_value())
                        text_widget.add_property(text_prop)

                    for step in widget.get_steps():
                        text_step = Step(step.get_name(),
                                         step.get_display_name(),
                                         step.get_type(), step.get_template())
                        text_widget.add_step(text_step)

                        for property in step.get_properties():
                            step_prop = Property(property.get_id(),
                                                 property.get_key(),
                                                 property.get_value())
                            text_step.add_property(step_prop)

            json_content = dumper(dashboard_pages)
            save_file(
                self.output + "/" + display_name + "_dashboard_metadata.json",
                json_content)
Example #5
0
    def test_handler_decorator_for_list(self):
        @encode.to_list()
        class ValueList(object):
            def __init__(self, *values):
                self.values = values

            @encode.handler
            def to_list(self):
                return self.values

        self.assertEqual(encode.dumper(ValueList(1, 2, 3)), "[1, 2, 3]")
Example #6
0
    def test_handler_decorator_for_list(self):

        @encode.to_list()
        class ValueList(object):
            def __init__(self, *values):
                self.values = values
            @encode.handler
            def to_list(self):
                return self.values
            
        self.assertEqual(encode.dumper(ValueList(1 ,2, 3)), "[1, 2, 3]")
Example #7
0
def main():
    address = Address(7, 'Bamboo Av', 'Earlwood')
    print 'address='
    js = dumper(address, indent=4)
    print js
    print loader(js)

    job = Job('Engineer', True)
    print 'job='
    js = dumper(job, indent=4)
    print js
    print loader(js)

    dob = datetime.strptime('1968-08-08 06:00:00', '%Y-%m-%d %H:%M:%S')
    person = Person(0, 'David Edson', dob, 'Chief Engineer', address, [job])
    print 'person='
    js = dumper(person, indent=4)
    print js
    other = loader(js)
    print other

    print
    addressSchema = AddressSchema()
    print addressSchema, dumper(addressSchema, indent=4)
    jobSchema = JobSchema()
    print jobSchema, dumper(jobSchema, indent=4)
    personSchema = PersonSchema()
    print personSchema, dumper(personSchema, indent=4)

    return
Example #8
0
 def test_json_object_decorator(self):
     from jsonweb.encode import to_object, dumper
             
     @to_object(suppress=["foo", "__type__"])
     class Person(object):
         def __init__(self, first_name, last_name):
             self.foo = "bar"
             self.first_name = first_name
             self.last_name = last_name
     
     person = Person("shawn", "adams")
     json_obj = json.loads(dumper(person))
     
     self.assertEqual(json_obj, {"first_name": "shawn", "last_name": "adams"})
Example #9
0
    def test_handler_decorator_for_object(self):

        @to_object()
        class Person(object):
            def __init__(self, first_name, last_name):
                self.first_name = first_name
                self.last_name = last_name
                
            @encode.handler   
            def to_json(self):
                return {"FirstName": self.first_name, "LastName": self.last_name}                
                
        person = Person("shawn", "adams")
        json_obj = json.loads(encode.dumper(person))
        
        self.assertEqual(json_obj, {"FirstName": "shawn", "LastName": "adams"})
Example #10
0
 def test_supplied_obj_handler(self):
     from jsonweb.encode import to_object, dumper
             
     def person_handler(obj):
         return {"FirstName": obj.first_name, "LastName": obj.last_name}
     
     @to_object(handler=person_handler)
     class Person(object):
         def __init__(self, first_name, last_name):
             self.foo = "bar"
             self.first_name = first_name
             self.last_name = last_name
     
     person = Person("shawn", "adams")
     json_obj = json.loads(dumper(person))
     
     self.assertEqual(json_obj, {"FirstName": "shawn", "LastName": "adams"})        
Example #11
0
    def test_handler_decorator_for_object(self):
        @to_object()
        class Person(object):
            def __init__(self, first_name, last_name):
                self.first_name = first_name
                self.last_name = last_name

            @encode.handler
            def to_json(self):
                return {
                    "FirstName": self.first_name,
                    "LastName": self.last_name
                }

        person = Person("shawn", "adams")
        json_obj = json.loads(encode.dumper(person))

        self.assertEqual(json_obj, {"FirstName": "shawn", "LastName": "adams"})
Example #12
0
 def test_subclass_json_web_encoder(self):
     from jsonweb.encode import to_object, JsonWebEncoder, dumper
     
     message = []
     class MyJsonWebEncoder(JsonWebEncoder):
         def object_handler(self, obj):
             message.append("my_object_handler")
             suppress = obj._encode.suppress
             json_obj = dict([(k,v) for k,v in obj.__dict__.iteritems() if not k.startswith("_") and k not in suppress])
             if "__type__" not in suppress:
                 json_obj["__type__"] = obj._encode.__type__
             return json_obj
     
     @to_object()
     class Person(object):
         def __init__(self, first_name, last_name):
             self.first_name = first_name
             self.last_name = last_name
     
     person = Person("shawn", "adams")
     json_obj = json.loads(dumper(person, cls=MyJsonWebEncoder))
     
     self.assertEqual(json_obj, {"__type__": "Person", "first_name": "shawn", "last_name": "adams"})
     self.assertEqual(message[0], "my_object_handler")
Example #13
0
 def test_decode_encode(self, policy):
     " Test that encoding/decoding save all policy serializable features "
     policy_repr = encode.dumper(policy)
     new_policy = decode.loader(policy_repr)
     assert policy == new_policy
Example #14
0
def main():
    test = TestObject()
    # test.add_simple_part(SimpleItem('TestSingle',   Values(
    #                                    'TestValue',
    #                                    'TestValue'
    #                                 )))
    test.add_simple_part(SimpleItem('TestSingle2',
                                        SimpleValue('TestValue2'),
                                        SimpleValue('TestValue2')
    ))

    test.add_complex_part(ComplexItem(
        'TestSingleValue1', ComplexValue(
            'TestValue', 'm', '+3.6'
        ),
        ComplexValue(
            'TestValue', 'm', '+3.6'
        )
    ))

    test.add_complex_part(ComplexItem(
        'TestSingleValue2', ComplexValue(
            'TestValue', 'm', '-3.6'
        ),
        ComplexValue(
            'TestValue', 'm', '+3.6'
        )
    ))

    # test.add_complex_part(ComplexItem(
    #     'TestSingleValue3', ComplexValue(
    #         'TestValue', 'm', '+3.6'
    #     )
    # ))

    # test.add_composite_part(CompositeItem(
    #     "Composite",
    #     SimpleItem("TestComposite1", "TestCompVal1"),
    #     SimpleItem("TestComposite1", "TestCompVal1")
    #     # CompositeParts(
    #     #     SinglePart('TestComposite3', 'TestValue3', 'm3'),
    #     #     SinglePart('TestComposite4', 'TestValue4', 'm4')
    #     # )
    # ))

    test.add_multi_part(MultiItem(
        'TestMulti1',
                SimpleValue('MultiValue1'),
                SimpleValue('MultiValue2'),
                SimpleValue('MultiValue3')
        )
        # 'TestMulti1',
        #     SimpleValue('MultiValue1'),
        #     SimpleValue('MultiValue2'),
        #     SimpleValue('MultiValue3')
        # )
        # 'TestMulti1', MultiValue(
        #     'MultiValue1',
        #     'MultiValue2',
        #     'MultiValue3'
        # )
    )

    print dumper(test, indent=2)
Example #15
0
 def show(o):
     if args.json:
         print dumper(o,exclude_nulls=False,indent=4)
 
     if args.xml:
         alloro(o,Base=Base,colour=args.colour,output=sys.stdout)
 def test_decode_encode(self, policy):
     """Test that encoding/decoding save all policy serializable features"""
     policy_repr = encode.dumper(policy)
     new_policy = decode.loader(policy_repr)
     assert policy == new_policy
Example #17
0
def read_tree(tree: List[str]) -> Node:
    nodes: List[Node] = list()
    temp: List[Node] = list()
    stack: List[Node] = list()
    for row in tree:
        if len(stack) > 0:
            while stack[-1].name.count("|") > row.count("|"):
                stack.pop()
        if row.count("|") == 0:
            if len(nodes) > 0 and row.replace(
                    "<=", "").replace(">", "").replace(" ", "").replace(
                        "\n", "") == nodes[-1].name.replace("<=", "").replace(
                            ">", "").replace(" ", "").replace("\n", ""):
                stack.append(nodes[-1])
                continue
            parent = Node(row)
            stack.append(parent)
            nodes.append(parent)
            continue
        elif row.count("|") > stack[-1].name.count("|"):
            if row.replace("<=", "").replace(">", "").replace(" ", "").replace(
                    "\n", "") == stack[-1].name.replace("<=", "").replace(
                        ">", "").replace(" ", "").replace(
                            "\n", "") and ":" not in row:
                print("jestem  te same nazwy przy warunku wiecej || i bez : ")
                continue
            if ":" in row:
                if row.split(":")[0].replace("<=", "").replace(">", "").replace(" ", "").replace("\n", "") == \
                        stack[-1].name.split(":")[0].replace("<=", "").replace(
                            ">", "").replace(" ", "").replace("\n", ""):
                    print("jestem xddd ")
                    split = row.split(":")
                    child = Node(split[1])
                    stack[-1].add_child(child)
                    continue
                split = row.split(":")
                child = Node(split[0])
                stack[-1].add_child(child)
                stack.append(child)
                child = Node(split[1])
                stack[-1].add_child(child)
            else:
                child = Node(row)
                stack[-1].add_child(child)
                stack.append(child)

            continue
        elif row.count("|") == stack[-1].name.count("|"):
            if row.replace("<=", "").replace(">", "").replace(" ", "").replace(
                    "\n", "") == stack[-1].name.replace("<=", "").replace(
                        ">", "").replace(" ", "").replace(
                            "\n", "") and ":" not in row:
                print("jestem  te same nazwy  i bez : ")
                continue
            # else:
            #     stack.pop()

            if ":" in row:
                if row.split(":")[0].replace("<=", "").replace(">", "").replace(" ", "").replace("\n", "") == \
                        stack[-1].name.split(":")[0].replace("<=", "").replace(
                            ">", "").replace(" ", "").replace("\n", ""):
                    print("jestem xddd ")
                    split = row.split(":")
                    child = Node(split[1])
                    stack[-1].add_child(child)
                    continue
                print("jestem  te rózne  nazwy  i  : ")
                split = row.split(":")
                child = Node(split[0])
                stack[-1].add_child(child)
                stack.append(child)
                child = Node(split[1])
                stack[-1].add_child(child)
            else:
                child = Node(row)
                stack[-1].add_child(child)
                stack.append(child)
            continue
    print(dumper(nodes[0]))
    return nodes[0]
Example #18
0
def get_json_progress(progress: ProgressData) -> str:
    return dumper(progress)
Example #19
0
def get_json_tree(tree: List[Node], info) -> str:
    print(info)
    info['tree'] = tree
    return dumper(info)
Example #20
0
def jsonweb_response(obj, status_code=200, headers=None):
    return Response(encode.dumper(obj),
                    status_code,
                    headers=headers,
                    mimetype="application/json")
Example #21
0
        print("PDF file hasn't changed since the last time. Exiting.")
        exit(1)

    new_addresses = new_pdf.get_addresses()
    old_addresses = {Address(**address) for address in old_json["addresses"]}

    # Google Geocoding calls are pretty expensive, so we don't really want to query every address every time.
    addresses_to_be_removed = old_addresses - new_addresses
    addresses_to_be_added = new_addresses - old_addresses

    new_addresses = old_addresses - addresses_to_be_removed
    json_data = {
        "generation_time": str(datetime.now()),
        "referenced_pdf_hash": new_pdf_hash
    }
    for address in addresses_to_be_added:
        address_latlng = geocoder.google(address).latlng
        if address_latlng is None:
            sys.stderr.write("Geocoder returned empty result for address: " +
                             str(address) + "\n")
            continue
        address.lat = address_latlng[0]
        address.lng = address_latlng[1]

        new_addresses.add(address)
    json_data["addresses"] = list(new_addresses)

    write_to_file(new_generated_JSON_file_path, dumper(json_data))
    print("Removed: ", len(addresses_to_be_removed), "Added: ",
          len(addresses_to_be_added), "Number of addresses: ",
          len(new_addresses))
Example #22
0
def jsonweb_response(obj, status_code=200, headers=None):
    return Response(encode.dumper(obj), status_code, headers=headers, mimetype="application/json")