<s> """<STR_LIT>""" <EOL> import json <EOL> import os . path <EOL> import re <EOL> from types import NoneType <EOL> from collections import OrderedDict <EOL> from jinja2 import Environment , PackageLoader <EOL> class EveGenie ( object ) : <EOL> template_env = Environment ( loader = PackageLoader ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> objectidregex = re . compile ( '<STR_LIT>' , flags = re . M ) <EOL> intrangeregex = re . compile ( '<STR_LIT>' , flags = re . M ) <EOL> floatrangeregex = re . compile ( '<STR_LIT>' , flags = re . M ) <EOL> def __init__ ( self , data = None , filename = None ) : <EOL> """<STR_LIT>""" <EOL> self . endpoints = OrderedDict ( ) <EOL> if filename and not data : <EOL> if os . path . isfile ( filename ) : <EOL> with open ( filename , '<STR_LIT:r>' ) as ifile : <EOL> data = ifile . read ( ) . strip ( ) <EOL> if not isinstance ( data , ( basestring , dict , OrderedDict ) ) : <EOL> raise TypeError ( '<STR_LIT>' . format ( data ) ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if isinstance ( data , basestring ) : <EOL> data = json . loads ( data , object_pairs_hook = OrderedDict ) <EOL> self . endpoints = OrderedDict ( [ ( k , OrderedDict ( [ ( '<STR_LIT>' , self . parse_endpoint ( v ) ) ] ) ) for k , v in data . iteritems ( ) ] ) <EOL> def parse_endpoint ( self , endpoint_source ) : <EOL> """<STR_LIT>""" <EOL> return OrderedDict ( [ ( k , self . parse_item ( v ) ) for k , v in endpoint_source . iteritems ( ) ] ) <EOL> def parse_item ( self , endpoint_item ) : <EOL> """<STR_LIT>""" <EOL> item = OrderedDict ( [ ( '<STR_LIT:type>' , self . get_type ( endpoint_item ) ) ] ) <EOL> if item [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> item [ '<STR_LIT>' ] = OrderedDict ( ) <EOL> for k , i in endpoint_item . iteritems ( ) : <EOL> item [ '<STR_LIT>' ] [ k ] = self . parse_item ( i ) <EOL> if k == '<STR_LIT>' and isinstance ( i , bool ) : <EOL> del item [ '<STR_LIT>' ] <EOL> del item [ '<STR_LIT:type>' ] <EOL> item [ k ] = i <EOL> elif item [ '<STR_LIT:type>' ] == '<STR_LIT:list>' : <EOL> item [ '<STR_LIT>' ] = OrderedDict ( ) <EOL> for i in endpoint_item : <EOL> item [ '<STR_LIT>' ] = self . parse_item ( i ) <EOL> elif item [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> match = self . objectidregex . match ( endpoint_item ) . group ( <NUM_LIT:1> ) <EOL> if match : <EOL> item [ '<STR_LIT>' ] = OrderedDict ( [ <EOL> ( '<STR_LIT>' , match ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , True ) , <EOL> ] ) <EOL> elif item [ '<STR_LIT:type>' ] == '<STR_LIT>' : <EOL> if isinstance ( endpoint_item , basestring ) : <EOL> match = self . intrangeregex . match ( endpoint_item ) . group ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> if match : <EOL> item [ '<STR_LIT>' ] = int ( match [ <NUM_LIT:0> ] ) <EOL> item [ '<STR_LIT>' ] = int ( match [ <NUM_LIT:1> ] ) <EOL> elif item [ '<STR_LIT:type>' ] == '<STR_LIT:float>' : <EOL> if isinstance ( endpoint_item , basestring ) : <EOL> match = self . floatrangeregex . match ( endpoint_item ) . group ( <NUM_LIT:1> , <NUM_LIT:2> ) <EOL> if match : <EOL> item [ '<STR_LIT>' ] = float ( match [ <NUM_LIT:0> ] ) <EOL> item [ '<STR_LIT>' ] = float ( match [ <NUM_LIT:1> ] ) <EOL> elif item [ '<STR_LIT:type>' ] == '<STR_LIT:null>' : <EOL> item [ '<STR_LIT>' ] = True <EOL> del item [ '<STR_LIT:type>' ] <EOL> return item <EOL> def get_type ( self , source ) : <EOL> """<STR_LIT>""" <EOL> type_mapper = { <EOL> unicode : '<STR_LIT:string>' , <EOL> str : '<STR_LIT:string>' , <EOL> bool : '<STR_LIT>' , <EOL> int : '<STR_LIT>' , <EOL> float : '<STR_LIT:float>' , <EOL> dict : '<STR_LIT>' , <EOL> list : '<STR_LIT:list>' , <EOL> OrderedDict : '<STR_LIT>' , <EOL> NoneType : '<STR_LIT:null>' , <EOL> } <EOL> source_type = type ( source ) <EOL> if source_type in type_mapper : <EOL> eve_type = type_mapper [ source_type ] <EOL> else : <EOL> raise TypeError ( '<STR_LIT>' . format ( '<STR_LIT:U+002CU+0020>' . join ( type_mapper . values ( ) ) ) ) <EOL> if eve_type == '<STR_LIT:string>' : <EOL> if self . objectidregex . match ( source ) : <EOL> eve_type = '<STR_LIT>' <EOL> elif self . intrangeregex . match ( source ) : <EOL> eve_type = '<STR_LIT>' <EOL> elif self . floatrangeregex . match ( source ) : <EOL> eve_type = '<STR_LIT:float>' <EOL> return eve_type <EOL> def format_endpoint ( self , endpoint_schema ) : <EOL> """<STR_LIT>""" <EOL> endpoint = json . dumps ( endpoint_schema , indent = <NUM_LIT:4> , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) , sort_keys = False ) <EOL> updates = [ <EOL> ( '<STR_LIT:">' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT:true>' , '<STR_LIT:True>' ) , <EOL> ( '<STR_LIT:false>' , '<STR_LIT:False>' ) <EOL> ] <EOL> for needle , sub in updates : <EOL> endpoint = endpoint . replace ( needle , sub ) <EOL> return endpoint <EOL> def write_file ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> template = self . template_env . get_template ( '<STR_LIT>' ) <EOL> settings = template . render ( <EOL> endpoints = OrderedDict ( [ ( endpoint , self . format_endpoint ( schema ) ) for endpoint , schema in self . endpoints . iteritems ( ) ] ) <EOL> ) <EOL> with open ( filename , '<STR_LIT:w>' ) as ofile : <EOL> ofile . write ( settings + "<STR_LIT:\n>" ) <EOL> def __iter__ ( self ) : <EOL> for k , v in self . endpoints . iteritems ( ) : <EOL> yield k , v <EOL> def __repr__ ( self ) : <EOL> return json . dumps ( self . endpoints ) <EOL> def __str__ ( self ) : <EOL> return json . dumps ( self . endpoints , indent = <NUM_LIT:4> , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) ) <EOL> def __len__ ( self ) : <EOL> return len ( self . endpoints ) <EOL> def __getitem__ ( self , k ) : <EOL> return self . endpoints [ k ] <EOL> def __sizeof__ ( self ) : <EOL> return len ( self . endpoints ) </s>
