Skip to content

Commit 3cebe1c

Browse files
committed
use PyReader in- and externally for newest_change_from_file()
1 parent 6dc8fb3 commit 3cebe1c

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

lib/replication.cc

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
* For a full list of authors see the git log.
77
*/
88
#include <pybind11/pybind11.h>
9+
#include <pybind11/stl/filesystem.h>
910

1011
#include <osmium/osm.hpp>
11-
#include <osmium/io/any_input.hpp>
1212
#include <osmium/handler.hpp>
1313
#include <osmium/visitor.hpp>
14+
15+
#include <filesystem>
16+
1417
#include "cast.h"
18+
#include "io.h"
1519

1620
namespace py = pybind11;
1721

@@ -29,6 +33,12 @@ struct LastChangeHandler : public osmium::handler::Handler
2933
}
3034
};
3135

36+
osmium::Timestamp newest_change_from_file(pyosmium::PyReader &reader) {
37+
LastChangeHandler handler;
38+
osmium::apply(*reader.get(), handler);
39+
return handler.last_change;
40+
}
41+
3242
} // namespace
3343

3444
#ifdef Py_GIL_DISABLED
@@ -37,14 +47,18 @@ PYBIND11_MODULE(_replication, m, py::mod_gil_not_used())
3747
PYBIND11_MODULE(_replication, m)
3848
#endif
3949
{
40-
m.def("newest_change_from_file", [](char const *filename)
41-
{
42-
osmium::io::Reader reader(filename, osmium::osm_entity_bits::nwr);
43-
44-
LastChangeHandler handler;
45-
osmium::apply(reader, handler);
46-
reader.close();
47-
48-
return handler.last_change;
49-
});
50+
m.def("newest_change_from_file", &newest_change_from_file,
51+
py::arg("reader"));
52+
m.def("newest_change_from_file", [](std::string file) {
53+
pyosmium::PyReader reader(osmium::io::File(std::move(file)));
54+
return newest_change_from_file(reader);
55+
});
56+
m.def("newest_change_from_file", [](std::filesystem::path const &file) {
57+
pyosmium::PyReader reader(osmium::io::File(file.string()));
58+
return newest_change_from_file(reader);
59+
});
60+
m.def("newest_change_from_file", [](osmium::io::File file) {
61+
pyosmium::PyReader reader(std::move(file));
62+
return newest_change_from_file(reader);
63+
});
5064
}

src/osmium/replication/_replication.pyi

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
#
33
# This file is part of pyosmium. (https://osmcode.org/pyosmium/)
44
#
5-
# Copyright (C) 2024 Sarah Hoffmann <[email protected]> and others.
5+
# Copyright (C) 2025 Sarah Hoffmann <[email protected]> and others.
66
# For a full list of authors see the git log.
7+
from typing import Union
78
import datetime
9+
import os
810

9-
def newest_change_from_file(filename: str) -> datetime.datetime:
10-
""" Find the data of the most recent change in a file.
11+
from ..io import Reader, File
12+
13+
def newest_change_from_file(file: Union[str, 'os.PathLike[str]', File, Reader]
14+
) -> datetime.datetime:
15+
""" Find the date of the most recent change in a file.
1116
"""

0 commit comments

Comments
 (0)