|
| 1 | +package org.python.modules.posix; |
| 2 | + |
| 3 | +import jnr.constants.platform.Errno; |
| 4 | +import org.python.core.ArgParser; |
| 5 | +import org.python.core.Py; |
| 6 | +import org.python.core.PyObject; |
| 7 | +import org.python.core.PyType; |
| 8 | +import org.python.core.PyUnicode; |
| 9 | +import org.python.expose.ExposedGet; |
| 10 | +import org.python.expose.ExposedMethod; |
| 11 | +import org.python.expose.ExposedType; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.LinkOption; |
| 16 | +import java.nio.file.NoSuchFileException; |
| 17 | +import java.nio.file.Path; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +/** |
| 21 | + * Created by isaiah on 7/16/16. |
| 22 | + */ |
| 23 | +@ExposedType(name = "posix.DirEntry") |
| 24 | +public class PyDirEntry extends PyObject { |
| 25 | + private static final PyType TYPE = PyType.fromClass(PyDirEntry.class); |
| 26 | + |
| 27 | + private Path entry; |
| 28 | + |
| 29 | + public PyDirEntry(Path entry) { |
| 30 | + super(TYPE); |
| 31 | + this.entry = entry; |
| 32 | + } |
| 33 | + |
| 34 | + @ExposedMethod |
| 35 | + public PyObject is_dir(PyObject[] args, String[] keywords) { |
| 36 | + return Py.newBoolean(Files.isDirectory(entry, follow_symlinks("is_dir", args, keywords))); |
| 37 | + } |
| 38 | + |
| 39 | + @ExposedMethod |
| 40 | + public PyObject is_file(PyObject[] args, String[] keywords) { |
| 41 | + return Py.newBoolean(Files.isRegularFile(entry, follow_symlinks("is_file", args, keywords))); |
| 42 | + } |
| 43 | + |
| 44 | + @ExposedMethod |
| 45 | + public PyObject is_symlink() { |
| 46 | + return Py.newBoolean(Files.isSymbolicLink(entry)); |
| 47 | + } |
| 48 | + |
| 49 | + @ExposedMethod |
| 50 | + public PyObject stat(PyObject[] args, String[] keywords) { |
| 51 | + return stat(follow_symlinks("stat", args, keywords)); |
| 52 | + } |
| 53 | + |
| 54 | + private PyObject stat(LinkOption... linkOptions) { |
| 55 | + try { |
| 56 | + Map<String, Object> attributes = Files.readAttributes(entry, "unix:*", linkOptions); |
| 57 | + return PyStatResult.fromUnixFileAttributes(attributes); |
| 58 | + } catch (NoSuchFileException ex) { |
| 59 | + throw Py.OSError(Errno.ENOENT, entry.toAbsolutePath().toString()); |
| 60 | + } catch (IOException ioe) { |
| 61 | + throw Py.OSError(Errno.EBADF, entry.toAbsolutePath().toString()); |
| 62 | + } catch (SecurityException ex) { |
| 63 | + throw Py.OSError(Errno.EACCES, entry.toAbsolutePath().toString()); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + @ExposedMethod |
| 68 | + public PyObject inode() { |
| 69 | + return ((PyStatResult) stat()).st_ino; |
| 70 | + } |
| 71 | + |
| 72 | + @ExposedGet(name = "path") |
| 73 | + public PyObject getPath() { |
| 74 | + return new PyUnicode(entry.toAbsolutePath().toString()); |
| 75 | + } |
| 76 | + |
| 77 | + @ExposedGet(name = "name") |
| 78 | + public PyObject getName() { |
| 79 | + return new PyUnicode(entry.getFileName().toString()); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public String toString() { |
| 84 | + return String.format("<%.500s '%s'>", getType().fastGetName(), getName()); |
| 85 | + } |
| 86 | + |
| 87 | + private LinkOption[] follow_symlinks(String methodName, PyObject[] args, String[] keywords) { |
| 88 | + ArgParser ap = new ArgParser(methodName, args, keywords, "*", "follow_symlinks"); |
| 89 | + if (ap.getPyObject(1, Py.True).__bool__()) { |
| 90 | + return new LinkOption[] {LinkOption.NOFOLLOW_LINKS}; |
| 91 | + } |
| 92 | + return new LinkOption[0]; |
| 93 | + } |
| 94 | +} |
0 commit comments