Mantid
Loading...
Searching...
No Matches
ScriptRepository.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
10
11#include <boost/python/class.hpp>
12#include <boost/python/def.hpp>
13#include <boost/python/docstring_options.hpp>
14#include <boost/python/register_ptr_to_python.hpp>
15#include <boost/python/tuple.hpp>
16
17using namespace Mantid::API;
18using namespace boost::python;
19
21
22namespace {
25//------------------------------------------------------------------------------------------------------
31PyObject *getListFiles(ScriptRepository &self) {
32 std::vector<std::string> files = self.listFiles();
33
34 PyObject *registered = PyList_New(0);
35 for (const auto &file : files) {
36 PyObject *value = to_python_value<const std::string &>()(file);
37 if (PyList_Append(registered, value))
38 throw std::runtime_error("Failed to insert value into PyList");
39 }
40
41 return registered;
42}
43
44tuple getInfo(ScriptRepository &self, const std::string &path) {
45 ScriptInfo info = self.info(path);
46 return boost::python::make_tuple<std::string>(info.author, info.pub_date.toSimpleString());
47}
48
49PyObject *getStatus(ScriptRepository &self, const std::string &path) {
50 SCRIPTSTATUS st = self.fileStatus(path);
51 PyObject *value;
52 switch (st) {
53 case BOTH_UNCHANGED:
54 value = to_python_value<char const *&>()("BOTH_UNCHANGED");
55 break;
56 case REMOTE_ONLY:
57 value = to_python_value<char const *&>()("REMOTE_ONLY");
58 break;
59 case LOCAL_ONLY:
60 value = to_python_value<char const *&>()("LOCAL_ONLY");
61 break;
62 case REMOTE_CHANGED:
63 value = to_python_value<char const *&>()("REMOTE_CHANGED");
64 break;
65 case LOCAL_CHANGED:
66 value = to_python_value<char const *&>()("LOCAL_CHANGED");
67 break;
68 case BOTH_CHANGED:
69 value = to_python_value<char const *&>()("BOTH_CHANGED");
70 break;
71 default:
72 value = to_python_value<char const *&>()("BOTH_UNCHANGED");
73 break;
74 }
75 return value;
76}
77
78PyObject *getDescription(ScriptRepository &self, const std::string &path) {
79 return to_python_value<const std::string &>()(self.description(path));
80}
81
83} // namespace
84
86
87 register_ptr_to_python<std::shared_ptr<ScriptRepository>>();
88
89 // reset the option to
90 docstring_options local_docstring_options(true, true, false);
91
92 const char *repo_desc = "Manage the interaction between the users and the Script folder (mantid subproject). \n\
93\n\
94Inside the mantid repository (https://github.com/mantidproject) there is also a subproject \n\
95called scripts (https://github.com/mantidproject/scripts), created to allow users to share \n\
96their scripts, as well as to allow Mantid Team to distribute to the Mantid community \n\
97scripts for analysis and also to enhance the quality of the scripts used for the sake of \n\
98data analysis. \n\
99\n\
100The ScriptRepository aims to provide a simple way to interact with that repository in \n\
101order to promote its usage. In order to enhance the usage, it is necessary:\n\
102 \n\
103 - List all scripts available at the repository\n\
104 - Download selected scripts. \n\
105 - Check for updates\n\
106 - Allow to publish users scripts/folders. \n\
107\n\
108Basically, you will need to install the repository through ::install(local_path). \n\
109After, you will be able to ::listFiles inside your repository to check the files that are \n\
110available, eventually, you may want to check the information of a single entry \n\
111::fileInfo(path). You may also be interested to know if there is a new version available, \n\
112or if the file has been modified since last downloaded (::fileStatus(path)). You may want \n\
113to download scripts through ::download(path), or check for updates through ::update(). \n\
114\n\
115'''NOTE:''' Upload is not implemented yet.\n";
116
117 const char *list_files_desc = "Return an array with all the entries inside the repository. \n\
118\n\
119Folder os files, locally or remotely, all will be listed together through the listFiles. \n\
120The listFiles has another function, which is related to update the internal cache about \n\
121the status and information of the files. So, local changes or remote changes will only be \n\
122available to fileStatus of fileInfo after listFiles.\n\
123\n\
124Returns:\n\
125\n\
126 list: entries inside the repository.\n";
127
128 const char *file_info_desc = "Return general information from the entries inside ScriptRepository. \n\
129\n\
130The author, description and publication date are available through this method. \n\
131\n\
132Arguments:\n\
133\n\
134 - path(str): Path to the entry.\n\
135\n\
136Returns:\n\\n\
137 - tuple: (author, last publication date)\n";
138
139 const char *file_description_desc = "Return description of the entry inside ScriptRepository. \n\
140\n\
141Arguments:\n\
142\n\
143 - path: Path to the entry.\n\
144\n\
145Return:\n\
146 - string with the description\n";
147
148 const char *file_status_desc = "Return the status of a given entry.\n\
149\n\
150The following status are applied to the entries:\n\
151 - REMOTE_ONLY: The file is only at the central repository\n\
152 - LOCAL_ONLY: The file is only in your file system\n\
153 - BOTH_UNCHAGED: The file is in your file system and remotely, and are iqual.\n\
154 - REMOTE_CHANGED: A new version is available for this file.\n\
155 - LOCAL_CHANGED: You have edited the file\n\
156 - BOTH_CHANGED: There is a new version and you have changed as well\n\
157\n\
158'''NOTE:''' ScriptRepository recognizes changes locally and remotely only through \n\
159listFiles method.\n\
160Arguments:\n\
161\n\
162 - path (str): The path for the entry.\n\
163\n\
164Returns:\n\
165\n\
166 - str: Status of the entry.\n";
167
168 const char *download_desc = "Download from repository into your local file system.\n\
169\n\
170You may give a file or folder. If the later is given, ScriptRepository will \n\
171download all the files inside that folder from the remote repository to you.\n\
172\n\
173Arguments:\n\
174\n\
175 - path (str): Path for the entry do download";
176
177 const char *update_desc = "Check for updates at the remote repository.\n\
178\n\
179New versions of the files may be available, and the update method will check the \n\
180remote repository to see if there is anything new. It will not download new versions \n\
181of the available files unless you ask to do so. You should do this often to check if \n\
182there is a new script to solve your problem ;)";
183
184 const char *install_desc = "Install the ScriptRepository in your local file system\n\
185\n\
186The installation of the ScriptRepository is very simple. You must only provide a path, \n\
187existing or new folder, where the ScriptRepository will put the database it requires to \n\
188run itself. The installation requires network connection, to connect to the central \n\
189repository but usually takes very few moments to be installed. After installing, all the \n\
190others methods will be available.\n\
191Arguments:\n\
192\n\
193 - path (str): An existing or path to a new folder to be created, where the ScriptRepository will install itself.\
194";
195
197 class_<ScriptRepository, boost::noncopyable>("ScriptRepository", repo_desc, no_init)
198 .def("install", &ScriptRepository::install, (arg("self"), arg("local_path")), install_desc)
199 .def("listFiles", &getListFiles, arg("self"), list_files_desc)
200 .def("fileInfo", &getInfo, (arg("self"), arg("path")), file_info_desc)
201 .def("description", &getDescription, (arg("self"), arg("path")), file_description_desc)
202 .def("fileStatus", &getStatus, (arg("self"), arg("path")), file_status_desc)
203 .def("download", &ScriptRepository::download, (arg("self"), arg("file_path")), download_desc)
204 .def("update", &ScriptRepository::check4Update, arg("self"), update_desc);
205}
double value
The value of the point.
Definition: FitMW.cpp:51
#define GET_POINTER_SPECIALIZATION(TYPE)
Definition: GetPointer.h:17
void export_ScriptRepository()
virtual void install(const std::string &local_path)=0
Install the necessary resources at the local_path given that allows the ScriptRepository to operate l...
virtual const std::string & description(const std::string &path)=0
Provide the description of the file given the path.
virtual SCRIPTSTATUS fileStatus(const std::string &file_path)=0
Return the status of the file, according to the status defined in Mantid::API::SCRIPTSTATUS.
virtual ScriptInfo info(const std::string &path)=0
Return the information about the script through the Mantid::API::ScriptInfo struct.
virtual std::vector< std::string > listFiles()=0
Return the list of files inside the repository.
virtual void download(const std::string &file_path)=0
Create a copy of the remote file/folder inside the local repository.
virtual std::vector< std::string > check4Update()=0
Connects to the remote repository checking for updates.
SCRIPTSTATUS
Represent the possible states for a given file:
Information about the files inside the repository.
Types::Core::DateAndTime pub_date
Time of the last update of this file (remotelly)
std::string author
Identification of the author of the script.