id
int32 0
252k
| repo
stringlengths 7
55
| path
stringlengths 4
127
| func_name
stringlengths 1
88
| original_string
stringlengths 75
19.8k
| language
stringclasses 1
value | code
stringlengths 51
19.8k
| code_tokens
sequence | docstring
stringlengths 3
17.3k
| docstring_tokens
sequence | sha
stringlengths 40
40
| url
stringlengths 87
242
|
---|---|---|---|---|---|---|---|---|---|---|---|
4,100 | peterjc/backports.lzma | backports/lzma/__init__.py | LZMAFile.peek | def peek(self, size=-1):
"""Return buffered data without advancing the file position.
Always returns at least one byte of data, unless at EOF.
The exact number of bytes returned is unspecified.
"""
self._check_can_read()
if self._mode == _MODE_READ_EOF or not self._fill_buffer():
return b""
return self._buffer | python | def peek(self, size=-1):
self._check_can_read()
if self._mode == _MODE_READ_EOF or not self._fill_buffer():
return b""
return self._buffer | [
"def",
"peek",
"(",
"self",
",",
"size",
"=",
"-",
"1",
")",
":",
"self",
".",
"_check_can_read",
"(",
")",
"if",
"self",
".",
"_mode",
"==",
"_MODE_READ_EOF",
"or",
"not",
"self",
".",
"_fill_buffer",
"(",
")",
":",
"return",
"b\"\"",
"return",
"self",
".",
"_buffer"
] | Return buffered data without advancing the file position.
Always returns at least one byte of data, unless at EOF.
The exact number of bytes returned is unspecified. | [
"Return",
"buffered",
"data",
"without",
"advancing",
"the",
"file",
"position",
"."
] | 6555d8b8e493a35159025b4cfc204dfb54c33d3e | https://github.com/peterjc/backports.lzma/blob/6555d8b8e493a35159025b4cfc204dfb54c33d3e/backports/lzma/__init__.py#L268-L277 |
4,101 | peterjc/backports.lzma | backports/lzma/__init__.py | LZMAFile.read | def read(self, size=-1):
"""Read up to size uncompressed bytes from the file.
If size is negative or omitted, read until EOF is reached.
Returns b"" if the file is already at EOF.
"""
self._check_can_read()
if size is None:
#This is not needed on Python 3 where the comparison to zeo
#will fail with a TypeError.
raise TypeError("Read size should be an integer, not None")
if self._mode == _MODE_READ_EOF or size == 0:
return b""
elif size < 0:
return self._read_all()
else:
return self._read_block(size) | python | def read(self, size=-1):
self._check_can_read()
if size is None:
#This is not needed on Python 3 where the comparison to zeo
#will fail with a TypeError.
raise TypeError("Read size should be an integer, not None")
if self._mode == _MODE_READ_EOF or size == 0:
return b""
elif size < 0:
return self._read_all()
else:
return self._read_block(size) | [
"def",
"read",
"(",
"self",
",",
"size",
"=",
"-",
"1",
")",
":",
"self",
".",
"_check_can_read",
"(",
")",
"if",
"size",
"is",
"None",
":",
"#This is not needed on Python 3 where the comparison to zeo",
"#will fail with a TypeError.",
"raise",
"TypeError",
"(",
"\"Read size should be an integer, not None\"",
")",
"if",
"self",
".",
"_mode",
"==",
"_MODE_READ_EOF",
"or",
"size",
"==",
"0",
":",
"return",
"b\"\"",
"elif",
"size",
"<",
"0",
":",
"return",
"self",
".",
"_read_all",
"(",
")",
"else",
":",
"return",
"self",
".",
"_read_block",
"(",
"size",
")"
] | Read up to size uncompressed bytes from the file.
If size is negative or omitted, read until EOF is reached.
Returns b"" if the file is already at EOF. | [
"Read",
"up",
"to",
"size",
"uncompressed",
"bytes",
"from",
"the",
"file",
"."
] | 6555d8b8e493a35159025b4cfc204dfb54c33d3e | https://github.com/peterjc/backports.lzma/blob/6555d8b8e493a35159025b4cfc204dfb54c33d3e/backports/lzma/__init__.py#L279-L295 |
4,102 | peterjc/backports.lzma | backports/lzma/__init__.py | LZMAFile.read1 | def read1(self, size=-1):
"""Read up to size uncompressed bytes, while trying to avoid
making multiple reads from the underlying stream.
Returns b"" if the file is at EOF.
"""
# Usually, read1() calls _fp.read() at most once. However, sometimes
# this does not give enough data for the decompressor to make progress.
# In this case we make multiple reads, to avoid returning b"".
self._check_can_read()
if size is None:
#This is not needed on Python 3 where the comparison to zero
#will fail with a TypeError.
raise TypeError("Read size should be an integer, not None")
if (size == 0 or self._mode == _MODE_READ_EOF or
not self._fill_buffer()):
return b""
if 0 < size < len(self._buffer):
data = self._buffer[:size]
self._buffer = self._buffer[size:]
else:
data = self._buffer
self._buffer = None
self._pos += len(data)
return data | python | def read1(self, size=-1):
# Usually, read1() calls _fp.read() at most once. However, sometimes
# this does not give enough data for the decompressor to make progress.
# In this case we make multiple reads, to avoid returning b"".
self._check_can_read()
if size is None:
#This is not needed on Python 3 where the comparison to zero
#will fail with a TypeError.
raise TypeError("Read size should be an integer, not None")
if (size == 0 or self._mode == _MODE_READ_EOF or
not self._fill_buffer()):
return b""
if 0 < size < len(self._buffer):
data = self._buffer[:size]
self._buffer = self._buffer[size:]
else:
data = self._buffer
self._buffer = None
self._pos += len(data)
return data | [
"def",
"read1",
"(",
"self",
",",
"size",
"=",
"-",
"1",
")",
":",
"# Usually, read1() calls _fp.read() at most once. However, sometimes",
"# this does not give enough data for the decompressor to make progress.",
"# In this case we make multiple reads, to avoid returning b\"\".",
"self",
".",
"_check_can_read",
"(",
")",
"if",
"size",
"is",
"None",
":",
"#This is not needed on Python 3 where the comparison to zero",
"#will fail with a TypeError. ",
"raise",
"TypeError",
"(",
"\"Read size should be an integer, not None\"",
")",
"if",
"(",
"size",
"==",
"0",
"or",
"self",
".",
"_mode",
"==",
"_MODE_READ_EOF",
"or",
"not",
"self",
".",
"_fill_buffer",
"(",
")",
")",
":",
"return",
"b\"\"",
"if",
"0",
"<",
"size",
"<",
"len",
"(",
"self",
".",
"_buffer",
")",
":",
"data",
"=",
"self",
".",
"_buffer",
"[",
":",
"size",
"]",
"self",
".",
"_buffer",
"=",
"self",
".",
"_buffer",
"[",
"size",
":",
"]",
"else",
":",
"data",
"=",
"self",
".",
"_buffer",
"self",
".",
"_buffer",
"=",
"None",
"self",
".",
"_pos",
"+=",
"len",
"(",
"data",
")",
"return",
"data"
] | Read up to size uncompressed bytes, while trying to avoid
making multiple reads from the underlying stream.
Returns b"" if the file is at EOF. | [
"Read",
"up",
"to",
"size",
"uncompressed",
"bytes",
"while",
"trying",
"to",
"avoid",
"making",
"multiple",
"reads",
"from",
"the",
"underlying",
"stream",
"."
] | 6555d8b8e493a35159025b4cfc204dfb54c33d3e | https://github.com/peterjc/backports.lzma/blob/6555d8b8e493a35159025b4cfc204dfb54c33d3e/backports/lzma/__init__.py#L297-L321 |
4,103 | peterjc/backports.lzma | backports/lzma/__init__.py | LZMAFile.write | def write(self, data):
"""Write a bytes object to the file.
Returns the number of uncompressed bytes written, which is
always len(data). Note that due to buffering, the file on disk
may not reflect the data written until close() is called.
"""
self._check_can_write()
compressed = self._compressor.compress(data)
self._fp.write(compressed)
self._pos += len(data)
return len(data) | python | def write(self, data):
self._check_can_write()
compressed = self._compressor.compress(data)
self._fp.write(compressed)
self._pos += len(data)
return len(data) | [
"def",
"write",
"(",
"self",
",",
"data",
")",
":",
"self",
".",
"_check_can_write",
"(",
")",
"compressed",
"=",
"self",
".",
"_compressor",
".",
"compress",
"(",
"data",
")",
"self",
".",
"_fp",
".",
"write",
"(",
"compressed",
")",
"self",
".",
"_pos",
"+=",
"len",
"(",
"data",
")",
"return",
"len",
"(",
"data",
")"
] | Write a bytes object to the file.
Returns the number of uncompressed bytes written, which is
always len(data). Note that due to buffering, the file on disk
may not reflect the data written until close() is called. | [
"Write",
"a",
"bytes",
"object",
"to",
"the",
"file",
"."
] | 6555d8b8e493a35159025b4cfc204dfb54c33d3e | https://github.com/peterjc/backports.lzma/blob/6555d8b8e493a35159025b4cfc204dfb54c33d3e/backports/lzma/__init__.py#L323-L334 |
4,104 | peterjc/backports.lzma | backports/lzma/__init__.py | LZMAFile.seek | def seek(self, offset, whence=0):
"""Change the file position.
The new position is specified by offset, relative to the
position indicated by whence. Possible values for whence are:
0: start of stream (default): offset must not be negative
1: current stream position
2: end of stream; offset must not be positive
Returns the new file position.
Note that seeking is emulated, sp depending on the parameters,
this operation may be extremely slow.
"""
self._check_can_seek()
# Recalculate offset as an absolute file position.
if whence == 0:
pass
elif whence == 1:
offset = self._pos + offset
elif whence == 2:
# Seeking relative to EOF - we need to know the file's size.
if self._size < 0:
self._read_all(return_data=False)
offset = self._size + offset
else:
raise ValueError("Invalid value for whence: {}".format(whence))
# Make it so that offset is the number of bytes to skip forward.
if offset is None:
#This is not needed on Python 3 where the comparison to self._pos
#will fail with a TypeError.
raise TypeError("Seek offset should be an integer, not None")
if offset < self._pos:
self._rewind()
else:
offset -= self._pos
# Read and discard data until we reach the desired position.
if self._mode != _MODE_READ_EOF:
self._read_block(offset, return_data=False)
return self._pos | python | def seek(self, offset, whence=0):
self._check_can_seek()
# Recalculate offset as an absolute file position.
if whence == 0:
pass
elif whence == 1:
offset = self._pos + offset
elif whence == 2:
# Seeking relative to EOF - we need to know the file's size.
if self._size < 0:
self._read_all(return_data=False)
offset = self._size + offset
else:
raise ValueError("Invalid value for whence: {}".format(whence))
# Make it so that offset is the number of bytes to skip forward.
if offset is None:
#This is not needed on Python 3 where the comparison to self._pos
#will fail with a TypeError.
raise TypeError("Seek offset should be an integer, not None")
if offset < self._pos:
self._rewind()
else:
offset -= self._pos
# Read and discard data until we reach the desired position.
if self._mode != _MODE_READ_EOF:
self._read_block(offset, return_data=False)
return self._pos | [
"def",
"seek",
"(",
"self",
",",
"offset",
",",
"whence",
"=",
"0",
")",
":",
"self",
".",
"_check_can_seek",
"(",
")",
"# Recalculate offset as an absolute file position.",
"if",
"whence",
"==",
"0",
":",
"pass",
"elif",
"whence",
"==",
"1",
":",
"offset",
"=",
"self",
".",
"_pos",
"+",
"offset",
"elif",
"whence",
"==",
"2",
":",
"# Seeking relative to EOF - we need to know the file's size.",
"if",
"self",
".",
"_size",
"<",
"0",
":",
"self",
".",
"_read_all",
"(",
"return_data",
"=",
"False",
")",
"offset",
"=",
"self",
".",
"_size",
"+",
"offset",
"else",
":",
"raise",
"ValueError",
"(",
"\"Invalid value for whence: {}\"",
".",
"format",
"(",
"whence",
")",
")",
"# Make it so that offset is the number of bytes to skip forward.",
"if",
"offset",
"is",
"None",
":",
"#This is not needed on Python 3 where the comparison to self._pos",
"#will fail with a TypeError.",
"raise",
"TypeError",
"(",
"\"Seek offset should be an integer, not None\"",
")",
"if",
"offset",
"<",
"self",
".",
"_pos",
":",
"self",
".",
"_rewind",
"(",
")",
"else",
":",
"offset",
"-=",
"self",
".",
"_pos",
"# Read and discard data until we reach the desired position.",
"if",
"self",
".",
"_mode",
"!=",
"_MODE_READ_EOF",
":",
"self",
".",
"_read_block",
"(",
"offset",
",",
"return_data",
"=",
"False",
")",
"return",
"self",
".",
"_pos"
] | Change the file position.
The new position is specified by offset, relative to the
position indicated by whence. Possible values for whence are:
0: start of stream (default): offset must not be negative
1: current stream position
2: end of stream; offset must not be positive
Returns the new file position.
Note that seeking is emulated, sp depending on the parameters,
this operation may be extremely slow. | [
"Change",
"the",
"file",
"position",
"."
] | 6555d8b8e493a35159025b4cfc204dfb54c33d3e | https://github.com/peterjc/backports.lzma/blob/6555d8b8e493a35159025b4cfc204dfb54c33d3e/backports/lzma/__init__.py#L344-L388 |
4,105 | inveniosoftware/invenio-github | invenio_github/models.py | Repository.create | def create(cls, user_id, github_id=None, name=None, **kwargs):
"""Create the repository."""
with db.session.begin_nested():
obj = cls(user_id=user_id, github_id=github_id, name=name,
**kwargs)
db.session.add(obj)
return obj | python | def create(cls, user_id, github_id=None, name=None, **kwargs):
with db.session.begin_nested():
obj = cls(user_id=user_id, github_id=github_id, name=name,
**kwargs)
db.session.add(obj)
return obj | [
"def",
"create",
"(",
"cls",
",",
"user_id",
",",
"github_id",
"=",
"None",
",",
"name",
"=",
"None",
",",
"*",
"*",
"kwargs",
")",
":",
"with",
"db",
".",
"session",
".",
"begin_nested",
"(",
")",
":",
"obj",
"=",
"cls",
"(",
"user_id",
"=",
"user_id",
",",
"github_id",
"=",
"github_id",
",",
"name",
"=",
"name",
",",
"*",
"*",
"kwargs",
")",
"db",
".",
"session",
".",
"add",
"(",
"obj",
")",
"return",
"obj"
] | Create the repository. | [
"Create",
"the",
"repository",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/models.py#L172-L178 |
4,106 | inveniosoftware/invenio-github | invenio_github/models.py | Repository.get | def get(cls, user_id, github_id=None, name=None, check_owner=True):
"""Return a repository.
:param integer user_id: User identifier.
:param integer github_id: GitHub repository identifier.
:param str name: GitHub repository full name.
:returns: The repository object.
:raises: :py:exc:`~sqlalchemy.orm.exc.NoResultFound`: if the repository
doesn't exist.
:raises: :py:exc:`~sqlalchemy.orm.exc.MultipleResultsFound`: if
multiple repositories with the specified GitHub id and/or name
exist.
:raises: :py:exc:`RepositoryAccessError`: if the user is not the owner
of the repository.
"""
repo = cls.query.filter((Repository.github_id == github_id) |
(Repository.name == name)).one()
if (check_owner and repo and repo.user_id and
repo.user_id != int(user_id)):
raise RepositoryAccessError(
u'User {user} cannot access repository {repo}({repo_id}).'
.format(user=user_id, repo=name, repo_id=github_id)
)
return repo | python | def get(cls, user_id, github_id=None, name=None, check_owner=True):
repo = cls.query.filter((Repository.github_id == github_id) |
(Repository.name == name)).one()
if (check_owner and repo and repo.user_id and
repo.user_id != int(user_id)):
raise RepositoryAccessError(
u'User {user} cannot access repository {repo}({repo_id}).'
.format(user=user_id, repo=name, repo_id=github_id)
)
return repo | [
"def",
"get",
"(",
"cls",
",",
"user_id",
",",
"github_id",
"=",
"None",
",",
"name",
"=",
"None",
",",
"check_owner",
"=",
"True",
")",
":",
"repo",
"=",
"cls",
".",
"query",
".",
"filter",
"(",
"(",
"Repository",
".",
"github_id",
"==",
"github_id",
")",
"|",
"(",
"Repository",
".",
"name",
"==",
"name",
")",
")",
".",
"one",
"(",
")",
"if",
"(",
"check_owner",
"and",
"repo",
"and",
"repo",
".",
"user_id",
"and",
"repo",
".",
"user_id",
"!=",
"int",
"(",
"user_id",
")",
")",
":",
"raise",
"RepositoryAccessError",
"(",
"u'User {user} cannot access repository {repo}({repo_id}).'",
".",
"format",
"(",
"user",
"=",
"user_id",
",",
"repo",
"=",
"name",
",",
"repo_id",
"=",
"github_id",
")",
")",
"return",
"repo"
] | Return a repository.
:param integer user_id: User identifier.
:param integer github_id: GitHub repository identifier.
:param str name: GitHub repository full name.
:returns: The repository object.
:raises: :py:exc:`~sqlalchemy.orm.exc.NoResultFound`: if the repository
doesn't exist.
:raises: :py:exc:`~sqlalchemy.orm.exc.MultipleResultsFound`: if
multiple repositories with the specified GitHub id and/or name
exist.
:raises: :py:exc:`RepositoryAccessError`: if the user is not the owner
of the repository. | [
"Return",
"a",
"repository",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/models.py#L181-L204 |
4,107 | inveniosoftware/invenio-github | invenio_github/models.py | Repository.enable | def enable(cls, user_id, github_id, name, hook):
"""Enable webhooks for a repository.
If the repository does not exist it will create one.
:param user_id: User identifier.
:param repo_id: GitHub repository identifier.
:param name: Fully qualified name of the repository.
:param hook: GitHub hook identifier.
"""
try:
repo = cls.get(user_id, github_id=github_id, name=name)
except NoResultFound:
repo = cls.create(user_id=user_id, github_id=github_id, name=name)
repo.hook = hook
repo.user_id = user_id
return repo | python | def enable(cls, user_id, github_id, name, hook):
try:
repo = cls.get(user_id, github_id=github_id, name=name)
except NoResultFound:
repo = cls.create(user_id=user_id, github_id=github_id, name=name)
repo.hook = hook
repo.user_id = user_id
return repo | [
"def",
"enable",
"(",
"cls",
",",
"user_id",
",",
"github_id",
",",
"name",
",",
"hook",
")",
":",
"try",
":",
"repo",
"=",
"cls",
".",
"get",
"(",
"user_id",
",",
"github_id",
"=",
"github_id",
",",
"name",
"=",
"name",
")",
"except",
"NoResultFound",
":",
"repo",
"=",
"cls",
".",
"create",
"(",
"user_id",
"=",
"user_id",
",",
"github_id",
"=",
"github_id",
",",
"name",
"=",
"name",
")",
"repo",
".",
"hook",
"=",
"hook",
"repo",
".",
"user_id",
"=",
"user_id",
"return",
"repo"
] | Enable webhooks for a repository.
If the repository does not exist it will create one.
:param user_id: User identifier.
:param repo_id: GitHub repository identifier.
:param name: Fully qualified name of the repository.
:param hook: GitHub hook identifier. | [
"Enable",
"webhooks",
"for",
"a",
"repository",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/models.py#L207-L223 |
4,108 | inveniosoftware/invenio-github | invenio_github/models.py | Repository.disable | def disable(cls, user_id, github_id, name):
"""Disable webhooks for a repository.
Disables the webhook from a repository if it exists in the DB.
:param user_id: User identifier.
:param repo_id: GitHub id of the repository.
:param name: Fully qualified name of the repository.
"""
repo = cls.get(user_id, github_id=github_id, name=name)
repo.hook = None
repo.user_id = None
return repo | python | def disable(cls, user_id, github_id, name):
repo = cls.get(user_id, github_id=github_id, name=name)
repo.hook = None
repo.user_id = None
return repo | [
"def",
"disable",
"(",
"cls",
",",
"user_id",
",",
"github_id",
",",
"name",
")",
":",
"repo",
"=",
"cls",
".",
"get",
"(",
"user_id",
",",
"github_id",
"=",
"github_id",
",",
"name",
"=",
"name",
")",
"repo",
".",
"hook",
"=",
"None",
"repo",
".",
"user_id",
"=",
"None",
"return",
"repo"
] | Disable webhooks for a repository.
Disables the webhook from a repository if it exists in the DB.
:param user_id: User identifier.
:param repo_id: GitHub id of the repository.
:param name: Fully qualified name of the repository. | [
"Disable",
"webhooks",
"for",
"a",
"repository",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/models.py#L226-L238 |
4,109 | inveniosoftware/invenio-github | invenio_github/models.py | Release.create | def create(cls, event):
"""Create a new Release model."""
# Check if the release has already been received
release_id = event.payload['release']['id']
existing_release = Release.query.filter_by(
release_id=release_id,
).first()
if existing_release:
raise ReleaseAlreadyReceivedError(
u'{release} has already been received.'
.format(release=existing_release)
)
# Create the Release
repo_id = event.payload['repository']['id']
repo = Repository.get(user_id=event.user_id, github_id=repo_id)
if repo.enabled:
with db.session.begin_nested():
release = cls(
release_id=release_id,
tag=event.payload['release']['tag_name'],
repository=repo,
event=event,
status=ReleaseStatus.RECEIVED,
)
db.session.add(release)
return release
else:
current_app.logger.warning(
u'Release creation attempt on disabled {repo}.'
.format(repo=repo)
)
raise RepositoryDisabledError(
u'{repo} is not enabled for webhooks.'.format(repo=repo)
) | python | def create(cls, event):
# Check if the release has already been received
release_id = event.payload['release']['id']
existing_release = Release.query.filter_by(
release_id=release_id,
).first()
if existing_release:
raise ReleaseAlreadyReceivedError(
u'{release} has already been received.'
.format(release=existing_release)
)
# Create the Release
repo_id = event.payload['repository']['id']
repo = Repository.get(user_id=event.user_id, github_id=repo_id)
if repo.enabled:
with db.session.begin_nested():
release = cls(
release_id=release_id,
tag=event.payload['release']['tag_name'],
repository=repo,
event=event,
status=ReleaseStatus.RECEIVED,
)
db.session.add(release)
return release
else:
current_app.logger.warning(
u'Release creation attempt on disabled {repo}.'
.format(repo=repo)
)
raise RepositoryDisabledError(
u'{repo} is not enabled for webhooks.'.format(repo=repo)
) | [
"def",
"create",
"(",
"cls",
",",
"event",
")",
":",
"# Check if the release has already been received",
"release_id",
"=",
"event",
".",
"payload",
"[",
"'release'",
"]",
"[",
"'id'",
"]",
"existing_release",
"=",
"Release",
".",
"query",
".",
"filter_by",
"(",
"release_id",
"=",
"release_id",
",",
")",
".",
"first",
"(",
")",
"if",
"existing_release",
":",
"raise",
"ReleaseAlreadyReceivedError",
"(",
"u'{release} has already been received.'",
".",
"format",
"(",
"release",
"=",
"existing_release",
")",
")",
"# Create the Release",
"repo_id",
"=",
"event",
".",
"payload",
"[",
"'repository'",
"]",
"[",
"'id'",
"]",
"repo",
"=",
"Repository",
".",
"get",
"(",
"user_id",
"=",
"event",
".",
"user_id",
",",
"github_id",
"=",
"repo_id",
")",
"if",
"repo",
".",
"enabled",
":",
"with",
"db",
".",
"session",
".",
"begin_nested",
"(",
")",
":",
"release",
"=",
"cls",
"(",
"release_id",
"=",
"release_id",
",",
"tag",
"=",
"event",
".",
"payload",
"[",
"'release'",
"]",
"[",
"'tag_name'",
"]",
",",
"repository",
"=",
"repo",
",",
"event",
"=",
"event",
",",
"status",
"=",
"ReleaseStatus",
".",
"RECEIVED",
",",
")",
"db",
".",
"session",
".",
"add",
"(",
"release",
")",
"return",
"release",
"else",
":",
"current_app",
".",
"logger",
".",
"warning",
"(",
"u'Release creation attempt on disabled {repo}.'",
".",
"format",
"(",
"repo",
"=",
"repo",
")",
")",
"raise",
"RepositoryDisabledError",
"(",
"u'{repo} is not enabled for webhooks.'",
".",
"format",
"(",
"repo",
"=",
"repo",
")",
")"
] | Create a new Release model. | [
"Create",
"a",
"new",
"Release",
"model",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/models.py#L314-L348 |
4,110 | inveniosoftware/invenio-github | invenio_github/models.py | Release.record | def record(self):
"""Get Record object."""
if self.recordmetadata:
return Record(self.recordmetadata.json, model=self.recordmetadata)
else:
return None | python | def record(self):
if self.recordmetadata:
return Record(self.recordmetadata.json, model=self.recordmetadata)
else:
return None | [
"def",
"record",
"(",
"self",
")",
":",
"if",
"self",
".",
"recordmetadata",
":",
"return",
"Record",
"(",
"self",
".",
"recordmetadata",
".",
"json",
",",
"model",
"=",
"self",
".",
"recordmetadata",
")",
"else",
":",
"return",
"None"
] | Get Record object. | [
"Get",
"Record",
"object",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/models.py#L351-L356 |
4,111 | inveniosoftware/invenio-github | invenio_github/views/badge.py | get_badge_image_url | def get_badge_image_url(pid, ext='svg'):
"""Return the badge for a DOI."""
return url_for('invenio_formatter_badges.badge',
title=pid.pid_type, value=pid.pid_value, ext=ext) | python | def get_badge_image_url(pid, ext='svg'):
return url_for('invenio_formatter_badges.badge',
title=pid.pid_type, value=pid.pid_value, ext=ext) | [
"def",
"get_badge_image_url",
"(",
"pid",
",",
"ext",
"=",
"'svg'",
")",
":",
"return",
"url_for",
"(",
"'invenio_formatter_badges.badge'",
",",
"title",
"=",
"pid",
".",
"pid_type",
",",
"value",
"=",
"pid",
".",
"pid_value",
",",
"ext",
"=",
"ext",
")"
] | Return the badge for a DOI. | [
"Return",
"the",
"badge",
"for",
"a",
"DOI",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/views/badge.py#L51-L54 |
4,112 | inveniosoftware/invenio-github | invenio_github/views/badge.py | index_old | def index_old(user_id, repo_name):
"""Generate a badge for a specific GitHub repository."""
pid = get_pid_of_latest_release_or_404(name=repo_name)
return redirect(get_badge_image_url(pid)) | python | def index_old(user_id, repo_name):
pid = get_pid_of_latest_release_or_404(name=repo_name)
return redirect(get_badge_image_url(pid)) | [
"def",
"index_old",
"(",
"user_id",
",",
"repo_name",
")",
":",
"pid",
"=",
"get_pid_of_latest_release_or_404",
"(",
"name",
"=",
"repo_name",
")",
"return",
"redirect",
"(",
"get_badge_image_url",
"(",
"pid",
")",
")"
] | Generate a badge for a specific GitHub repository. | [
"Generate",
"a",
"badge",
"for",
"a",
"specific",
"GitHub",
"repository",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/views/badge.py#L73-L76 |
4,113 | inveniosoftware/invenio-github | invenio_github/tasks.py | disconnect_github | def disconnect_github(access_token, repo_hooks):
"""Uninstall webhooks."""
# Note at this point the remote account and all associated data have
# already been deleted. The celery task is passed the access_token to make
# some last cleanup and afterwards delete itself remotely.
import github3
from .api import GitHubAPI
try:
gh = github3.login(token=access_token)
for repo_id, repo_hook in repo_hooks:
ghrepo = gh.repository_with_id(repo_id)
if ghrepo:
hook = ghrepo.hook(repo_hook)
if hook and hook.delete():
info_msg = u'Deleted hook {hook} from {repo}'.format(
hook=hook.id, repo=ghrepo.full_name)
current_app.logger.info(info_msg)
# If we finished our clean-up successfully, we can revoke the token
GitHubAPI.revoke_token(access_token)
except Exception as exc:
# Retry in case GitHub may be down...
disconnect_github.retry(exc=exc) | python | def disconnect_github(access_token, repo_hooks):
# Note at this point the remote account and all associated data have
# already been deleted. The celery task is passed the access_token to make
# some last cleanup and afterwards delete itself remotely.
import github3
from .api import GitHubAPI
try:
gh = github3.login(token=access_token)
for repo_id, repo_hook in repo_hooks:
ghrepo = gh.repository_with_id(repo_id)
if ghrepo:
hook = ghrepo.hook(repo_hook)
if hook and hook.delete():
info_msg = u'Deleted hook {hook} from {repo}'.format(
hook=hook.id, repo=ghrepo.full_name)
current_app.logger.info(info_msg)
# If we finished our clean-up successfully, we can revoke the token
GitHubAPI.revoke_token(access_token)
except Exception as exc:
# Retry in case GitHub may be down...
disconnect_github.retry(exc=exc) | [
"def",
"disconnect_github",
"(",
"access_token",
",",
"repo_hooks",
")",
":",
"# Note at this point the remote account and all associated data have",
"# already been deleted. The celery task is passed the access_token to make",
"# some last cleanup and afterwards delete itself remotely.",
"import",
"github3",
"from",
".",
"api",
"import",
"GitHubAPI",
"try",
":",
"gh",
"=",
"github3",
".",
"login",
"(",
"token",
"=",
"access_token",
")",
"for",
"repo_id",
",",
"repo_hook",
"in",
"repo_hooks",
":",
"ghrepo",
"=",
"gh",
".",
"repository_with_id",
"(",
"repo_id",
")",
"if",
"ghrepo",
":",
"hook",
"=",
"ghrepo",
".",
"hook",
"(",
"repo_hook",
")",
"if",
"hook",
"and",
"hook",
".",
"delete",
"(",
")",
":",
"info_msg",
"=",
"u'Deleted hook {hook} from {repo}'",
".",
"format",
"(",
"hook",
"=",
"hook",
".",
"id",
",",
"repo",
"=",
"ghrepo",
".",
"full_name",
")",
"current_app",
".",
"logger",
".",
"info",
"(",
"info_msg",
")",
"# If we finished our clean-up successfully, we can revoke the token",
"GitHubAPI",
".",
"revoke_token",
"(",
"access_token",
")",
"except",
"Exception",
"as",
"exc",
":",
"# Retry in case GitHub may be down...",
"disconnect_github",
".",
"retry",
"(",
"exc",
"=",
"exc",
")"
] | Uninstall webhooks. | [
"Uninstall",
"webhooks",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/tasks.py#L38-L60 |
4,114 | inveniosoftware/invenio-github | invenio_github/tasks.py | sync_hooks | def sync_hooks(user_id, repositories):
"""Sync repository hooks for a user."""
from .api import GitHubAPI
try:
# Sync hooks
gh = GitHubAPI(user_id=user_id)
for repo_id in repositories:
try:
with db.session.begin_nested():
gh.sync_repo_hook(repo_id)
# We commit per repository, because while the task is running
# the user might enable/disable a hook.
db.session.commit()
except RepositoryAccessError as e:
current_app.logger.warning(e.message, exc_info=True)
except NoResultFound:
pass # Repository not in DB yet
except Exception as exc:
sync_hooks.retry(exc=exc) | python | def sync_hooks(user_id, repositories):
from .api import GitHubAPI
try:
# Sync hooks
gh = GitHubAPI(user_id=user_id)
for repo_id in repositories:
try:
with db.session.begin_nested():
gh.sync_repo_hook(repo_id)
# We commit per repository, because while the task is running
# the user might enable/disable a hook.
db.session.commit()
except RepositoryAccessError as e:
current_app.logger.warning(e.message, exc_info=True)
except NoResultFound:
pass # Repository not in DB yet
except Exception as exc:
sync_hooks.retry(exc=exc) | [
"def",
"sync_hooks",
"(",
"user_id",
",",
"repositories",
")",
":",
"from",
".",
"api",
"import",
"GitHubAPI",
"try",
":",
"# Sync hooks",
"gh",
"=",
"GitHubAPI",
"(",
"user_id",
"=",
"user_id",
")",
"for",
"repo_id",
"in",
"repositories",
":",
"try",
":",
"with",
"db",
".",
"session",
".",
"begin_nested",
"(",
")",
":",
"gh",
".",
"sync_repo_hook",
"(",
"repo_id",
")",
"# We commit per repository, because while the task is running",
"# the user might enable/disable a hook.",
"db",
".",
"session",
".",
"commit",
"(",
")",
"except",
"RepositoryAccessError",
"as",
"e",
":",
"current_app",
".",
"logger",
".",
"warning",
"(",
"e",
".",
"message",
",",
"exc_info",
"=",
"True",
")",
"except",
"NoResultFound",
":",
"pass",
"# Repository not in DB yet",
"except",
"Exception",
"as",
"exc",
":",
"sync_hooks",
".",
"retry",
"(",
"exc",
"=",
"exc",
")"
] | Sync repository hooks for a user. | [
"Sync",
"repository",
"hooks",
"for",
"a",
"user",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/tasks.py#L64-L83 |
4,115 | inveniosoftware/invenio-github | invenio_github/tasks.py | process_release | def process_release(release_id, verify_sender=False):
"""Process a received Release."""
from invenio_db import db
from invenio_rest.errors import RESTException
from .errors import InvalidSenderError
from .models import Release, ReleaseStatus
from .proxies import current_github
release_model = Release.query.filter(
Release.release_id == release_id,
Release.status.in_([ReleaseStatus.RECEIVED, ReleaseStatus.FAILED]),
).one()
release_model.status = ReleaseStatus.PROCESSING
db.session.commit()
release = current_github.release_api_class(release_model)
if verify_sender and not release.verify_sender():
raise InvalidSenderError(
u'Invalid sender for event {event} for user {user}'
.format(event=release.event.id, user=release.event.user_id)
)
def _get_err_obj(msg):
"""Generate the error entry with a Sentry ID."""
err = {'errors': msg}
if hasattr(g, 'sentry_event_id'):
err['error_id'] = str(g.sentry_event_id)
return err
try:
release.publish()
release.model.status = ReleaseStatus.PUBLISHED
except RESTException as rest_ex:
release.model.errors = json.loads(rest_ex.get_body())
release.model.status = ReleaseStatus.FAILED
current_app.logger.exception(
u'Error while processing {release}'.format(release=release.model))
# TODO: We may want to handle GitHub errors differently in the future
# except GitHubError as github_ex:
# release.model.errors = {'error': str(e)}
# release.model.status = ReleaseStatus.FAILED
# current_app.logger.exception(
# 'Error while processing {release}'
# .format(release=release.model))
except CustomGitHubMetadataError as e:
release.model.errors = _get_err_obj(str(e))
release.model.status = ReleaseStatus.FAILED
current_app.logger.exception(
u'Error while processing {release}'.format(release=release.model))
except Exception:
release.model.errors = _get_err_obj('Unknown error occured.')
release.model.status = ReleaseStatus.FAILED
current_app.logger.exception(
u'Error while processing {release}'.format(release=release.model))
finally:
db.session.commit() | python | def process_release(release_id, verify_sender=False):
from invenio_db import db
from invenio_rest.errors import RESTException
from .errors import InvalidSenderError
from .models import Release, ReleaseStatus
from .proxies import current_github
release_model = Release.query.filter(
Release.release_id == release_id,
Release.status.in_([ReleaseStatus.RECEIVED, ReleaseStatus.FAILED]),
).one()
release_model.status = ReleaseStatus.PROCESSING
db.session.commit()
release = current_github.release_api_class(release_model)
if verify_sender and not release.verify_sender():
raise InvalidSenderError(
u'Invalid sender for event {event} for user {user}'
.format(event=release.event.id, user=release.event.user_id)
)
def _get_err_obj(msg):
"""Generate the error entry with a Sentry ID."""
err = {'errors': msg}
if hasattr(g, 'sentry_event_id'):
err['error_id'] = str(g.sentry_event_id)
return err
try:
release.publish()
release.model.status = ReleaseStatus.PUBLISHED
except RESTException as rest_ex:
release.model.errors = json.loads(rest_ex.get_body())
release.model.status = ReleaseStatus.FAILED
current_app.logger.exception(
u'Error while processing {release}'.format(release=release.model))
# TODO: We may want to handle GitHub errors differently in the future
# except GitHubError as github_ex:
# release.model.errors = {'error': str(e)}
# release.model.status = ReleaseStatus.FAILED
# current_app.logger.exception(
# 'Error while processing {release}'
# .format(release=release.model))
except CustomGitHubMetadataError as e:
release.model.errors = _get_err_obj(str(e))
release.model.status = ReleaseStatus.FAILED
current_app.logger.exception(
u'Error while processing {release}'.format(release=release.model))
except Exception:
release.model.errors = _get_err_obj('Unknown error occured.')
release.model.status = ReleaseStatus.FAILED
current_app.logger.exception(
u'Error while processing {release}'.format(release=release.model))
finally:
db.session.commit() | [
"def",
"process_release",
"(",
"release_id",
",",
"verify_sender",
"=",
"False",
")",
":",
"from",
"invenio_db",
"import",
"db",
"from",
"invenio_rest",
".",
"errors",
"import",
"RESTException",
"from",
".",
"errors",
"import",
"InvalidSenderError",
"from",
".",
"models",
"import",
"Release",
",",
"ReleaseStatus",
"from",
".",
"proxies",
"import",
"current_github",
"release_model",
"=",
"Release",
".",
"query",
".",
"filter",
"(",
"Release",
".",
"release_id",
"==",
"release_id",
",",
"Release",
".",
"status",
".",
"in_",
"(",
"[",
"ReleaseStatus",
".",
"RECEIVED",
",",
"ReleaseStatus",
".",
"FAILED",
"]",
")",
",",
")",
".",
"one",
"(",
")",
"release_model",
".",
"status",
"=",
"ReleaseStatus",
".",
"PROCESSING",
"db",
".",
"session",
".",
"commit",
"(",
")",
"release",
"=",
"current_github",
".",
"release_api_class",
"(",
"release_model",
")",
"if",
"verify_sender",
"and",
"not",
"release",
".",
"verify_sender",
"(",
")",
":",
"raise",
"InvalidSenderError",
"(",
"u'Invalid sender for event {event} for user {user}'",
".",
"format",
"(",
"event",
"=",
"release",
".",
"event",
".",
"id",
",",
"user",
"=",
"release",
".",
"event",
".",
"user_id",
")",
")",
"def",
"_get_err_obj",
"(",
"msg",
")",
":",
"\"\"\"Generate the error entry with a Sentry ID.\"\"\"",
"err",
"=",
"{",
"'errors'",
":",
"msg",
"}",
"if",
"hasattr",
"(",
"g",
",",
"'sentry_event_id'",
")",
":",
"err",
"[",
"'error_id'",
"]",
"=",
"str",
"(",
"g",
".",
"sentry_event_id",
")",
"return",
"err",
"try",
":",
"release",
".",
"publish",
"(",
")",
"release",
".",
"model",
".",
"status",
"=",
"ReleaseStatus",
".",
"PUBLISHED",
"except",
"RESTException",
"as",
"rest_ex",
":",
"release",
".",
"model",
".",
"errors",
"=",
"json",
".",
"loads",
"(",
"rest_ex",
".",
"get_body",
"(",
")",
")",
"release",
".",
"model",
".",
"status",
"=",
"ReleaseStatus",
".",
"FAILED",
"current_app",
".",
"logger",
".",
"exception",
"(",
"u'Error while processing {release}'",
".",
"format",
"(",
"release",
"=",
"release",
".",
"model",
")",
")",
"# TODO: We may want to handle GitHub errors differently in the future",
"# except GitHubError as github_ex:",
"# release.model.errors = {'error': str(e)}",
"# release.model.status = ReleaseStatus.FAILED",
"# current_app.logger.exception(",
"# 'Error while processing {release}'",
"# .format(release=release.model))",
"except",
"CustomGitHubMetadataError",
"as",
"e",
":",
"release",
".",
"model",
".",
"errors",
"=",
"_get_err_obj",
"(",
"str",
"(",
"e",
")",
")",
"release",
".",
"model",
".",
"status",
"=",
"ReleaseStatus",
".",
"FAILED",
"current_app",
".",
"logger",
".",
"exception",
"(",
"u'Error while processing {release}'",
".",
"format",
"(",
"release",
"=",
"release",
".",
"model",
")",
")",
"except",
"Exception",
":",
"release",
".",
"model",
".",
"errors",
"=",
"_get_err_obj",
"(",
"'Unknown error occured.'",
")",
"release",
".",
"model",
".",
"status",
"=",
"ReleaseStatus",
".",
"FAILED",
"current_app",
".",
"logger",
".",
"exception",
"(",
"u'Error while processing {release}'",
".",
"format",
"(",
"release",
"=",
"release",
".",
"model",
")",
")",
"finally",
":",
"db",
".",
"session",
".",
"commit",
"(",
")"
] | Process a received Release. | [
"Process",
"a",
"received",
"Release",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/tasks.py#L87-L143 |
4,116 | inveniosoftware/invenio-github | invenio_github/views/github.py | naturaltime | def naturaltime(val):
"""Get humanized version of time."""
val = val.replace(tzinfo=pytz.utc) \
if isinstance(val, datetime) else parse(val)
now = datetime.utcnow().replace(tzinfo=pytz.utc)
return humanize.naturaltime(now - val) | python | def naturaltime(val):
val = val.replace(tzinfo=pytz.utc) \
if isinstance(val, datetime) else parse(val)
now = datetime.utcnow().replace(tzinfo=pytz.utc)
return humanize.naturaltime(now - val) | [
"def",
"naturaltime",
"(",
"val",
")",
":",
"val",
"=",
"val",
".",
"replace",
"(",
"tzinfo",
"=",
"pytz",
".",
"utc",
")",
"if",
"isinstance",
"(",
"val",
",",
"datetime",
")",
"else",
"parse",
"(",
"val",
")",
"now",
"=",
"datetime",
".",
"utcnow",
"(",
")",
".",
"replace",
"(",
"tzinfo",
"=",
"pytz",
".",
"utc",
")",
"return",
"humanize",
".",
"naturaltime",
"(",
"now",
"-",
"val",
")"
] | Get humanized version of time. | [
"Get",
"humanized",
"version",
"of",
"time",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/views/github.py#L61-L67 |
4,117 | inveniosoftware/invenio-github | invenio_github/views/github.py | index | def index():
"""Display list of the user's repositories."""
github = GitHubAPI(user_id=current_user.id)
token = github.session_token
ctx = dict(connected=False)
if token:
# The user is authenticated and the token we have is still valid.
if github.account.extra_data.get('login') is None:
github.init_account()
db.session.commit()
# Sync if needed
if request.method == 'POST' or github.check_sync():
# When we're in an XHR request, we want to synchronously sync hooks
github.sync(async_hooks=(not request.is_xhr))
db.session.commit()
# Generate the repositories view object
extra_data = github.account.extra_data
repos = extra_data['repos']
if repos:
# 'Enhance' our repos dict, from our database model
db_repos = Repository.query.filter(
Repository.github_id.in_([int(k) for k in repos.keys()]),
).all()
for repo in db_repos:
repos[str(repo.github_id)]['instance'] = repo
repos[str(repo.github_id)]['latest'] = GitHubRelease(
repo.latest_release())
last_sync = humanize.naturaltime(
(utcnow() - parse_timestamp(extra_data['last_sync'])))
ctx.update({
'connected': True,
'repos': sorted(repos.items(), key=lambda x: x[1]['full_name']),
'last_sync': last_sync,
})
return render_template(current_app.config['GITHUB_TEMPLATE_INDEX'], **ctx) | python | def index():
github = GitHubAPI(user_id=current_user.id)
token = github.session_token
ctx = dict(connected=False)
if token:
# The user is authenticated and the token we have is still valid.
if github.account.extra_data.get('login') is None:
github.init_account()
db.session.commit()
# Sync if needed
if request.method == 'POST' or github.check_sync():
# When we're in an XHR request, we want to synchronously sync hooks
github.sync(async_hooks=(not request.is_xhr))
db.session.commit()
# Generate the repositories view object
extra_data = github.account.extra_data
repos = extra_data['repos']
if repos:
# 'Enhance' our repos dict, from our database model
db_repos = Repository.query.filter(
Repository.github_id.in_([int(k) for k in repos.keys()]),
).all()
for repo in db_repos:
repos[str(repo.github_id)]['instance'] = repo
repos[str(repo.github_id)]['latest'] = GitHubRelease(
repo.latest_release())
last_sync = humanize.naturaltime(
(utcnow() - parse_timestamp(extra_data['last_sync'])))
ctx.update({
'connected': True,
'repos': sorted(repos.items(), key=lambda x: x[1]['full_name']),
'last_sync': last_sync,
})
return render_template(current_app.config['GITHUB_TEMPLATE_INDEX'], **ctx) | [
"def",
"index",
"(",
")",
":",
"github",
"=",
"GitHubAPI",
"(",
"user_id",
"=",
"current_user",
".",
"id",
")",
"token",
"=",
"github",
".",
"session_token",
"ctx",
"=",
"dict",
"(",
"connected",
"=",
"False",
")",
"if",
"token",
":",
"# The user is authenticated and the token we have is still valid.",
"if",
"github",
".",
"account",
".",
"extra_data",
".",
"get",
"(",
"'login'",
")",
"is",
"None",
":",
"github",
".",
"init_account",
"(",
")",
"db",
".",
"session",
".",
"commit",
"(",
")",
"# Sync if needed",
"if",
"request",
".",
"method",
"==",
"'POST'",
"or",
"github",
".",
"check_sync",
"(",
")",
":",
"# When we're in an XHR request, we want to synchronously sync hooks",
"github",
".",
"sync",
"(",
"async_hooks",
"=",
"(",
"not",
"request",
".",
"is_xhr",
")",
")",
"db",
".",
"session",
".",
"commit",
"(",
")",
"# Generate the repositories view object",
"extra_data",
"=",
"github",
".",
"account",
".",
"extra_data",
"repos",
"=",
"extra_data",
"[",
"'repos'",
"]",
"if",
"repos",
":",
"# 'Enhance' our repos dict, from our database model",
"db_repos",
"=",
"Repository",
".",
"query",
".",
"filter",
"(",
"Repository",
".",
"github_id",
".",
"in_",
"(",
"[",
"int",
"(",
"k",
")",
"for",
"k",
"in",
"repos",
".",
"keys",
"(",
")",
"]",
")",
",",
")",
".",
"all",
"(",
")",
"for",
"repo",
"in",
"db_repos",
":",
"repos",
"[",
"str",
"(",
"repo",
".",
"github_id",
")",
"]",
"[",
"'instance'",
"]",
"=",
"repo",
"repos",
"[",
"str",
"(",
"repo",
".",
"github_id",
")",
"]",
"[",
"'latest'",
"]",
"=",
"GitHubRelease",
"(",
"repo",
".",
"latest_release",
"(",
")",
")",
"last_sync",
"=",
"humanize",
".",
"naturaltime",
"(",
"(",
"utcnow",
"(",
")",
"-",
"parse_timestamp",
"(",
"extra_data",
"[",
"'last_sync'",
"]",
")",
")",
")",
"ctx",
".",
"update",
"(",
"{",
"'connected'",
":",
"True",
",",
"'repos'",
":",
"sorted",
"(",
"repos",
".",
"items",
"(",
")",
",",
"key",
"=",
"lambda",
"x",
":",
"x",
"[",
"1",
"]",
"[",
"'full_name'",
"]",
")",
",",
"'last_sync'",
":",
"last_sync",
",",
"}",
")",
"return",
"render_template",
"(",
"current_app",
".",
"config",
"[",
"'GITHUB_TEMPLATE_INDEX'",
"]",
",",
"*",
"*",
"ctx",
")"
] | Display list of the user's repositories. | [
"Display",
"list",
"of",
"the",
"user",
"s",
"repositories",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/views/github.py#L94-L134 |
4,118 | inveniosoftware/invenio-github | invenio_github/views/github.py | hook | def hook():
"""Install or remove GitHub webhook."""
repo_id = request.json['id']
github = GitHubAPI(user_id=current_user.id)
repos = github.account.extra_data['repos']
if repo_id not in repos:
abort(404)
if request.method == 'DELETE':
try:
if github.remove_hook(repo_id, repos[repo_id]['full_name']):
db.session.commit()
return '', 204
else:
abort(400)
except Exception:
abort(403)
elif request.method == 'POST':
try:
if github.create_hook(repo_id, repos[repo_id]['full_name']):
db.session.commit()
return '', 201
else:
abort(400)
except Exception:
abort(403)
else:
abort(400) | python | def hook():
repo_id = request.json['id']
github = GitHubAPI(user_id=current_user.id)
repos = github.account.extra_data['repos']
if repo_id not in repos:
abort(404)
if request.method == 'DELETE':
try:
if github.remove_hook(repo_id, repos[repo_id]['full_name']):
db.session.commit()
return '', 204
else:
abort(400)
except Exception:
abort(403)
elif request.method == 'POST':
try:
if github.create_hook(repo_id, repos[repo_id]['full_name']):
db.session.commit()
return '', 201
else:
abort(400)
except Exception:
abort(403)
else:
abort(400) | [
"def",
"hook",
"(",
")",
":",
"repo_id",
"=",
"request",
".",
"json",
"[",
"'id'",
"]",
"github",
"=",
"GitHubAPI",
"(",
"user_id",
"=",
"current_user",
".",
"id",
")",
"repos",
"=",
"github",
".",
"account",
".",
"extra_data",
"[",
"'repos'",
"]",
"if",
"repo_id",
"not",
"in",
"repos",
":",
"abort",
"(",
"404",
")",
"if",
"request",
".",
"method",
"==",
"'DELETE'",
":",
"try",
":",
"if",
"github",
".",
"remove_hook",
"(",
"repo_id",
",",
"repos",
"[",
"repo_id",
"]",
"[",
"'full_name'",
"]",
")",
":",
"db",
".",
"session",
".",
"commit",
"(",
")",
"return",
"''",
",",
"204",
"else",
":",
"abort",
"(",
"400",
")",
"except",
"Exception",
":",
"abort",
"(",
"403",
")",
"elif",
"request",
".",
"method",
"==",
"'POST'",
":",
"try",
":",
"if",
"github",
".",
"create_hook",
"(",
"repo_id",
",",
"repos",
"[",
"repo_id",
"]",
"[",
"'full_name'",
"]",
")",
":",
"db",
".",
"session",
".",
"commit",
"(",
")",
"return",
"''",
",",
"201",
"else",
":",
"abort",
"(",
"400",
")",
"except",
"Exception",
":",
"abort",
"(",
"403",
")",
"else",
":",
"abort",
"(",
"400",
")"
] | Install or remove GitHub webhook. | [
"Install",
"or",
"remove",
"GitHub",
"webhook",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/views/github.py#L192-L221 |
4,119 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubAPI.access_token | def access_token(self):
"""Return OAuth access token."""
if self.user_id:
return RemoteToken.get(
self.user_id, self.remote.consumer_key
).access_token
return self.remote.get_request_token()[0] | python | def access_token(self):
if self.user_id:
return RemoteToken.get(
self.user_id, self.remote.consumer_key
).access_token
return self.remote.get_request_token()[0] | [
"def",
"access_token",
"(",
"self",
")",
":",
"if",
"self",
".",
"user_id",
":",
"return",
"RemoteToken",
".",
"get",
"(",
"self",
".",
"user_id",
",",
"self",
".",
"remote",
".",
"consumer_key",
")",
".",
"access_token",
"return",
"self",
".",
"remote",
".",
"get_request_token",
"(",
")",
"[",
"0",
"]"
] | Return OAuth access token. | [
"Return",
"OAuth",
"access",
"token",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L60-L66 |
4,120 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubAPI.session_token | def session_token(self):
"""Return OAuth session token."""
session_token = None
if self.user_id is not None:
session_token = token_getter(self.remote)
if session_token:
token = RemoteToken.get(
self.user_id, self.remote.consumer_key,
access_token=session_token[0]
)
return token
return None | python | def session_token(self):
session_token = None
if self.user_id is not None:
session_token = token_getter(self.remote)
if session_token:
token = RemoteToken.get(
self.user_id, self.remote.consumer_key,
access_token=session_token[0]
)
return token
return None | [
"def",
"session_token",
"(",
"self",
")",
":",
"session_token",
"=",
"None",
"if",
"self",
".",
"user_id",
"is",
"not",
"None",
":",
"session_token",
"=",
"token_getter",
"(",
"self",
".",
"remote",
")",
"if",
"session_token",
":",
"token",
"=",
"RemoteToken",
".",
"get",
"(",
"self",
".",
"user_id",
",",
"self",
".",
"remote",
".",
"consumer_key",
",",
"access_token",
"=",
"session_token",
"[",
"0",
"]",
")",
"return",
"token",
"return",
"None"
] | Return OAuth session token. | [
"Return",
"OAuth",
"session",
"token",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L69-L80 |
4,121 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubAPI.webhook_url | def webhook_url(self):
"""Return the url to be used by a GitHub webhook."""
webhook_token = ProviderToken.query.filter_by(
id=self.account.extra_data['tokens']['webhook']
).first()
if webhook_token:
wh_url = current_app.config.get('GITHUB_WEBHOOK_RECEIVER_URL')
if wh_url:
return wh_url.format(token=webhook_token.access_token)
else:
raise RuntimeError('You must set GITHUB_WEBHOOK_RECEIVER_URL.') | python | def webhook_url(self):
webhook_token = ProviderToken.query.filter_by(
id=self.account.extra_data['tokens']['webhook']
).first()
if webhook_token:
wh_url = current_app.config.get('GITHUB_WEBHOOK_RECEIVER_URL')
if wh_url:
return wh_url.format(token=webhook_token.access_token)
else:
raise RuntimeError('You must set GITHUB_WEBHOOK_RECEIVER_URL.') | [
"def",
"webhook_url",
"(",
"self",
")",
":",
"webhook_token",
"=",
"ProviderToken",
".",
"query",
".",
"filter_by",
"(",
"id",
"=",
"self",
".",
"account",
".",
"extra_data",
"[",
"'tokens'",
"]",
"[",
"'webhook'",
"]",
")",
".",
"first",
"(",
")",
"if",
"webhook_token",
":",
"wh_url",
"=",
"current_app",
".",
"config",
".",
"get",
"(",
"'GITHUB_WEBHOOK_RECEIVER_URL'",
")",
"if",
"wh_url",
":",
"return",
"wh_url",
".",
"format",
"(",
"token",
"=",
"webhook_token",
".",
"access_token",
")",
"else",
":",
"raise",
"RuntimeError",
"(",
"'You must set GITHUB_WEBHOOK_RECEIVER_URL.'",
")"
] | Return the url to be used by a GitHub webhook. | [
"Return",
"the",
"url",
"to",
"be",
"used",
"by",
"a",
"GitHub",
"webhook",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L95-L105 |
4,122 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubAPI.init_account | def init_account(self):
"""Setup a new GitHub account."""
ghuser = self.api.me()
# Setup local access tokens to be used by the webhooks
hook_token = ProviderToken.create_personal(
'github-webhook',
self.user_id,
scopes=['webhooks:event'],
is_internal=True,
)
# Initial structure of extra data
self.account.extra_data = dict(
id=ghuser.id,
login=ghuser.login,
name=ghuser.name,
tokens=dict(
webhook=hook_token.id,
),
repos=dict(),
last_sync=iso_utcnow(),
)
db.session.add(self.account)
# Sync data from GitHub, but don't check repository hooks yet.
self.sync(hooks=False) | python | def init_account(self):
ghuser = self.api.me()
# Setup local access tokens to be used by the webhooks
hook_token = ProviderToken.create_personal(
'github-webhook',
self.user_id,
scopes=['webhooks:event'],
is_internal=True,
)
# Initial structure of extra data
self.account.extra_data = dict(
id=ghuser.id,
login=ghuser.login,
name=ghuser.name,
tokens=dict(
webhook=hook_token.id,
),
repos=dict(),
last_sync=iso_utcnow(),
)
db.session.add(self.account)
# Sync data from GitHub, but don't check repository hooks yet.
self.sync(hooks=False) | [
"def",
"init_account",
"(",
"self",
")",
":",
"ghuser",
"=",
"self",
".",
"api",
".",
"me",
"(",
")",
"# Setup local access tokens to be used by the webhooks",
"hook_token",
"=",
"ProviderToken",
".",
"create_personal",
"(",
"'github-webhook'",
",",
"self",
".",
"user_id",
",",
"scopes",
"=",
"[",
"'webhooks:event'",
"]",
",",
"is_internal",
"=",
"True",
",",
")",
"# Initial structure of extra data",
"self",
".",
"account",
".",
"extra_data",
"=",
"dict",
"(",
"id",
"=",
"ghuser",
".",
"id",
",",
"login",
"=",
"ghuser",
".",
"login",
",",
"name",
"=",
"ghuser",
".",
"name",
",",
"tokens",
"=",
"dict",
"(",
"webhook",
"=",
"hook_token",
".",
"id",
",",
")",
",",
"repos",
"=",
"dict",
"(",
")",
",",
"last_sync",
"=",
"iso_utcnow",
"(",
")",
",",
")",
"db",
".",
"session",
".",
"add",
"(",
"self",
".",
"account",
")",
"# Sync data from GitHub, but don't check repository hooks yet.",
"self",
".",
"sync",
"(",
"hooks",
"=",
"False",
")"
] | Setup a new GitHub account. | [
"Setup",
"a",
"new",
"GitHub",
"account",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L107-L131 |
4,123 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubAPI.sync | def sync(self, hooks=True, async_hooks=True):
"""Synchronize user repositories.
:param bool hooks: True for syncing hooks.
:param bool async_hooks: True for sending of an asynchronous task to
sync hooks.
.. note::
Syncing happens from GitHub's direction only. This means that we
consider the information on GitHub as valid, and we overwrite our
own state based on this information.
"""
active_repos = {}
github_repos = {repo.id: repo for repo in self.api.repositories()
if repo.permissions['admin']}
for gh_repo_id, gh_repo in github_repos.items():
active_repos[gh_repo_id] = {
'id': gh_repo_id,
'full_name': gh_repo.full_name,
'description': gh_repo.description,
}
if hooks:
self._sync_hooks(list(active_repos.keys()),
asynchronous=async_hooks)
# Update changed names for repositories stored in DB
db_repos = Repository.query.filter(
Repository.user_id == self.user_id,
Repository.github_id.in_(github_repos.keys())
)
for repo in db_repos:
gh_repo = github_repos.get(repo.github_id)
if gh_repo and repo.name != gh_repo.full_name:
repo.name = gh_repo.full_name
db.session.add(repo)
# Remove ownership from repositories that the user has no longer
# 'admin' permissions, or have been deleted.
Repository.query.filter(
Repository.user_id == self.user_id,
~Repository.github_id.in_(github_repos.keys())
).update(dict(user_id=None, hook=None), synchronize_session=False)
# Update repos and last sync
self.account.extra_data.update(dict(
repos=active_repos,
last_sync=iso_utcnow(),
))
self.account.extra_data.changed()
db.session.add(self.account) | python | def sync(self, hooks=True, async_hooks=True):
active_repos = {}
github_repos = {repo.id: repo for repo in self.api.repositories()
if repo.permissions['admin']}
for gh_repo_id, gh_repo in github_repos.items():
active_repos[gh_repo_id] = {
'id': gh_repo_id,
'full_name': gh_repo.full_name,
'description': gh_repo.description,
}
if hooks:
self._sync_hooks(list(active_repos.keys()),
asynchronous=async_hooks)
# Update changed names for repositories stored in DB
db_repos = Repository.query.filter(
Repository.user_id == self.user_id,
Repository.github_id.in_(github_repos.keys())
)
for repo in db_repos:
gh_repo = github_repos.get(repo.github_id)
if gh_repo and repo.name != gh_repo.full_name:
repo.name = gh_repo.full_name
db.session.add(repo)
# Remove ownership from repositories that the user has no longer
# 'admin' permissions, or have been deleted.
Repository.query.filter(
Repository.user_id == self.user_id,
~Repository.github_id.in_(github_repos.keys())
).update(dict(user_id=None, hook=None), synchronize_session=False)
# Update repos and last sync
self.account.extra_data.update(dict(
repos=active_repos,
last_sync=iso_utcnow(),
))
self.account.extra_data.changed()
db.session.add(self.account) | [
"def",
"sync",
"(",
"self",
",",
"hooks",
"=",
"True",
",",
"async_hooks",
"=",
"True",
")",
":",
"active_repos",
"=",
"{",
"}",
"github_repos",
"=",
"{",
"repo",
".",
"id",
":",
"repo",
"for",
"repo",
"in",
"self",
".",
"api",
".",
"repositories",
"(",
")",
"if",
"repo",
".",
"permissions",
"[",
"'admin'",
"]",
"}",
"for",
"gh_repo_id",
",",
"gh_repo",
"in",
"github_repos",
".",
"items",
"(",
")",
":",
"active_repos",
"[",
"gh_repo_id",
"]",
"=",
"{",
"'id'",
":",
"gh_repo_id",
",",
"'full_name'",
":",
"gh_repo",
".",
"full_name",
",",
"'description'",
":",
"gh_repo",
".",
"description",
",",
"}",
"if",
"hooks",
":",
"self",
".",
"_sync_hooks",
"(",
"list",
"(",
"active_repos",
".",
"keys",
"(",
")",
")",
",",
"asynchronous",
"=",
"async_hooks",
")",
"# Update changed names for repositories stored in DB",
"db_repos",
"=",
"Repository",
".",
"query",
".",
"filter",
"(",
"Repository",
".",
"user_id",
"==",
"self",
".",
"user_id",
",",
"Repository",
".",
"github_id",
".",
"in_",
"(",
"github_repos",
".",
"keys",
"(",
")",
")",
")",
"for",
"repo",
"in",
"db_repos",
":",
"gh_repo",
"=",
"github_repos",
".",
"get",
"(",
"repo",
".",
"github_id",
")",
"if",
"gh_repo",
"and",
"repo",
".",
"name",
"!=",
"gh_repo",
".",
"full_name",
":",
"repo",
".",
"name",
"=",
"gh_repo",
".",
"full_name",
"db",
".",
"session",
".",
"add",
"(",
"repo",
")",
"# Remove ownership from repositories that the user has no longer",
"# 'admin' permissions, or have been deleted.",
"Repository",
".",
"query",
".",
"filter",
"(",
"Repository",
".",
"user_id",
"==",
"self",
".",
"user_id",
",",
"~",
"Repository",
".",
"github_id",
".",
"in_",
"(",
"github_repos",
".",
"keys",
"(",
")",
")",
")",
".",
"update",
"(",
"dict",
"(",
"user_id",
"=",
"None",
",",
"hook",
"=",
"None",
")",
",",
"synchronize_session",
"=",
"False",
")",
"# Update repos and last sync",
"self",
".",
"account",
".",
"extra_data",
".",
"update",
"(",
"dict",
"(",
"repos",
"=",
"active_repos",
",",
"last_sync",
"=",
"iso_utcnow",
"(",
")",
",",
")",
")",
"self",
".",
"account",
".",
"extra_data",
".",
"changed",
"(",
")",
"db",
".",
"session",
".",
"add",
"(",
"self",
".",
"account",
")"
] | Synchronize user repositories.
:param bool hooks: True for syncing hooks.
:param bool async_hooks: True for sending of an asynchronous task to
sync hooks.
.. note::
Syncing happens from GitHub's direction only. This means that we
consider the information on GitHub as valid, and we overwrite our
own state based on this information. | [
"Synchronize",
"user",
"repositories",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L133-L185 |
4,124 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubAPI._sync_hooks | def _sync_hooks(self, repos, asynchronous=True):
"""Check if a hooks sync task needs to be started."""
if not asynchronous:
for repo_id in repos:
try:
with db.session.begin_nested():
self.sync_repo_hook(repo_id)
db.session.commit()
except RepositoryAccessError as e:
current_app.logger.warning(e.message, exc_info=True)
except NoResultFound:
pass # Repository not in DB yet
else:
# FIXME: We have to commit, in order to have all necessary data?
db.session.commit()
sync_hooks.delay(self.user_id, repos) | python | def _sync_hooks(self, repos, asynchronous=True):
if not asynchronous:
for repo_id in repos:
try:
with db.session.begin_nested():
self.sync_repo_hook(repo_id)
db.session.commit()
except RepositoryAccessError as e:
current_app.logger.warning(e.message, exc_info=True)
except NoResultFound:
pass # Repository not in DB yet
else:
# FIXME: We have to commit, in order to have all necessary data?
db.session.commit()
sync_hooks.delay(self.user_id, repos) | [
"def",
"_sync_hooks",
"(",
"self",
",",
"repos",
",",
"asynchronous",
"=",
"True",
")",
":",
"if",
"not",
"asynchronous",
":",
"for",
"repo_id",
"in",
"repos",
":",
"try",
":",
"with",
"db",
".",
"session",
".",
"begin_nested",
"(",
")",
":",
"self",
".",
"sync_repo_hook",
"(",
"repo_id",
")",
"db",
".",
"session",
".",
"commit",
"(",
")",
"except",
"RepositoryAccessError",
"as",
"e",
":",
"current_app",
".",
"logger",
".",
"warning",
"(",
"e",
".",
"message",
",",
"exc_info",
"=",
"True",
")",
"except",
"NoResultFound",
":",
"pass",
"# Repository not in DB yet",
"else",
":",
"# FIXME: We have to commit, in order to have all necessary data?",
"db",
".",
"session",
".",
"commit",
"(",
")",
"sync_hooks",
".",
"delay",
"(",
"self",
".",
"user_id",
",",
"repos",
")"
] | Check if a hooks sync task needs to be started. | [
"Check",
"if",
"a",
"hooks",
"sync",
"task",
"needs",
"to",
"be",
"started",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L187-L202 |
4,125 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubAPI.sync_repo_hook | def sync_repo_hook(self, repo_id):
"""Sync a GitHub repo's hook with the locally stored repo."""
# Get the hook that we may have set in the past
gh_repo = self.api.repository_with_id(repo_id)
hooks = (hook.id for hook in gh_repo.hooks()
if hook.config.get('url', '') == self.webhook_url)
hook_id = next(hooks, None)
# If hook on GitHub exists, get or create corresponding db object and
# enable the hook. Otherwise remove the old hook information.
if hook_id:
Repository.enable(user_id=self.user_id,
github_id=gh_repo.id,
name=gh_repo.full_name,
hook=hook_id)
else:
Repository.disable(user_id=self.user_id,
github_id=gh_repo.id,
name=gh_repo.full_name) | python | def sync_repo_hook(self, repo_id):
# Get the hook that we may have set in the past
gh_repo = self.api.repository_with_id(repo_id)
hooks = (hook.id for hook in gh_repo.hooks()
if hook.config.get('url', '') == self.webhook_url)
hook_id = next(hooks, None)
# If hook on GitHub exists, get or create corresponding db object and
# enable the hook. Otherwise remove the old hook information.
if hook_id:
Repository.enable(user_id=self.user_id,
github_id=gh_repo.id,
name=gh_repo.full_name,
hook=hook_id)
else:
Repository.disable(user_id=self.user_id,
github_id=gh_repo.id,
name=gh_repo.full_name) | [
"def",
"sync_repo_hook",
"(",
"self",
",",
"repo_id",
")",
":",
"# Get the hook that we may have set in the past",
"gh_repo",
"=",
"self",
".",
"api",
".",
"repository_with_id",
"(",
"repo_id",
")",
"hooks",
"=",
"(",
"hook",
".",
"id",
"for",
"hook",
"in",
"gh_repo",
".",
"hooks",
"(",
")",
"if",
"hook",
".",
"config",
".",
"get",
"(",
"'url'",
",",
"''",
")",
"==",
"self",
".",
"webhook_url",
")",
"hook_id",
"=",
"next",
"(",
"hooks",
",",
"None",
")",
"# If hook on GitHub exists, get or create corresponding db object and",
"# enable the hook. Otherwise remove the old hook information.",
"if",
"hook_id",
":",
"Repository",
".",
"enable",
"(",
"user_id",
"=",
"self",
".",
"user_id",
",",
"github_id",
"=",
"gh_repo",
".",
"id",
",",
"name",
"=",
"gh_repo",
".",
"full_name",
",",
"hook",
"=",
"hook_id",
")",
"else",
":",
"Repository",
".",
"disable",
"(",
"user_id",
"=",
"self",
".",
"user_id",
",",
"github_id",
"=",
"gh_repo",
".",
"id",
",",
"name",
"=",
"gh_repo",
".",
"full_name",
")"
] | Sync a GitHub repo's hook with the locally stored repo. | [
"Sync",
"a",
"GitHub",
"repo",
"s",
"hook",
"with",
"the",
"locally",
"stored",
"repo",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L204-L222 |
4,126 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubAPI.check_sync | def check_sync(self):
"""Check if sync is required based on last sync date."""
# If refresh interval is not specified, we should refresh every time.
expiration = utcnow()
refresh_td = current_app.config.get('GITHUB_REFRESH_TIMEDELTA')
if refresh_td:
expiration -= refresh_td
last_sync = parse_timestamp(self.account.extra_data['last_sync'])
return last_sync < expiration | python | def check_sync(self):
# If refresh interval is not specified, we should refresh every time.
expiration = utcnow()
refresh_td = current_app.config.get('GITHUB_REFRESH_TIMEDELTA')
if refresh_td:
expiration -= refresh_td
last_sync = parse_timestamp(self.account.extra_data['last_sync'])
return last_sync < expiration | [
"def",
"check_sync",
"(",
"self",
")",
":",
"# If refresh interval is not specified, we should refresh every time.",
"expiration",
"=",
"utcnow",
"(",
")",
"refresh_td",
"=",
"current_app",
".",
"config",
".",
"get",
"(",
"'GITHUB_REFRESH_TIMEDELTA'",
")",
"if",
"refresh_td",
":",
"expiration",
"-=",
"refresh_td",
"last_sync",
"=",
"parse_timestamp",
"(",
"self",
".",
"account",
".",
"extra_data",
"[",
"'last_sync'",
"]",
")",
"return",
"last_sync",
"<",
"expiration"
] | Check if sync is required based on last sync date. | [
"Check",
"if",
"sync",
"is",
"required",
"based",
"on",
"last",
"sync",
"date",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L224-L232 |
4,127 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubAPI.create_hook | def create_hook(self, repo_id, repo_name):
"""Create repository hook."""
config = dict(
url=self.webhook_url,
content_type='json',
secret=current_app.config['GITHUB_SHARED_SECRET'],
insecure_ssl='1' if current_app.config['GITHUB_INSECURE_SSL']
else '0',
)
ghrepo = self.api.repository_with_id(repo_id)
if ghrepo:
try:
hook = ghrepo.create_hook(
'web', # GitHub identifier for webhook service
config,
events=['release'],
)
except github3.GitHubError as e:
# Check if hook is already installed
hook_errors = (m for m in e.errors
if m['code'] == 'custom' and
m['resource'] == 'Hook')
if next(hook_errors, None):
hooks = (h for h in ghrepo.hooks()
if h.config.get('url', '') == config['url'])
hook = next(hooks, None)
if hook:
hook.edit(config=config, events=['release'])
finally:
if hook:
Repository.enable(user_id=self.user_id,
github_id=repo_id,
name=repo_name,
hook=hook.id)
return True
return False | python | def create_hook(self, repo_id, repo_name):
config = dict(
url=self.webhook_url,
content_type='json',
secret=current_app.config['GITHUB_SHARED_SECRET'],
insecure_ssl='1' if current_app.config['GITHUB_INSECURE_SSL']
else '0',
)
ghrepo = self.api.repository_with_id(repo_id)
if ghrepo:
try:
hook = ghrepo.create_hook(
'web', # GitHub identifier for webhook service
config,
events=['release'],
)
except github3.GitHubError as e:
# Check if hook is already installed
hook_errors = (m for m in e.errors
if m['code'] == 'custom' and
m['resource'] == 'Hook')
if next(hook_errors, None):
hooks = (h for h in ghrepo.hooks()
if h.config.get('url', '') == config['url'])
hook = next(hooks, None)
if hook:
hook.edit(config=config, events=['release'])
finally:
if hook:
Repository.enable(user_id=self.user_id,
github_id=repo_id,
name=repo_name,
hook=hook.id)
return True
return False | [
"def",
"create_hook",
"(",
"self",
",",
"repo_id",
",",
"repo_name",
")",
":",
"config",
"=",
"dict",
"(",
"url",
"=",
"self",
".",
"webhook_url",
",",
"content_type",
"=",
"'json'",
",",
"secret",
"=",
"current_app",
".",
"config",
"[",
"'GITHUB_SHARED_SECRET'",
"]",
",",
"insecure_ssl",
"=",
"'1'",
"if",
"current_app",
".",
"config",
"[",
"'GITHUB_INSECURE_SSL'",
"]",
"else",
"'0'",
",",
")",
"ghrepo",
"=",
"self",
".",
"api",
".",
"repository_with_id",
"(",
"repo_id",
")",
"if",
"ghrepo",
":",
"try",
":",
"hook",
"=",
"ghrepo",
".",
"create_hook",
"(",
"'web'",
",",
"# GitHub identifier for webhook service",
"config",
",",
"events",
"=",
"[",
"'release'",
"]",
",",
")",
"except",
"github3",
".",
"GitHubError",
"as",
"e",
":",
"# Check if hook is already installed",
"hook_errors",
"=",
"(",
"m",
"for",
"m",
"in",
"e",
".",
"errors",
"if",
"m",
"[",
"'code'",
"]",
"==",
"'custom'",
"and",
"m",
"[",
"'resource'",
"]",
"==",
"'Hook'",
")",
"if",
"next",
"(",
"hook_errors",
",",
"None",
")",
":",
"hooks",
"=",
"(",
"h",
"for",
"h",
"in",
"ghrepo",
".",
"hooks",
"(",
")",
"if",
"h",
".",
"config",
".",
"get",
"(",
"'url'",
",",
"''",
")",
"==",
"config",
"[",
"'url'",
"]",
")",
"hook",
"=",
"next",
"(",
"hooks",
",",
"None",
")",
"if",
"hook",
":",
"hook",
".",
"edit",
"(",
"config",
"=",
"config",
",",
"events",
"=",
"[",
"'release'",
"]",
")",
"finally",
":",
"if",
"hook",
":",
"Repository",
".",
"enable",
"(",
"user_id",
"=",
"self",
".",
"user_id",
",",
"github_id",
"=",
"repo_id",
",",
"name",
"=",
"repo_name",
",",
"hook",
"=",
"hook",
".",
"id",
")",
"return",
"True",
"return",
"False"
] | Create repository hook. | [
"Create",
"repository",
"hook",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L234-L270 |
4,128 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubAPI.remove_hook | def remove_hook(self, repo_id, name):
"""Remove repository hook."""
ghrepo = self.api.repository_with_id(repo_id)
if ghrepo:
hooks = (h for h in ghrepo.hooks()
if h.config.get('url', '') == self.webhook_url)
hook = next(hooks, None)
if not hook or hook.delete():
Repository.disable(user_id=self.user_id,
github_id=repo_id,
name=name)
return True
return False | python | def remove_hook(self, repo_id, name):
ghrepo = self.api.repository_with_id(repo_id)
if ghrepo:
hooks = (h for h in ghrepo.hooks()
if h.config.get('url', '') == self.webhook_url)
hook = next(hooks, None)
if not hook or hook.delete():
Repository.disable(user_id=self.user_id,
github_id=repo_id,
name=name)
return True
return False | [
"def",
"remove_hook",
"(",
"self",
",",
"repo_id",
",",
"name",
")",
":",
"ghrepo",
"=",
"self",
".",
"api",
".",
"repository_with_id",
"(",
"repo_id",
")",
"if",
"ghrepo",
":",
"hooks",
"=",
"(",
"h",
"for",
"h",
"in",
"ghrepo",
".",
"hooks",
"(",
")",
"if",
"h",
".",
"config",
".",
"get",
"(",
"'url'",
",",
"''",
")",
"==",
"self",
".",
"webhook_url",
")",
"hook",
"=",
"next",
"(",
"hooks",
",",
"None",
")",
"if",
"not",
"hook",
"or",
"hook",
".",
"delete",
"(",
")",
":",
"Repository",
".",
"disable",
"(",
"user_id",
"=",
"self",
".",
"user_id",
",",
"github_id",
"=",
"repo_id",
",",
"name",
"=",
"name",
")",
"return",
"True",
"return",
"False"
] | Remove repository hook. | [
"Remove",
"repository",
"hook",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L272-L284 |
4,129 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubAPI._dev_api | def _dev_api(cls):
"""Get a developer instance for GitHub API access."""
gh = github3.GitHub()
gh.set_client_id(cls.remote.consumer_key, cls.remote.consumer_secret)
return gh | python | def _dev_api(cls):
gh = github3.GitHub()
gh.set_client_id(cls.remote.consumer_key, cls.remote.consumer_secret)
return gh | [
"def",
"_dev_api",
"(",
"cls",
")",
":",
"gh",
"=",
"github3",
".",
"GitHub",
"(",
")",
"gh",
".",
"set_client_id",
"(",
"cls",
".",
"remote",
".",
"consumer_key",
",",
"cls",
".",
"remote",
".",
"consumer_secret",
")",
"return",
"gh"
] | Get a developer instance for GitHub API access. | [
"Get",
"a",
"developer",
"instance",
"for",
"GitHub",
"API",
"access",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L287-L291 |
4,130 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubRelease.deposit_class | def deposit_class(self):
"""Return a class implementing `publish` method."""
cls = current_app.config['GITHUB_DEPOSIT_CLASS']
if isinstance(cls, string_types):
cls = import_string(cls)
assert isinstance(cls, type)
return cls | python | def deposit_class(self):
cls = current_app.config['GITHUB_DEPOSIT_CLASS']
if isinstance(cls, string_types):
cls = import_string(cls)
assert isinstance(cls, type)
return cls | [
"def",
"deposit_class",
"(",
"self",
")",
":",
"cls",
"=",
"current_app",
".",
"config",
"[",
"'GITHUB_DEPOSIT_CLASS'",
"]",
"if",
"isinstance",
"(",
"cls",
",",
"string_types",
")",
":",
"cls",
"=",
"import_string",
"(",
"cls",
")",
"assert",
"isinstance",
"(",
"cls",
",",
"type",
")",
"return",
"cls"
] | Return a class implementing `publish` method. | [
"Return",
"a",
"class",
"implementing",
"publish",
"method",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L317-L323 |
4,131 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubRelease.repo_model | def repo_model(self):
"""Return repository model from database."""
return Repository.query.filter_by(
user_id=self.event.user_id,
github_id=self.repository['id'],
).one() | python | def repo_model(self):
return Repository.query.filter_by(
user_id=self.event.user_id,
github_id=self.repository['id'],
).one() | [
"def",
"repo_model",
"(",
"self",
")",
":",
"return",
"Repository",
".",
"query",
".",
"filter_by",
"(",
"user_id",
"=",
"self",
".",
"event",
".",
"user_id",
",",
"github_id",
"=",
"self",
".",
"repository",
"[",
"'id'",
"]",
",",
")",
".",
"one",
"(",
")"
] | Return repository model from database. | [
"Return",
"repository",
"model",
"from",
"database",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L346-L351 |
4,132 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubRelease.title | def title(self):
"""Extract title from a release."""
if self.event:
if self.release['name']:
return u'{0}: {1}'.format(
self.repository['full_name'], self.release['name']
)
return u'{0} {1}'.format(self.repo_model.name, self.model.tag) | python | def title(self):
if self.event:
if self.release['name']:
return u'{0}: {1}'.format(
self.repository['full_name'], self.release['name']
)
return u'{0} {1}'.format(self.repo_model.name, self.model.tag) | [
"def",
"title",
"(",
"self",
")",
":",
"if",
"self",
".",
"event",
":",
"if",
"self",
".",
"release",
"[",
"'name'",
"]",
":",
"return",
"u'{0}: {1}'",
".",
"format",
"(",
"self",
".",
"repository",
"[",
"'full_name'",
"]",
",",
"self",
".",
"release",
"[",
"'name'",
"]",
")",
"return",
"u'{0} {1}'",
".",
"format",
"(",
"self",
".",
"repo_model",
".",
"name",
",",
"self",
".",
"model",
".",
"tag",
")"
] | Extract title from a release. | [
"Extract",
"title",
"from",
"a",
"release",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L354-L361 |
4,133 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubRelease.description | def description(self):
"""Extract description from a release."""
if self.release.get('body'):
return markdown(self.release['body'])
elif self.repository.get('description'):
return self.repository['description']
return 'No description provided.' | python | def description(self):
if self.release.get('body'):
return markdown(self.release['body'])
elif self.repository.get('description'):
return self.repository['description']
return 'No description provided.' | [
"def",
"description",
"(",
"self",
")",
":",
"if",
"self",
".",
"release",
".",
"get",
"(",
"'body'",
")",
":",
"return",
"markdown",
"(",
"self",
".",
"release",
"[",
"'body'",
"]",
")",
"elif",
"self",
".",
"repository",
".",
"get",
"(",
"'description'",
")",
":",
"return",
"self",
".",
"repository",
"[",
"'description'",
"]",
"return",
"'No description provided.'"
] | Extract description from a release. | [
"Extract",
"description",
"from",
"a",
"release",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L364-L370 |
4,134 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubRelease.related_identifiers | def related_identifiers(self):
"""Yield related identifiers."""
yield dict(
identifier=u'https://github.com/{0}/tree/{1}'.format(
self.repository['full_name'], self.release['tag_name']
),
relation='isSupplementTo',
) | python | def related_identifiers(self):
yield dict(
identifier=u'https://github.com/{0}/tree/{1}'.format(
self.repository['full_name'], self.release['tag_name']
),
relation='isSupplementTo',
) | [
"def",
"related_identifiers",
"(",
"self",
")",
":",
"yield",
"dict",
"(",
"identifier",
"=",
"u'https://github.com/{0}/tree/{1}'",
".",
"format",
"(",
"self",
".",
"repository",
"[",
"'full_name'",
"]",
",",
"self",
".",
"release",
"[",
"'tag_name'",
"]",
")",
",",
"relation",
"=",
"'isSupplementTo'",
",",
")"
] | Yield related identifiers. | [
"Yield",
"related",
"identifiers",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L378-L385 |
4,135 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubRelease.defaults | def defaults(self):
"""Return default metadata."""
return dict(
access_right='open',
description=self.description,
license='other-open',
publication_date=self.release['published_at'][:10],
related_identifiers=list(self.related_identifiers),
version=self.version,
title=self.title,
upload_type='software',
) | python | def defaults(self):
return dict(
access_right='open',
description=self.description,
license='other-open',
publication_date=self.release['published_at'][:10],
related_identifiers=list(self.related_identifiers),
version=self.version,
title=self.title,
upload_type='software',
) | [
"def",
"defaults",
"(",
"self",
")",
":",
"return",
"dict",
"(",
"access_right",
"=",
"'open'",
",",
"description",
"=",
"self",
".",
"description",
",",
"license",
"=",
"'other-open'",
",",
"publication_date",
"=",
"self",
".",
"release",
"[",
"'published_at'",
"]",
"[",
":",
"10",
"]",
",",
"related_identifiers",
"=",
"list",
"(",
"self",
".",
"related_identifiers",
")",
",",
"version",
"=",
"self",
".",
"version",
",",
"title",
"=",
"self",
".",
"title",
",",
"upload_type",
"=",
"'software'",
",",
")"
] | Return default metadata. | [
"Return",
"default",
"metadata",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L393-L404 |
4,136 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubRelease.extra_metadata | def extra_metadata(self):
"""Get extra metadata for file in repository."""
return get_extra_metadata(
self.gh.api,
self.repository['owner']['login'],
self.repository['name'],
self.release['tag_name'],
) | python | def extra_metadata(self):
return get_extra_metadata(
self.gh.api,
self.repository['owner']['login'],
self.repository['name'],
self.release['tag_name'],
) | [
"def",
"extra_metadata",
"(",
"self",
")",
":",
"return",
"get_extra_metadata",
"(",
"self",
".",
"gh",
".",
"api",
",",
"self",
".",
"repository",
"[",
"'owner'",
"]",
"[",
"'login'",
"]",
",",
"self",
".",
"repository",
"[",
"'name'",
"]",
",",
"self",
".",
"release",
"[",
"'tag_name'",
"]",
",",
")"
] | Get extra metadata for file in repository. | [
"Get",
"extra",
"metadata",
"for",
"file",
"in",
"repository",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L407-L414 |
4,137 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubRelease.files | def files(self):
"""Extract files to download from GitHub payload."""
tag_name = self.release['tag_name']
repo_name = self.repository['full_name']
zipball_url = self.release['zipball_url']
filename = u'{name}-{tag}.zip'.format(name=repo_name, tag=tag_name)
response = self.gh.api.session.head(zipball_url)
assert response.status_code == 302, \
u'Could not retrieve archive from GitHub: {0}'.format(zipball_url)
yield filename, zipball_url | python | def files(self):
tag_name = self.release['tag_name']
repo_name = self.repository['full_name']
zipball_url = self.release['zipball_url']
filename = u'{name}-{tag}.zip'.format(name=repo_name, tag=tag_name)
response = self.gh.api.session.head(zipball_url)
assert response.status_code == 302, \
u'Could not retrieve archive from GitHub: {0}'.format(zipball_url)
yield filename, zipball_url | [
"def",
"files",
"(",
"self",
")",
":",
"tag_name",
"=",
"self",
".",
"release",
"[",
"'tag_name'",
"]",
"repo_name",
"=",
"self",
".",
"repository",
"[",
"'full_name'",
"]",
"zipball_url",
"=",
"self",
".",
"release",
"[",
"'zipball_url'",
"]",
"filename",
"=",
"u'{name}-{tag}.zip'",
".",
"format",
"(",
"name",
"=",
"repo_name",
",",
"tag",
"=",
"tag_name",
")",
"response",
"=",
"self",
".",
"gh",
".",
"api",
".",
"session",
".",
"head",
"(",
"zipball_url",
")",
"assert",
"response",
".",
"status_code",
"==",
"302",
",",
"u'Could not retrieve archive from GitHub: {0}'",
".",
"format",
"(",
"zipball_url",
")",
"yield",
"filename",
",",
"zipball_url"
] | Extract files to download from GitHub payload. | [
"Extract",
"files",
"to",
"download",
"from",
"GitHub",
"payload",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L417-L429 |
4,138 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubRelease.metadata | def metadata(self):
"""Return extracted metadata."""
output = dict(self.defaults)
output.update(self.extra_metadata)
return output | python | def metadata(self):
output = dict(self.defaults)
output.update(self.extra_metadata)
return output | [
"def",
"metadata",
"(",
"self",
")",
":",
"output",
"=",
"dict",
"(",
"self",
".",
"defaults",
")",
"output",
".",
"update",
"(",
"self",
".",
"extra_metadata",
")",
"return",
"output"
] | Return extracted metadata. | [
"Return",
"extracted",
"metadata",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L432-L436 |
4,139 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubRelease.pid | def pid(self):
"""Get PID object for the Release record."""
if self.model.status == ReleaseStatus.PUBLISHED and self.record:
fetcher = current_pidstore.fetchers[
current_app.config.get('GITHUB_PID_FETCHER')]
return fetcher(self.record.id, self.record) | python | def pid(self):
if self.model.status == ReleaseStatus.PUBLISHED and self.record:
fetcher = current_pidstore.fetchers[
current_app.config.get('GITHUB_PID_FETCHER')]
return fetcher(self.record.id, self.record) | [
"def",
"pid",
"(",
"self",
")",
":",
"if",
"self",
".",
"model",
".",
"status",
"==",
"ReleaseStatus",
".",
"PUBLISHED",
"and",
"self",
".",
"record",
":",
"fetcher",
"=",
"current_pidstore",
".",
"fetchers",
"[",
"current_app",
".",
"config",
".",
"get",
"(",
"'GITHUB_PID_FETCHER'",
")",
"]",
"return",
"fetcher",
"(",
"self",
".",
"record",
".",
"id",
",",
"self",
".",
"record",
")"
] | Get PID object for the Release record. | [
"Get",
"PID",
"object",
"for",
"the",
"Release",
"record",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L449-L454 |
4,140 | inveniosoftware/invenio-github | invenio_github/api.py | GitHubRelease.publish | def publish(self):
"""Publish GitHub release as record."""
with db.session.begin_nested():
deposit = self.deposit_class.create(self.metadata)
deposit['_deposit']['created_by'] = self.event.user_id
deposit['_deposit']['owners'] = [self.event.user_id]
# Fetch the deposit files
for key, url in self.files:
deposit.files[key] = self.gh.api.session.get(
url, stream=True).raw
deposit.publish()
recid, record = deposit.fetch_published()
self.model.recordmetadata = record.model | python | def publish(self):
with db.session.begin_nested():
deposit = self.deposit_class.create(self.metadata)
deposit['_deposit']['created_by'] = self.event.user_id
deposit['_deposit']['owners'] = [self.event.user_id]
# Fetch the deposit files
for key, url in self.files:
deposit.files[key] = self.gh.api.session.get(
url, stream=True).raw
deposit.publish()
recid, record = deposit.fetch_published()
self.model.recordmetadata = record.model | [
"def",
"publish",
"(",
"self",
")",
":",
"with",
"db",
".",
"session",
".",
"begin_nested",
"(",
")",
":",
"deposit",
"=",
"self",
".",
"deposit_class",
".",
"create",
"(",
"self",
".",
"metadata",
")",
"deposit",
"[",
"'_deposit'",
"]",
"[",
"'created_by'",
"]",
"=",
"self",
".",
"event",
".",
"user_id",
"deposit",
"[",
"'_deposit'",
"]",
"[",
"'owners'",
"]",
"=",
"[",
"self",
".",
"event",
".",
"user_id",
"]",
"# Fetch the deposit files",
"for",
"key",
",",
"url",
"in",
"self",
".",
"files",
":",
"deposit",
".",
"files",
"[",
"key",
"]",
"=",
"self",
".",
"gh",
".",
"api",
".",
"session",
".",
"get",
"(",
"url",
",",
"stream",
"=",
"True",
")",
".",
"raw",
"deposit",
".",
"publish",
"(",
")",
"recid",
",",
"record",
"=",
"deposit",
".",
"fetch_published",
"(",
")",
"self",
".",
"model",
".",
"recordmetadata",
"=",
"record",
".",
"model"
] | Publish GitHub release as record. | [
"Publish",
"GitHub",
"release",
"as",
"record",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/api.py#L461-L475 |
4,141 | inveniosoftware/invenio-github | invenio_github/receivers.py | GitHubReceiver.run | def run(self, event):
"""Process an event.
.. note::
We should only do basic server side operation here, since we send
the rest of the processing to a Celery task which will be mainly
accessing the GitHub API.
"""
repo_id = event.payload['repository']['id']
# Ping event - update the ping timestamp of the repository
if 'hook_id' in event.payload and 'zen' in event.payload:
repository = Repository.query.filter_by(
github_id=repo_id
).one()
repository.ping = datetime.utcnow()
db.session.commit()
return
# Release event
if 'release' in event.payload and \
event.payload.get('action') == 'published':
try:
release = Release.create(event)
db.session.commit()
# FIXME: If we want to skip the processing, we should do it
# here (eg. We're in the middle of a migration).
# if current_app.config['GITHUB_PROCESS_RELEASES']:
process_release.delay(
release.release_id,
verify_sender=self.verify_sender
)
except (ReleaseAlreadyReceivedError, RepositoryDisabledError) as e:
event.response_code = 409
event.response = dict(message=str(e), status=409)
except RepositoryAccessError as e:
event.response = 403
event.response = dict(message=str(e), status=403) | python | def run(self, event):
repo_id = event.payload['repository']['id']
# Ping event - update the ping timestamp of the repository
if 'hook_id' in event.payload and 'zen' in event.payload:
repository = Repository.query.filter_by(
github_id=repo_id
).one()
repository.ping = datetime.utcnow()
db.session.commit()
return
# Release event
if 'release' in event.payload and \
event.payload.get('action') == 'published':
try:
release = Release.create(event)
db.session.commit()
# FIXME: If we want to skip the processing, we should do it
# here (eg. We're in the middle of a migration).
# if current_app.config['GITHUB_PROCESS_RELEASES']:
process_release.delay(
release.release_id,
verify_sender=self.verify_sender
)
except (ReleaseAlreadyReceivedError, RepositoryDisabledError) as e:
event.response_code = 409
event.response = dict(message=str(e), status=409)
except RepositoryAccessError as e:
event.response = 403
event.response = dict(message=str(e), status=403) | [
"def",
"run",
"(",
"self",
",",
"event",
")",
":",
"repo_id",
"=",
"event",
".",
"payload",
"[",
"'repository'",
"]",
"[",
"'id'",
"]",
"# Ping event - update the ping timestamp of the repository",
"if",
"'hook_id'",
"in",
"event",
".",
"payload",
"and",
"'zen'",
"in",
"event",
".",
"payload",
":",
"repository",
"=",
"Repository",
".",
"query",
".",
"filter_by",
"(",
"github_id",
"=",
"repo_id",
")",
".",
"one",
"(",
")",
"repository",
".",
"ping",
"=",
"datetime",
".",
"utcnow",
"(",
")",
"db",
".",
"session",
".",
"commit",
"(",
")",
"return",
"# Release event",
"if",
"'release'",
"in",
"event",
".",
"payload",
"and",
"event",
".",
"payload",
".",
"get",
"(",
"'action'",
")",
"==",
"'published'",
":",
"try",
":",
"release",
"=",
"Release",
".",
"create",
"(",
"event",
")",
"db",
".",
"session",
".",
"commit",
"(",
")",
"# FIXME: If we want to skip the processing, we should do it",
"# here (eg. We're in the middle of a migration).",
"# if current_app.config['GITHUB_PROCESS_RELEASES']:",
"process_release",
".",
"delay",
"(",
"release",
".",
"release_id",
",",
"verify_sender",
"=",
"self",
".",
"verify_sender",
")",
"except",
"(",
"ReleaseAlreadyReceivedError",
",",
"RepositoryDisabledError",
")",
"as",
"e",
":",
"event",
".",
"response_code",
"=",
"409",
"event",
".",
"response",
"=",
"dict",
"(",
"message",
"=",
"str",
"(",
"e",
")",
",",
"status",
"=",
"409",
")",
"except",
"RepositoryAccessError",
"as",
"e",
":",
"event",
".",
"response",
"=",
"403",
"event",
".",
"response",
"=",
"dict",
"(",
"message",
"=",
"str",
"(",
"e",
")",
",",
"status",
"=",
"403",
")"
] | Process an event.
.. note::
We should only do basic server side operation here, since we send
the rest of the processing to a Celery task which will be mainly
accessing the GitHub API. | [
"Process",
"an",
"event",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/receivers.py#L43-L82 |
4,142 | inveniosoftware/invenio-github | invenio_github/utils.py | parse_timestamp | def parse_timestamp(x):
"""Parse ISO8601 formatted timestamp."""
dt = dateutil.parser.parse(x)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=pytz.utc)
return dt | python | def parse_timestamp(x):
dt = dateutil.parser.parse(x)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=pytz.utc)
return dt | [
"def",
"parse_timestamp",
"(",
"x",
")",
":",
"dt",
"=",
"dateutil",
".",
"parser",
".",
"parse",
"(",
"x",
")",
"if",
"dt",
".",
"tzinfo",
"is",
"None",
":",
"dt",
"=",
"dt",
".",
"replace",
"(",
"tzinfo",
"=",
"pytz",
".",
"utc",
")",
"return",
"dt"
] | Parse ISO8601 formatted timestamp. | [
"Parse",
"ISO8601",
"formatted",
"timestamp",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/utils.py#L44-L49 |
4,143 | inveniosoftware/invenio-github | invenio_github/utils.py | get_extra_metadata | def get_extra_metadata(gh, owner, repo_name, ref):
"""Get the metadata file."""
try:
content = gh.repository(owner, repo_name).file_contents(
path=current_app.config['GITHUB_METADATA_FILE'], ref=ref
)
if not content:
# File does not exists in the given ref
return {}
return json.loads(content.decoded.decode('utf-8'))
except ValueError:
raise CustomGitHubMetadataError(
u'Metadata file "{file}" is not valid JSON.'
.format(file=current_app.config['GITHUB_METADATA_FILE'])
) | python | def get_extra_metadata(gh, owner, repo_name, ref):
try:
content = gh.repository(owner, repo_name).file_contents(
path=current_app.config['GITHUB_METADATA_FILE'], ref=ref
)
if not content:
# File does not exists in the given ref
return {}
return json.loads(content.decoded.decode('utf-8'))
except ValueError:
raise CustomGitHubMetadataError(
u'Metadata file "{file}" is not valid JSON.'
.format(file=current_app.config['GITHUB_METADATA_FILE'])
) | [
"def",
"get_extra_metadata",
"(",
"gh",
",",
"owner",
",",
"repo_name",
",",
"ref",
")",
":",
"try",
":",
"content",
"=",
"gh",
".",
"repository",
"(",
"owner",
",",
"repo_name",
")",
".",
"file_contents",
"(",
"path",
"=",
"current_app",
".",
"config",
"[",
"'GITHUB_METADATA_FILE'",
"]",
",",
"ref",
"=",
"ref",
")",
"if",
"not",
"content",
":",
"# File does not exists in the given ref",
"return",
"{",
"}",
"return",
"json",
".",
"loads",
"(",
"content",
".",
"decoded",
".",
"decode",
"(",
"'utf-8'",
")",
")",
"except",
"ValueError",
":",
"raise",
"CustomGitHubMetadataError",
"(",
"u'Metadata file \"{file}\" is not valid JSON.'",
".",
"format",
"(",
"file",
"=",
"current_app",
".",
"config",
"[",
"'GITHUB_METADATA_FILE'",
"]",
")",
")"
] | Get the metadata file. | [
"Get",
"the",
"metadata",
"file",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/utils.py#L52-L66 |
4,144 | inveniosoftware/invenio-github | invenio_github/utils.py | get_owner | def get_owner(gh, owner):
"""Get owner of repository as a creator."""
try:
u = gh.user(owner)
name = u.name or u.login
company = u.company or ''
return [dict(name=name, affiliation=company)]
except Exception:
return None | python | def get_owner(gh, owner):
try:
u = gh.user(owner)
name = u.name or u.login
company = u.company or ''
return [dict(name=name, affiliation=company)]
except Exception:
return None | [
"def",
"get_owner",
"(",
"gh",
",",
"owner",
")",
":",
"try",
":",
"u",
"=",
"gh",
".",
"user",
"(",
"owner",
")",
"name",
"=",
"u",
".",
"name",
"or",
"u",
".",
"login",
"company",
"=",
"u",
".",
"company",
"or",
"''",
"return",
"[",
"dict",
"(",
"name",
"=",
"name",
",",
"affiliation",
"=",
"company",
")",
"]",
"except",
"Exception",
":",
"return",
"None"
] | Get owner of repository as a creator. | [
"Get",
"owner",
"of",
"repository",
"as",
"a",
"creator",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/utils.py#L69-L77 |
4,145 | inveniosoftware/invenio-github | invenio_github/utils.py | get_contributors | def get_contributors(gh, repo_id):
"""Get list of contributors to a repository."""
try:
# FIXME: Use `github3.Repository.contributors` to get this information
contrib_url = gh.repository_with_id(repo_id).contributors_url
r = requests.get(contrib_url)
if r.status_code == 200:
contributors = r.json()
def get_author(contributor):
r = requests.get(contributor['url'])
if r.status_code == 200:
data = r.json()
return dict(
name=(data['name'] if 'name' in data and data['name']
else data['login']),
affiliation=data.get('company') or '',
)
# Sort according to number of contributions
contributors.sort(key=itemgetter('contributions'))
contributors = [get_author(x) for x in reversed(contributors)
if x['type'] == 'User']
contributors = filter(lambda x: x is not None, contributors)
return contributors
except Exception:
return None | python | def get_contributors(gh, repo_id):
try:
# FIXME: Use `github3.Repository.contributors` to get this information
contrib_url = gh.repository_with_id(repo_id).contributors_url
r = requests.get(contrib_url)
if r.status_code == 200:
contributors = r.json()
def get_author(contributor):
r = requests.get(contributor['url'])
if r.status_code == 200:
data = r.json()
return dict(
name=(data['name'] if 'name' in data and data['name']
else data['login']),
affiliation=data.get('company') or '',
)
# Sort according to number of contributions
contributors.sort(key=itemgetter('contributions'))
contributors = [get_author(x) for x in reversed(contributors)
if x['type'] == 'User']
contributors = filter(lambda x: x is not None, contributors)
return contributors
except Exception:
return None | [
"def",
"get_contributors",
"(",
"gh",
",",
"repo_id",
")",
":",
"try",
":",
"# FIXME: Use `github3.Repository.contributors` to get this information",
"contrib_url",
"=",
"gh",
".",
"repository_with_id",
"(",
"repo_id",
")",
".",
"contributors_url",
"r",
"=",
"requests",
".",
"get",
"(",
"contrib_url",
")",
"if",
"r",
".",
"status_code",
"==",
"200",
":",
"contributors",
"=",
"r",
".",
"json",
"(",
")",
"def",
"get_author",
"(",
"contributor",
")",
":",
"r",
"=",
"requests",
".",
"get",
"(",
"contributor",
"[",
"'url'",
"]",
")",
"if",
"r",
".",
"status_code",
"==",
"200",
":",
"data",
"=",
"r",
".",
"json",
"(",
")",
"return",
"dict",
"(",
"name",
"=",
"(",
"data",
"[",
"'name'",
"]",
"if",
"'name'",
"in",
"data",
"and",
"data",
"[",
"'name'",
"]",
"else",
"data",
"[",
"'login'",
"]",
")",
",",
"affiliation",
"=",
"data",
".",
"get",
"(",
"'company'",
")",
"or",
"''",
",",
")",
"# Sort according to number of contributions",
"contributors",
".",
"sort",
"(",
"key",
"=",
"itemgetter",
"(",
"'contributions'",
")",
")",
"contributors",
"=",
"[",
"get_author",
"(",
"x",
")",
"for",
"x",
"in",
"reversed",
"(",
"contributors",
")",
"if",
"x",
"[",
"'type'",
"]",
"==",
"'User'",
"]",
"contributors",
"=",
"filter",
"(",
"lambda",
"x",
":",
"x",
"is",
"not",
"None",
",",
"contributors",
")",
"return",
"contributors",
"except",
"Exception",
":",
"return",
"None"
] | Get list of contributors to a repository. | [
"Get",
"list",
"of",
"contributors",
"to",
"a",
"repository",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/utils.py#L80-L108 |
4,146 | inveniosoftware/invenio-github | invenio_github/handlers.py | account_setup | def account_setup(remote, token=None, response=None,
account_setup=None):
"""Setup user account."""
gh = GitHubAPI(user_id=token.remote_account.user_id)
with db.session.begin_nested():
gh.init_account()
# Create user <-> external id link.
oauth_link_external_id(
token.remote_account.user,
dict(id=str(gh.account.extra_data['id']), method="github")
) | python | def account_setup(remote, token=None, response=None,
account_setup=None):
gh = GitHubAPI(user_id=token.remote_account.user_id)
with db.session.begin_nested():
gh.init_account()
# Create user <-> external id link.
oauth_link_external_id(
token.remote_account.user,
dict(id=str(gh.account.extra_data['id']), method="github")
) | [
"def",
"account_setup",
"(",
"remote",
",",
"token",
"=",
"None",
",",
"response",
"=",
"None",
",",
"account_setup",
"=",
"None",
")",
":",
"gh",
"=",
"GitHubAPI",
"(",
"user_id",
"=",
"token",
".",
"remote_account",
".",
"user_id",
")",
"with",
"db",
".",
"session",
".",
"begin_nested",
"(",
")",
":",
"gh",
".",
"init_account",
"(",
")",
"# Create user <-> external id link.",
"oauth_link_external_id",
"(",
"token",
".",
"remote_account",
".",
"user",
",",
"dict",
"(",
"id",
"=",
"str",
"(",
"gh",
".",
"account",
".",
"extra_data",
"[",
"'id'",
"]",
")",
",",
"method",
"=",
"\"github\"",
")",
")"
] | Setup user account. | [
"Setup",
"user",
"account",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/handlers.py#L41-L52 |
4,147 | inveniosoftware/invenio-github | invenio_github/handlers.py | account_post_init | def account_post_init(remote, token=None):
"""Perform post initialization."""
gh = GitHubAPI(user_id=token.remote_account.user_id)
repos = [r.id for r in gh.api.repositories() if r.permissions['admin']]
sync_hooks.delay(token.remote_account.user_id, repos) | python | def account_post_init(remote, token=None):
gh = GitHubAPI(user_id=token.remote_account.user_id)
repos = [r.id for r in gh.api.repositories() if r.permissions['admin']]
sync_hooks.delay(token.remote_account.user_id, repos) | [
"def",
"account_post_init",
"(",
"remote",
",",
"token",
"=",
"None",
")",
":",
"gh",
"=",
"GitHubAPI",
"(",
"user_id",
"=",
"token",
".",
"remote_account",
".",
"user_id",
")",
"repos",
"=",
"[",
"r",
".",
"id",
"for",
"r",
"in",
"gh",
".",
"api",
".",
"repositories",
"(",
")",
"if",
"r",
".",
"permissions",
"[",
"'admin'",
"]",
"]",
"sync_hooks",
".",
"delay",
"(",
"token",
".",
"remote_account",
".",
"user_id",
",",
"repos",
")"
] | Perform post initialization. | [
"Perform",
"post",
"initialization",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/handlers.py#L55-L59 |
4,148 | inveniosoftware/invenio-github | invenio_github/handlers.py | disconnect | def disconnect(remote):
"""Disconnect callback handler for GitHub."""
# User must be authenticated
if not current_user.is_authenticated:
return current_app.login_manager.unauthorized()
external_method = 'github'
external_ids = [i.id for i in current_user.external_identifiers
if i.method == external_method]
if external_ids:
oauth_unlink_external_id(dict(id=external_ids[0],
method=external_method))
user_id = int(current_user.get_id())
token = RemoteToken.get(user_id, remote.consumer_key)
if token:
extra_data = token.remote_account.extra_data
# Delete the token that we issued for GitHub to deliver webhooks
webhook_token_id = extra_data.get('tokens', {}).get('webhook')
ProviderToken.query.filter_by(id=webhook_token_id).delete()
# Disable GitHub webhooks from our side
db_repos = Repository.query.filter_by(user_id=user_id).all()
# Keep repositories with hooks to pass to the celery task later on
repos_with_hooks = [(r.github_id, r.hook) for r in db_repos if r.hook]
for repo in db_repos:
try:
Repository.disable(user_id=user_id,
github_id=repo.github_id,
name=repo.name)
except NoResultFound:
# If the repository doesn't exist, no action is necessary
pass
db.session.commit()
# Send Celery task for webhooks removal and token revocation
disconnect_github.delay(token.access_token, repos_with_hooks)
# Delete the RemoteAccount (along with the associated RemoteToken)
token.remote_account.delete()
return redirect(url_for('invenio_oauthclient_settings.index')) | python | def disconnect(remote):
# User must be authenticated
if not current_user.is_authenticated:
return current_app.login_manager.unauthorized()
external_method = 'github'
external_ids = [i.id for i in current_user.external_identifiers
if i.method == external_method]
if external_ids:
oauth_unlink_external_id(dict(id=external_ids[0],
method=external_method))
user_id = int(current_user.get_id())
token = RemoteToken.get(user_id, remote.consumer_key)
if token:
extra_data = token.remote_account.extra_data
# Delete the token that we issued for GitHub to deliver webhooks
webhook_token_id = extra_data.get('tokens', {}).get('webhook')
ProviderToken.query.filter_by(id=webhook_token_id).delete()
# Disable GitHub webhooks from our side
db_repos = Repository.query.filter_by(user_id=user_id).all()
# Keep repositories with hooks to pass to the celery task later on
repos_with_hooks = [(r.github_id, r.hook) for r in db_repos if r.hook]
for repo in db_repos:
try:
Repository.disable(user_id=user_id,
github_id=repo.github_id,
name=repo.name)
except NoResultFound:
# If the repository doesn't exist, no action is necessary
pass
db.session.commit()
# Send Celery task for webhooks removal and token revocation
disconnect_github.delay(token.access_token, repos_with_hooks)
# Delete the RemoteAccount (along with the associated RemoteToken)
token.remote_account.delete()
return redirect(url_for('invenio_oauthclient_settings.index')) | [
"def",
"disconnect",
"(",
"remote",
")",
":",
"# User must be authenticated",
"if",
"not",
"current_user",
".",
"is_authenticated",
":",
"return",
"current_app",
".",
"login_manager",
".",
"unauthorized",
"(",
")",
"external_method",
"=",
"'github'",
"external_ids",
"=",
"[",
"i",
".",
"id",
"for",
"i",
"in",
"current_user",
".",
"external_identifiers",
"if",
"i",
".",
"method",
"==",
"external_method",
"]",
"if",
"external_ids",
":",
"oauth_unlink_external_id",
"(",
"dict",
"(",
"id",
"=",
"external_ids",
"[",
"0",
"]",
",",
"method",
"=",
"external_method",
")",
")",
"user_id",
"=",
"int",
"(",
"current_user",
".",
"get_id",
"(",
")",
")",
"token",
"=",
"RemoteToken",
".",
"get",
"(",
"user_id",
",",
"remote",
".",
"consumer_key",
")",
"if",
"token",
":",
"extra_data",
"=",
"token",
".",
"remote_account",
".",
"extra_data",
"# Delete the token that we issued for GitHub to deliver webhooks",
"webhook_token_id",
"=",
"extra_data",
".",
"get",
"(",
"'tokens'",
",",
"{",
"}",
")",
".",
"get",
"(",
"'webhook'",
")",
"ProviderToken",
".",
"query",
".",
"filter_by",
"(",
"id",
"=",
"webhook_token_id",
")",
".",
"delete",
"(",
")",
"# Disable GitHub webhooks from our side",
"db_repos",
"=",
"Repository",
".",
"query",
".",
"filter_by",
"(",
"user_id",
"=",
"user_id",
")",
".",
"all",
"(",
")",
"# Keep repositories with hooks to pass to the celery task later on",
"repos_with_hooks",
"=",
"[",
"(",
"r",
".",
"github_id",
",",
"r",
".",
"hook",
")",
"for",
"r",
"in",
"db_repos",
"if",
"r",
".",
"hook",
"]",
"for",
"repo",
"in",
"db_repos",
":",
"try",
":",
"Repository",
".",
"disable",
"(",
"user_id",
"=",
"user_id",
",",
"github_id",
"=",
"repo",
".",
"github_id",
",",
"name",
"=",
"repo",
".",
"name",
")",
"except",
"NoResultFound",
":",
"# If the repository doesn't exist, no action is necessary",
"pass",
"db",
".",
"session",
".",
"commit",
"(",
")",
"# Send Celery task for webhooks removal and token revocation",
"disconnect_github",
".",
"delay",
"(",
"token",
".",
"access_token",
",",
"repos_with_hooks",
")",
"# Delete the RemoteAccount (along with the associated RemoteToken)",
"token",
".",
"remote_account",
".",
"delete",
"(",
")",
"return",
"redirect",
"(",
"url_for",
"(",
"'invenio_oauthclient_settings.index'",
")",
")"
] | Disconnect callback handler for GitHub. | [
"Disconnect",
"callback",
"handler",
"for",
"GitHub",
"."
] | ec42fd6a06079310dcbe2c46d9fd79d5197bbe26 | https://github.com/inveniosoftware/invenio-github/blob/ec42fd6a06079310dcbe2c46d9fd79d5197bbe26/invenio_github/handlers.py#L62-L103 |
4,149 | django-fluent/django-fluent-comments | fluent_comments/moderation.py | comments_are_open | def comments_are_open(content_object):
"""
Return whether comments are still open for a given target object.
"""
moderator = get_model_moderator(content_object.__class__)
if moderator is None:
return True
# Check the 'enable_field', 'auto_close_field' and 'close_after',
# by reusing the basic Django policies.
return CommentModerator.allow(moderator, None, content_object, None) | python | def comments_are_open(content_object):
moderator = get_model_moderator(content_object.__class__)
if moderator is None:
return True
# Check the 'enable_field', 'auto_close_field' and 'close_after',
# by reusing the basic Django policies.
return CommentModerator.allow(moderator, None, content_object, None) | [
"def",
"comments_are_open",
"(",
"content_object",
")",
":",
"moderator",
"=",
"get_model_moderator",
"(",
"content_object",
".",
"__class__",
")",
"if",
"moderator",
"is",
"None",
":",
"return",
"True",
"# Check the 'enable_field', 'auto_close_field' and 'close_after',",
"# by reusing the basic Django policies.",
"return",
"CommentModerator",
".",
"allow",
"(",
"moderator",
",",
"None",
",",
"content_object",
",",
"None",
")"
] | Return whether comments are still open for a given target object. | [
"Return",
"whether",
"comments",
"are",
"still",
"open",
"for",
"a",
"given",
"target",
"object",
"."
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/moderation.py#L189-L199 |
4,150 | django-fluent/django-fluent-comments | fluent_comments/moderation.py | comments_are_moderated | def comments_are_moderated(content_object):
"""
Return whether comments are moderated for a given target object.
"""
moderator = get_model_moderator(content_object.__class__)
if moderator is None:
return False
# Check the 'auto_moderate_field', 'moderate_after',
# by reusing the basic Django policies.
return CommentModerator.moderate(moderator, None, content_object, None) | python | def comments_are_moderated(content_object):
moderator = get_model_moderator(content_object.__class__)
if moderator is None:
return False
# Check the 'auto_moderate_field', 'moderate_after',
# by reusing the basic Django policies.
return CommentModerator.moderate(moderator, None, content_object, None) | [
"def",
"comments_are_moderated",
"(",
"content_object",
")",
":",
"moderator",
"=",
"get_model_moderator",
"(",
"content_object",
".",
"__class__",
")",
"if",
"moderator",
"is",
"None",
":",
"return",
"False",
"# Check the 'auto_moderate_field', 'moderate_after',",
"# by reusing the basic Django policies.",
"return",
"CommentModerator",
".",
"moderate",
"(",
"moderator",
",",
"None",
",",
"content_object",
",",
"None",
")"
] | Return whether comments are moderated for a given target object. | [
"Return",
"whether",
"comments",
"are",
"moderated",
"for",
"a",
"given",
"target",
"object",
"."
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/moderation.py#L202-L212 |
4,151 | django-fluent/django-fluent-comments | fluent_comments/moderation.py | FluentCommentsModerator.email | def email(self, comment, content_object, request):
"""
Overwritten for a better email notification.
"""
if not self.email_notification:
return
send_comment_posted(comment, request) | python | def email(self, comment, content_object, request):
if not self.email_notification:
return
send_comment_posted(comment, request) | [
"def",
"email",
"(",
"self",
",",
"comment",
",",
"content_object",
",",
"request",
")",
":",
"if",
"not",
"self",
".",
"email_notification",
":",
"return",
"send_comment_posted",
"(",
"comment",
",",
"request",
")"
] | Overwritten for a better email notification. | [
"Overwritten",
"for",
"a",
"better",
"email",
"notification",
"."
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/moderation.py#L106-L113 |
4,152 | django-fluent/django-fluent-comments | fluent_comments/email.py | send_comment_posted | def send_comment_posted(comment, request):
"""
Send the email to staff that an comment was posted.
While the django_comments module has email support,
it doesn't pass the 'request' to the context.
This also changes the subject to show the page title.
"""
recipient_list = [manager_tuple[1] for manager_tuple in settings.MANAGERS]
site = get_current_site(request)
content_object = comment.content_object
content_title = force_text(content_object)
if comment.is_removed:
subject = u'[{0}] Spam comment on "{1}"'.format(site.name, content_title)
elif not comment.is_public:
subject = u'[{0}] Moderated comment on "{1}"'.format(site.name, content_title)
else:
subject = u'[{0}] New comment posted on "{1}"'.format(site.name, content_title)
context = {
'site': site,
'comment': comment,
'content_object': content_object
}
message = render_to_string("comments/comment_notification_email.txt", context, request=request)
if appsettings.FLUENT_COMMENTS_MULTIPART_EMAILS:
html_message = render_to_string("comments/comment_notification_email.html", context, request=request)
else:
html_message = None
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
recipient_list, fail_silently=True, html_message=html_message) | python | def send_comment_posted(comment, request):
recipient_list = [manager_tuple[1] for manager_tuple in settings.MANAGERS]
site = get_current_site(request)
content_object = comment.content_object
content_title = force_text(content_object)
if comment.is_removed:
subject = u'[{0}] Spam comment on "{1}"'.format(site.name, content_title)
elif not comment.is_public:
subject = u'[{0}] Moderated comment on "{1}"'.format(site.name, content_title)
else:
subject = u'[{0}] New comment posted on "{1}"'.format(site.name, content_title)
context = {
'site': site,
'comment': comment,
'content_object': content_object
}
message = render_to_string("comments/comment_notification_email.txt", context, request=request)
if appsettings.FLUENT_COMMENTS_MULTIPART_EMAILS:
html_message = render_to_string("comments/comment_notification_email.html", context, request=request)
else:
html_message = None
send_mail(subject, message, settings.DEFAULT_FROM_EMAIL,
recipient_list, fail_silently=True, html_message=html_message) | [
"def",
"send_comment_posted",
"(",
"comment",
",",
"request",
")",
":",
"recipient_list",
"=",
"[",
"manager_tuple",
"[",
"1",
"]",
"for",
"manager_tuple",
"in",
"settings",
".",
"MANAGERS",
"]",
"site",
"=",
"get_current_site",
"(",
"request",
")",
"content_object",
"=",
"comment",
".",
"content_object",
"content_title",
"=",
"force_text",
"(",
"content_object",
")",
"if",
"comment",
".",
"is_removed",
":",
"subject",
"=",
"u'[{0}] Spam comment on \"{1}\"'",
".",
"format",
"(",
"site",
".",
"name",
",",
"content_title",
")",
"elif",
"not",
"comment",
".",
"is_public",
":",
"subject",
"=",
"u'[{0}] Moderated comment on \"{1}\"'",
".",
"format",
"(",
"site",
".",
"name",
",",
"content_title",
")",
"else",
":",
"subject",
"=",
"u'[{0}] New comment posted on \"{1}\"'",
".",
"format",
"(",
"site",
".",
"name",
",",
"content_title",
")",
"context",
"=",
"{",
"'site'",
":",
"site",
",",
"'comment'",
":",
"comment",
",",
"'content_object'",
":",
"content_object",
"}",
"message",
"=",
"render_to_string",
"(",
"\"comments/comment_notification_email.txt\"",
",",
"context",
",",
"request",
"=",
"request",
")",
"if",
"appsettings",
".",
"FLUENT_COMMENTS_MULTIPART_EMAILS",
":",
"html_message",
"=",
"render_to_string",
"(",
"\"comments/comment_notification_email.html\"",
",",
"context",
",",
"request",
"=",
"request",
")",
"else",
":",
"html_message",
"=",
"None",
"send_mail",
"(",
"subject",
",",
"message",
",",
"settings",
".",
"DEFAULT_FROM_EMAIL",
",",
"recipient_list",
",",
"fail_silently",
"=",
"True",
",",
"html_message",
"=",
"html_message",
")"
] | Send the email to staff that an comment was posted.
While the django_comments module has email support,
it doesn't pass the 'request' to the context.
This also changes the subject to show the page title. | [
"Send",
"the",
"email",
"to",
"staff",
"that",
"an",
"comment",
"was",
"posted",
"."
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/email.py#L9-L42 |
4,153 | django-fluent/django-fluent-comments | fluent_comments/templatetags/fluent_comments_tags.py | AjaxCommentTags.get_context_data | def get_context_data(self, parent_context, *tag_args, **tag_kwargs):
"""
The main logic for the inclusion node, analogous to ``@register.inclusion_node``.
"""
target_object = tag_args[0] # moved one spot due to .pop(0)
new_context = {
'STATIC_URL': parent_context.get('STATIC_URL', None),
'USE_THREADEDCOMMENTS': appsettings.USE_THREADEDCOMMENTS,
'target_object': target_object,
}
# Be configuration independent:
if new_context['STATIC_URL'] is None:
try:
request = parent_context['request']
except KeyError:
new_context.update({'STATIC_URL': settings.STATIC_URL})
else:
new_context.update(context_processors.static(request))
return new_context | python | def get_context_data(self, parent_context, *tag_args, **tag_kwargs):
target_object = tag_args[0] # moved one spot due to .pop(0)
new_context = {
'STATIC_URL': parent_context.get('STATIC_URL', None),
'USE_THREADEDCOMMENTS': appsettings.USE_THREADEDCOMMENTS,
'target_object': target_object,
}
# Be configuration independent:
if new_context['STATIC_URL'] is None:
try:
request = parent_context['request']
except KeyError:
new_context.update({'STATIC_URL': settings.STATIC_URL})
else:
new_context.update(context_processors.static(request))
return new_context | [
"def",
"get_context_data",
"(",
"self",
",",
"parent_context",
",",
"*",
"tag_args",
",",
"*",
"*",
"tag_kwargs",
")",
":",
"target_object",
"=",
"tag_args",
"[",
"0",
"]",
"# moved one spot due to .pop(0)",
"new_context",
"=",
"{",
"'STATIC_URL'",
":",
"parent_context",
".",
"get",
"(",
"'STATIC_URL'",
",",
"None",
")",
",",
"'USE_THREADEDCOMMENTS'",
":",
"appsettings",
".",
"USE_THREADEDCOMMENTS",
",",
"'target_object'",
":",
"target_object",
",",
"}",
"# Be configuration independent:",
"if",
"new_context",
"[",
"'STATIC_URL'",
"]",
"is",
"None",
":",
"try",
":",
"request",
"=",
"parent_context",
"[",
"'request'",
"]",
"except",
"KeyError",
":",
"new_context",
".",
"update",
"(",
"{",
"'STATIC_URL'",
":",
"settings",
".",
"STATIC_URL",
"}",
")",
"else",
":",
"new_context",
".",
"update",
"(",
"context_processors",
".",
"static",
"(",
"request",
")",
")",
"return",
"new_context"
] | The main logic for the inclusion node, analogous to ``@register.inclusion_node``. | [
"The",
"main",
"logic",
"for",
"the",
"inclusion",
"node",
"analogous",
"to"
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/templatetags/fluent_comments_tags.py#L49-L69 |
4,154 | django-fluent/django-fluent-comments | fluent_comments/utils.py | get_comment_template_name | def get_comment_template_name(comment):
"""
Internal function for the rendering of comments.
"""
ctype = ContentType.objects.get_for_id(comment.content_type_id)
return [
"comments/%s/%s/comment.html" % (ctype.app_label, ctype.model),
"comments/%s/comment.html" % ctype.app_label,
"comments/comment.html"
] | python | def get_comment_template_name(comment):
ctype = ContentType.objects.get_for_id(comment.content_type_id)
return [
"comments/%s/%s/comment.html" % (ctype.app_label, ctype.model),
"comments/%s/comment.html" % ctype.app_label,
"comments/comment.html"
] | [
"def",
"get_comment_template_name",
"(",
"comment",
")",
":",
"ctype",
"=",
"ContentType",
".",
"objects",
".",
"get_for_id",
"(",
"comment",
".",
"content_type_id",
")",
"return",
"[",
"\"comments/%s/%s/comment.html\"",
"%",
"(",
"ctype",
".",
"app_label",
",",
"ctype",
".",
"model",
")",
",",
"\"comments/%s/comment.html\"",
"%",
"ctype",
".",
"app_label",
",",
"\"comments/comment.html\"",
"]"
] | Internal function for the rendering of comments. | [
"Internal",
"function",
"for",
"the",
"rendering",
"of",
"comments",
"."
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/utils.py#L14-L23 |
4,155 | django-fluent/django-fluent-comments | fluent_comments/models.py | get_comments_for_model | def get_comments_for_model(content_object, include_moderated=False):
"""
Return the QuerySet with all comments for a given model.
"""
qs = get_comments_model().objects.for_model(content_object)
if not include_moderated:
qs = qs.filter(is_public=True, is_removed=False)
return qs | python | def get_comments_for_model(content_object, include_moderated=False):
qs = get_comments_model().objects.for_model(content_object)
if not include_moderated:
qs = qs.filter(is_public=True, is_removed=False)
return qs | [
"def",
"get_comments_for_model",
"(",
"content_object",
",",
"include_moderated",
"=",
"False",
")",
":",
"qs",
"=",
"get_comments_model",
"(",
")",
".",
"objects",
".",
"for_model",
"(",
"content_object",
")",
"if",
"not",
"include_moderated",
":",
"qs",
"=",
"qs",
".",
"filter",
"(",
"is_public",
"=",
"True",
",",
"is_removed",
"=",
"False",
")",
"return",
"qs"
] | Return the QuerySet with all comments for a given model. | [
"Return",
"the",
"QuerySet",
"with",
"all",
"comments",
"for",
"a",
"given",
"model",
"."
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/models.py#L36-L45 |
4,156 | django-fluent/django-fluent-comments | fluent_comments/forms/_captcha.py | CaptchaFormMixin._reorder_fields | def _reorder_fields(self, ordering):
"""
Test that the 'captcha' field is really present.
This could be broken by a bad FLUENT_COMMENTS_FIELD_ORDER configuration.
"""
if 'captcha' not in ordering:
raise ImproperlyConfigured(
"When using 'FLUENT_COMMENTS_FIELD_ORDER', "
"make sure the 'captcha' field included too to use '{}' form. ".format(
self.__class__.__name__
)
)
super(CaptchaFormMixin, self)._reorder_fields(ordering)
# Avoid making captcha required for previews.
if self.is_preview:
self.fields.pop('captcha') | python | def _reorder_fields(self, ordering):
if 'captcha' not in ordering:
raise ImproperlyConfigured(
"When using 'FLUENT_COMMENTS_FIELD_ORDER', "
"make sure the 'captcha' field included too to use '{}' form. ".format(
self.__class__.__name__
)
)
super(CaptchaFormMixin, self)._reorder_fields(ordering)
# Avoid making captcha required for previews.
if self.is_preview:
self.fields.pop('captcha') | [
"def",
"_reorder_fields",
"(",
"self",
",",
"ordering",
")",
":",
"if",
"'captcha'",
"not",
"in",
"ordering",
":",
"raise",
"ImproperlyConfigured",
"(",
"\"When using 'FLUENT_COMMENTS_FIELD_ORDER', \"",
"\"make sure the 'captcha' field included too to use '{}' form. \"",
".",
"format",
"(",
"self",
".",
"__class__",
".",
"__name__",
")",
")",
"super",
"(",
"CaptchaFormMixin",
",",
"self",
")",
".",
"_reorder_fields",
"(",
"ordering",
")",
"# Avoid making captcha required for previews.",
"if",
"self",
".",
"is_preview",
":",
"self",
".",
"fields",
".",
"pop",
"(",
"'captcha'",
")"
] | Test that the 'captcha' field is really present.
This could be broken by a bad FLUENT_COMMENTS_FIELD_ORDER configuration. | [
"Test",
"that",
"the",
"captcha",
"field",
"is",
"really",
"present",
".",
"This",
"could",
"be",
"broken",
"by",
"a",
"bad",
"FLUENT_COMMENTS_FIELD_ORDER",
"configuration",
"."
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/forms/_captcha.py#L5-L21 |
4,157 | django-fluent/django-fluent-comments | fluent_comments/views.py | _render_errors | def _render_errors(field):
"""
Render form errors in crispy-forms style.
"""
template = '{0}/layout/field_errors.html'.format(appsettings.CRISPY_TEMPLATE_PACK)
return render_to_string(template, {
'field': field,
'form_show_errors': True,
}) | python | def _render_errors(field):
template = '{0}/layout/field_errors.html'.format(appsettings.CRISPY_TEMPLATE_PACK)
return render_to_string(template, {
'field': field,
'form_show_errors': True,
}) | [
"def",
"_render_errors",
"(",
"field",
")",
":",
"template",
"=",
"'{0}/layout/field_errors.html'",
".",
"format",
"(",
"appsettings",
".",
"CRISPY_TEMPLATE_PACK",
")",
"return",
"render_to_string",
"(",
"template",
",",
"{",
"'field'",
":",
"field",
",",
"'form_show_errors'",
":",
"True",
",",
"}",
")"
] | Render form errors in crispy-forms style. | [
"Render",
"form",
"errors",
"in",
"crispy",
"-",
"forms",
"style",
"."
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/views.py#L154-L162 |
4,158 | django-fluent/django-fluent-comments | fluent_comments/__init__.py | get_form | def get_form():
"""
Return the form to use for commenting.
"""
global form_class
from fluent_comments import appsettings
if form_class is None:
if appsettings.FLUENT_COMMENTS_FORM_CLASS:
from django.utils.module_loading import import_string
form_class = import_string(appsettings.FLUENT_COMMENTS_FORM_CLASS)
else:
from fluent_comments.forms import FluentCommentForm
form_class = FluentCommentForm
return form_class | python | def get_form():
global form_class
from fluent_comments import appsettings
if form_class is None:
if appsettings.FLUENT_COMMENTS_FORM_CLASS:
from django.utils.module_loading import import_string
form_class = import_string(appsettings.FLUENT_COMMENTS_FORM_CLASS)
else:
from fluent_comments.forms import FluentCommentForm
form_class = FluentCommentForm
return form_class | [
"def",
"get_form",
"(",
")",
":",
"global",
"form_class",
"from",
"fluent_comments",
"import",
"appsettings",
"if",
"form_class",
"is",
"None",
":",
"if",
"appsettings",
".",
"FLUENT_COMMENTS_FORM_CLASS",
":",
"from",
"django",
".",
"utils",
".",
"module_loading",
"import",
"import_string",
"form_class",
"=",
"import_string",
"(",
"appsettings",
".",
"FLUENT_COMMENTS_FORM_CLASS",
")",
"else",
":",
"from",
"fluent_comments",
".",
"forms",
"import",
"FluentCommentForm",
"form_class",
"=",
"FluentCommentForm",
"return",
"form_class"
] | Return the form to use for commenting. | [
"Return",
"the",
"form",
"to",
"use",
"for",
"commenting",
"."
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/__init__.py#L26-L40 |
4,159 | django-fluent/django-fluent-comments | fluent_comments/receivers.py | load_default_moderator | def load_default_moderator():
"""
Find a moderator object
"""
if appsettings.FLUENT_COMMENTS_DEFAULT_MODERATOR == 'default':
# Perform spam checks
return moderation.FluentCommentsModerator(None)
elif appsettings.FLUENT_COMMENTS_DEFAULT_MODERATOR == 'deny':
# Deny all comments not from known registered models.
return moderation.AlwaysDeny(None)
elif str(appsettings.FLUENT_COMMENTS_DEFAULT_MODERATOR).lower() == 'none':
# Disables default moderator
return moderation.NullModerator(None)
elif '.' in appsettings.FLUENT_COMMENTS_DEFAULT_MODERATOR:
return import_string(appsettings.FLUENT_COMMENTS_DEFAULT_MODERATOR)(None)
else:
raise ImproperlyConfigured(
"Bad FLUENT_COMMENTS_DEFAULT_MODERATOR value. Provide default/deny/none or a dotted path"
) | python | def load_default_moderator():
if appsettings.FLUENT_COMMENTS_DEFAULT_MODERATOR == 'default':
# Perform spam checks
return moderation.FluentCommentsModerator(None)
elif appsettings.FLUENT_COMMENTS_DEFAULT_MODERATOR == 'deny':
# Deny all comments not from known registered models.
return moderation.AlwaysDeny(None)
elif str(appsettings.FLUENT_COMMENTS_DEFAULT_MODERATOR).lower() == 'none':
# Disables default moderator
return moderation.NullModerator(None)
elif '.' in appsettings.FLUENT_COMMENTS_DEFAULT_MODERATOR:
return import_string(appsettings.FLUENT_COMMENTS_DEFAULT_MODERATOR)(None)
else:
raise ImproperlyConfigured(
"Bad FLUENT_COMMENTS_DEFAULT_MODERATOR value. Provide default/deny/none or a dotted path"
) | [
"def",
"load_default_moderator",
"(",
")",
":",
"if",
"appsettings",
".",
"FLUENT_COMMENTS_DEFAULT_MODERATOR",
"==",
"'default'",
":",
"# Perform spam checks",
"return",
"moderation",
".",
"FluentCommentsModerator",
"(",
"None",
")",
"elif",
"appsettings",
".",
"FLUENT_COMMENTS_DEFAULT_MODERATOR",
"==",
"'deny'",
":",
"# Deny all comments not from known registered models.",
"return",
"moderation",
".",
"AlwaysDeny",
"(",
"None",
")",
"elif",
"str",
"(",
"appsettings",
".",
"FLUENT_COMMENTS_DEFAULT_MODERATOR",
")",
".",
"lower",
"(",
")",
"==",
"'none'",
":",
"# Disables default moderator",
"return",
"moderation",
".",
"NullModerator",
"(",
"None",
")",
"elif",
"'.'",
"in",
"appsettings",
".",
"FLUENT_COMMENTS_DEFAULT_MODERATOR",
":",
"return",
"import_string",
"(",
"appsettings",
".",
"FLUENT_COMMENTS_DEFAULT_MODERATOR",
")",
"(",
"None",
")",
"else",
":",
"raise",
"ImproperlyConfigured",
"(",
"\"Bad FLUENT_COMMENTS_DEFAULT_MODERATOR value. Provide default/deny/none or a dotted path\"",
")"
] | Find a moderator object | [
"Find",
"a",
"moderator",
"object"
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/receivers.py#L24-L42 |
4,160 | django-fluent/django-fluent-comments | fluent_comments/receivers.py | on_comment_will_be_posted | def on_comment_will_be_posted(sender, comment, request, **kwargs):
"""
Make sure both the Ajax and regular comments are checked for moderation.
This signal is also used to link moderators to the comment posting.
"""
content_object = comment.content_object
moderator = moderation.get_model_moderator(content_object.__class__)
if moderator and comment.__class__ is not CommentModel:
# Help with some hard to diagnose problems. The default Django moderator connects
# to the configured comment model. When this model differs from the signal sender,
# the the form stores a different model then COMMENTS_APP provides.
moderator = None
logger.warning(
"Comment of type '%s' was not moderated by '%s', "
"because the parent '%s' has a moderator installed for '%s' instead",
comment.__class__.__name__, moderator.__class__.__name__,
content_object.__class__.__name__, CommentModel.__name__
)
if moderator is None:
logger.info(
"Using default moderator for comment '%s' on parent '%s'",
comment.__class__.__name__, content_object.__class__.__name__
)
_run_default_moderator(comment, content_object, request) | python | def on_comment_will_be_posted(sender, comment, request, **kwargs):
content_object = comment.content_object
moderator = moderation.get_model_moderator(content_object.__class__)
if moderator and comment.__class__ is not CommentModel:
# Help with some hard to diagnose problems. The default Django moderator connects
# to the configured comment model. When this model differs from the signal sender,
# the the form stores a different model then COMMENTS_APP provides.
moderator = None
logger.warning(
"Comment of type '%s' was not moderated by '%s', "
"because the parent '%s' has a moderator installed for '%s' instead",
comment.__class__.__name__, moderator.__class__.__name__,
content_object.__class__.__name__, CommentModel.__name__
)
if moderator is None:
logger.info(
"Using default moderator for comment '%s' on parent '%s'",
comment.__class__.__name__, content_object.__class__.__name__
)
_run_default_moderator(comment, content_object, request) | [
"def",
"on_comment_will_be_posted",
"(",
"sender",
",",
"comment",
",",
"request",
",",
"*",
"*",
"kwargs",
")",
":",
"content_object",
"=",
"comment",
".",
"content_object",
"moderator",
"=",
"moderation",
".",
"get_model_moderator",
"(",
"content_object",
".",
"__class__",
")",
"if",
"moderator",
"and",
"comment",
".",
"__class__",
"is",
"not",
"CommentModel",
":",
"# Help with some hard to diagnose problems. The default Django moderator connects",
"# to the configured comment model. When this model differs from the signal sender,",
"# the the form stores a different model then COMMENTS_APP provides.",
"moderator",
"=",
"None",
"logger",
".",
"warning",
"(",
"\"Comment of type '%s' was not moderated by '%s', \"",
"\"because the parent '%s' has a moderator installed for '%s' instead\"",
",",
"comment",
".",
"__class__",
".",
"__name__",
",",
"moderator",
".",
"__class__",
".",
"__name__",
",",
"content_object",
".",
"__class__",
".",
"__name__",
",",
"CommentModel",
".",
"__name__",
")",
"if",
"moderator",
"is",
"None",
":",
"logger",
".",
"info",
"(",
"\"Using default moderator for comment '%s' on parent '%s'\"",
",",
"comment",
".",
"__class__",
".",
"__name__",
",",
"content_object",
".",
"__class__",
".",
"__name__",
")",
"_run_default_moderator",
"(",
"comment",
",",
"content_object",
",",
"request",
")"
] | Make sure both the Ajax and regular comments are checked for moderation.
This signal is also used to link moderators to the comment posting. | [
"Make",
"sure",
"both",
"the",
"Ajax",
"and",
"regular",
"comments",
"are",
"checked",
"for",
"moderation",
".",
"This",
"signal",
"is",
"also",
"used",
"to",
"link",
"moderators",
"to",
"the",
"comment",
"posting",
"."
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/receivers.py#L50-L74 |
4,161 | django-fluent/django-fluent-comments | fluent_comments/receivers.py | _run_default_moderator | def _run_default_moderator(comment, content_object, request):
"""
Run the default moderator
"""
# The default moderator will likely not check things like "auto close".
# It can still provide akismet and bad word checking.
if not default_moderator.allow(comment, content_object, request):
# Comment will be disallowed outright (HTTP 403 response)
return False
if default_moderator.moderate(comment, content_object, request):
comment.is_public = False | python | def _run_default_moderator(comment, content_object, request):
# The default moderator will likely not check things like "auto close".
# It can still provide akismet and bad word checking.
if not default_moderator.allow(comment, content_object, request):
# Comment will be disallowed outright (HTTP 403 response)
return False
if default_moderator.moderate(comment, content_object, request):
comment.is_public = False | [
"def",
"_run_default_moderator",
"(",
"comment",
",",
"content_object",
",",
"request",
")",
":",
"# The default moderator will likely not check things like \"auto close\".",
"# It can still provide akismet and bad word checking.",
"if",
"not",
"default_moderator",
".",
"allow",
"(",
"comment",
",",
"content_object",
",",
"request",
")",
":",
"# Comment will be disallowed outright (HTTP 403 response)",
"return",
"False",
"if",
"default_moderator",
".",
"moderate",
"(",
"comment",
",",
"content_object",
",",
"request",
")",
":",
"comment",
".",
"is_public",
"=",
"False"
] | Run the default moderator | [
"Run",
"the",
"default",
"moderator"
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/receivers.py#L77-L88 |
4,162 | django-fluent/django-fluent-comments | fluent_comments/akismet.py | akismet_check | def akismet_check(comment, content_object, request):
"""
Connects to Akismet and evaluates to True if Akismet marks this comment as spam.
:rtype: akismet.SpamStatus
"""
# Return previously cached response
akismet_result = getattr(comment, '_akismet_result_', None)
if akismet_result is not None:
return akismet_result
# Get Akismet data
AKISMET_API_KEY = appsettings.AKISMET_API_KEY
if not AKISMET_API_KEY:
raise ImproperlyConfigured('You must set AKISMET_API_KEY to use comment moderation with Akismet.')
current_domain = get_current_site(request).domain
auto_blog_url = '{0}://{1}/'.format(request.is_secure() and 'https' or 'http', current_domain)
blog_url = appsettings.AKISMET_BLOG_URL or auto_blog_url
akismet = Akismet(
AKISMET_API_KEY,
blog=blog_url,
is_test=int(bool(appsettings.AKISMET_IS_TEST)),
application_user_agent='django-fluent-comments/{0}'.format(fluent_comments.__version__),
)
akismet_data = _get_akismet_data(blog_url, comment, content_object, request)
akismet_result = akismet.check(**akismet_data) # raises AkismetServerError when key is invalid
setattr(comment, "_akismet_result_", akismet_result)
return akismet_result | python | def akismet_check(comment, content_object, request):
# Return previously cached response
akismet_result = getattr(comment, '_akismet_result_', None)
if akismet_result is not None:
return akismet_result
# Get Akismet data
AKISMET_API_KEY = appsettings.AKISMET_API_KEY
if not AKISMET_API_KEY:
raise ImproperlyConfigured('You must set AKISMET_API_KEY to use comment moderation with Akismet.')
current_domain = get_current_site(request).domain
auto_blog_url = '{0}://{1}/'.format(request.is_secure() and 'https' or 'http', current_domain)
blog_url = appsettings.AKISMET_BLOG_URL or auto_blog_url
akismet = Akismet(
AKISMET_API_KEY,
blog=blog_url,
is_test=int(bool(appsettings.AKISMET_IS_TEST)),
application_user_agent='django-fluent-comments/{0}'.format(fluent_comments.__version__),
)
akismet_data = _get_akismet_data(blog_url, comment, content_object, request)
akismet_result = akismet.check(**akismet_data) # raises AkismetServerError when key is invalid
setattr(comment, "_akismet_result_", akismet_result)
return akismet_result | [
"def",
"akismet_check",
"(",
"comment",
",",
"content_object",
",",
"request",
")",
":",
"# Return previously cached response",
"akismet_result",
"=",
"getattr",
"(",
"comment",
",",
"'_akismet_result_'",
",",
"None",
")",
"if",
"akismet_result",
"is",
"not",
"None",
":",
"return",
"akismet_result",
"# Get Akismet data",
"AKISMET_API_KEY",
"=",
"appsettings",
".",
"AKISMET_API_KEY",
"if",
"not",
"AKISMET_API_KEY",
":",
"raise",
"ImproperlyConfigured",
"(",
"'You must set AKISMET_API_KEY to use comment moderation with Akismet.'",
")",
"current_domain",
"=",
"get_current_site",
"(",
"request",
")",
".",
"domain",
"auto_blog_url",
"=",
"'{0}://{1}/'",
".",
"format",
"(",
"request",
".",
"is_secure",
"(",
")",
"and",
"'https'",
"or",
"'http'",
",",
"current_domain",
")",
"blog_url",
"=",
"appsettings",
".",
"AKISMET_BLOG_URL",
"or",
"auto_blog_url",
"akismet",
"=",
"Akismet",
"(",
"AKISMET_API_KEY",
",",
"blog",
"=",
"blog_url",
",",
"is_test",
"=",
"int",
"(",
"bool",
"(",
"appsettings",
".",
"AKISMET_IS_TEST",
")",
")",
",",
"application_user_agent",
"=",
"'django-fluent-comments/{0}'",
".",
"format",
"(",
"fluent_comments",
".",
"__version__",
")",
",",
")",
"akismet_data",
"=",
"_get_akismet_data",
"(",
"blog_url",
",",
"comment",
",",
"content_object",
",",
"request",
")",
"akismet_result",
"=",
"akismet",
".",
"check",
"(",
"*",
"*",
"akismet_data",
")",
"# raises AkismetServerError when key is invalid",
"setattr",
"(",
"comment",
",",
"\"_akismet_result_\"",
",",
"akismet_result",
")",
"return",
"akismet_result"
] | Connects to Akismet and evaluates to True if Akismet marks this comment as spam.
:rtype: akismet.SpamStatus | [
"Connects",
"to",
"Akismet",
"and",
"evaluates",
"to",
"True",
"if",
"Akismet",
"marks",
"this",
"comment",
"as",
"spam",
"."
] | bfe98d55b56fedd8ca2e2659eed53a6390e53adf | https://github.com/django-fluent/django-fluent-comments/blob/bfe98d55b56fedd8ca2e2659eed53a6390e53adf/fluent_comments/akismet.py#L17-L47 |
4,163 | DinoTools/python-overpy | overpy/__init__.py | Overpass._handle_remark_msg | def _handle_remark_msg(self, msg):
"""
Try to parse the message provided with the remark tag or element.
:param str msg: The message
:raises overpy.exception.OverpassRuntimeError: If message starts with 'runtime error:'
:raises overpy.exception.OverpassRuntimeRemark: If message starts with 'runtime remark:'
:raises overpy.exception.OverpassUnknownError: If we are unable to identify the error
"""
msg = msg.strip()
if msg.startswith("runtime error:"):
raise exception.OverpassRuntimeError(msg=msg)
elif msg.startswith("runtime remark:"):
raise exception.OverpassRuntimeRemark(msg=msg)
raise exception.OverpassUnknownError(msg=msg) | python | def _handle_remark_msg(self, msg):
msg = msg.strip()
if msg.startswith("runtime error:"):
raise exception.OverpassRuntimeError(msg=msg)
elif msg.startswith("runtime remark:"):
raise exception.OverpassRuntimeRemark(msg=msg)
raise exception.OverpassUnknownError(msg=msg) | [
"def",
"_handle_remark_msg",
"(",
"self",
",",
"msg",
")",
":",
"msg",
"=",
"msg",
".",
"strip",
"(",
")",
"if",
"msg",
".",
"startswith",
"(",
"\"runtime error:\"",
")",
":",
"raise",
"exception",
".",
"OverpassRuntimeError",
"(",
"msg",
"=",
"msg",
")",
"elif",
"msg",
".",
"startswith",
"(",
"\"runtime remark:\"",
")",
":",
"raise",
"exception",
".",
"OverpassRuntimeRemark",
"(",
"msg",
"=",
"msg",
")",
"raise",
"exception",
".",
"OverpassUnknownError",
"(",
"msg",
"=",
"msg",
")"
] | Try to parse the message provided with the remark tag or element.
:param str msg: The message
:raises overpy.exception.OverpassRuntimeError: If message starts with 'runtime error:'
:raises overpy.exception.OverpassRuntimeRemark: If message starts with 'runtime remark:'
:raises overpy.exception.OverpassUnknownError: If we are unable to identify the error | [
"Try",
"to",
"parse",
"the",
"message",
"provided",
"with",
"the",
"remark",
"tag",
"or",
"element",
"."
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L97-L111 |
4,164 | DinoTools/python-overpy | overpy/__init__.py | Overpass.query | def query(self, query):
"""
Query the Overpass API
:param String|Bytes query: The query string in Overpass QL
:return: The parsed result
:rtype: overpy.Result
"""
if not isinstance(query, bytes):
query = query.encode("utf-8")
retry_num = 0
retry_exceptions = []
do_retry = True if self.max_retry_count > 0 else False
while retry_num <= self.max_retry_count:
if retry_num > 0:
time.sleep(self.retry_timeout)
retry_num += 1
try:
f = urlopen(self.url, query)
except HTTPError as e:
f = e
response = f.read(self.read_chunk_size)
while True:
data = f.read(self.read_chunk_size)
if len(data) == 0:
break
response = response + data
f.close()
if f.code == 200:
if PY2:
http_info = f.info()
content_type = http_info.getheader("content-type")
else:
content_type = f.getheader("Content-Type")
if content_type == "application/json":
return self.parse_json(response)
if content_type == "application/osm3s+xml":
return self.parse_xml(response)
e = exception.OverpassUnknownContentType(content_type)
if not do_retry:
raise e
retry_exceptions.append(e)
continue
if f.code == 400:
msgs = []
for msg in self._regex_extract_error_msg.finditer(response):
tmp = self._regex_remove_tag.sub(b"", msg.group("msg"))
try:
tmp = tmp.decode("utf-8")
except UnicodeDecodeError:
tmp = repr(tmp)
msgs.append(tmp)
e = exception.OverpassBadRequest(
query,
msgs=msgs
)
if not do_retry:
raise e
retry_exceptions.append(e)
continue
if f.code == 429:
e = exception.OverpassTooManyRequests
if not do_retry:
raise e
retry_exceptions.append(e)
continue
if f.code == 504:
e = exception.OverpassGatewayTimeout
if not do_retry:
raise e
retry_exceptions.append(e)
continue
e = exception.OverpassUnknownHTTPStatusCode(f.code)
if not do_retry:
raise e
retry_exceptions.append(e)
continue
raise exception.MaxRetriesReached(retry_count=retry_num, exceptions=retry_exceptions) | python | def query(self, query):
if not isinstance(query, bytes):
query = query.encode("utf-8")
retry_num = 0
retry_exceptions = []
do_retry = True if self.max_retry_count > 0 else False
while retry_num <= self.max_retry_count:
if retry_num > 0:
time.sleep(self.retry_timeout)
retry_num += 1
try:
f = urlopen(self.url, query)
except HTTPError as e:
f = e
response = f.read(self.read_chunk_size)
while True:
data = f.read(self.read_chunk_size)
if len(data) == 0:
break
response = response + data
f.close()
if f.code == 200:
if PY2:
http_info = f.info()
content_type = http_info.getheader("content-type")
else:
content_type = f.getheader("Content-Type")
if content_type == "application/json":
return self.parse_json(response)
if content_type == "application/osm3s+xml":
return self.parse_xml(response)
e = exception.OverpassUnknownContentType(content_type)
if not do_retry:
raise e
retry_exceptions.append(e)
continue
if f.code == 400:
msgs = []
for msg in self._regex_extract_error_msg.finditer(response):
tmp = self._regex_remove_tag.sub(b"", msg.group("msg"))
try:
tmp = tmp.decode("utf-8")
except UnicodeDecodeError:
tmp = repr(tmp)
msgs.append(tmp)
e = exception.OverpassBadRequest(
query,
msgs=msgs
)
if not do_retry:
raise e
retry_exceptions.append(e)
continue
if f.code == 429:
e = exception.OverpassTooManyRequests
if not do_retry:
raise e
retry_exceptions.append(e)
continue
if f.code == 504:
e = exception.OverpassGatewayTimeout
if not do_retry:
raise e
retry_exceptions.append(e)
continue
e = exception.OverpassUnknownHTTPStatusCode(f.code)
if not do_retry:
raise e
retry_exceptions.append(e)
continue
raise exception.MaxRetriesReached(retry_count=retry_num, exceptions=retry_exceptions) | [
"def",
"query",
"(",
"self",
",",
"query",
")",
":",
"if",
"not",
"isinstance",
"(",
"query",
",",
"bytes",
")",
":",
"query",
"=",
"query",
".",
"encode",
"(",
"\"utf-8\"",
")",
"retry_num",
"=",
"0",
"retry_exceptions",
"=",
"[",
"]",
"do_retry",
"=",
"True",
"if",
"self",
".",
"max_retry_count",
">",
"0",
"else",
"False",
"while",
"retry_num",
"<=",
"self",
".",
"max_retry_count",
":",
"if",
"retry_num",
">",
"0",
":",
"time",
".",
"sleep",
"(",
"self",
".",
"retry_timeout",
")",
"retry_num",
"+=",
"1",
"try",
":",
"f",
"=",
"urlopen",
"(",
"self",
".",
"url",
",",
"query",
")",
"except",
"HTTPError",
"as",
"e",
":",
"f",
"=",
"e",
"response",
"=",
"f",
".",
"read",
"(",
"self",
".",
"read_chunk_size",
")",
"while",
"True",
":",
"data",
"=",
"f",
".",
"read",
"(",
"self",
".",
"read_chunk_size",
")",
"if",
"len",
"(",
"data",
")",
"==",
"0",
":",
"break",
"response",
"=",
"response",
"+",
"data",
"f",
".",
"close",
"(",
")",
"if",
"f",
".",
"code",
"==",
"200",
":",
"if",
"PY2",
":",
"http_info",
"=",
"f",
".",
"info",
"(",
")",
"content_type",
"=",
"http_info",
".",
"getheader",
"(",
"\"content-type\"",
")",
"else",
":",
"content_type",
"=",
"f",
".",
"getheader",
"(",
"\"Content-Type\"",
")",
"if",
"content_type",
"==",
"\"application/json\"",
":",
"return",
"self",
".",
"parse_json",
"(",
"response",
")",
"if",
"content_type",
"==",
"\"application/osm3s+xml\"",
":",
"return",
"self",
".",
"parse_xml",
"(",
"response",
")",
"e",
"=",
"exception",
".",
"OverpassUnknownContentType",
"(",
"content_type",
")",
"if",
"not",
"do_retry",
":",
"raise",
"e",
"retry_exceptions",
".",
"append",
"(",
"e",
")",
"continue",
"if",
"f",
".",
"code",
"==",
"400",
":",
"msgs",
"=",
"[",
"]",
"for",
"msg",
"in",
"self",
".",
"_regex_extract_error_msg",
".",
"finditer",
"(",
"response",
")",
":",
"tmp",
"=",
"self",
".",
"_regex_remove_tag",
".",
"sub",
"(",
"b\"\"",
",",
"msg",
".",
"group",
"(",
"\"msg\"",
")",
")",
"try",
":",
"tmp",
"=",
"tmp",
".",
"decode",
"(",
"\"utf-8\"",
")",
"except",
"UnicodeDecodeError",
":",
"tmp",
"=",
"repr",
"(",
"tmp",
")",
"msgs",
".",
"append",
"(",
"tmp",
")",
"e",
"=",
"exception",
".",
"OverpassBadRequest",
"(",
"query",
",",
"msgs",
"=",
"msgs",
")",
"if",
"not",
"do_retry",
":",
"raise",
"e",
"retry_exceptions",
".",
"append",
"(",
"e",
")",
"continue",
"if",
"f",
".",
"code",
"==",
"429",
":",
"e",
"=",
"exception",
".",
"OverpassTooManyRequests",
"if",
"not",
"do_retry",
":",
"raise",
"e",
"retry_exceptions",
".",
"append",
"(",
"e",
")",
"continue",
"if",
"f",
".",
"code",
"==",
"504",
":",
"e",
"=",
"exception",
".",
"OverpassGatewayTimeout",
"if",
"not",
"do_retry",
":",
"raise",
"e",
"retry_exceptions",
".",
"append",
"(",
"e",
")",
"continue",
"e",
"=",
"exception",
".",
"OverpassUnknownHTTPStatusCode",
"(",
"f",
".",
"code",
")",
"if",
"not",
"do_retry",
":",
"raise",
"e",
"retry_exceptions",
".",
"append",
"(",
"e",
")",
"continue",
"raise",
"exception",
".",
"MaxRetriesReached",
"(",
"retry_count",
"=",
"retry_num",
",",
"exceptions",
"=",
"retry_exceptions",
")"
] | Query the Overpass API
:param String|Bytes query: The query string in Overpass QL
:return: The parsed result
:rtype: overpy.Result | [
"Query",
"the",
"Overpass",
"API"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L113-L202 |
4,165 | DinoTools/python-overpy | overpy/__init__.py | Overpass.parse_json | def parse_json(self, data, encoding="utf-8"):
"""
Parse raw response from Overpass service.
:param data: Raw JSON Data
:type data: String or Bytes
:param encoding: Encoding to decode byte string
:type encoding: String
:return: Result object
:rtype: overpy.Result
"""
if isinstance(data, bytes):
data = data.decode(encoding)
data = json.loads(data, parse_float=Decimal)
if "remark" in data:
self._handle_remark_msg(msg=data.get("remark"))
return Result.from_json(data, api=self) | python | def parse_json(self, data, encoding="utf-8"):
if isinstance(data, bytes):
data = data.decode(encoding)
data = json.loads(data, parse_float=Decimal)
if "remark" in data:
self._handle_remark_msg(msg=data.get("remark"))
return Result.from_json(data, api=self) | [
"def",
"parse_json",
"(",
"self",
",",
"data",
",",
"encoding",
"=",
"\"utf-8\"",
")",
":",
"if",
"isinstance",
"(",
"data",
",",
"bytes",
")",
":",
"data",
"=",
"data",
".",
"decode",
"(",
"encoding",
")",
"data",
"=",
"json",
".",
"loads",
"(",
"data",
",",
"parse_float",
"=",
"Decimal",
")",
"if",
"\"remark\"",
"in",
"data",
":",
"self",
".",
"_handle_remark_msg",
"(",
"msg",
"=",
"data",
".",
"get",
"(",
"\"remark\"",
")",
")",
"return",
"Result",
".",
"from_json",
"(",
"data",
",",
"api",
"=",
"self",
")"
] | Parse raw response from Overpass service.
:param data: Raw JSON Data
:type data: String or Bytes
:param encoding: Encoding to decode byte string
:type encoding: String
:return: Result object
:rtype: overpy.Result | [
"Parse",
"raw",
"response",
"from",
"Overpass",
"service",
"."
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L204-L220 |
4,166 | DinoTools/python-overpy | overpy/__init__.py | Result.expand | def expand(self, other):
"""
Add all elements from an other result to the list of elements of this result object.
It is used by the auto resolve feature.
:param other: Expand the result with the elements from this result.
:type other: overpy.Result
:raises ValueError: If provided parameter is not instance of :class:`overpy.Result`
"""
if not isinstance(other, Result):
raise ValueError("Provided argument has to be instance of overpy:Result()")
other_collection_map = {Node: other.nodes, Way: other.ways, Relation: other.relations, Area: other.areas}
for element_type, own_collection in self._class_collection_map.items():
for element in other_collection_map[element_type]:
if is_valid_type(element, element_type) and element.id not in own_collection:
own_collection[element.id] = element | python | def expand(self, other):
if not isinstance(other, Result):
raise ValueError("Provided argument has to be instance of overpy:Result()")
other_collection_map = {Node: other.nodes, Way: other.ways, Relation: other.relations, Area: other.areas}
for element_type, own_collection in self._class_collection_map.items():
for element in other_collection_map[element_type]:
if is_valid_type(element, element_type) and element.id not in own_collection:
own_collection[element.id] = element | [
"def",
"expand",
"(",
"self",
",",
"other",
")",
":",
"if",
"not",
"isinstance",
"(",
"other",
",",
"Result",
")",
":",
"raise",
"ValueError",
"(",
"\"Provided argument has to be instance of overpy:Result()\"",
")",
"other_collection_map",
"=",
"{",
"Node",
":",
"other",
".",
"nodes",
",",
"Way",
":",
"other",
".",
"ways",
",",
"Relation",
":",
"other",
".",
"relations",
",",
"Area",
":",
"other",
".",
"areas",
"}",
"for",
"element_type",
",",
"own_collection",
"in",
"self",
".",
"_class_collection_map",
".",
"items",
"(",
")",
":",
"for",
"element",
"in",
"other_collection_map",
"[",
"element_type",
"]",
":",
"if",
"is_valid_type",
"(",
"element",
",",
"element_type",
")",
"and",
"element",
".",
"id",
"not",
"in",
"own_collection",
":",
"own_collection",
"[",
"element",
".",
"id",
"]",
"=",
"element"
] | Add all elements from an other result to the list of elements of this result object.
It is used by the auto resolve feature.
:param other: Expand the result with the elements from this result.
:type other: overpy.Result
:raises ValueError: If provided parameter is not instance of :class:`overpy.Result` | [
"Add",
"all",
"elements",
"from",
"an",
"other",
"result",
"to",
"the",
"list",
"of",
"elements",
"of",
"this",
"result",
"object",
"."
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L270-L287 |
4,167 | DinoTools/python-overpy | overpy/__init__.py | Result.append | def append(self, element):
"""
Append a new element to the result.
:param element: The element to append
:type element: overpy.Element
"""
if is_valid_type(element, Element):
self._class_collection_map[element.__class__].setdefault(element.id, element) | python | def append(self, element):
if is_valid_type(element, Element):
self._class_collection_map[element.__class__].setdefault(element.id, element) | [
"def",
"append",
"(",
"self",
",",
"element",
")",
":",
"if",
"is_valid_type",
"(",
"element",
",",
"Element",
")",
":",
"self",
".",
"_class_collection_map",
"[",
"element",
".",
"__class__",
"]",
".",
"setdefault",
"(",
"element",
".",
"id",
",",
"element",
")"
] | Append a new element to the result.
:param element: The element to append
:type element: overpy.Element | [
"Append",
"a",
"new",
"element",
"to",
"the",
"result",
"."
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L289-L297 |
4,168 | DinoTools/python-overpy | overpy/__init__.py | Result.get_elements | def get_elements(self, filter_cls, elem_id=None):
"""
Get a list of elements from the result and filter the element type by a class.
:param filter_cls:
:param elem_id: ID of the object
:type elem_id: Integer
:return: List of available elements
:rtype: List
"""
result = []
if elem_id is not None:
try:
result = [self._class_collection_map[filter_cls][elem_id]]
except KeyError:
result = []
else:
for e in self._class_collection_map[filter_cls].values():
result.append(e)
return result | python | def get_elements(self, filter_cls, elem_id=None):
result = []
if elem_id is not None:
try:
result = [self._class_collection_map[filter_cls][elem_id]]
except KeyError:
result = []
else:
for e in self._class_collection_map[filter_cls].values():
result.append(e)
return result | [
"def",
"get_elements",
"(",
"self",
",",
"filter_cls",
",",
"elem_id",
"=",
"None",
")",
":",
"result",
"=",
"[",
"]",
"if",
"elem_id",
"is",
"not",
"None",
":",
"try",
":",
"result",
"=",
"[",
"self",
".",
"_class_collection_map",
"[",
"filter_cls",
"]",
"[",
"elem_id",
"]",
"]",
"except",
"KeyError",
":",
"result",
"=",
"[",
"]",
"else",
":",
"for",
"e",
"in",
"self",
".",
"_class_collection_map",
"[",
"filter_cls",
"]",
".",
"values",
"(",
")",
":",
"result",
".",
"append",
"(",
"e",
")",
"return",
"result"
] | Get a list of elements from the result and filter the element type by a class.
:param filter_cls:
:param elem_id: ID of the object
:type elem_id: Integer
:return: List of available elements
:rtype: List | [
"Get",
"a",
"list",
"of",
"elements",
"from",
"the",
"result",
"and",
"filter",
"the",
"element",
"type",
"by",
"a",
"class",
"."
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L299-L318 |
4,169 | DinoTools/python-overpy | overpy/__init__.py | Result.from_json | def from_json(cls, data, api=None):
"""
Create a new instance and load data from json object.
:param data: JSON data returned by the Overpass API
:type data: Dict
:param api:
:type api: overpy.Overpass
:return: New instance of Result object
:rtype: overpy.Result
"""
result = cls(api=api)
for elem_cls in [Node, Way, Relation, Area]:
for element in data.get("elements", []):
e_type = element.get("type")
if hasattr(e_type, "lower") and e_type.lower() == elem_cls._type_value:
result.append(elem_cls.from_json(element, result=result))
return result | python | def from_json(cls, data, api=None):
result = cls(api=api)
for elem_cls in [Node, Way, Relation, Area]:
for element in data.get("elements", []):
e_type = element.get("type")
if hasattr(e_type, "lower") and e_type.lower() == elem_cls._type_value:
result.append(elem_cls.from_json(element, result=result))
return result | [
"def",
"from_json",
"(",
"cls",
",",
"data",
",",
"api",
"=",
"None",
")",
":",
"result",
"=",
"cls",
"(",
"api",
"=",
"api",
")",
"for",
"elem_cls",
"in",
"[",
"Node",
",",
"Way",
",",
"Relation",
",",
"Area",
"]",
":",
"for",
"element",
"in",
"data",
".",
"get",
"(",
"\"elements\"",
",",
"[",
"]",
")",
":",
"e_type",
"=",
"element",
".",
"get",
"(",
"\"type\"",
")",
"if",
"hasattr",
"(",
"e_type",
",",
"\"lower\"",
")",
"and",
"e_type",
".",
"lower",
"(",
")",
"==",
"elem_cls",
".",
"_type_value",
":",
"result",
".",
"append",
"(",
"elem_cls",
".",
"from_json",
"(",
"element",
",",
"result",
"=",
"result",
")",
")",
"return",
"result"
] | Create a new instance and load data from json object.
:param data: JSON data returned by the Overpass API
:type data: Dict
:param api:
:type api: overpy.Overpass
:return: New instance of Result object
:rtype: overpy.Result | [
"Create",
"a",
"new",
"instance",
"and",
"load",
"data",
"from",
"json",
"object",
"."
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L341-L359 |
4,170 | DinoTools/python-overpy | overpy/__init__.py | Result.get_area | def get_area(self, area_id, resolve_missing=False):
"""
Get an area by its ID.
:param area_id: The area ID
:type area_id: Integer
:param resolve_missing: Query the Overpass API if the area is missing in the result set.
:return: The area
:rtype: overpy.Area
:raises overpy.exception.DataIncomplete: The requested way is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and the area can't be resolved.
"""
areas = self.get_areas(area_id=area_id)
if len(areas) == 0:
if resolve_missing is False:
raise exception.DataIncomplete("Resolve missing area is disabled")
query = ("\n"
"[out:json];\n"
"area({area_id});\n"
"out body;\n"
)
query = query.format(
area_id=area_id
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
areas = self.get_areas(area_id=area_id)
if len(areas) == 0:
raise exception.DataIncomplete("Unable to resolve requested areas")
return areas[0] | python | def get_area(self, area_id, resolve_missing=False):
areas = self.get_areas(area_id=area_id)
if len(areas) == 0:
if resolve_missing is False:
raise exception.DataIncomplete("Resolve missing area is disabled")
query = ("\n"
"[out:json];\n"
"area({area_id});\n"
"out body;\n"
)
query = query.format(
area_id=area_id
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
areas = self.get_areas(area_id=area_id)
if len(areas) == 0:
raise exception.DataIncomplete("Unable to resolve requested areas")
return areas[0] | [
"def",
"get_area",
"(",
"self",
",",
"area_id",
",",
"resolve_missing",
"=",
"False",
")",
":",
"areas",
"=",
"self",
".",
"get_areas",
"(",
"area_id",
"=",
"area_id",
")",
"if",
"len",
"(",
"areas",
")",
"==",
"0",
":",
"if",
"resolve_missing",
"is",
"False",
":",
"raise",
"exception",
".",
"DataIncomplete",
"(",
"\"Resolve missing area is disabled\"",
")",
"query",
"=",
"(",
"\"\\n\"",
"\"[out:json];\\n\"",
"\"area({area_id});\\n\"",
"\"out body;\\n\"",
")",
"query",
"=",
"query",
".",
"format",
"(",
"area_id",
"=",
"area_id",
")",
"tmp_result",
"=",
"self",
".",
"api",
".",
"query",
"(",
"query",
")",
"self",
".",
"expand",
"(",
"tmp_result",
")",
"areas",
"=",
"self",
".",
"get_areas",
"(",
"area_id",
"=",
"area_id",
")",
"if",
"len",
"(",
"areas",
")",
"==",
"0",
":",
"raise",
"exception",
".",
"DataIncomplete",
"(",
"\"Unable to resolve requested areas\"",
")",
"return",
"areas",
"[",
"0",
"]"
] | Get an area by its ID.
:param area_id: The area ID
:type area_id: Integer
:param resolve_missing: Query the Overpass API if the area is missing in the result set.
:return: The area
:rtype: overpy.Area
:raises overpy.exception.DataIncomplete: The requested way is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and the area can't be resolved. | [
"Get",
"an",
"area",
"by",
"its",
"ID",
"."
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L416-L449 |
4,171 | DinoTools/python-overpy | overpy/__init__.py | Result.get_node | def get_node(self, node_id, resolve_missing=False):
"""
Get a node by its ID.
:param node_id: The node ID
:type node_id: Integer
:param resolve_missing: Query the Overpass API if the node is missing in the result set.
:return: The node
:rtype: overpy.Node
:raises overpy.exception.DataIncomplete: At least one referenced node is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and at least one node can't be resolved.
"""
nodes = self.get_nodes(node_id=node_id)
if len(nodes) == 0:
if not resolve_missing:
raise exception.DataIncomplete("Resolve missing nodes is disabled")
query = ("\n"
"[out:json];\n"
"node({node_id});\n"
"out body;\n"
)
query = query.format(
node_id=node_id
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
nodes = self.get_nodes(node_id=node_id)
if len(nodes) == 0:
raise exception.DataIncomplete("Unable to resolve all nodes")
return nodes[0] | python | def get_node(self, node_id, resolve_missing=False):
nodes = self.get_nodes(node_id=node_id)
if len(nodes) == 0:
if not resolve_missing:
raise exception.DataIncomplete("Resolve missing nodes is disabled")
query = ("\n"
"[out:json];\n"
"node({node_id});\n"
"out body;\n"
)
query = query.format(
node_id=node_id
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
nodes = self.get_nodes(node_id=node_id)
if len(nodes) == 0:
raise exception.DataIncomplete("Unable to resolve all nodes")
return nodes[0] | [
"def",
"get_node",
"(",
"self",
",",
"node_id",
",",
"resolve_missing",
"=",
"False",
")",
":",
"nodes",
"=",
"self",
".",
"get_nodes",
"(",
"node_id",
"=",
"node_id",
")",
"if",
"len",
"(",
"nodes",
")",
"==",
"0",
":",
"if",
"not",
"resolve_missing",
":",
"raise",
"exception",
".",
"DataIncomplete",
"(",
"\"Resolve missing nodes is disabled\"",
")",
"query",
"=",
"(",
"\"\\n\"",
"\"[out:json];\\n\"",
"\"node({node_id});\\n\"",
"\"out body;\\n\"",
")",
"query",
"=",
"query",
".",
"format",
"(",
"node_id",
"=",
"node_id",
")",
"tmp_result",
"=",
"self",
".",
"api",
".",
"query",
"(",
"query",
")",
"self",
".",
"expand",
"(",
"tmp_result",
")",
"nodes",
"=",
"self",
".",
"get_nodes",
"(",
"node_id",
"=",
"node_id",
")",
"if",
"len",
"(",
"nodes",
")",
"==",
"0",
":",
"raise",
"exception",
".",
"DataIncomplete",
"(",
"\"Unable to resolve all nodes\"",
")",
"return",
"nodes",
"[",
"0",
"]"
] | Get a node by its ID.
:param node_id: The node ID
:type node_id: Integer
:param resolve_missing: Query the Overpass API if the node is missing in the result set.
:return: The node
:rtype: overpy.Node
:raises overpy.exception.DataIncomplete: At least one referenced node is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and at least one node can't be resolved. | [
"Get",
"a",
"node",
"by",
"its",
"ID",
"."
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L461-L494 |
4,172 | DinoTools/python-overpy | overpy/__init__.py | Result.get_relation | def get_relation(self, rel_id, resolve_missing=False):
"""
Get a relation by its ID.
:param rel_id: The relation ID
:type rel_id: Integer
:param resolve_missing: Query the Overpass API if the relation is missing in the result set.
:return: The relation
:rtype: overpy.Relation
:raises overpy.exception.DataIncomplete: The requested relation is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and the relation can't be resolved.
"""
relations = self.get_relations(rel_id=rel_id)
if len(relations) == 0:
if resolve_missing is False:
raise exception.DataIncomplete("Resolve missing relations is disabled")
query = ("\n"
"[out:json];\n"
"relation({relation_id});\n"
"out body;\n"
)
query = query.format(
relation_id=rel_id
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
relations = self.get_relations(rel_id=rel_id)
if len(relations) == 0:
raise exception.DataIncomplete("Unable to resolve requested reference")
return relations[0] | python | def get_relation(self, rel_id, resolve_missing=False):
relations = self.get_relations(rel_id=rel_id)
if len(relations) == 0:
if resolve_missing is False:
raise exception.DataIncomplete("Resolve missing relations is disabled")
query = ("\n"
"[out:json];\n"
"relation({relation_id});\n"
"out body;\n"
)
query = query.format(
relation_id=rel_id
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
relations = self.get_relations(rel_id=rel_id)
if len(relations) == 0:
raise exception.DataIncomplete("Unable to resolve requested reference")
return relations[0] | [
"def",
"get_relation",
"(",
"self",
",",
"rel_id",
",",
"resolve_missing",
"=",
"False",
")",
":",
"relations",
"=",
"self",
".",
"get_relations",
"(",
"rel_id",
"=",
"rel_id",
")",
"if",
"len",
"(",
"relations",
")",
"==",
"0",
":",
"if",
"resolve_missing",
"is",
"False",
":",
"raise",
"exception",
".",
"DataIncomplete",
"(",
"\"Resolve missing relations is disabled\"",
")",
"query",
"=",
"(",
"\"\\n\"",
"\"[out:json];\\n\"",
"\"relation({relation_id});\\n\"",
"\"out body;\\n\"",
")",
"query",
"=",
"query",
".",
"format",
"(",
"relation_id",
"=",
"rel_id",
")",
"tmp_result",
"=",
"self",
".",
"api",
".",
"query",
"(",
"query",
")",
"self",
".",
"expand",
"(",
"tmp_result",
")",
"relations",
"=",
"self",
".",
"get_relations",
"(",
"rel_id",
"=",
"rel_id",
")",
"if",
"len",
"(",
"relations",
")",
"==",
"0",
":",
"raise",
"exception",
".",
"DataIncomplete",
"(",
"\"Unable to resolve requested reference\"",
")",
"return",
"relations",
"[",
"0",
"]"
] | Get a relation by its ID.
:param rel_id: The relation ID
:type rel_id: Integer
:param resolve_missing: Query the Overpass API if the relation is missing in the result set.
:return: The relation
:rtype: overpy.Relation
:raises overpy.exception.DataIncomplete: The requested relation is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and the relation can't be resolved. | [
"Get",
"a",
"relation",
"by",
"its",
"ID",
"."
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L506-L539 |
4,173 | DinoTools/python-overpy | overpy/__init__.py | Result.get_way | def get_way(self, way_id, resolve_missing=False):
"""
Get a way by its ID.
:param way_id: The way ID
:type way_id: Integer
:param resolve_missing: Query the Overpass API if the way is missing in the result set.
:return: The way
:rtype: overpy.Way
:raises overpy.exception.DataIncomplete: The requested way is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and the way can't be resolved.
"""
ways = self.get_ways(way_id=way_id)
if len(ways) == 0:
if resolve_missing is False:
raise exception.DataIncomplete("Resolve missing way is disabled")
query = ("\n"
"[out:json];\n"
"way({way_id});\n"
"out body;\n"
)
query = query.format(
way_id=way_id
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
ways = self.get_ways(way_id=way_id)
if len(ways) == 0:
raise exception.DataIncomplete("Unable to resolve requested way")
return ways[0] | python | def get_way(self, way_id, resolve_missing=False):
ways = self.get_ways(way_id=way_id)
if len(ways) == 0:
if resolve_missing is False:
raise exception.DataIncomplete("Resolve missing way is disabled")
query = ("\n"
"[out:json];\n"
"way({way_id});\n"
"out body;\n"
)
query = query.format(
way_id=way_id
)
tmp_result = self.api.query(query)
self.expand(tmp_result)
ways = self.get_ways(way_id=way_id)
if len(ways) == 0:
raise exception.DataIncomplete("Unable to resolve requested way")
return ways[0] | [
"def",
"get_way",
"(",
"self",
",",
"way_id",
",",
"resolve_missing",
"=",
"False",
")",
":",
"ways",
"=",
"self",
".",
"get_ways",
"(",
"way_id",
"=",
"way_id",
")",
"if",
"len",
"(",
"ways",
")",
"==",
"0",
":",
"if",
"resolve_missing",
"is",
"False",
":",
"raise",
"exception",
".",
"DataIncomplete",
"(",
"\"Resolve missing way is disabled\"",
")",
"query",
"=",
"(",
"\"\\n\"",
"\"[out:json];\\n\"",
"\"way({way_id});\\n\"",
"\"out body;\\n\"",
")",
"query",
"=",
"query",
".",
"format",
"(",
"way_id",
"=",
"way_id",
")",
"tmp_result",
"=",
"self",
".",
"api",
".",
"query",
"(",
"query",
")",
"self",
".",
"expand",
"(",
"tmp_result",
")",
"ways",
"=",
"self",
".",
"get_ways",
"(",
"way_id",
"=",
"way_id",
")",
"if",
"len",
"(",
"ways",
")",
"==",
"0",
":",
"raise",
"exception",
".",
"DataIncomplete",
"(",
"\"Unable to resolve requested way\"",
")",
"return",
"ways",
"[",
"0",
"]"
] | Get a way by its ID.
:param way_id: The way ID
:type way_id: Integer
:param resolve_missing: Query the Overpass API if the way is missing in the result set.
:return: The way
:rtype: overpy.Way
:raises overpy.exception.DataIncomplete: The requested way is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and the way can't be resolved. | [
"Get",
"a",
"way",
"by",
"its",
"ID",
"."
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L551-L584 |
4,174 | DinoTools/python-overpy | overpy/__init__.py | Element.get_center_from_json | def get_center_from_json(cls, data):
"""
Get center information from json data
:param data: json data
:return: tuple with two elements: lat and lon
:rtype: tuple
"""
center_lat = None
center_lon = None
center = data.get("center")
if isinstance(center, dict):
center_lat = center.get("lat")
center_lon = center.get("lon")
if center_lat is None or center_lon is None:
raise ValueError("Unable to get lat or lon of way center.")
center_lat = Decimal(center_lat)
center_lon = Decimal(center_lon)
return (center_lat, center_lon) | python | def get_center_from_json(cls, data):
center_lat = None
center_lon = None
center = data.get("center")
if isinstance(center, dict):
center_lat = center.get("lat")
center_lon = center.get("lon")
if center_lat is None or center_lon is None:
raise ValueError("Unable to get lat or lon of way center.")
center_lat = Decimal(center_lat)
center_lon = Decimal(center_lon)
return (center_lat, center_lon) | [
"def",
"get_center_from_json",
"(",
"cls",
",",
"data",
")",
":",
"center_lat",
"=",
"None",
"center_lon",
"=",
"None",
"center",
"=",
"data",
".",
"get",
"(",
"\"center\"",
")",
"if",
"isinstance",
"(",
"center",
",",
"dict",
")",
":",
"center_lat",
"=",
"center",
".",
"get",
"(",
"\"lat\"",
")",
"center_lon",
"=",
"center",
".",
"get",
"(",
"\"lon\"",
")",
"if",
"center_lat",
"is",
"None",
"or",
"center_lon",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"\"Unable to get lat or lon of way center.\"",
")",
"center_lat",
"=",
"Decimal",
"(",
"center_lat",
")",
"center_lon",
"=",
"Decimal",
"(",
"center_lon",
")",
"return",
"(",
"center_lat",
",",
"center_lon",
")"
] | Get center information from json data
:param data: json data
:return: tuple with two elements: lat and lon
:rtype: tuple | [
"Get",
"center",
"information",
"from",
"json",
"data"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L631-L649 |
4,175 | DinoTools/python-overpy | overpy/__init__.py | Node.from_json | def from_json(cls, data, result=None):
"""
Create new Node element from JSON data
:param data: Element data from JSON
:type data: Dict
:param result: The result this element belongs to
:type result: overpy.Result
:return: New instance of Node
:rtype: overpy.Node
:raises overpy.exception.ElementDataWrongType: If type value of the passed JSON data does not match.
"""
if data.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=data.get("type")
)
tags = data.get("tags", {})
node_id = data.get("id")
lat = data.get("lat")
lon = data.get("lon")
attributes = {}
ignore = ["type", "id", "lat", "lon", "tags"]
for n, v in data.items():
if n in ignore:
continue
attributes[n] = v
return cls(node_id=node_id, lat=lat, lon=lon, tags=tags, attributes=attributes, result=result) | python | def from_json(cls, data, result=None):
if data.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=data.get("type")
)
tags = data.get("tags", {})
node_id = data.get("id")
lat = data.get("lat")
lon = data.get("lon")
attributes = {}
ignore = ["type", "id", "lat", "lon", "tags"]
for n, v in data.items():
if n in ignore:
continue
attributes[n] = v
return cls(node_id=node_id, lat=lat, lon=lon, tags=tags, attributes=attributes, result=result) | [
"def",
"from_json",
"(",
"cls",
",",
"data",
",",
"result",
"=",
"None",
")",
":",
"if",
"data",
".",
"get",
"(",
"\"type\"",
")",
"!=",
"cls",
".",
"_type_value",
":",
"raise",
"exception",
".",
"ElementDataWrongType",
"(",
"type_expected",
"=",
"cls",
".",
"_type_value",
",",
"type_provided",
"=",
"data",
".",
"get",
"(",
"\"type\"",
")",
")",
"tags",
"=",
"data",
".",
"get",
"(",
"\"tags\"",
",",
"{",
"}",
")",
"node_id",
"=",
"data",
".",
"get",
"(",
"\"id\"",
")",
"lat",
"=",
"data",
".",
"get",
"(",
"\"lat\"",
")",
"lon",
"=",
"data",
".",
"get",
"(",
"\"lon\"",
")",
"attributes",
"=",
"{",
"}",
"ignore",
"=",
"[",
"\"type\"",
",",
"\"id\"",
",",
"\"lat\"",
",",
"\"lon\"",
",",
"\"tags\"",
"]",
"for",
"n",
",",
"v",
"in",
"data",
".",
"items",
"(",
")",
":",
"if",
"n",
"in",
"ignore",
":",
"continue",
"attributes",
"[",
"n",
"]",
"=",
"v",
"return",
"cls",
"(",
"node_id",
"=",
"node_id",
",",
"lat",
"=",
"lat",
",",
"lon",
"=",
"lon",
",",
"tags",
"=",
"tags",
",",
"attributes",
"=",
"attributes",
",",
"result",
"=",
"result",
")"
] | Create new Node element from JSON data
:param data: Element data from JSON
:type data: Dict
:param result: The result this element belongs to
:type result: overpy.Result
:return: New instance of Node
:rtype: overpy.Node
:raises overpy.exception.ElementDataWrongType: If type value of the passed JSON data does not match. | [
"Create",
"new",
"Node",
"element",
"from",
"JSON",
"data"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L788-L819 |
4,176 | DinoTools/python-overpy | overpy/__init__.py | Way.get_nodes | def get_nodes(self, resolve_missing=False):
"""
Get the nodes defining the geometry of the way
:param resolve_missing: Try to resolve missing nodes.
:type resolve_missing: Boolean
:return: List of nodes
:rtype: List of overpy.Node
:raises overpy.exception.DataIncomplete: At least one referenced node is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and at least one node can't be resolved.
"""
result = []
resolved = False
for node_id in self._node_ids:
try:
node = self._result.get_node(node_id)
except exception.DataIncomplete:
node = None
if node is not None:
result.append(node)
continue
if not resolve_missing:
raise exception.DataIncomplete("Resolve missing nodes is disabled")
# We tried to resolve the data but some nodes are still missing
if resolved:
raise exception.DataIncomplete("Unable to resolve all nodes")
query = ("\n"
"[out:json];\n"
"way({way_id});\n"
"node(w);\n"
"out body;\n"
)
query = query.format(
way_id=self.id
)
tmp_result = self._result.api.query(query)
self._result.expand(tmp_result)
resolved = True
try:
node = self._result.get_node(node_id)
except exception.DataIncomplete:
node = None
if node is None:
raise exception.DataIncomplete("Unable to resolve all nodes")
result.append(node)
return result | python | def get_nodes(self, resolve_missing=False):
result = []
resolved = False
for node_id in self._node_ids:
try:
node = self._result.get_node(node_id)
except exception.DataIncomplete:
node = None
if node is not None:
result.append(node)
continue
if not resolve_missing:
raise exception.DataIncomplete("Resolve missing nodes is disabled")
# We tried to resolve the data but some nodes are still missing
if resolved:
raise exception.DataIncomplete("Unable to resolve all nodes")
query = ("\n"
"[out:json];\n"
"way({way_id});\n"
"node(w);\n"
"out body;\n"
)
query = query.format(
way_id=self.id
)
tmp_result = self._result.api.query(query)
self._result.expand(tmp_result)
resolved = True
try:
node = self._result.get_node(node_id)
except exception.DataIncomplete:
node = None
if node is None:
raise exception.DataIncomplete("Unable to resolve all nodes")
result.append(node)
return result | [
"def",
"get_nodes",
"(",
"self",
",",
"resolve_missing",
"=",
"False",
")",
":",
"result",
"=",
"[",
"]",
"resolved",
"=",
"False",
"for",
"node_id",
"in",
"self",
".",
"_node_ids",
":",
"try",
":",
"node",
"=",
"self",
".",
"_result",
".",
"get_node",
"(",
"node_id",
")",
"except",
"exception",
".",
"DataIncomplete",
":",
"node",
"=",
"None",
"if",
"node",
"is",
"not",
"None",
":",
"result",
".",
"append",
"(",
"node",
")",
"continue",
"if",
"not",
"resolve_missing",
":",
"raise",
"exception",
".",
"DataIncomplete",
"(",
"\"Resolve missing nodes is disabled\"",
")",
"# We tried to resolve the data but some nodes are still missing",
"if",
"resolved",
":",
"raise",
"exception",
".",
"DataIncomplete",
"(",
"\"Unable to resolve all nodes\"",
")",
"query",
"=",
"(",
"\"\\n\"",
"\"[out:json];\\n\"",
"\"way({way_id});\\n\"",
"\"node(w);\\n\"",
"\"out body;\\n\"",
")",
"query",
"=",
"query",
".",
"format",
"(",
"way_id",
"=",
"self",
".",
"id",
")",
"tmp_result",
"=",
"self",
".",
"_result",
".",
"api",
".",
"query",
"(",
"query",
")",
"self",
".",
"_result",
".",
"expand",
"(",
"tmp_result",
")",
"resolved",
"=",
"True",
"try",
":",
"node",
"=",
"self",
".",
"_result",
".",
"get_node",
"(",
"node_id",
")",
"except",
"exception",
".",
"DataIncomplete",
":",
"node",
"=",
"None",
"if",
"node",
"is",
"None",
":",
"raise",
"exception",
".",
"DataIncomplete",
"(",
"\"Unable to resolve all nodes\"",
")",
"result",
".",
"append",
"(",
"node",
")",
"return",
"result"
] | Get the nodes defining the geometry of the way
:param resolve_missing: Try to resolve missing nodes.
:type resolve_missing: Boolean
:return: List of nodes
:rtype: List of overpy.Node
:raises overpy.exception.DataIncomplete: At least one referenced node is not available in the result cache.
:raises overpy.exception.DataIncomplete: If resolve_missing is True and at least one node can't be resolved. | [
"Get",
"the",
"nodes",
"defining",
"the",
"geometry",
"of",
"the",
"way"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L909-L963 |
4,177 | DinoTools/python-overpy | overpy/__init__.py | Way.from_json | def from_json(cls, data, result=None):
"""
Create new Way element from JSON data
:param data: Element data from JSON
:type data: Dict
:param result: The result this element belongs to
:type result: overpy.Result
:return: New instance of Way
:rtype: overpy.Way
:raises overpy.exception.ElementDataWrongType: If type value of the passed JSON data does not match.
"""
if data.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=data.get("type")
)
tags = data.get("tags", {})
way_id = data.get("id")
node_ids = data.get("nodes")
(center_lat, center_lon) = cls.get_center_from_json(data=data)
attributes = {}
ignore = ["center", "id", "nodes", "tags", "type"]
for n, v in data.items():
if n in ignore:
continue
attributes[n] = v
return cls(
attributes=attributes,
center_lat=center_lat,
center_lon=center_lon,
node_ids=node_ids,
tags=tags,
result=result,
way_id=way_id
) | python | def from_json(cls, data, result=None):
if data.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=data.get("type")
)
tags = data.get("tags", {})
way_id = data.get("id")
node_ids = data.get("nodes")
(center_lat, center_lon) = cls.get_center_from_json(data=data)
attributes = {}
ignore = ["center", "id", "nodes", "tags", "type"]
for n, v in data.items():
if n in ignore:
continue
attributes[n] = v
return cls(
attributes=attributes,
center_lat=center_lat,
center_lon=center_lon,
node_ids=node_ids,
tags=tags,
result=result,
way_id=way_id
) | [
"def",
"from_json",
"(",
"cls",
",",
"data",
",",
"result",
"=",
"None",
")",
":",
"if",
"data",
".",
"get",
"(",
"\"type\"",
")",
"!=",
"cls",
".",
"_type_value",
":",
"raise",
"exception",
".",
"ElementDataWrongType",
"(",
"type_expected",
"=",
"cls",
".",
"_type_value",
",",
"type_provided",
"=",
"data",
".",
"get",
"(",
"\"type\"",
")",
")",
"tags",
"=",
"data",
".",
"get",
"(",
"\"tags\"",
",",
"{",
"}",
")",
"way_id",
"=",
"data",
".",
"get",
"(",
"\"id\"",
")",
"node_ids",
"=",
"data",
".",
"get",
"(",
"\"nodes\"",
")",
"(",
"center_lat",
",",
"center_lon",
")",
"=",
"cls",
".",
"get_center_from_json",
"(",
"data",
"=",
"data",
")",
"attributes",
"=",
"{",
"}",
"ignore",
"=",
"[",
"\"center\"",
",",
"\"id\"",
",",
"\"nodes\"",
",",
"\"tags\"",
",",
"\"type\"",
"]",
"for",
"n",
",",
"v",
"in",
"data",
".",
"items",
"(",
")",
":",
"if",
"n",
"in",
"ignore",
":",
"continue",
"attributes",
"[",
"n",
"]",
"=",
"v",
"return",
"cls",
"(",
"attributes",
"=",
"attributes",
",",
"center_lat",
"=",
"center_lat",
",",
"center_lon",
"=",
"center_lon",
",",
"node_ids",
"=",
"node_ids",
",",
"tags",
"=",
"tags",
",",
"result",
"=",
"result",
",",
"way_id",
"=",
"way_id",
")"
] | Create new Way element from JSON data
:param data: Element data from JSON
:type data: Dict
:param result: The result this element belongs to
:type result: overpy.Result
:return: New instance of Way
:rtype: overpy.Way
:raises overpy.exception.ElementDataWrongType: If type value of the passed JSON data does not match. | [
"Create",
"new",
"Way",
"element",
"from",
"JSON",
"data"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L966-L1005 |
4,178 | DinoTools/python-overpy | overpy/__init__.py | Relation.from_json | def from_json(cls, data, result=None):
"""
Create new Relation element from JSON data
:param data: Element data from JSON
:type data: Dict
:param result: The result this element belongs to
:type result: overpy.Result
:return: New instance of Relation
:rtype: overpy.Relation
:raises overpy.exception.ElementDataWrongType: If type value of the passed JSON data does not match.
"""
if data.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=data.get("type")
)
tags = data.get("tags", {})
rel_id = data.get("id")
(center_lat, center_lon) = cls.get_center_from_json(data=data)
members = []
supported_members = [RelationNode, RelationWay, RelationRelation]
for member in data.get("members", []):
type_value = member.get("type")
for member_cls in supported_members:
if member_cls._type_value == type_value:
members.append(
member_cls.from_json(
member,
result=result
)
)
attributes = {}
ignore = ["id", "members", "tags", "type"]
for n, v in data.items():
if n in ignore:
continue
attributes[n] = v
return cls(
rel_id=rel_id,
attributes=attributes,
center_lat=center_lat,
center_lon=center_lon,
members=members,
tags=tags,
result=result
) | python | def from_json(cls, data, result=None):
if data.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=data.get("type")
)
tags = data.get("tags", {})
rel_id = data.get("id")
(center_lat, center_lon) = cls.get_center_from_json(data=data)
members = []
supported_members = [RelationNode, RelationWay, RelationRelation]
for member in data.get("members", []):
type_value = member.get("type")
for member_cls in supported_members:
if member_cls._type_value == type_value:
members.append(
member_cls.from_json(
member,
result=result
)
)
attributes = {}
ignore = ["id", "members", "tags", "type"]
for n, v in data.items():
if n in ignore:
continue
attributes[n] = v
return cls(
rel_id=rel_id,
attributes=attributes,
center_lat=center_lat,
center_lon=center_lon,
members=members,
tags=tags,
result=result
) | [
"def",
"from_json",
"(",
"cls",
",",
"data",
",",
"result",
"=",
"None",
")",
":",
"if",
"data",
".",
"get",
"(",
"\"type\"",
")",
"!=",
"cls",
".",
"_type_value",
":",
"raise",
"exception",
".",
"ElementDataWrongType",
"(",
"type_expected",
"=",
"cls",
".",
"_type_value",
",",
"type_provided",
"=",
"data",
".",
"get",
"(",
"\"type\"",
")",
")",
"tags",
"=",
"data",
".",
"get",
"(",
"\"tags\"",
",",
"{",
"}",
")",
"rel_id",
"=",
"data",
".",
"get",
"(",
"\"id\"",
")",
"(",
"center_lat",
",",
"center_lon",
")",
"=",
"cls",
".",
"get_center_from_json",
"(",
"data",
"=",
"data",
")",
"members",
"=",
"[",
"]",
"supported_members",
"=",
"[",
"RelationNode",
",",
"RelationWay",
",",
"RelationRelation",
"]",
"for",
"member",
"in",
"data",
".",
"get",
"(",
"\"members\"",
",",
"[",
"]",
")",
":",
"type_value",
"=",
"member",
".",
"get",
"(",
"\"type\"",
")",
"for",
"member_cls",
"in",
"supported_members",
":",
"if",
"member_cls",
".",
"_type_value",
"==",
"type_value",
":",
"members",
".",
"append",
"(",
"member_cls",
".",
"from_json",
"(",
"member",
",",
"result",
"=",
"result",
")",
")",
"attributes",
"=",
"{",
"}",
"ignore",
"=",
"[",
"\"id\"",
",",
"\"members\"",
",",
"\"tags\"",
",",
"\"type\"",
"]",
"for",
"n",
",",
"v",
"in",
"data",
".",
"items",
"(",
")",
":",
"if",
"n",
"in",
"ignore",
":",
"continue",
"attributes",
"[",
"n",
"]",
"=",
"v",
"return",
"cls",
"(",
"rel_id",
"=",
"rel_id",
",",
"attributes",
"=",
"attributes",
",",
"center_lat",
"=",
"center_lat",
",",
"center_lon",
"=",
"center_lon",
",",
"members",
"=",
"members",
",",
"tags",
"=",
"tags",
",",
"result",
"=",
"result",
")"
] | Create new Relation element from JSON data
:param data: Element data from JSON
:type data: Dict
:param result: The result this element belongs to
:type result: overpy.Result
:return: New instance of Relation
:rtype: overpy.Relation
:raises overpy.exception.ElementDataWrongType: If type value of the passed JSON data does not match. | [
"Create",
"new",
"Relation",
"element",
"from",
"JSON",
"data"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1092-L1144 |
4,179 | DinoTools/python-overpy | overpy/__init__.py | RelationMember.from_json | def from_json(cls, data, result=None):
"""
Create new RelationMember element from JSON data
:param child: Element data from JSON
:type child: Dict
:param result: The result this element belongs to
:type result: overpy.Result
:return: New instance of RelationMember
:rtype: overpy.RelationMember
:raises overpy.exception.ElementDataWrongType: If type value of the passed JSON data does not match.
"""
if data.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=data.get("type")
)
ref = data.get("ref")
role = data.get("role")
attributes = {}
ignore = ["geometry", "type", "ref", "role"]
for n, v in data.items():
if n in ignore:
continue
attributes[n] = v
geometry = data.get("geometry")
if isinstance(geometry, list):
geometry_orig = geometry
geometry = []
for v in geometry_orig:
geometry.append(
RelationWayGeometryValue(
lat=v.get("lat"),
lon=v.get("lon")
)
)
else:
geometry = None
return cls(
attributes=attributes,
geometry=geometry,
ref=ref,
role=role,
result=result
) | python | def from_json(cls, data, result=None):
if data.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=data.get("type")
)
ref = data.get("ref")
role = data.get("role")
attributes = {}
ignore = ["geometry", "type", "ref", "role"]
for n, v in data.items():
if n in ignore:
continue
attributes[n] = v
geometry = data.get("geometry")
if isinstance(geometry, list):
geometry_orig = geometry
geometry = []
for v in geometry_orig:
geometry.append(
RelationWayGeometryValue(
lat=v.get("lat"),
lon=v.get("lon")
)
)
else:
geometry = None
return cls(
attributes=attributes,
geometry=geometry,
ref=ref,
role=role,
result=result
) | [
"def",
"from_json",
"(",
"cls",
",",
"data",
",",
"result",
"=",
"None",
")",
":",
"if",
"data",
".",
"get",
"(",
"\"type\"",
")",
"!=",
"cls",
".",
"_type_value",
":",
"raise",
"exception",
".",
"ElementDataWrongType",
"(",
"type_expected",
"=",
"cls",
".",
"_type_value",
",",
"type_provided",
"=",
"data",
".",
"get",
"(",
"\"type\"",
")",
")",
"ref",
"=",
"data",
".",
"get",
"(",
"\"ref\"",
")",
"role",
"=",
"data",
".",
"get",
"(",
"\"role\"",
")",
"attributes",
"=",
"{",
"}",
"ignore",
"=",
"[",
"\"geometry\"",
",",
"\"type\"",
",",
"\"ref\"",
",",
"\"role\"",
"]",
"for",
"n",
",",
"v",
"in",
"data",
".",
"items",
"(",
")",
":",
"if",
"n",
"in",
"ignore",
":",
"continue",
"attributes",
"[",
"n",
"]",
"=",
"v",
"geometry",
"=",
"data",
".",
"get",
"(",
"\"geometry\"",
")",
"if",
"isinstance",
"(",
"geometry",
",",
"list",
")",
":",
"geometry_orig",
"=",
"geometry",
"geometry",
"=",
"[",
"]",
"for",
"v",
"in",
"geometry_orig",
":",
"geometry",
".",
"append",
"(",
"RelationWayGeometryValue",
"(",
"lat",
"=",
"v",
".",
"get",
"(",
"\"lat\"",
")",
",",
"lon",
"=",
"v",
".",
"get",
"(",
"\"lon\"",
")",
")",
")",
"else",
":",
"geometry",
"=",
"None",
"return",
"cls",
"(",
"attributes",
"=",
"attributes",
",",
"geometry",
"=",
"geometry",
",",
"ref",
"=",
"ref",
",",
"role",
"=",
"role",
",",
"result",
"=",
"result",
")"
] | Create new RelationMember element from JSON data
:param child: Element data from JSON
:type child: Dict
:param result: The result this element belongs to
:type result: overpy.Result
:return: New instance of RelationMember
:rtype: overpy.RelationMember
:raises overpy.exception.ElementDataWrongType: If type value of the passed JSON data does not match. | [
"Create",
"new",
"RelationMember",
"element",
"from",
"JSON",
"data"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1234-L1282 |
4,180 | DinoTools/python-overpy | overpy/__init__.py | RelationMember.from_xml | def from_xml(cls, child, result=None):
"""
Create new RelationMember from XML data
:param child: XML node to be parsed
:type child: xml.etree.ElementTree.Element
:param result: The result this element belongs to
:type result: overpy.Result
:return: New relation member oject
:rtype: overpy.RelationMember
:raises overpy.exception.ElementDataWrongType: If name of the xml child node doesn't match
"""
if child.attrib.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=child.tag.lower()
)
ref = child.attrib.get("ref")
if ref is not None:
ref = int(ref)
role = child.attrib.get("role")
attributes = {}
ignore = ["geometry", "ref", "role", "type"]
for n, v in child.attrib.items():
if n in ignore:
continue
attributes[n] = v
geometry = None
for sub_child in child:
if sub_child.tag.lower() == "nd":
if geometry is None:
geometry = []
geometry.append(
RelationWayGeometryValue(
lat=Decimal(sub_child.attrib["lat"]),
lon=Decimal(sub_child.attrib["lon"])
)
)
return cls(
attributes=attributes,
geometry=geometry,
ref=ref,
role=role,
result=result
) | python | def from_xml(cls, child, result=None):
if child.attrib.get("type") != cls._type_value:
raise exception.ElementDataWrongType(
type_expected=cls._type_value,
type_provided=child.tag.lower()
)
ref = child.attrib.get("ref")
if ref is not None:
ref = int(ref)
role = child.attrib.get("role")
attributes = {}
ignore = ["geometry", "ref", "role", "type"]
for n, v in child.attrib.items():
if n in ignore:
continue
attributes[n] = v
geometry = None
for sub_child in child:
if sub_child.tag.lower() == "nd":
if geometry is None:
geometry = []
geometry.append(
RelationWayGeometryValue(
lat=Decimal(sub_child.attrib["lat"]),
lon=Decimal(sub_child.attrib["lon"])
)
)
return cls(
attributes=attributes,
geometry=geometry,
ref=ref,
role=role,
result=result
) | [
"def",
"from_xml",
"(",
"cls",
",",
"child",
",",
"result",
"=",
"None",
")",
":",
"if",
"child",
".",
"attrib",
".",
"get",
"(",
"\"type\"",
")",
"!=",
"cls",
".",
"_type_value",
":",
"raise",
"exception",
".",
"ElementDataWrongType",
"(",
"type_expected",
"=",
"cls",
".",
"_type_value",
",",
"type_provided",
"=",
"child",
".",
"tag",
".",
"lower",
"(",
")",
")",
"ref",
"=",
"child",
".",
"attrib",
".",
"get",
"(",
"\"ref\"",
")",
"if",
"ref",
"is",
"not",
"None",
":",
"ref",
"=",
"int",
"(",
"ref",
")",
"role",
"=",
"child",
".",
"attrib",
".",
"get",
"(",
"\"role\"",
")",
"attributes",
"=",
"{",
"}",
"ignore",
"=",
"[",
"\"geometry\"",
",",
"\"ref\"",
",",
"\"role\"",
",",
"\"type\"",
"]",
"for",
"n",
",",
"v",
"in",
"child",
".",
"attrib",
".",
"items",
"(",
")",
":",
"if",
"n",
"in",
"ignore",
":",
"continue",
"attributes",
"[",
"n",
"]",
"=",
"v",
"geometry",
"=",
"None",
"for",
"sub_child",
"in",
"child",
":",
"if",
"sub_child",
".",
"tag",
".",
"lower",
"(",
")",
"==",
"\"nd\"",
":",
"if",
"geometry",
"is",
"None",
":",
"geometry",
"=",
"[",
"]",
"geometry",
".",
"append",
"(",
"RelationWayGeometryValue",
"(",
"lat",
"=",
"Decimal",
"(",
"sub_child",
".",
"attrib",
"[",
"\"lat\"",
"]",
")",
",",
"lon",
"=",
"Decimal",
"(",
"sub_child",
".",
"attrib",
"[",
"\"lon\"",
"]",
")",
")",
")",
"return",
"cls",
"(",
"attributes",
"=",
"attributes",
",",
"geometry",
"=",
"geometry",
",",
"ref",
"=",
"ref",
",",
"role",
"=",
"role",
",",
"result",
"=",
"result",
")"
] | Create new RelationMember from XML data
:param child: XML node to be parsed
:type child: xml.etree.ElementTree.Element
:param result: The result this element belongs to
:type result: overpy.Result
:return: New relation member oject
:rtype: overpy.RelationMember
:raises overpy.exception.ElementDataWrongType: If name of the xml child node doesn't match | [
"Create",
"new",
"RelationMember",
"from",
"XML",
"data"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1285-L1333 |
4,181 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler.startElement | def startElement(self, name, attrs):
"""
Handle opening elements.
:param name: Name of the element
:type name: String
:param attrs: Attributes of the element
:type attrs: Dict
"""
if name in self.ignore_start:
return
try:
handler = getattr(self, '_handle_start_%s' % name)
except AttributeError:
raise KeyError("Unknown element start '%s'" % name)
handler(attrs) | python | def startElement(self, name, attrs):
if name in self.ignore_start:
return
try:
handler = getattr(self, '_handle_start_%s' % name)
except AttributeError:
raise KeyError("Unknown element start '%s'" % name)
handler(attrs) | [
"def",
"startElement",
"(",
"self",
",",
"name",
",",
"attrs",
")",
":",
"if",
"name",
"in",
"self",
".",
"ignore_start",
":",
"return",
"try",
":",
"handler",
"=",
"getattr",
"(",
"self",
",",
"'_handle_start_%s'",
"%",
"name",
")",
"except",
"AttributeError",
":",
"raise",
"KeyError",
"(",
"\"Unknown element start '%s'\"",
"%",
"name",
")",
"handler",
"(",
"attrs",
")"
] | Handle opening elements.
:param name: Name of the element
:type name: String
:param attrs: Attributes of the element
:type attrs: Dict | [
"Handle",
"opening",
"elements",
"."
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1405-L1420 |
4,182 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler.endElement | def endElement(self, name):
"""
Handle closing elements
:param name: Name of the element
:type name: String
"""
if name in self.ignore_end:
return
try:
handler = getattr(self, '_handle_end_%s' % name)
except AttributeError:
raise KeyError("Unknown element end '%s'" % name)
handler() | python | def endElement(self, name):
if name in self.ignore_end:
return
try:
handler = getattr(self, '_handle_end_%s' % name)
except AttributeError:
raise KeyError("Unknown element end '%s'" % name)
handler() | [
"def",
"endElement",
"(",
"self",
",",
"name",
")",
":",
"if",
"name",
"in",
"self",
".",
"ignore_end",
":",
"return",
"try",
":",
"handler",
"=",
"getattr",
"(",
"self",
",",
"'_handle_end_%s'",
"%",
"name",
")",
"except",
"AttributeError",
":",
"raise",
"KeyError",
"(",
"\"Unknown element end '%s'\"",
"%",
"name",
")",
"handler",
"(",
")"
] | Handle closing elements
:param name: Name of the element
:type name: String | [
"Handle",
"closing",
"elements"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1422-L1435 |
4,183 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_start_center | def _handle_start_center(self, attrs):
"""
Handle opening center element
:param attrs: Attributes of the element
:type attrs: Dict
"""
center_lat = attrs.get("lat")
center_lon = attrs.get("lon")
if center_lat is None or center_lon is None:
raise ValueError("Unable to get lat or lon of way center.")
self._curr["center_lat"] = Decimal(center_lat)
self._curr["center_lon"] = Decimal(center_lon) | python | def _handle_start_center(self, attrs):
center_lat = attrs.get("lat")
center_lon = attrs.get("lon")
if center_lat is None or center_lon is None:
raise ValueError("Unable to get lat or lon of way center.")
self._curr["center_lat"] = Decimal(center_lat)
self._curr["center_lon"] = Decimal(center_lon) | [
"def",
"_handle_start_center",
"(",
"self",
",",
"attrs",
")",
":",
"center_lat",
"=",
"attrs",
".",
"get",
"(",
"\"lat\"",
")",
"center_lon",
"=",
"attrs",
".",
"get",
"(",
"\"lon\"",
")",
"if",
"center_lat",
"is",
"None",
"or",
"center_lon",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"\"Unable to get lat or lon of way center.\"",
")",
"self",
".",
"_curr",
"[",
"\"center_lat\"",
"]",
"=",
"Decimal",
"(",
"center_lat",
")",
"self",
".",
"_curr",
"[",
"\"center_lon\"",
"]",
"=",
"Decimal",
"(",
"center_lon",
")"
] | Handle opening center element
:param attrs: Attributes of the element
:type attrs: Dict | [
"Handle",
"opening",
"center",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1437-L1449 |
4,184 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_start_tag | def _handle_start_tag(self, attrs):
"""
Handle opening tag element
:param attrs: Attributes of the element
:type attrs: Dict
"""
try:
tag_key = attrs['k']
except KeyError:
raise ValueError("Tag without name/key.")
self._curr['tags'][tag_key] = attrs.get('v') | python | def _handle_start_tag(self, attrs):
try:
tag_key = attrs['k']
except KeyError:
raise ValueError("Tag without name/key.")
self._curr['tags'][tag_key] = attrs.get('v') | [
"def",
"_handle_start_tag",
"(",
"self",
",",
"attrs",
")",
":",
"try",
":",
"tag_key",
"=",
"attrs",
"[",
"'k'",
"]",
"except",
"KeyError",
":",
"raise",
"ValueError",
"(",
"\"Tag without name/key.\"",
")",
"self",
".",
"_curr",
"[",
"'tags'",
"]",
"[",
"tag_key",
"]",
"=",
"attrs",
".",
"get",
"(",
"'v'",
")"
] | Handle opening tag element
:param attrs: Attributes of the element
:type attrs: Dict | [
"Handle",
"opening",
"tag",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1451-L1462 |
4,185 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_start_node | def _handle_start_node(self, attrs):
"""
Handle opening node element
:param attrs: Attributes of the element
:type attrs: Dict
"""
self._curr = {
'attributes': dict(attrs),
'lat': None,
'lon': None,
'node_id': None,
'tags': {}
}
if attrs.get('id', None) is not None:
self._curr['node_id'] = int(attrs['id'])
del self._curr['attributes']['id']
if attrs.get('lat', None) is not None:
self._curr['lat'] = Decimal(attrs['lat'])
del self._curr['attributes']['lat']
if attrs.get('lon', None) is not None:
self._curr['lon'] = Decimal(attrs['lon'])
del self._curr['attributes']['lon'] | python | def _handle_start_node(self, attrs):
self._curr = {
'attributes': dict(attrs),
'lat': None,
'lon': None,
'node_id': None,
'tags': {}
}
if attrs.get('id', None) is not None:
self._curr['node_id'] = int(attrs['id'])
del self._curr['attributes']['id']
if attrs.get('lat', None) is not None:
self._curr['lat'] = Decimal(attrs['lat'])
del self._curr['attributes']['lat']
if attrs.get('lon', None) is not None:
self._curr['lon'] = Decimal(attrs['lon'])
del self._curr['attributes']['lon'] | [
"def",
"_handle_start_node",
"(",
"self",
",",
"attrs",
")",
":",
"self",
".",
"_curr",
"=",
"{",
"'attributes'",
":",
"dict",
"(",
"attrs",
")",
",",
"'lat'",
":",
"None",
",",
"'lon'",
":",
"None",
",",
"'node_id'",
":",
"None",
",",
"'tags'",
":",
"{",
"}",
"}",
"if",
"attrs",
".",
"get",
"(",
"'id'",
",",
"None",
")",
"is",
"not",
"None",
":",
"self",
".",
"_curr",
"[",
"'node_id'",
"]",
"=",
"int",
"(",
"attrs",
"[",
"'id'",
"]",
")",
"del",
"self",
".",
"_curr",
"[",
"'attributes'",
"]",
"[",
"'id'",
"]",
"if",
"attrs",
".",
"get",
"(",
"'lat'",
",",
"None",
")",
"is",
"not",
"None",
":",
"self",
".",
"_curr",
"[",
"'lat'",
"]",
"=",
"Decimal",
"(",
"attrs",
"[",
"'lat'",
"]",
")",
"del",
"self",
".",
"_curr",
"[",
"'attributes'",
"]",
"[",
"'lat'",
"]",
"if",
"attrs",
".",
"get",
"(",
"'lon'",
",",
"None",
")",
"is",
"not",
"None",
":",
"self",
".",
"_curr",
"[",
"'lon'",
"]",
"=",
"Decimal",
"(",
"attrs",
"[",
"'lon'",
"]",
")",
"del",
"self",
".",
"_curr",
"[",
"'attributes'",
"]",
"[",
"'lon'",
"]"
] | Handle opening node element
:param attrs: Attributes of the element
:type attrs: Dict | [
"Handle",
"opening",
"node",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1464-L1486 |
4,186 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_end_node | def _handle_end_node(self):
"""
Handle closing node element
"""
self._result.append(Node(result=self._result, **self._curr))
self._curr = {} | python | def _handle_end_node(self):
self._result.append(Node(result=self._result, **self._curr))
self._curr = {} | [
"def",
"_handle_end_node",
"(",
"self",
")",
":",
"self",
".",
"_result",
".",
"append",
"(",
"Node",
"(",
"result",
"=",
"self",
".",
"_result",
",",
"*",
"*",
"self",
".",
"_curr",
")",
")",
"self",
".",
"_curr",
"=",
"{",
"}"
] | Handle closing node element | [
"Handle",
"closing",
"node",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1488-L1493 |
4,187 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_start_way | def _handle_start_way(self, attrs):
"""
Handle opening way element
:param attrs: Attributes of the element
:type attrs: Dict
"""
self._curr = {
'center_lat': None,
'center_lon': None,
'attributes': dict(attrs),
'node_ids': [],
'tags': {},
'way_id': None
}
if attrs.get('id', None) is not None:
self._curr['way_id'] = int(attrs['id'])
del self._curr['attributes']['id'] | python | def _handle_start_way(self, attrs):
self._curr = {
'center_lat': None,
'center_lon': None,
'attributes': dict(attrs),
'node_ids': [],
'tags': {},
'way_id': None
}
if attrs.get('id', None) is not None:
self._curr['way_id'] = int(attrs['id'])
del self._curr['attributes']['id'] | [
"def",
"_handle_start_way",
"(",
"self",
",",
"attrs",
")",
":",
"self",
".",
"_curr",
"=",
"{",
"'center_lat'",
":",
"None",
",",
"'center_lon'",
":",
"None",
",",
"'attributes'",
":",
"dict",
"(",
"attrs",
")",
",",
"'node_ids'",
":",
"[",
"]",
",",
"'tags'",
":",
"{",
"}",
",",
"'way_id'",
":",
"None",
"}",
"if",
"attrs",
".",
"get",
"(",
"'id'",
",",
"None",
")",
"is",
"not",
"None",
":",
"self",
".",
"_curr",
"[",
"'way_id'",
"]",
"=",
"int",
"(",
"attrs",
"[",
"'id'",
"]",
")",
"del",
"self",
".",
"_curr",
"[",
"'attributes'",
"]",
"[",
"'id'",
"]"
] | Handle opening way element
:param attrs: Attributes of the element
:type attrs: Dict | [
"Handle",
"opening",
"way",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1495-L1512 |
4,188 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_end_way | def _handle_end_way(self):
"""
Handle closing way element
"""
self._result.append(Way(result=self._result, **self._curr))
self._curr = {} | python | def _handle_end_way(self):
self._result.append(Way(result=self._result, **self._curr))
self._curr = {} | [
"def",
"_handle_end_way",
"(",
"self",
")",
":",
"self",
".",
"_result",
".",
"append",
"(",
"Way",
"(",
"result",
"=",
"self",
".",
"_result",
",",
"*",
"*",
"self",
".",
"_curr",
")",
")",
"self",
".",
"_curr",
"=",
"{",
"}"
] | Handle closing way element | [
"Handle",
"closing",
"way",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1514-L1519 |
4,189 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_start_area | def _handle_start_area(self, attrs):
"""
Handle opening area element
:param attrs: Attributes of the element
:type attrs: Dict
"""
self._curr = {
'attributes': dict(attrs),
'tags': {},
'area_id': None
}
if attrs.get('id', None) is not None:
self._curr['area_id'] = int(attrs['id'])
del self._curr['attributes']['id'] | python | def _handle_start_area(self, attrs):
self._curr = {
'attributes': dict(attrs),
'tags': {},
'area_id': None
}
if attrs.get('id', None) is not None:
self._curr['area_id'] = int(attrs['id'])
del self._curr['attributes']['id'] | [
"def",
"_handle_start_area",
"(",
"self",
",",
"attrs",
")",
":",
"self",
".",
"_curr",
"=",
"{",
"'attributes'",
":",
"dict",
"(",
"attrs",
")",
",",
"'tags'",
":",
"{",
"}",
",",
"'area_id'",
":",
"None",
"}",
"if",
"attrs",
".",
"get",
"(",
"'id'",
",",
"None",
")",
"is",
"not",
"None",
":",
"self",
".",
"_curr",
"[",
"'area_id'",
"]",
"=",
"int",
"(",
"attrs",
"[",
"'id'",
"]",
")",
"del",
"self",
".",
"_curr",
"[",
"'attributes'",
"]",
"[",
"'id'",
"]"
] | Handle opening area element
:param attrs: Attributes of the element
:type attrs: Dict | [
"Handle",
"opening",
"area",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1521-L1535 |
4,190 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_end_area | def _handle_end_area(self):
"""
Handle closing area element
"""
self._result.append(Area(result=self._result, **self._curr))
self._curr = {} | python | def _handle_end_area(self):
self._result.append(Area(result=self._result, **self._curr))
self._curr = {} | [
"def",
"_handle_end_area",
"(",
"self",
")",
":",
"self",
".",
"_result",
".",
"append",
"(",
"Area",
"(",
"result",
"=",
"self",
".",
"_result",
",",
"*",
"*",
"self",
".",
"_curr",
")",
")",
"self",
".",
"_curr",
"=",
"{",
"}"
] | Handle closing area element | [
"Handle",
"closing",
"area",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1537-L1542 |
4,191 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_start_nd | def _handle_start_nd(self, attrs):
"""
Handle opening nd element
:param attrs: Attributes of the element
:type attrs: Dict
"""
if isinstance(self.cur_relation_member, RelationWay):
if self.cur_relation_member.geometry is None:
self.cur_relation_member.geometry = []
self.cur_relation_member.geometry.append(
RelationWayGeometryValue(
lat=Decimal(attrs["lat"]),
lon=Decimal(attrs["lon"])
)
)
else:
try:
node_ref = attrs['ref']
except KeyError:
raise ValueError("Unable to find required ref value.")
self._curr['node_ids'].append(int(node_ref)) | python | def _handle_start_nd(self, attrs):
if isinstance(self.cur_relation_member, RelationWay):
if self.cur_relation_member.geometry is None:
self.cur_relation_member.geometry = []
self.cur_relation_member.geometry.append(
RelationWayGeometryValue(
lat=Decimal(attrs["lat"]),
lon=Decimal(attrs["lon"])
)
)
else:
try:
node_ref = attrs['ref']
except KeyError:
raise ValueError("Unable to find required ref value.")
self._curr['node_ids'].append(int(node_ref)) | [
"def",
"_handle_start_nd",
"(",
"self",
",",
"attrs",
")",
":",
"if",
"isinstance",
"(",
"self",
".",
"cur_relation_member",
",",
"RelationWay",
")",
":",
"if",
"self",
".",
"cur_relation_member",
".",
"geometry",
"is",
"None",
":",
"self",
".",
"cur_relation_member",
".",
"geometry",
"=",
"[",
"]",
"self",
".",
"cur_relation_member",
".",
"geometry",
".",
"append",
"(",
"RelationWayGeometryValue",
"(",
"lat",
"=",
"Decimal",
"(",
"attrs",
"[",
"\"lat\"",
"]",
")",
",",
"lon",
"=",
"Decimal",
"(",
"attrs",
"[",
"\"lon\"",
"]",
")",
")",
")",
"else",
":",
"try",
":",
"node_ref",
"=",
"attrs",
"[",
"'ref'",
"]",
"except",
"KeyError",
":",
"raise",
"ValueError",
"(",
"\"Unable to find required ref value.\"",
")",
"self",
".",
"_curr",
"[",
"'node_ids'",
"]",
".",
"append",
"(",
"int",
"(",
"node_ref",
")",
")"
] | Handle opening nd element
:param attrs: Attributes of the element
:type attrs: Dict | [
"Handle",
"opening",
"nd",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1544-L1565 |
4,192 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_start_relation | def _handle_start_relation(self, attrs):
"""
Handle opening relation element
:param attrs: Attributes of the element
:type attrs: Dict
"""
self._curr = {
'attributes': dict(attrs),
'members': [],
'rel_id': None,
'tags': {}
}
if attrs.get('id', None) is not None:
self._curr['rel_id'] = int(attrs['id'])
del self._curr['attributes']['id'] | python | def _handle_start_relation(self, attrs):
self._curr = {
'attributes': dict(attrs),
'members': [],
'rel_id': None,
'tags': {}
}
if attrs.get('id', None) is not None:
self._curr['rel_id'] = int(attrs['id'])
del self._curr['attributes']['id'] | [
"def",
"_handle_start_relation",
"(",
"self",
",",
"attrs",
")",
":",
"self",
".",
"_curr",
"=",
"{",
"'attributes'",
":",
"dict",
"(",
"attrs",
")",
",",
"'members'",
":",
"[",
"]",
",",
"'rel_id'",
":",
"None",
",",
"'tags'",
":",
"{",
"}",
"}",
"if",
"attrs",
".",
"get",
"(",
"'id'",
",",
"None",
")",
"is",
"not",
"None",
":",
"self",
".",
"_curr",
"[",
"'rel_id'",
"]",
"=",
"int",
"(",
"attrs",
"[",
"'id'",
"]",
")",
"del",
"self",
".",
"_curr",
"[",
"'attributes'",
"]",
"[",
"'id'",
"]"
] | Handle opening relation element
:param attrs: Attributes of the element
:type attrs: Dict | [
"Handle",
"opening",
"relation",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1567-L1582 |
4,193 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_end_relation | def _handle_end_relation(self):
"""
Handle closing relation element
"""
self._result.append(Relation(result=self._result, **self._curr))
self._curr = {} | python | def _handle_end_relation(self):
self._result.append(Relation(result=self._result, **self._curr))
self._curr = {} | [
"def",
"_handle_end_relation",
"(",
"self",
")",
":",
"self",
".",
"_result",
".",
"append",
"(",
"Relation",
"(",
"result",
"=",
"self",
".",
"_result",
",",
"*",
"*",
"self",
".",
"_curr",
")",
")",
"self",
".",
"_curr",
"=",
"{",
"}"
] | Handle closing relation element | [
"Handle",
"closing",
"relation",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1584-L1589 |
4,194 | DinoTools/python-overpy | overpy/__init__.py | OSMSAXHandler._handle_start_member | def _handle_start_member(self, attrs):
"""
Handle opening member element
:param attrs: Attributes of the element
:type attrs: Dict
"""
params = {
# ToDo: Parse attributes
'attributes': {},
'ref': None,
'result': self._result,
'role': None
}
if attrs.get('ref', None):
params['ref'] = int(attrs['ref'])
if attrs.get('role', None):
params['role'] = attrs['role']
cls_map = {
"area": RelationArea,
"node": RelationNode,
"relation": RelationRelation,
"way": RelationWay
}
cls = cls_map.get(attrs["type"])
if cls is None:
raise ValueError("Undefined type for member: '%s'" % attrs['type'])
self.cur_relation_member = cls(**params)
self._curr['members'].append(self.cur_relation_member) | python | def _handle_start_member(self, attrs):
params = {
# ToDo: Parse attributes
'attributes': {},
'ref': None,
'result': self._result,
'role': None
}
if attrs.get('ref', None):
params['ref'] = int(attrs['ref'])
if attrs.get('role', None):
params['role'] = attrs['role']
cls_map = {
"area": RelationArea,
"node": RelationNode,
"relation": RelationRelation,
"way": RelationWay
}
cls = cls_map.get(attrs["type"])
if cls is None:
raise ValueError("Undefined type for member: '%s'" % attrs['type'])
self.cur_relation_member = cls(**params)
self._curr['members'].append(self.cur_relation_member) | [
"def",
"_handle_start_member",
"(",
"self",
",",
"attrs",
")",
":",
"params",
"=",
"{",
"# ToDo: Parse attributes",
"'attributes'",
":",
"{",
"}",
",",
"'ref'",
":",
"None",
",",
"'result'",
":",
"self",
".",
"_result",
",",
"'role'",
":",
"None",
"}",
"if",
"attrs",
".",
"get",
"(",
"'ref'",
",",
"None",
")",
":",
"params",
"[",
"'ref'",
"]",
"=",
"int",
"(",
"attrs",
"[",
"'ref'",
"]",
")",
"if",
"attrs",
".",
"get",
"(",
"'role'",
",",
"None",
")",
":",
"params",
"[",
"'role'",
"]",
"=",
"attrs",
"[",
"'role'",
"]",
"cls_map",
"=",
"{",
"\"area\"",
":",
"RelationArea",
",",
"\"node\"",
":",
"RelationNode",
",",
"\"relation\"",
":",
"RelationRelation",
",",
"\"way\"",
":",
"RelationWay",
"}",
"cls",
"=",
"cls_map",
".",
"get",
"(",
"attrs",
"[",
"\"type\"",
"]",
")",
"if",
"cls",
"is",
"None",
":",
"raise",
"ValueError",
"(",
"\"Undefined type for member: '%s'\"",
"%",
"attrs",
"[",
"'type'",
"]",
")",
"self",
".",
"cur_relation_member",
"=",
"cls",
"(",
"*",
"*",
"params",
")",
"self",
".",
"_curr",
"[",
"'members'",
"]",
".",
"append",
"(",
"self",
".",
"cur_relation_member",
")"
] | Handle opening member element
:param attrs: Attributes of the element
:type attrs: Dict | [
"Handle",
"opening",
"member",
"element"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/__init__.py#L1591-L1622 |
4,195 | DinoTools/python-overpy | overpy/helper.py | get_street | def get_street(street, areacode, api=None):
"""
Retrieve streets in a given bounding area
:param overpy.Overpass api: First street of intersection
:param String street: Name of street
:param String areacode: The OSM id of the bounding area
:return: Parsed result
:raises overpy.exception.OverPyException: If something bad happens.
"""
if api is None:
api = overpy.Overpass()
query = """
area(%s)->.location;
(
way[highway][name="%s"](area.location);
- (
way[highway=service](area.location);
way[highway=track](area.location);
);
);
out body;
>;
out skel qt;
"""
data = api.query(query % (areacode, street))
return data | python | def get_street(street, areacode, api=None):
if api is None:
api = overpy.Overpass()
query = """
area(%s)->.location;
(
way[highway][name="%s"](area.location);
- (
way[highway=service](area.location);
way[highway=track](area.location);
);
);
out body;
>;
out skel qt;
"""
data = api.query(query % (areacode, street))
return data | [
"def",
"get_street",
"(",
"street",
",",
"areacode",
",",
"api",
"=",
"None",
")",
":",
"if",
"api",
"is",
"None",
":",
"api",
"=",
"overpy",
".",
"Overpass",
"(",
")",
"query",
"=",
"\"\"\"\n area(%s)->.location;\n (\n way[highway][name=\"%s\"](area.location);\n - (\n way[highway=service](area.location);\n way[highway=track](area.location);\n );\n );\n out body;\n >;\n out skel qt;\n \"\"\"",
"data",
"=",
"api",
".",
"query",
"(",
"query",
"%",
"(",
"areacode",
",",
"street",
")",
")",
"return",
"data"
] | Retrieve streets in a given bounding area
:param overpy.Overpass api: First street of intersection
:param String street: Name of street
:param String areacode: The OSM id of the bounding area
:return: Parsed result
:raises overpy.exception.OverPyException: If something bad happens. | [
"Retrieve",
"streets",
"in",
"a",
"given",
"bounding",
"area"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/helper.py#L6-L35 |
4,196 | DinoTools/python-overpy | overpy/helper.py | get_intersection | def get_intersection(street1, street2, areacode, api=None):
"""
Retrieve intersection of two streets in a given bounding area
:param overpy.Overpass api: First street of intersection
:param String street1: Name of first street of intersection
:param String street2: Name of second street of intersection
:param String areacode: The OSM id of the bounding area
:return: List of intersections
:raises overpy.exception.OverPyException: If something bad happens.
"""
if api is None:
api = overpy.Overpass()
query = """
area(%s)->.location;
(
way[highway][name="%s"](area.location); node(w)->.n1;
way[highway][name="%s"](area.location); node(w)->.n2;
);
node.n1.n2;
out meta;
"""
data = api.query(query % (areacode, street1, street2))
return data.get_nodes() | python | def get_intersection(street1, street2, areacode, api=None):
if api is None:
api = overpy.Overpass()
query = """
area(%s)->.location;
(
way[highway][name="%s"](area.location); node(w)->.n1;
way[highway][name="%s"](area.location); node(w)->.n2;
);
node.n1.n2;
out meta;
"""
data = api.query(query % (areacode, street1, street2))
return data.get_nodes() | [
"def",
"get_intersection",
"(",
"street1",
",",
"street2",
",",
"areacode",
",",
"api",
"=",
"None",
")",
":",
"if",
"api",
"is",
"None",
":",
"api",
"=",
"overpy",
".",
"Overpass",
"(",
")",
"query",
"=",
"\"\"\"\n area(%s)->.location;\n (\n way[highway][name=\"%s\"](area.location); node(w)->.n1;\n way[highway][name=\"%s\"](area.location); node(w)->.n2;\n );\n node.n1.n2;\n out meta;\n \"\"\"",
"data",
"=",
"api",
".",
"query",
"(",
"query",
"%",
"(",
"areacode",
",",
"street1",
",",
"street2",
")",
")",
"return",
"data",
".",
"get_nodes",
"(",
")"
] | Retrieve intersection of two streets in a given bounding area
:param overpy.Overpass api: First street of intersection
:param String street1: Name of first street of intersection
:param String street2: Name of second street of intersection
:param String areacode: The OSM id of the bounding area
:return: List of intersections
:raises overpy.exception.OverPyException: If something bad happens. | [
"Retrieve",
"intersection",
"of",
"two",
"streets",
"in",
"a",
"given",
"bounding",
"area"
] | db8f80eeb1b4d1405816bd62c16ddb3364e0c46d | https://github.com/DinoTools/python-overpy/blob/db8f80eeb1b4d1405816bd62c16ddb3364e0c46d/overpy/helper.py#L38-L64 |
4,197 | architv/chcli | challenges/cli.py | check_platforms | def check_platforms(platforms):
"""Checks if the platforms have a valid platform code"""
if len(platforms) > 0:
return all(platform in PLATFORM_IDS for platform in platforms)
return True | python | def check_platforms(platforms):
if len(platforms) > 0:
return all(platform in PLATFORM_IDS for platform in platforms)
return True | [
"def",
"check_platforms",
"(",
"platforms",
")",
":",
"if",
"len",
"(",
"platforms",
")",
">",
"0",
":",
"return",
"all",
"(",
"platform",
"in",
"PLATFORM_IDS",
"for",
"platform",
"in",
"platforms",
")",
"return",
"True"
] | Checks if the platforms have a valid platform code | [
"Checks",
"if",
"the",
"platforms",
"have",
"a",
"valid",
"platform",
"code"
] | e9e387b9a85c6b64bc74b1a7c5b85baa4d4ea7d7 | https://github.com/architv/chcli/blob/e9e387b9a85c6b64bc74b1a7c5b85baa4d4ea7d7/challenges/cli.py#L17-L21 |
4,198 | architv/chcli | challenges/cli.py | main | def main(active, upcoming, hiring, short, goto, platforms, time):
"""A CLI for active and upcoming programming challenges from various platforms"""
if not check_platforms(platforms):
raise IncorrectParametersException('Invlaid code for platform. Please check the platform ids')
try:
if active:
active_challenges = active_contests(platforms)
if goto:
webbrowser.open(active_challenges[goto - 1]["contest_url"], new=2)
else:
writers.write_contests(active_challenges, "active")
return
if upcoming:
upcoming_challenges = upcoming_contests(platforms, time)
if goto:
goto = int(goto)
webbrowser.open(upcoming_challenges[goto - 1]["contest_url"], new=2)
else:
writers.write_contests(upcoming_challenges, "upcoming")
return
if hiring:
hiring_challenges = hiring_contests()
if goto:
webbrowser.open(hiring_challenges[goto - 1]["contest_url"], new=2)
else:
writers.write_contests(hiring_challenges, "hiring")
return
if short:
short_challenges = short_contests(platforms)
if goto:
goto = int(goto)
webbrowser.open(short_challenges[goto - 1]["contest_url"], new=2)
else:
writers.write_contests(short_challenges, "short")
return
all_contests = get_all_contests(platforms, time)
if goto:
webbrowser.open(all_contests[goto - 1]["contest_url"], new=2)
else:
writers.write_contests(all_contests, "all")
except IncorrectParametersException as e:
click.secho(e.message, fg="red", bold=True) | python | def main(active, upcoming, hiring, short, goto, platforms, time):
if not check_platforms(platforms):
raise IncorrectParametersException('Invlaid code for platform. Please check the platform ids')
try:
if active:
active_challenges = active_contests(platforms)
if goto:
webbrowser.open(active_challenges[goto - 1]["contest_url"], new=2)
else:
writers.write_contests(active_challenges, "active")
return
if upcoming:
upcoming_challenges = upcoming_contests(platforms, time)
if goto:
goto = int(goto)
webbrowser.open(upcoming_challenges[goto - 1]["contest_url"], new=2)
else:
writers.write_contests(upcoming_challenges, "upcoming")
return
if hiring:
hiring_challenges = hiring_contests()
if goto:
webbrowser.open(hiring_challenges[goto - 1]["contest_url"], new=2)
else:
writers.write_contests(hiring_challenges, "hiring")
return
if short:
short_challenges = short_contests(platforms)
if goto:
goto = int(goto)
webbrowser.open(short_challenges[goto - 1]["contest_url"], new=2)
else:
writers.write_contests(short_challenges, "short")
return
all_contests = get_all_contests(platforms, time)
if goto:
webbrowser.open(all_contests[goto - 1]["contest_url"], new=2)
else:
writers.write_contests(all_contests, "all")
except IncorrectParametersException as e:
click.secho(e.message, fg="red", bold=True) | [
"def",
"main",
"(",
"active",
",",
"upcoming",
",",
"hiring",
",",
"short",
",",
"goto",
",",
"platforms",
",",
"time",
")",
":",
"if",
"not",
"check_platforms",
"(",
"platforms",
")",
":",
"raise",
"IncorrectParametersException",
"(",
"'Invlaid code for platform. Please check the platform ids'",
")",
"try",
":",
"if",
"active",
":",
"active_challenges",
"=",
"active_contests",
"(",
"platforms",
")",
"if",
"goto",
":",
"webbrowser",
".",
"open",
"(",
"active_challenges",
"[",
"goto",
"-",
"1",
"]",
"[",
"\"contest_url\"",
"]",
",",
"new",
"=",
"2",
")",
"else",
":",
"writers",
".",
"write_contests",
"(",
"active_challenges",
",",
"\"active\"",
")",
"return",
"if",
"upcoming",
":",
"upcoming_challenges",
"=",
"upcoming_contests",
"(",
"platforms",
",",
"time",
")",
"if",
"goto",
":",
"goto",
"=",
"int",
"(",
"goto",
")",
"webbrowser",
".",
"open",
"(",
"upcoming_challenges",
"[",
"goto",
"-",
"1",
"]",
"[",
"\"contest_url\"",
"]",
",",
"new",
"=",
"2",
")",
"else",
":",
"writers",
".",
"write_contests",
"(",
"upcoming_challenges",
",",
"\"upcoming\"",
")",
"return",
"if",
"hiring",
":",
"hiring_challenges",
"=",
"hiring_contests",
"(",
")",
"if",
"goto",
":",
"webbrowser",
".",
"open",
"(",
"hiring_challenges",
"[",
"goto",
"-",
"1",
"]",
"[",
"\"contest_url\"",
"]",
",",
"new",
"=",
"2",
")",
"else",
":",
"writers",
".",
"write_contests",
"(",
"hiring_challenges",
",",
"\"hiring\"",
")",
"return",
"if",
"short",
":",
"short_challenges",
"=",
"short_contests",
"(",
"platforms",
")",
"if",
"goto",
":",
"goto",
"=",
"int",
"(",
"goto",
")",
"webbrowser",
".",
"open",
"(",
"short_challenges",
"[",
"goto",
"-",
"1",
"]",
"[",
"\"contest_url\"",
"]",
",",
"new",
"=",
"2",
")",
"else",
":",
"writers",
".",
"write_contests",
"(",
"short_challenges",
",",
"\"short\"",
")",
"return",
"all_contests",
"=",
"get_all_contests",
"(",
"platforms",
",",
"time",
")",
"if",
"goto",
":",
"webbrowser",
".",
"open",
"(",
"all_contests",
"[",
"goto",
"-",
"1",
"]",
"[",
"\"contest_url\"",
"]",
",",
"new",
"=",
"2",
")",
"else",
":",
"writers",
".",
"write_contests",
"(",
"all_contests",
",",
"\"all\"",
")",
"except",
"IncorrectParametersException",
"as",
"e",
":",
"click",
".",
"secho",
"(",
"e",
".",
"message",
",",
"fg",
"=",
"\"red\"",
",",
"bold",
"=",
"True",
")"
] | A CLI for active and upcoming programming challenges from various platforms | [
"A",
"CLI",
"for",
"active",
"and",
"upcoming",
"programming",
"challenges",
"from",
"various",
"platforms"
] | e9e387b9a85c6b64bc74b1a7c5b85baa4d4ea7d7 | https://github.com/architv/chcli/blob/e9e387b9a85c6b64bc74b1a7c5b85baa4d4ea7d7/challenges/cli.py#L117-L164 |
4,199 | architv/chcli | challenges/writers.py | colors | def colors():
"""Creates an enum for colors"""
enums = dict(
TIME_LEFT="red",
CONTEST_NAME="yellow",
HOST="green",
MISC="blue",
TIME_TO_START="green",
)
return type('Enum', (), enums) | python | def colors():
enums = dict(
TIME_LEFT="red",
CONTEST_NAME="yellow",
HOST="green",
MISC="blue",
TIME_TO_START="green",
)
return type('Enum', (), enums) | [
"def",
"colors",
"(",
")",
":",
"enums",
"=",
"dict",
"(",
"TIME_LEFT",
"=",
"\"red\"",
",",
"CONTEST_NAME",
"=",
"\"yellow\"",
",",
"HOST",
"=",
"\"green\"",
",",
"MISC",
"=",
"\"blue\"",
",",
"TIME_TO_START",
"=",
"\"green\"",
",",
")",
"return",
"type",
"(",
"'Enum'",
",",
"(",
")",
",",
"enums",
")"
] | Creates an enum for colors | [
"Creates",
"an",
"enum",
"for",
"colors"
] | e9e387b9a85c6b64bc74b1a7c5b85baa4d4ea7d7 | https://github.com/architv/chcli/blob/e9e387b9a85c6b64bc74b1a7c5b85baa4d4ea7d7/challenges/writers.py#L7-L17 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.