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: aips2-request@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 0 : State( ) { } 46 : 47 0 : virtual bool initialized( ) const { 48 0 : 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 0 : virtual std::list<std::string> dataPath( ) const { 55 0 : 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 0 : std::string measuresDir( ) const { 63 0 : 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 0 : virtual std::string distroDataPath( ) const { 73 0 : return distro_data_path; 74 : } 75 : 76 0 : virtual std::string logPath( ) const { 77 0 : return log_path; 78 : } 79 : 80 0 : virtual bool noGui( ) const { 81 0 : return no_gui; 82 : } 83 : 84 0 : virtual bool agg( ) const { 85 0 : return do_agg; 86 : } 87 : 88 0 : virtual bool pipeline( ) const { 89 0 : return do_pipeline; 90 : } 91 : 92 : void clearDataPath( ) { 93 : // protect critical section... 94 : std::lock_guard<std::mutex> guard(data_path_mutex); 95 : data_path.clear( ); 96 : } 97 : 98 : void setDataPath(const std::vector<std::string> &new_list) { 99 : std::list<std::string> existing_paths; 100 : struct stat s; 101 : 102 : // accept only strings that are the path to a directory 103 : std::copy_if( new_list.begin( ), new_list.end( ), std::back_inserter(existing_paths), 104 : [&s]( std::string d ) { 105 : return (stat(d.c_str( ),&s) == 0) && (s.st_mode & S_IFDIR); 106 : } ); 107 : 108 : // protect critical section... 109 : std::lock_guard<std::mutex> guard(data_path_mutex); 110 : 111 : // always clear the existing path 112 : data_path.clear( ); 113 : 114 : // convert the paths to fully qualified paths 115 : std::transform( existing_paths.begin( ), existing_paths.end( ), 116 : std::back_inserter( data_path ), 117 : [ ]( const std::string &f ) { 118 : char *expanded = realpath(f.c_str( ), NULL); 119 : std::string result( expanded ? expanded : "" ); 120 : free(expanded); 121 : return result; 122 : } ); 123 : } 124 : 125 : void setPythonPath(const std::string &pypath) { 126 : // protect critical section... 127 : std::lock_guard<std::mutex> guard(data_path_mutex); 128 : python_path = pypath; 129 : } 130 : 131 : void setDistroDataPath(const std::string &path) { 132 : // protect critical section... 133 : std::lock_guard<std::mutex> guard(data_path_mutex); 134 : distro_data_path = path; 135 : } 136 : 137 : void setLogPath(const std::string &logpath) { 138 : // protect critical section... 139 : std::lock_guard<std::mutex> guard(data_path_mutex); 140 : log_path = logpath; 141 : } 142 : 143 : void setNoGui(bool nogui) { 144 : // protect critical section... 145 : std::lock_guard<std::mutex> guard(data_path_mutex); 146 : no_gui = nogui; 147 : } 148 : 149 : void setAgg(bool agg) { 150 : // protect critical section... 151 : std::lock_guard<std::mutex> guard(data_path_mutex); 152 : do_agg = agg; 153 : } 154 : 155 : void setPipeline(bool pipeline) { 156 : // protect critical section... 157 : std::lock_guard<std::mutex> guard(data_path_mutex); 158 : do_pipeline = pipeline; 159 : } 160 : 161 : // get map of registrations 162 : std::list<ServiceId> services( ) { return registrar.services( ); } 163 : // returns true if a registration for 'id' was found 164 : bool removeService( std::string id ) { return registrar.remove(id); } 165 : // returns assigned identifier (likely based upon the proposed_id) 166 : ServiceId addService( std::string proposed_id, std::string uri, const std::list<std::string> &types ) { 167 : return registrar.add(ServiceId(proposed_id, uri, types)); 168 : } 169 : ServiceId addService( const ServiceId &new_service ) { 170 : return registrar.add(new_service); 171 : } 172 : 173 : 174 : std::string registryURI( ) { 175 : return registrar.uri( ); 176 : } 177 : 178 : // do any necessary shutdown functions, 179 : void shutdown( ); 180 : 181 : private: 182 : std::mutex data_path_mutex; 183 : std::list<std::string> data_path; 184 : std::string python_path; 185 : std::string log_path; 186 : std::string distro_data_path; // path to data as provide by casadata pkg 187 : std::string measures_dir; 188 : bool no_gui, do_agg, do_pipeline; 189 : Registrar registrar; 190 : }; 191 : 192 : extern State &get_state( ); 193 : 194 : } 195 : 196 : 197 : #endif