Debian Codegraph @

plugins/script/interfaces/FileSystemInterface.h

raw
1 #pragma once
2
3 #include <pybind11/pybind11.h>
4
5 #include "iscript.h"
6 #include "iscriptinterface.h"
7 #include "ifilesystem.h"
8
9 namespace script
10 {
11
12 /**
13 * Adaptor interface for a VFS traversor object.
14 */
15 class VirtualFileSystemVisitor
16 {
17 public:
18 virtual ~VirtualFileSystemVisitor() {}
19
20 /**
21 * Required visit method. Takes the filename is relative
22 * to the base path passed to the GlobalFileSystem().foreachFile method.
23 */
24 virtual void visit(const std::string& filename) = 0;
25 };
26
27 // Scripts will derive from this class
28 class FileVisitorWrapper :
29 public VirtualFileSystemVisitor
30 {
31 public:
32 void visit(const std::string& filename) override
33 {
34 // Wrap this method to python
35 PYBIND11_OVERLOAD_PURE(
36 void, /* Return type */
37 VirtualFileSystemVisitor, /* Parent class */
38 visit, /* Name of function in C++ (must match Python name) */
39 filename /* Argument(s) */
40 );
41 }
42 };
43
44 /**
45 * greebo: This class registers the VFS interface with the
46 * scripting system.
47 */
48 class FileSystemInterface :
49 public IScriptInterface
50 {
51 public:
52 // Reads a text file and returns the contents to the script
53 // Filename is a VFS path, not an absolute OS path
54 // Returns an empty string if not found
55 std::string readTextFile(const std::string& filename);
56
57 // Wrapped methods, see "ifilesystem.h" for documentation
58 void forEachFile(const std::string& basedir, const std::string& extension,
59 VirtualFileSystemVisitor& visitor, std::size_t depth);
60
61 int getFileCount(const std::string& filename);
62 std::string findFile(const std::string& name);
63 std::string findRoot(const std::string& name);
64
65 // IScriptInterface implementation
66 void registerInterface(py::module& scope, py::dict& globals) override;
67 };
68
69 } // namespace script