def test_log_with_bad_level_uses_info(set_console_ip, info_threshold, tmpdir): log_path = os.path.join(tmpdir.strpath, 'app.log') with patch('qpylib.log_qpylib._log_file_location') as mock_log_location: mock_log_location.return_value = log_path qpylib.create_log() qpylib.log('hello', 'BAD') verify_log_file_content(log_path, [{'level': 'INFO', 'text': 'hello'}])
def test_log_with_bad_level_raises_error(info_threshold, tmpdir): log_path = os.path.join(tmpdir.strpath, 'app.log') with patch('qpylib.log_qpylib._log_file_location') as mock_log_location: mock_log_location.return_value = log_path qpylib.create_log() with pytest.raises(ValueError, match="Unknown level: 'BAD'"): qpylib.log('hello', 'BAD')
def start_db(): if os.path.isfile(app.config['DATABASE']): qpylib.log("mystore db file exists, do nothing ...") else: qpylib.log( "mystore db file does not exist, starting db creation and initialisation" ) init_db()
def getIP(): try: console_ip = qpylib.get_console_address() return jsonify(console=console_ip) except Exception as e: qpylib.log( "Error " + str(e) ) raise return jsonify(console=console_ip)
def restData(): qpylib.log("Folder is" + APP_STATIC) try: return send_from_directory(APP_STATIC, "index.html" ) #return render_template("index.html") except Exception as e: qpylib.log( "Error " + str(e) ) raise
def list_function(): qpylib.log("ListFunction", "debug") # appContext will contain the id's from the selected table row/rows rows = request.args.get("appContext") qpylib.log("appContext=" + rows, "debug") # You can process the data and return any value here, that will be passed into javascript return rows
def get_offense(offense_id): try: offense_json = get_offense_json_ld(offense_id) return Response(response=offense_json, status=200, mimetype='application/json') except Exception as e: qpylib.log('Error calling get_offense_json_ld: ' + str(e), 'ERROR') raise
def get_entity_by_id(id, entity_endpoint, headers): try: full_entity_endpoint = entity_endpoint + '/' +str(id) response = qpylib.REST('GET', full_entity_endpoint, headers=headers) if response.status_code != 200: qpylib.log('API returned an error. Error: {0}'.format(response.content), level='error') return response.json() except Exception, e: qpylib.log('Unable to retrieve entity records from QRadar. Error: {0}'.format(str(e)), level='error')
def query_ariel_databases(): try: response = qpylib.REST('get', '/api/ariel/databases') qpylib.log('response=' + str(response.json())) return response except Exception as ex: qpylib.log( 'Error calling REST api GET /api/ariel/databases: ' + str(ex), 'ERROR') raise
def get_offenses(): offenses_endpoint = '/api/siem/offenses' headers = {"Content-type": "application/json", "Accept" : "application/json"} try: response = qpylib.REST('GET', offenses_endpoint, headers=headers) if response.status_code != 200: qpylib.log('API returned an error. Error: {0}'.format(response.content), level='error') return response.json() except Exception, e: qpylib.log('unable to retrieve offense records from QRadar. Error: {0}'.format(str(e)), level='error')
def getExampleDashboardItem(): try: qpylib.log("getExampleDashboardItem>>>") return json.dumps({ 'id': 'ExampleDashBoardItem', 'title': 'Example Dashboard', 'HTML': render_template('dashboard.html') }) except Exception as e: qpylib.log("Error " + str(e)) raise
def get_ariel_databases(): response = query_ariel_databases() if response.status_code == 401: results = "A 'Deploy' is needed before this app can operate." \ " Please navigate to 'Admin' tab and click 'Deploy Changes'." elif response.status_code == 200: results = response.json() else: qpylib.log( 'get_ariel_databases() Unexpected response status code: ' + response.status_code, 'ERROR') results = "An unexpected error occurred see app logs for details." return render_template('oauth.html', results=results)
def upload_cert(): if 'cert' not in request.files: qpylib.log('no certificate file in upload request') return redirect('/', code=303) file = request.files['cert'] # If the user does not select a file, the browser also submits an empty part without filename if file.filename == '': qpylib.log('no certificate file in upload request') return redirect('/', code=303) filename = secure_filename(file.filename) file.save(os.path.join(CERTS_DIRECTORY, filename)) refresh_certs() return redirect('/', code=303)
def send_file(filename): qpylib.log(" >>> route resources >>>") qpylib.log(" filename=" + filename) qpylib.log(" app.static_folder=" + app.static_folder) qpylib.log(" full file path =" + app.static_folder + '/resources/' + filename) return send_from_directory(app.static_folder, 'resources/' + filename)
def get_ariel_databases(): try: ariel_databases = qpylib.REST('get', '/api/ariel/databases') options = {} for db_name in ariel_databases.json(): options[db_name] = db_name qpylib.log("Ariel DB name: " + db_name) item = { 'id': 'ArielDBs', 'title': 'Ariel DB names', 'HTML': render_template('ariel.html', options=options) } return json.dumps(item) except Exception as ex: qpylib.log( 'Error calling REST api GET /api/ariel/databases: ' + str(ex), 'ERROR') raise
def get_certificate_management_app(): params = { 'filter': 'manifest(name)="QRadar Certificate Management" and application_state(status)="RUNNING"', 'fields': 'application_state' } response = qpylib.REST(rest_action='GET', request_url='/api/gui_app_framework/applications', params=params) if not response.status_code == 200: qpylib.log('Failed to get Certificate Management App') jsonResult = response.json() address = "" if len(jsonResult) > 0: for app_id in jsonResult: cert_management_id = app_id['application_state']['application_id'] console_ip = qpylib.get_console_address() address = "https://{0}/console/plugins/{1}/app_proxy/#/browse/uploadRoot".format( console_ip, cert_management_id) return address
def get_entity(entity_endpoint, headers, query=None, method='GET', fields='', filter=''): try: full_request = str(entity_endpoint) is_fields = False if fields != '': full_request += '?fields=' + fields is_fields = True if filter != '': if is_fields: full_request += '&filter=' + filter else: full_request += '?filter=' + filter response = qpylib.REST(method, full_request, headers=headers, params=query) if response.status_code != 200: qpylib.log('API returned an error. Error: {0}'.format(response.content), level='error') return response.json() except Exception, e: qpylib.log('Unable to retrieve entity records from QRadar. Error: {0}'.format(str(e)), level='error')
def offenseHeader(offense_id): headers = { 'content_type': 'application/json', 'Version': OFFENSES_API_VERSION } params = {'fields': 'offense_source'} restReturn = qpylib.REST('GET', 'api/siem/offenses/' + str(offense_id), headers=headers, params=params) lastrep = None offenseSource = None cats = None try: if restReturn.status_code == 200: restReturnJson = restReturn.json() qpylib.log('Offenses API returned: %s' % restReturnJson, level='debug') offenseSource = restReturnJson['offense_source'] xResponse = requests.get( 'https://api.xforce.ibmcloud.com/ipr/history/' + offenseSource, auth=(API_KEY, API_PASS)) if xResponse.status_code == 200: xJson = xResponse.json() qpylib.log('Xforce API returned: %s' % xJson, level='debug') history = xJson.get("history") lastrep = history[-1] cats = lastrep.get("cats").keys() html = render_template('xforce_details.html', ip=offenseSource, rep=lastrep, cats=cats) return jsonify({'html': html}) except Exception as e: html = render_template('xforce_details.html', ip=offenseSource, rep=lastrep, cats=cats, error=str(e)) return jsonify({'html': html})
def getLocationDataDest(ipaddrdest): #qpylib.log('entered in to getLocationDataDest function') # get source ip location -- api url freegeoip.net/{format}/{ipaddr} # always use json for format and pass in ip # example of url : http://freegeoip.net/json/9.183.105.12 where 9.183.105.12 = ipaddr # locationData = {} #from response: locationData['longitude'] = response['longitude'], # locationData['latitude'] = response['latitude'] #return locationData try: qpylib.log('entered try of getLocationData destination') #headers = {'content-type' : 'text/plain'} destinationLocationAPI = requests.get('http://freegeoip.net/json/%s' % ipaddrdest) # qpylib.log('successfully retrieved locationAPI response') destinationLocationData = {} result = destinationLocationAPI.json() # qpylib.log(json.dumps(result, indent=2)) destinationLocationData['longitude'] = result['longitude'] destinationLocationData['latitude'] = result['latitude'] #qpylib.log('longitude :: %s' % result['longitude']) # qpylib.log('latitude :: %s' % result['latitude']) # qpylib.log(json.dumps(destinationLocationData, indent = 2)) qpylib.log('got destination') return destinationLocationData except Exception as e: qpylib.log( "Error /// " + str(e), level='error' ) return 'getLocationDataDest failed'
def stopThread(): # qpylib.log('Stop Thread Call made', level='info') for threadObj in threading.enumerate(): threadName = threadObj.name qpylib.log('Thread Name %s' % threadName, level='info') if threadName == "securityscorecardthread": threadObj.event.set() terminate_thread(threadObj) is_alive = True while is_alive: qradar_found = False for threadObj in threading.enumerate(): threadName = threadObj.name qpylib.log('Thread Name %s' % threadName, level='info') if threadName == "securityscorecardthread": qradar_found = True if not qradar_found: is_alive = False
def restData(): try: qpylib.log("hi") headers = {'content-type' : 'text/plain'} searchQuery = {'query_expression' : "select sourceip, destinationip, sourceport, destinationport, sourcebytes, destinationbytes, sourcepackets, destinationpackets from \"flows\" limit 5",} apiResponse = qpylib.REST( 'post', 'api/ariel/searches', headers=headers, params=searchQuery) qpylib.log(apiResponse.content) response = json.loads(apiResponse.content) search_id = (response['search_id']) qpylib.log('SEARCH ID :: %s' % response['search_id']) params = {} # options = {} # for arielSearch in apiResponse.json(): # options[arielSearch] = arielSearch.capitalize() # qpylib.log( "Search value " + arielSearch) # return json.dumps({'id':'arielSearch', 'title':'search id', 'HTML':render_template("testAPICall.html", options=options) } ) return searchIDData(search_id) #return json.dumps({'id':'arielSearch', 'title':'search id'}) except Exception as e: qpylib.log( "Error " + str(e), level='error' )
def threadTask(self): qpylib.log('Calling process_events first time for actual work', level='info') process_events(self.config, self.access_key, self.domain) qpylib.log('Finished write_leefs first time for actual work', level='info') next_run = datetime.datetime.now() + datetime.timedelta( seconds=CONFIG.LEEF_FETCH_INTERVAL) while True: if next_run < datetime.datetime.now(): qpylib.log('Next run of write_leefs ', level='info') process_events(self.config, self.access_key, self.domain) qpylib.log( 'Finished write_leefs subsequent time for actual work', level='info') next_run = datetime.datetime.now() + datetime.timedelta( seconds=CONFIG.LEEF_FETCH_INTERVAL)
def searchIDData(search_id): try: headers = {'content-type' : 'text/plain'} while True: statusResponse = qpylib.REST( 'get', 'api/ariel/searches/%s' % search_id, headers=headers) response = json.loads(statusResponse.content) qpylib.log('STATUS :: %s' % response['status']) qpylib.log('SEARCH_ID :: %s' % response['search_id']) status = response['status'] statusSearch_id = response['search_id'] if status == "COMPLETED": break else: time.sleep(3) return flowData(statusSearch_id) except Exception as e: qpylib.log( "Error " + str(e), level='error' )
def send_file(file): qpylib.log(" >>> route resources >>>") qpylib.log(" file=" + file) qpylib.log(" app.static_folder=" + app.static_folder) qpylib.log(" full file path =" + app.static_folder + '/resources/'+file ) return send_from_directory(app.static_folder, 'resources/'+file)
def index(): # demonstrates how to request the accept-language header from qradar qpylib.log(request.headers.get('Accept-Language', '')) return render_template("index.html", title="QApp1 : Hello World !")
def flowData(statusSearch_id): try: headers = {'content-type' : 'text/plain'} #range = {"range":"items=0-5"} flowDataOptions = qpylib.REST( 'get', 'api/ariel/searches/%s' % statusSearch_id + '/results', headers=headers) #options = {} #group = {} #flow = flowDataOptions.json() #for flowInfo in flowDataOptions.json(): #options[flowInfo] = flowInfo.capitalize() #qpylib.log( "Search value " + flowInfo) #flowInfo2 = json.loads(flowDataOptions.content) #flowData = {} flowData = flowDataOptions.json() qpylib.log(json.dumps(flowData)) #qpylib.log(flowData) #flowDataJson = json.loads(flowData) #json = flowDataOptions.json() #qpylib.log(json) geoIpData = [] geoIpDataDestination = [] #for x in flowDataOptions.json(): # sourceIps['ipAddr'] = x['flows']['sourceip'] # qpylib.log( "result source ip " + x) qpylib.log('flowData flows: ' + json.dumps(flowData['flows'],indent=2)) for x in flowData['flows']: flowsData = {} #destinationIP = {} qpylib.log('inside flowdata loop') #sourceIP['ipAddr'] = flowData['flows']['sourceip'] flowsData['ipAddr'] = x['sourceip'] flowsData['ipAddrDest'] = x['destinationip'] flowsData['sourcePort'] = x['sourceport'] flowsData['destinationPort'] = x['destinationport'] flowsData['sourceBytes'] = x['sourcebytes'] flowsData['destinationBytes'] = x['destinationbytes'] flowsData['sourcePackets'] = x['sourcepackets'] flowsData['destinationPackets'] = x['destinationpackets'] #flowDataJson = json.loads(flowData) #qpylib.log(flowDataJson) #sourceIP[ipAddr] = flowDataJson['sourceip'] qpylib.log('source ip: ' + json.dumps(x['sourceip'])) qpylib.log('destination ip: ' + json.dumps(x['destinationip'])) qpylib.log('source port: ' + json.dumps(x['sourceport'])) #qpylib.log('source ip ipadd ' + json.dumps(sourceIP['ipAddr'])) #geoIpData.extend(sourceIP and destinationIP) geoIpData.append(flowsData) #geoIpData.append(destinationIP) #geoIpData = dict(sourceIP.items() + destinationIP.items()) qpylib.log('geoIpData : ' + json.dumps(geoIpData, indent=2)) for y in geoIpData: ipaddr = y['ipAddr'] qpylib.log('inside flowdata loop y') # write function where you request response from new freegeoip api # eg. locationData = return of getLocationData(ipaddr) #y['locationData'] = locationData locationData = getLocationData(ipaddr) qpylib.log('getLocationData result: ' + json.dumps(locationData,indent=2)) y['sourceLocationData'] = locationData for b in geoIpData: ipaddrdest = b['ipAddrDest'] qpylib.log('inside flowdata loop dest b') locationDataDest = getLocationDataDest(ipaddrdest) qpylib.log('getLocationDataDest result: ' + json.dumps(locationDataDest,indent=2)) b['destinationLocationData'] = locationDataDest qpylib.log('final geoIpData : ' + json.dumps(geoIpData, indent = 2)) ''' qpylib.log('final geoIpDataDestination : ' + json.dumps(geoIpDataDestination, indent = 2)) flowsData = [5]*(len(geoIpData)+len(geoIpDataDestination)) flowsData[::2] = geoIpData flowsData[1::2] = geoIpDataDestination qpylib.log('final DATA : ' + json.dumps(flowsData, indent = 2)) #data = geoIpData + geoIpDataDestination #qpylib.log('final DATA : ' + json.dumps(data, indent = 2)) ''' ''' for x in flowData['flows']: sourceIP = {} qpylib.log('inside flowdata loop') #sourceIP['ipAddr'] = flowData['flows']['sourceip'] sourceIP['ipAddr'] = x['sourceip'] #flowDataJson = json.loads(flowData) #qpylib.log(flowDataJson) #sourceIP[ipAddr] = flowDataJson['sourceip'] qpylib.log('source ip: ' + json.dumps(x['sourceip'])) #qpylib.log('source ip ipadd ' + json.dumps(sourceIP['ipAddr'])) geoIpData.append(sourceIP) qpylib.log('geoIpData : ' + json.dumps(geoIpData, indent=2)) for y in geoIpData: ipaddr = y['ipAddr'] qpylib.log('inside flowdata loop y') # write function where you request response from new freegeoip api # eg. locationData = return of getLocationData(ipaddr) #y['locationData'] = locationData locationData = getLocationData(ipaddr) qpylib.log('getLocationData result: ' + json.dumps(locationData,indent=2)) y['sourceLocationData'] = locationData qpylib.log('final geoIpData : ' + json.dumps(geoIpData, indent = 2)) for a in flowData['flows']: qpylib.log('inside flowdata loop dest') destinationIP = {} destinationIP['ipAddrDest'] = a['destinationip'] qpylib.log('destination ip: ' + json.dumps(a['destinationip'])) geoIpDataDestination.append(destinationIP) qpylib.log('geoIpDataDestination : ' + json.dumps(geoIpData, indent=2)) for b in geoIpDataDestination: ipaddrdest = b['ipAddrDest'] qpylib.log('inside flowdata loop dest b') locationDataDest = getLocationDataDest(ipaddrdest) qpylib.log('getLocationDataDest result: ' + json.dumps(locationDataDest,indent=2)) b['destinationLocationData'] = locationDataDest qpylib.log('final geoIpDataDestination : ' + json.dumps(geoIpDataDestination, indent = 2)) #data = geoIpData + geoIpDataDestination #qpylib.log('final DATA : ' + json.dumps(data, indent = 2)) ''' return render_template("map.html", data=json.dumps(geoIpData), base_url=qpylib.get_app_base_url()) #return render_template("map.html", data=json.dumps(data), base_url=qpylib.get_app_base_url()) except Exception as e: qpylib.log( "Error ---- " + str(e), level='error' )
def test_all_log_levels_with_set_debug_threshold(info_threshold, tmpdir): log_path = os.path.join(tmpdir.strpath, 'app.log') with patch('qpylib.log_qpylib._log_file_location') as mock_log_location: mock_log_location.return_value = log_path qpylib.create_log() qpylib.set_log_level('DEBUG') qpylib.log('hello debug', 'DEBUG') qpylib.log('hello default info') qpylib.log('hello info', 'INFO') qpylib.log('hello warning', 'WARNING') qpylib.log('hello error', 'ERROR') qpylib.log('hello critical', 'CRITICAL') qpylib.log('hello exception', 'EXCEPTION') verify_log_file_content(log_path, [{ 'level': 'DEBUG', 'text': 'hello debug' }, { 'level': 'INFO', 'text': 'hello default info' }, { 'level': 'INFO', 'text': 'hello info' }, { 'level': 'WARNING', 'text': 'hello warning' }, { 'level': 'ERROR', 'text': 'hello error' }, { 'level': 'CRITICAL', 'text': 'hello critical' }, { 'level': 'ERROR', 'text': 'hello exception' }])
def test_log_without_create_raises_error(): with pytest.raises( RuntimeError, match='You cannot use log before logging has been initialised'): qpylib.log('hello')
def test_set_log_level_with_bad_level_uses_info(set_console_ip, debug_threshold, tmpdir): log_path = os.path.join(tmpdir.strpath, 'app.log') with patch('qpylib.log_qpylib._log_file_location') as mock_log_location: mock_log_location.return_value = log_path qpylib.create_log() qpylib.set_log_level('BAD') qpylib.log('hello debug', 'DEBUG') qpylib.log('hello default info') qpylib.log('hello info', 'INFO') qpylib.log('hello warning', 'WARNING') qpylib.log('hello error', 'ERROR') qpylib.log('hello critical', 'CRITICAL') verify_log_file_content(log_path, [{ 'level': 'INFO', 'text': 'hello default info' }, { 'level': 'INFO', 'text': 'hello info' }, { 'level': 'WARNING', 'text': 'hello warning' }, { 'level': 'ERROR', 'text': 'hello error' }, { 'level': 'CRITICAL', 'text': 'hello critical' }], not_expected_lines=[{ 'level': 'DEBUG', 'text': 'hello debug' }])
def result(): import poll_n_write # Get the form form = request.form # Extract the form data ACCESS_KEY = form.get('access_key') DOMAIN = form.get('domain', '') URL = form.get('url') LEVEL_OVERALL_CHANGE = form.get('level_overall_change', '1') LEVEL_FACTOR_CHANGE = form.get('level_factor_change', '1') LEVEL_NEW_ISSUE_CHANGE = form.get('level_new_issue_change', '1') # If masked access key is present, unmask it using data store if ACCESS_KEY.endswith('**********'): current_config = read_data_store() ACCESS_KEY = current_config['access_key'] # Split portfolio ids if present if form.get('portfolio_ids', '').strip().strip(',').lower() == 'all': PORTFOLIO_IDS = 'all' elif not form.get('portfolio_ids'): PORTFOLIO_IDS = None else: PORTFOLIO_IDS = form.get('portfolio_ids', '').strip().strip(',') PORTFOLIO_IDS = PORTFOLIO_IDS.split(',') if PORTFOLIO_IDS else None FETCH_HISTORICAL_DATA = bool(form.get('fetch_historical_data', False)) MONITOR_CONFIG = { 'fetch_company_overall': form.get('fetch_company_overall', 'no').lower() == 'yes', 'fetch_company_factors': form.get('fetch_company_factors', 'no').lower() == 'yes', 'fetch_company_issues': form.get('fetch_company_issues', 'no').lower() == 'yes', 'fetch_portfolio_overall': form.get('fetch_portfolio_overall', 'no').lower() == 'yes', 'fetch_portfolio_factors': form.get('fetch_portfolio_factors', 'no').lower() == 'yes', 'fetch_portfolio_issues': form.get('fetch_portfolio_issues', 'no').lower() == 'yes', 'issue_level_findings': form.get('issue_level_findings', 'no').lower() == 'yes', 'proxy': form.get('proxy'), 'level': form.get('level', 'info').upper(), 'logLevel': form.get('level'), 'proxy_type': form.get('proxy_type', 'no'), 'host': form.get('host', 'no'), 'port': form.get('port', 'no'), 'username': form.get('username', 'no'), 'password': form.get('password', 'no'), } if MONITOR_CONFIG['proxy'] != 'on': MONITOR_CONFIG['proxy'] = False DIFF_OVERRIDE_CONFIG = { 'diff_override_own_overall': form.get('diff_override_own_overall', 'no').lower() == 'yes', 'diff_override_portfolio_overall': form.get('diff_override_portfolio_overall', 'no').lower() == 'yes', 'diff_override_own_factor': form.get('diff_override_own_factor', 'no').lower() == 'yes', 'diff_override_portfolio_factor': form.get('diff_override_portfolio_factor', 'no').lower() == 'yes', } if MONITOR_CONFIG['proxy']: proxy_dict = { 'proxy_type': MONITOR_CONFIG['proxy_type'], 'host': MONITOR_CONFIG['host'], 'port': MONITOR_CONFIG['port'], 'username': MONITOR_CONFIG['username'], 'password': MONITOR_CONFIG['password'], } else: proxy_dict = {} MONITOR_CONFIG['proxy_type'] = '' MONITOR_CONFIG['host'] = '' MONITOR_CONFIG['port'] = '' MONITOR_CONFIG['username'] = '' MONITOR_CONFIG['password'] = '' try: log_level = MONITOR_CONFIG.get('level', 'INFO') qpylib.set_log_level(log_level) except Exception as err: qpylib.log("Error " + str(err)) raise # Connect to the SecurityScorecard; Start polling and write to the LEEF logger res, msg = poll_n_write.poll_scorecard_n_write( ACCESS_KEY, DOMAIN, URL, LEVEL_OVERALL_CHANGE, LEVEL_FACTOR_CHANGE, LEVEL_NEW_ISSUE_CHANGE, PORTFOLIO_IDS, FETCH_HISTORICAL_DATA, MONITOR_CONFIG, DIFF_OVERRIDE_CONFIG, proxy_dict) # If polling starts successfully, render dloading.html; Otherwise render error_with_back.html if res == True: return render_template('dloading.html', qname="success") else: qpylib.log(msg, level='error') return render_template('error_with_back.html', err=msg)
def getLocationData(ipaddr): qpylib.log('entered in to getLocationData function') # get source ip location -- api url freegeoip.net/{format}/{ipaddr} # always use json for format and pass in ip # example of url : http://freegeoip.net/json/9.183.105.12 where 9.183.105.12 = ipaddr # locationData = {} #from response: locationData['longitude'] = response['longitude'], # locationData['latitude'] = response['latitude'] #return locationData try: qpylib.log('entered try of getLocationData') #headers = {'content-type' : 'text/plain'} locationAPI = requests.get('http://freegeoip.net/json/%s' % ipaddr) qpylib.log('successfully retrieved locationAPI response') sourceLocationData = {} result = locationAPI.json() qpylib.log(json.dumps(result, indent=2)) sourceLocationData['longitude'] = result['longitude'] sourceLocationData['latitude'] = result['latitude'] qpylib.log('longitude :: %s' % result['longitude']) qpylib.log('latitude :: %s' % result['latitude']) qpylib.log(json.dumps(sourceLocationData, indent = 2)) return sourceLocationData #for group[flows][0] in flowInfo2: # qpylib.log('STATUS :: %s' % responseapi['sourceip']) #for flowInfo in flowDataOptions.json(): #group = flowInfo.keys() #qpylib.log(group) # group[flowInfo][i] = flowInfo.capitalize() # qpylib.log( "FLOW DATA " + flowInfo + i) #for flowInfoInner in group[1]: # options[flowInfoInner] = flowInfoInner.capitalize() # qpylib.log( "FLOW DATA " + flowInfoInner ) #return json.dumps({'id':'ArielDBs', 'title':'Choose a DB', 'HTML':render_template("map.html", options=options)}) #return render_template("map.html") #return 'getLocationData success' except Exception as e: qpylib.log( "Error /// " + str(e), level='error' ) return 'getLocationData failed'
def reactIntl(requested): def put_in_container(container, key, value): key_parts = key.split(".") s = len(key_parts) l = 0 while s > 1: part = key_parts[l] if not part in container: container[part] = {} container = container[part] s = s - 1 l = l + 1 container[key_parts[l]] = value resources = os.path.dirname(os.path.abspath( sys.argv[0])) + "/app/static/resources" requested_language = None requested_locale = None lang_locale = re.compile("[_\\-]").split(requested) if len(lang_locale) == 2: requested_language = lang_locale[0] requested_locale = lang_locale[1] else: requested_language = requested requested_locale = None qpylib.log( "Requested language {0}, locale {1}".format(requested_language, requested_locale), "DEBUG") result = {"locales": [], "messages": {}} for f in os.listdir(resources): bundle_lang = f.split("_") # This will be either application_<lang>.properties, in which case we have 2 parts, or application_<lang>_<locale>.properties, which is 3 parts locale = None if len(bundle_lang) == 2: language = bundle_lang[1].split(".")[0] else: language = bundle_lang[1] locale = bundle_lang[2].split(".")[0] qpylib.log( "Bundle {0} language {1}, locale {2}".format(f, language, locale), "DEBUG") if language == requested_language: filepath = os.path.join(resources, f) if os.path.isfile(filepath): with open(filepath) as thefile: lang = {} for line in thefile: line = line.strip() if len(line) > 0: key_value = line.split("=") put_in_container( lang, key_value[0].strip(), key_value[1].decode('unicode-escape')) if locale is None: result["locales"].append(language) else: result["locales"].append(language + "_" + locale) result["messages"].update(lang) return json.dumps(result)
def add_entry(): qpylib.log("add_entry()>>>") g.db.execute('insert into entries (title, text) values (?, ?)', [request.form['title'], request.form['text']]) g.db.commit() return redirect(url_for('show_entries'), code=303)