Line data Source code
1 : //# State.h: application state for CASAtools python module 2 : //# Copyright (C) 2017 3 : //# Associated Universities, Inc. Washington DC, USA. 4 : //# 5 : //# This library is free software; you can redistribute it and/or modify it 6 : //# under the terms of the GNU Library General Public License as published by 7 : //# the Free Software Foundation; either version 2 of the License, or (at your 8 : //# option) any later version. 9 : //# 10 : //# This library is distributed in the hope that it will be useful, but WITHOUT 11 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 13 : //# License for more details. 14 : //# 15 : //# You should have received a copy of the GNU Library General Public License 16 : //# along with this library; if not, write to the Free Software Foundation, 17 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 18 : //# 19 : //# Correspondence concerning AIPS++ should be addressed as follows: 20 : //# Internet email: casa-feedback@nrao.edu. 21 : //# Postal address: AIPS++ Project Office 22 : //# National Radio Astronomy Observatory 23 : //# 520 Edgemont Road 24 : //# Charlottesville, VA 22903-2475 USA 25 : //# 26 : #ifndef CASATOOLS_CONFIG_STATE_H 27 : #define CASATOOLS_CONFIG_STATE_H 28 : #include <sys/stat.h> 29 : #include <list> 30 : #include <mutex> 31 : #include <string> 32 : #include <vector> 33 : #include <memory> 34 : #include <algorithm> 35 : #include <limits.h> 36 : #include <stdlib.h> 37 : #include <casacore/casa/System/AppState.h> 38 : #include <casatools/Proc/Registrar.h> 39 : 40 : namespace casatools { /** namespace for CASAtools classes within "CASA code" **/ 41 : 42 : class State: public casacore::AppState { 43 : public: 44 : 45 3 : State( ) { } 46 : 47 1349 : virtual bool initialized( ) const { 48 1349 : return true; 49 : } 50 : 51 : // use the data path to find the filename... 52 : virtual std::string resolve(const std::string &filename) const; 53 : 54 888 : virtual std::list<std::string> dataPath( ) const { 55 888 : return data_path; 56 : } 57 : 58 : // get directory containing IERS measures data 59 : // an exception is thrown if it does not exist or 60 : // does not contain the IERS tables 61 : // * should contain IERS tables 62 3613 : std::string measuresDir( ) const { 63 3613 : return distro_data_path; 64 : } 65 : 66 0 : virtual std::string pythonPath( ) const { 67 0 : return python_path; 68 : } 69 : 70 : // * should contain viewer colormap tables 71 : // * should contain IERS tables 72 1 : virtual std::string distroDataPath( ) const { 73 1 : return distro_data_path; 74 : } 75 : 76 0 : virtual std::string logPath( ) const { 77 0 : return log_path; 78 : } 79 : 80 8 : virtual bool noGui( ) const { 81 8 : return no_gui; 82 : } 83 : 84 8 : virtual bool agg( ) const { 85 8 : return do_agg; 86 : } 87 : 88 8 : virtual bool pipeline( ) const { 89 8 : return do_pipeline; 90 : } 91 : 92 0 : virtual std::string cachedir( ) const { 93 0 : return cache_dir; 94 : } 95 : 96 : void clearDataPath( ) { 97 : // protect critical section... 98 : std::lock_guard<std::mutex> guard(data_path_mutex); 99 : data_path.clear( ); 100 : } 101 : 102 : void setDataPath(const std::vector<std::string> &new_list) { 103 : std::list<std::string> existing_paths; 104 : struct stat s; 105 : 106 : // accept only strings that are the path to a directory 107 : std::copy_if( new_list.begin( ), new_list.end( ), std::back_inserter(existing_paths), 108 : [&s]( std::string d ) { 109 : return (stat(d.c_str( ),&s) == 0) && (s.st_mode & S_IFDIR); 110 : } ); 111 : 112 : // protect critical section... 113 : std::lock_guard<std::mutex> guard(data_path_mutex); 114 : 115 : // always clear the existing path 116 : data_path.clear( ); 117 : 118 : // convert the paths to fully qualified paths 119 : std::transform( existing_paths.begin( ), existing_paths.end( ), 120 : std::back_inserter( data_path ), 121 : [ ]( const std::string &f ) { 122 : char *expanded = realpath(f.c_str( ), NULL); 123 : std::string result( expanded ? expanded : "" ); 124 : free(expanded); 125 : return result; 126 : } ); 127 : } 128 : 129 : void setPythonPath(const std::string &pypath) { 130 : // protect critical section... 131 : std::lock_guard<std::mutex> guard(data_path_mutex); 132 : python_path = pypath; 133 : } 134 : 135 : void setDistroDataPath(const std::string &path) { 136 : // protect critical section... 137 : std::lock_guard<std::mutex> guard(data_path_mutex); 138 : distro_data_path = path; 139 : } 140 : 141 : void setLogPath(const std::string &logpath) { 142 : // protect critical section... 143 : std::lock_guard<std::mutex> guard(data_path_mutex); 144 : log_path = logpath; 145 : } 146 : 147 : void setNoGui(bool nogui) { 148 : // protect critical section... 149 : std::lock_guard<std::mutex> guard(data_path_mutex); 150 : no_gui = nogui; 151 : } 152 : 153 : void setAgg(bool agg) { 154 : // protect critical section... 155 : std::lock_guard<std::mutex> guard(data_path_mutex); 156 : do_agg = agg; 157 : } 158 : 159 : void setPipeline(bool pipeline) { 160 : // protect critical section... 161 : std::lock_guard<std::mutex> guard(data_path_mutex); 162 : do_pipeline = pipeline; 163 : } 164 : 165 : void setCachedir(const std::string &cacheDir) { 166 : // protect critical section... 167 : std::lock_guard<std::mutex> guard(data_path_mutex); 168 : cache_dir = cacheDir; 169 : } 170 : 171 : // get map of registrations 172 : std::list<ServiceId> services( ) { return registrar.services( ); } 173 : // returns true if a registration for 'id' was found 174 : bool removeService( std::string id ) { return registrar.remove(id); } 175 : // returns assigned identifier (likely based upon the proposed_id) 176 : ServiceId addService( std::string proposed_id, std::string uri, const std::list<std::string> &types ) { 177 : return registrar.add(ServiceId(proposed_id, uri, types)); 178 : } 179 : ServiceId addService( const ServiceId &new_service ) { 180 : return registrar.add(new_service); 181 : } 182 : 183 : 184 : std::string registryURI( ) { 185 : return registrar.uri( ); 186 : } 187 : 188 : // do any necessary shutdown functions, 189 : void shutdown( ); 190 : 191 : private: 192 : std::mutex data_path_mutex; 193 : std::list<std::string> data_path; 194 : std::string python_path; 195 : std::string log_path; 196 : std::string distro_data_path; // path to data as provide by casadata pkg 197 : std::string measures_dir; 198 : bool no_gui, do_agg, do_pipeline; 199 : std::string cache_dir; 200 : Registrar registrar; 201 : }; 202 : 203 : extern State &get_state( ); 204 : 205 : } 206 : 207 : 208 : #endif