Commit d442e770 authored by Loic Baron's avatar Loic Baron
Browse files

Merge branch 'master' of gitlab.noc.onelab.eu:onelab/myslicelib

parents de40a08e ae313819
import json
import os
from pprint import pprint
from functools import lru_cache
from myslicelib.model import Entities, Entity
from myslicelib.util import HDict
class Resources(Entities):
pass
......@@ -8,8 +12,14 @@ class Resource(Entity):
_type = "resource" # link, channel ???
_collection = "Resources"
#
#
def __init__(self, data = {}):
if 'location' in data:
# infere the city/country based on coordinates
data['location'] = self.get_address(HDict(data['location']))
# infere coordinates from the city/country
#location = get_coordinates(location)
super().__init__(data)
# from repoze.lru import lru_cache
# @lru_cache(100)
# def get_location(city):
......@@ -27,18 +37,73 @@ class Resource(Entity):
# print(e)
# return location
#
# @lru_cache(100)
# def get_country(location):
# try:
# from geopy.geocoders import GoogleV3
# geolocator = GoogleV3()
#
# address = geolocator.reverse(location)
# return address[0].address.split(', ')[-1]
# except Exception as e:
# print(e)
# return location
#
def getJsonLocation(self, location):
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(dir_path+'/resources.json') as json_data:
try:
resources = json.load(json_data)
except Exception as e:
import traceback
traceback.print_exc()
else:
for r in resources:
if 'location' in r:
if 'longitude' in location and r['location']['longitude'] == location['longitude'] and 'latitude' in location and r['location']['latitude'] == location['latitude']:
return r['location']
#else:
# print("Resource without location in JSON")
# pprint(r)
raise ValueError("location not found in JSON")
def get_component(self, location, component_types):
pprint(location.raw)
# Nominatim OpenStreetMap format
if 'address' in location.raw:
for key, value in location.raw['address'].items():
if key in component_types:
return value
# GoogleV3 format
else:
for component in location.raw['address_components']:
if len(set(component_types).intersection(component['types']))>0:
return component['long_name']
@lru_cache(100)
def get_address(self, location):
# if city or country are not specified, get it from the coordinates
if ('city' not in location or not location['city']) and ('country' not in location or not location['country']):
try:
# search for city/country in JSON based on coordinates
return self.getJsonLocation(location)
except ValueError as e:
# search for city/country using geopy based on coordinates
# provide a Google API key (as a string)
key = None
if key:
from geopy.geocoders import GoogleV3
geolocator = GoogleV3(api_key=key)
city = 'locality'
else:
from geopy.geocoders import Nominatim
geolocator = Nominatim(timeout=10)
city = ['city', 'town']
try:
address = geolocator.reverse(location['latitude']+", "+location['longitude'])
if isinstance(address, list):
address = address[0]
except Exception as e:
import traceback
traceback.print_exc()
pprint(address)
if address is not None:
location['city'] = self.get_component(address, city)
location['country'] = self.get_component(address, 'country')
except Exception as e:
import traceback
traceback.print_exc()
return location
def save(self, setup=None):
raise NotImplemented("A Resource has to be part of a slice to be saved slice.save()")
......
This diff is collapsed.
......@@ -2,6 +2,10 @@ from OpenSSL import crypto, SSL
import os.path
from myslicelib.util.url import validateUrl
class HDict(dict):
def __hash__(self):
return hash(frozenset(self.items()))
class Endpoint(object):
"""
An endpoint specifies a remote API endpoint.
......
......@@ -68,7 +68,7 @@ class Ple(SfaParser):
for el in elements:
if el.attrib['name']==name:
return el.attrib['value']
return ''
# <node component_manager_id="urn:publicid:IDN+ple+authority+cm" component_id="urn:publicid:IDN+ple:uitple+node+planetlab1.cs.uit.no" exclusive="false" component_name="planetlab1.cs.uit.no">
# <hardware_type name="plab-pc"/>
......
......@@ -16,21 +16,22 @@ from myslicelib.query import q
from pprint import pprint
s.endpoints = [
#Endpoint(url="https://sfa3.planet-lab.eu:12346",type="AM", name="ple"),
Endpoint(url="https://sfa.planet-lab.eu:443",type="AM", name="ple"),
#Endpoint(url="https://194.199.16.164:12346",type="AM", name="iotlab"),
#Endpoint(url="https://www.wilab2.ilabt.iminds.be:12369/protogeni/xmlrpc/am/3.0",type="AM",name="WiLab.t"),
#Endpoint(url="https://fuseco.fokus.fraunhofer.de/api/sfa/am/v3",type="AM"),
Endpoint(url="https://griffin.ipv6.lip6.fr:8001/RPC2",type="AM"),
#Endpoint(url="https://griffin.ipv6.lip6.fr:8001/RPC2",type="AM"),
#Endpoint(url="https://vnews.netmode.ntua.gr:8001/RPC2",type="AM", name="netmode"),
Endpoint(url="https://portal.onelab.eu:6080",type="Reg", name="OneLab Reg"),
#Endpoint(url="https://localhost:12345",type="Reg", name="OneLab Reg"),
#Endpoint(url="https://sfa-fed4fire.pl.sophia.inria.fr:443",type="Reg")
]
path = os.path.expanduser("~/.sfi/")
pkey = path + "onelab.upmc.loic_baron.pkey"
pkey = path + "onelab.upmc.loic_baron.prod.pkey"
hrn = "onelab.upmc.loic_baron"
email = "loic.baron@lip6.fr"
cert = path + "onelab.upmc.loic_baron.user.gid"
cert = path + "onelab.upmc.loic_baron.user.prod.gid"
# expired cred
credentials = None
#credentials = [{'user_credential':path+'onelab.upmc.loic_baron.user_for_onelab.myslice.user.cred'}
......@@ -61,7 +62,9 @@ s.authentication = Authentication(hrn=hrn, email=email, certificate=cert, privat
#l = q(Lease).get()
#pprint(l)
r = q(Resource).get().first()
r = q(Resource).get()
pprint(r)
exit(1)
import time
u = q(User).id('urn:publicid:IDN+onelab:upmc+user+loic_baron').get().first()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment