This repository has been archived by the owner on Jan 24, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 93
Peer service #1556
Merged
Merged
Peer service #1556
Changes from 22 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
06d70c0
Peer service initial commit
david4096 626f93e
Announce persistence to sqlite
david4096 93600b5
Simulated datarepo
david4096 7f16b8f
Add tests for adding and removing peers via repo cli
david4096 24d95e0
Add /info endpoint and improve simulated peers representation
david4096 fab13cd
Add peer to prepare compliance
david4096 a7b72b6
Merge remote-tracking branch 'upstream/master' into 1507_peers
david4096 1bf4012
Change URI to URL
david4096 1ce73b0
Add add peer data model, config setting for peer list
david4096 2d6035b
Add some comments, change uri to url
david4096 5ebdf5f
Merge remote-tracking branch 'upstream/master' into 1507_peers
david4096 eb66c55
Use datamodel in repo manager CLI
david4096 9f24c8b
Nicely add peer list from a file
david4096 25f6aef
Add updated client, add tests
david4096 7c82e2d
Set up client tests, add fallback for announcement
david4096 d23c681
Merge remote-tracking branch 'upstream/master' into 1507_peers
david4096 01bda2e
Add comments
david4096 2d0128e
Use iterator model
david4096 3c7553a
Handle announce a bit more cleanly
david4096 5b4914a
Remove redundant code paths
david4096 44138b2
Add announcement fields to database
david4096 38b6e1e
Add docs for configuration and managing peers
david4096 e406086
Use requests in place of client
david4096 a1625bd
Note the methods used only in testing
david4096 c9e3915
Move network methods to their own package
david4096 e766a1e
Flake fixes
david4096 60bb886
Add urlparse validation
david4096 49cc31e
Refactor m > models
david4096 8f13749
Add repo list and clear announcements
david4096 bc32445
Update constraints.txt
ejacox 7cb30fa
Merge branch 'master' into 1507_peers
ejacox File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
""" | ||
Peer datamodel for exchanging data about GA4GH services. | ||
""" | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
import json | ||
import re | ||
|
||
import ga4gh.server.exceptions as exceptions | ||
|
||
import ga4gh.schemas.protocol as protocol | ||
|
||
|
||
def isUrl(urlString): | ||
""" | ||
Attempts to return whether a given URL string is valid by applying | ||
a regex match. From http://stackoverflow.com/questions/7160737/ | ||
""" | ||
regex = re.compile( | ||
r'^(?:http|ftp)s?://' # http:// or https:// | ||
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)' | ||
r'+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain... | ||
r'localhost|' # localhost... | ||
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip | ||
r'(?::\d+)?' # optional port | ||
r'(?:/?|[/?]\S+)$', re.IGNORECASE) | ||
return regex.match(urlString) | ||
|
||
|
||
class Peer: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should always use new-style classes, which have |
||
""" | ||
This class represents an abstract Peer object. | ||
It sets default values and getters, as well as the | ||
toProtocolElement function. | ||
""" | ||
def __init__(self, url, attributes={}, record=None): | ||
self._url = "" | ||
self._attributes = {} | ||
self.setUrl(url) \ | ||
.setAttributes(attributes) | ||
if record is not None: | ||
self.populateFromRow(record) | ||
|
||
def setUrl(self, url): | ||
""" | ||
Attempt to safely set the URL by string. | ||
""" | ||
if isUrl(url): | ||
self._url = url | ||
else: | ||
raise exceptions.BadUrlException(url) | ||
return self | ||
|
||
def getUrl(self): | ||
return self._url | ||
|
||
def setAttributes(self, attributes): | ||
""" | ||
Sets the attributes message to the provided value. | ||
""" | ||
self._attributes = attributes | ||
return self | ||
|
||
def setAttributesJson(self, attributesJson): | ||
""" | ||
Sets the attributes dictionary from a JSON string. | ||
""" | ||
try: | ||
self._attributes = json.loads(attributesJson) | ||
except: | ||
raise exceptions.InvalidJsonException(attributesJson) | ||
return self | ||
|
||
def serializeAttributes(self, msg): | ||
""" | ||
Sets the attrbutes of a message during serialization. | ||
""" | ||
attributes = self.getAttributes() | ||
for key in attributes: | ||
protocol.setAttribute( | ||
msg.attributes.attr[key].values, attributes[key]) | ||
return msg | ||
|
||
def getAttributes(self): | ||
""" | ||
Returns the attributes for the DatamodelObject. | ||
""" | ||
return self._attributes | ||
|
||
def toProtocolElement(self): | ||
peer = protocol.Peer() | ||
peer.url = self._url | ||
self.serializeAttributes(peer) | ||
return peer | ||
|
||
def populateFromRow(self, peerRecord): | ||
""" | ||
This method accepts a model record and sets class variables. | ||
""" | ||
self.setUrl(peerRecord.url) \ | ||
.setAttributesJson(peerRecord.attributes) | ||
return self |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there isn't some way to do this with
urllib
orurlparse
or something?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(and if there isn't, this is general enough to be pushed down into a lower level... at least put a TODO, we don't need to put it in, say,
common
presently...)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, instead of a regex you can just catch an exception fromurllib
, I'll do that.http://grokbase.com/t/python/python-list/018fex83ty/check-url-simply
On closer inspection I found that the urlparse method was too lenient for valid addresses and that in practice some regex is needed. I added it and kept the regex for now. We could definitely move it out, added an issue for it here #1594