Skip to content

Commit

Permalink
Merge pull request #110 from metaodi/develop
Browse files Browse the repository at this point in the history
Release 1.3.0
  • Loading branch information
metaodi authored Oct 4, 2020
2 parents ced86e9 + 3749561 commit f50e785
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ MANIFEST
*.egg-info
.coverage
.tox
.pycache/*
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased][unreleased]

## 1.3.0 - 2020-10-05
### Added
- Add close() method to close the underlying http session (see issue #107)
- Add context manager to automatically open and close the http session (see issue #107)

### Fixed
- Correctly parse password file (thanks [Julien Palard](https://github.com/JulienPalard), see pull request #106)

## 1.2.2 - 2018-11-05
### Fixed
- Update PyPI password for deployment
Expand Down
27 changes: 21 additions & 6 deletions osmapi/OsmApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,17 +206,19 @@ def __init__(
if username:
self._username = username
elif passwordfile:
pass_line = open(passwordfile).readline()
with open(passwordfile) as f:
pass_line = f.readline()
self._username = pass_line.split(":")[0].strip()

# Get password
if password:
self._password = password
elif passwordfile:
for line in open(passwordfile).readlines():
line = line.strip().split(":")
if line[0] == self._username:
self._password = line[1]
with open(passwordfile) as f:
for line in f:
line = line.strip().split(":", 1)
if line[0] == self._username:
self._password = line[1]

# Changest informations
# auto create and close changesets
Expand Down Expand Up @@ -249,13 +251,26 @@ def __init__(
self._session = self._get_http_session()

def __del__(self):
self.close()

return None

def __enter__(self):
self._session = self._get_http_session()
return self

def __exit__(self, *args):
self.close()

def close(self):
try:
if self._changesetauto:
self._changesetautoflush(True)
except ResponseEmptyApiError:
pass

return None
if self._session:
self._session.close()

##################################################
# Capabilities #
Expand Down
2 changes: 1 addition & 1 deletion osmapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from __future__ import (absolute_import, print_function, unicode_literals)

__version__ = '1.2.2'
__version__ = '1.3.0'

from .OsmApi import * # noqa
18 changes: 9 additions & 9 deletions tests/changeset_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ def test_ChangesetUpdate(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osm version="0.6" generator="osmapi/1.2.2">\n'
b'<osm version="0.6" generator="osmapi/1.3.0">\n'
b' <changeset visible="true">\n'
b' <tag k="test" v="foobar"/>\n'
b' <tag k="created_by" v="osmapi/1.2.2"/>\n'
b' <tag k="created_by" v="osmapi/1.3.0"/>\n'
b' </changeset>\n'
b'</osm>\n'
)
Expand Down Expand Up @@ -125,7 +125,7 @@ def test_ChangesetUpdate_with_created_by(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osm version="0.6" generator="osmapi/1.2.2">\n'
b'<osm version="0.6" generator="osmapi/1.3.0">\n'
b' <changeset visible="true">\n'
b' <tag k="test" v="foobar"/>\n'
b' <tag k="created_by" v="MyTestOSMApp"/>\n'
Expand Down Expand Up @@ -163,10 +163,10 @@ def test_ChangesetCreate(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osm version="0.6" generator="osmapi/1.2.2">\n'
b'<osm version="0.6" generator="osmapi/1.3.0">\n'
b' <changeset visible="true">\n'
b' <tag k="foobar" v="A new test changeset"/>\n'
b' <tag k="created_by" v="osmapi/1.2.2"/>\n'
b' <tag k="created_by" v="osmapi/1.3.0"/>\n'
b' </changeset>\n'
b'</osm>\n'
)
Expand All @@ -190,7 +190,7 @@ def test_ChangesetCreate_with_created_by(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osm version="0.6" generator="osmapi/1.2.2">\n'
b'<osm version="0.6" generator="osmapi/1.3.0">\n'
b' <changeset visible="true">\n'
b' <tag k="foobar" v="A new test changeset"/>\n'
b' <tag k="created_by" v="CoolTestApp"/>\n'
Expand Down Expand Up @@ -276,7 +276,7 @@ def test_ChangesetUpload_create_node(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osmChange version="0.6" generator="osmapi/1.2.2">\n'
b'<osmChange version="0.6" generator="osmapi/1.3.0">\n'
b'<create>\n'
b' <node lat="47.123" lon="8.555" visible="true" '
b'changeset="4444">\n'
Expand Down Expand Up @@ -350,7 +350,7 @@ def test_ChangesetUpload_modify_way(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osmChange version="0.6" generator="osmapi/1.2.2">\n'
b'<osmChange version="0.6" generator="osmapi/1.3.0">\n'
b'<modify>\n'
b' <way id="4294967296" version="2" visible="true" '
b'changeset="4444">\n'
Expand Down Expand Up @@ -434,7 +434,7 @@ def test_ChangesetUpload_delete_relation(self):
xmltosorteddict(kwargs['data']),
xmltosorteddict(
b'<?xml version="1.0" encoding="UTF-8"?>\n'
b'<osmChange version="0.6" generator="osmapi/1.2.2">\n'
b'<osmChange version="0.6" generator="osmapi/1.3.0">\n'
b'<delete>\n'
b' <relation id="676" version="2" visible="true" '
b'changeset="4444">\n'
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/passwordfile_colon.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
testosm:testpass
testuser:test:userpass
20 changes: 20 additions & 0 deletions tests/helper_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def setupMock(self, status=200):
mock_response.reason = "test reason"
mock_response.content = 'test response'
self.api._session.request = mock.Mock(return_value=mock_response)
self.api._session.close = mock.Mock()
self.api._username = 'testuser'
self.api._password = 'testpassword'

Expand All @@ -46,6 +47,25 @@ def test_passwordfile_with_user(self):
self.assertEquals('testuser', my_api._username)
self.assertEquals('testuserpass', my_api._password)

def test_passwordfile_with_colon(self):
path = os.path.join(
__location__,
'fixtures',
'passwordfile_colon.txt'
)
my_api = osmapi.OsmApi(username='testuser', passwordfile=path)
self.assertEquals('testuser', my_api._username)
self.assertEquals('test:userpass', my_api._password)

def test_close_call(self):
self.api.close()
self.assertEquals(self.api._session.close.call_count, 1)

def test_close_context_manager(self):
with osmapi.OsmApi() as my_api:
my_api._session.close = mock.Mock()
self.assertEquals(my_api._session.close.call_count, 1)

def test_http_request_get(self):
response = self.api._http_request(
'GET',
Expand Down

0 comments on commit f50e785

Please sign in to comment.