SourceXtractorPlusPlus  0.13
Please provide a description of the project.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PyOutputWrapper.cpp
Go to the documentation of this file.
1 
17 /*
18  * @file PyOutputWrapper.cpp
19  * @author Alejandro Alvarez Ayllon
20  */
21 
23 #include <boost/locale/encoding_utf.hpp>
24 #include <boost/python/extract.hpp>
25 
26 namespace SourceXtractor {
27 
28 namespace bp = boost::python;
29 using boost::locale::conv::utf_to_utf;
30 
31 PyOutputWrapper::PyOutputWrapper(Elements::Logging& logger) : closed(false), m_logger(logger) {}
32 
34  PyErr_Format(PyExc_OSError, "Method %s not supported on wrapper file objects", __func__);
35  bp::throw_error_already_set();
36 }
37 
39  PyErr_Format(PyExc_OSError, "Method %s not supported on wrapper file objects", __func__);
40  bp::throw_error_already_set();
41  return -1;
42 }
43 
45 }
46 
48  return false;
49 }
50 
52  return false;
53 }
54 
56  PyErr_Format(PyExc_IOError, "Object is write only");
57  bp::throw_error_already_set();
58  return {};
59 }
60 
62  PyErr_Format(PyExc_IOError, "Object is write only");
63  bp::throw_error_already_set();
64  return {};
65 }
66 
68  PyErr_Format(PyExc_IOError, "Object is write only");
69  bp::throw_error_already_set();
70  return {};
71 }
72 
73 int PyOutputWrapper::seek(int, int) {
74  PyErr_Format(PyExc_OSError, "Method %s not supported on wrapper file objects", __func__);
75  bp::throw_error_already_set();
76  return 0;
77 }
78 
80  return false;
81 }
82 
83 int PyOutputWrapper::tell() const {
84  PyErr_Format(PyExc_OSError, "Method %s not supported on wrapper file objects", __func__);
85  bp::throw_error_already_set();
86  return 0;
87 }
88 
90  PyErr_Format(PyExc_OSError, "Method %s not supported on wrapper file objects", __func__);
91  bp::throw_error_already_set();
92 }
93 
95  return true;
96 }
97 
98 int PyOutputWrapper::write(const bp::object& obj) {
99  bp::extract<std::string> string_extractor{obj};
100  bp::extract<std::wstring> unicode_extractor{obj};
101  std::string str;
102 
103  if (string_extractor.check()) {
104  str = string_extractor;
105  }
106  else if (unicode_extractor.check()) {
107  std::wstring unicode = unicode_extractor;
108  str = utf_to_utf<char>(unicode.c_str(), unicode.c_str() + unicode.size());
109  }
110  else {
111  std::string obj_type_name = bp::extract<std::string>(obj.attr("__class__").attr("__name__"));
112  PyErr_Format(PyExc_TypeError,
113  "SourceXtractor output wrapper only accepts classic strings or unicode strings, got %s",
114  obj_type_name.c_str()
115  );
116  bp::throw_error_already_set();
117  }
118 
119  for (auto c : str) {
120  if (c == '\n') {
121  m_logger.info() << m_buffer.str();
123  } else {
124  m_buffer << c;
125  }
126  }
127  return str.size();
128 }
129 
130 void PyOutputWrapper::writelines(const bp::list& lines) {
131  for (int i = 0; i < bp::len(lines); ++i) {
132  write(lines[i]);
133  }
134 }
135 
136 } // end namespace SourceXtractor
137 
static auto logger
Definition: WCS.cpp:46
void info(const std::string &logMessage)
STL class.
boost::python::list readlines(int)
PyOutputWrapper(Elements::Logging &logger)
int write(const boost::python::object &)
T size(T...args)
T c_str(T...args)
void writelines(const boost::python::list &)