def remap_dsi(self) : """ to do : for geoloc call on only one DSI """ print() log.debug("... running remap_dsi ...")
def LocToDict(location_raw, src_geocoder=None) : """ location formater """ log.debug("... LocToDict / location_raw : \n%s ", pformat(location_raw) ) if location_raw != None : return { "src_geocoder" : src_geocoder, "raw" : location_raw.raw, "address" : location_raw.address, "point" : location_raw.point, # "latitude" : location_raw.latitude, # "longitude" : location_raw.longitude, "lat" : location_raw.latitude, "lon" : location_raw.longitude, } else : return { "src_geocoder" : src_geocoder, "raw" : None, "address" : None, "point" : None, # "latitude" : None, # "longitude" : None, "lat" : None, "lon" : None, }
def send_email(subject, to, sender=current_app.config["MAIL_DEFAULT_SENDER"], template=None): """ generic function to send an email """ log.debug("... sending email...") msg = Message( subject, recipients=[to], sender=sender, html=template, ) app = current_app._get_current_object() ### cf : http://flask.pocoo.org/docs/0.12/reqcontext/#notes-on-proxies ### simple and synchronous run # mail.send(msg) ### asynchronous run # thr = Thread(target=send_async_email, args=[app, msg]) # thr.start() ### asynchronous run based on send_async_email and async decorator send_async_email(app, msg)
def check_cred(self, window): """ Given credentials input check if we can perform action of login or registration on server Args: window (PySimpleGUIQt.Window): GUI-Window asking for creds Returns: bool: True if credentials are valid, else False """ event, values = window.read() log.debug(f"{event} - {values}\n\n") if event in (sg.WIN_CLOSED, "Cancel"): log.debug("Credentials entry canceled") return True else: self.set_config_ignore_ssl_cert(values.get("ignore_cert")) if self.is_cred_input_valid(values): server, user, pw = self.is_cred_input_valid(values) if event == "Login": if self.check_cred_login_and_save(server, user, pw): sg.popup( f"Credentials saved to config file: {Config.PATH_CONFIG_FILE}", title=f"{Config.APP_NAME}", ) return True elif event == "Register": if self.check_cred_register_and_save(server, user, pw): sg.popup( f"Credentials saved to config file: {Config.PATH_CONFIG_FILE}", title=f"{Config.APP_NAME}", ) return True return False
def read_file_with_pd(uploaded_file, file_extension, sep=",", encoding="utf-8"): if file_extension in ["csv", "tsv"]: if file_extension == "tsv": sep = "\t" log.debug("uploaded_file : %s", uploaded_file) try: df = pd.read_csv(uploaded_file, sep=sep, encoding=encoding) except: df = pd.read_csv(uploaded_file, sep=sep) elif file_extension in ["xls", "xlsx"]: df = pd.read_excel(uploaded_file, encoding=encoding) elif file_extension in ["xml"]: pass # ### TO DO !!! # df = pd.read_excel(uploaded_file) ### check if column names contain "." --> needs to be changed so bson could insert to db # df_cols = list(df.columns.values) # df_cols_clean = { colname : cleanColumnName(colname) for colname in df_cols } # df = df.rename( index=str, columns = df_cols_clean) df = cleanColNames(df, charsToReplace=u"°.[]", replaceWith="-") ### clean df from NaNs # df = df.dropna(how="all") # df = df.replace({np.nan:None}) df = cleanDfFromNans(df) return df
def build_projected_fields(ignore_fields_list=[], keep_fields_list=[]): """ projection of the query """ ### cf : https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/ print() print("-+- " * 40) log.debug("... build_projected_fields ") # add ignore_fields / keep_fields criterias to query if any projected_fields = None if ignore_fields_list != [] or keep_fields_list != []: projected_fields = {} # add fields to ignore if ignore_fields_list != []: ignore_fields = {f: 0 for f in ignore_fields_list} projected_fields.update(ignore_fields) # add fields to retrieve if keep_fields_list != []: keep_fields = {f: 1 for f in keep_fields_list} projected_fields.update(keep_fields) return projected_fields
def show_clip_list_window(self, clips): """ Show all received clips in a Windows with a Listview and allow user select and copy an entry """ # TODO: Show Only preview of Clip to reduce length layout = [ [ sg.Listbox( values=clips, size=(60, 6), select_mode="LISTBOX_SELECT_MODE_SINGLE", key="sel_clip", ) ], [sg.Button("Copy to clipboard", size=(20, 1)), sg.Cancel(size=(20, 1))], ] window = sg.Window( title=f"{Config.APP_NAME} - Your Clips", layout=layout, size=(600, 300), icon=Config.ICON_B64, ) event, values = window.read() if event in (sg.WIN_CLOSED, "Cancel"): log.debug("List Clips selection canceled") pass elif event == "Copy to clipboard": Api.paste(values.get("sel_clip")[0]) window.close()
def is_cred_input_valid(self, values): """ Are credentials complete and server address valid? """ server = values.get("server") if server == "": server = Config.DEFAULT_SERVER_URI user = values.get("user") pw = values.get("pw") if user == "" or pw == "": sg.popup( f"Please enter an username and password", title=f"{Config.APP_NAME}", ) return False if not self.is_valid_server_address(server): sg.popup( f"Invalid server address: {server}\nUse format https://domain.tld:port", title=f"{Config.APP_NAME}", ) return False if len(pw) < Config.MIN_PW_LENGTH: sg.popup( f"Your password must have at least 8 characters ", title=f"{Config.APP_NAME}", ) return False log.debug(f"{server} - {user} - {pw}") return server, user, pw
def cleanColumnName(colname, charsToReplace=u"°.[]", replaceWith="-"): log.debug("cleanColumnName / colname : %s", colname) colname_clean = colname.replace('"', "'") for char in charsToReplace: colname_clean = colname_clean.replace(char, replaceWith) log.debug("cleanColumnName / colname_clean : %s\n", colname_clean) return colname_clean
def update_view(self): info_serial = "Numer seryjny: %s" % self.info_collector.cfg_serial self.txt_var_serial.set(info_serial) str_mon_info = self.info_collector.cfg_address_mon + "\n" + self.info_collector.cfg_netmask_mon self.txt_var_mon_cfg.set(str_mon_info) config_info = "[odwiedź %s:8080/config/ aby zmienić]" % self.info_collector.cfg_address_mon cams_configs = self.info_collector.cams_configs cams_statuses = self.info_collector.cams_statuses cam_status = '' for i in range(0, MAX_CAM_AMOUNT): cam_config = cams_configs[i] address = cam_config.address port, type_name, auth, profiles = cam_config.cfg_info() status = cams_statuses[i] cam_address = '- nie skonfigurowano -' if not address and status != CAM_STATUS_INIT else address.rjust(15, ' ') if address: cam_status += 'kam:%s %s poł:%s port:%s %s %s %s\n' % ((i + 1), cam_address, status, port, type_name.ljust(17, ' '), auth, profiles) else: cam_status += 'kam:%s %s %s\n' % ((i + 1), cam_address.rjust(15, ' '), status) self.txt_var_cam_cfg_page.set("%s\n\n" % config_info) self.txt_var_cam_cfg_stream.set("%s" % cam_status.rstrip('\n')) self.txt_var_version.set("Wersja: %s.%s" % (self.info_collector.cfg_version, self.info_collector.cfg_revision)) log.debug("Labels updated.")
def send_async_email(curr_app, msg): """ send an email asynchronously due to the decorator """ log.debug("... sending async email...") log.debug("... msg : \n%s", pformat(msg)) with curr_app.app_context(): mail.send(msg)
def on_timer(self): log.debug("View update.") try: self.update_view() except BaseException as e: log.error('Could not update view (%s).' % repr(e)) if self.info_collector.is_enabled: self.after(TXT_UPDATE_INTERVAL, self.on_timer)
def fetch_serial_number(): log.debug('Opening serial number config file: %r.' % CFG_SERIAL_FILENAME) try: with open(CFG_SERIAL_FILENAME) as cfg_serial_file: serial_cfg_content = cfg_serial_file.read().strip() cfg_serial_file.close() return serial_cfg_content except IOError: log.debug('Serial number config file not found...') return DEFAULT_SERIAL
def fetch_version(): try: log.debug('Opening software version file: %r.' % CFG_VERSION_FILENAME) with open(CFG_VERSION_FILENAME) as version_file: version_cfg_content = version_file.read().strip() version_file.close() return version_cfg_content except IOError: log.debug('Camera IP config file not found...') return DEFAULT_VERSION
def fetch_cfg_mon(): try: log.debug('Opening monitor IP config file: %r.' % CFG_MON_IP_FILENAME) with open(CFG_MON_IP_FILENAME) as cfg_mon_file: str_mon_cfg = cfg_mon_file.read().strip() interface_cfg_content = parse_interfaces_cfg(str_mon_cfg) cfg_mon_file.close() return interface_cfg_content except IOError: log.error('Monitor IP config file not found...') return MON_ADDRESS_NOT_CONFIGURED, MON_NETMASK_NOT_CONFIGURED
def create_app(app_name='SOLIDATA_API', run_mode="dev", RSA_mode="yes", anojwt_mode="yes"): log.debug("... creating app ...") ### create Flask app app = Flask(app_name) # app.wsgi_app = ProxyFix(app.wsgi_app) app.config.SWAGGER_VALIDATOR_URL = 'http://domain.com/validator' ### load config if run_mode == "prod": app.config.from_object('solidata_api.config_prod.Prod') elif run_mode == "preprod": app.config.from_object('solidata_api.config_prod.Preprod') elif run_mode == "dev_email": app.config.from_object('solidata_api.config_prod.DevEmail') else: app.config.from_object('solidata_api.config.BaseConfig') ### append SALT and ANOJWT env vars to config app.config["RSA_MODE"] = RSA_mode app.config["ANOJWT_MODE"] = anojwt_mode app.config["APP_VERSION"] = "0.2.1 beta" app.config["SWAGGER_BASE_URL"] = os.environ[ "SWAGGER_BASE_URL"] = app.config["SERVER_NAME"] print() log.debug("... app.config :\n %s", pformat(app.config)) print() ### init JWT manager log.debug("... init jwt_manager ...") jwt_manager.init_app(app) ### init mongodb client log.debug("... init mongo ...") # cf : https://flask-pymongo.readthedocs.io/en/latest/#flask_pymongo.PyMongo.init_app # init_app(app, uri=None, *args, **kwargs)¶ # If uri is None, and a Flask config variable named MONGO_URI exists, use that as the uri as above. # You must now use a MongoDB URI to configure Flask-PyMongo mongo.init_app(app) ### ### init mail client log.debug("... init mail ...") mail.init_app(app) with app.app_context(): # import async functions and decorators from solidata_api._core.async_tasks import async
def wrapper(*args, **kwargs): log.debug("-@- confirm_email checker") verify_jwt_in_request() claims = get_jwt_claims() log.debug("claims : \n %s", pformat(claims)) if claims["confirm_email"] != True: return {"msg": "'confirm_email' token expected !!! "}, 403 else: return func(*args, **kwargs)
def was_configfile_modified(cls): """ Check if configfile has been changed since last modificatiom """ log.debug("Checking if configfile has been modified") if cls.CONFIGFILE_MTIME: mtime = Path(cls.PATH_CONFIG_FILE).stat().st_mtime if mtime > cls.CONFIGFILE_MTIME: return True else: return False cls.CONFIGFILE_MTIME = Path(cls.PATH_CONFIG_FILE).stat().st_mtime return False
def marshal_as_min(self, results_list): ns = self.ns self.results_list = results_list log.debug('results_list ...') # log.debug('results_list : \n%s', pformat(results_list[:1]) ) @ns.marshal_with(self.model_doc_min) def get_results(): return results_list return get_results()
def build_first_term_query(ds_oid, query_args, field_to_query="oid_dso"): """ build query understandable by mongodb inspired by work on openscraper """ print() print("-+- " * 40) log.debug("... build_first_term_query ") query = {field_to_query: ds_oid} log.debug('query_args : \n%s', pformat(query_args)) search_for = query_args.get('search_for', None) search_in = query_args.get('search_in', None) search_int = query_args.get('search_int', None) search_float = query_args.get('search_float', None) search_tags = query_args.get('search_tags', None) item_id = query_args.get('item_id', None) is_complete = query_args.get('is_complete', None) search_filters = query_args.get('search_filters', []) ### TO FINISH ... ### append filters if search_filters != None and search_filters != []: query = append_filters_to_query(query, search_filters) # search by item_id if item_id != None: q_item = {"_id": {"$in": [ObjectId(i_id) for i_id in item_id]}} query.update(q_item) ### TO DO ... # search by tags if search_tags != None: pass # q_tag = { "_id" : ObjectId(item_id) } # query.update(q_tag) ### search by content --> collection need to be indexed # cf : https://stackoverflow.com/questions/6790819/searching-for-value-of-any-field-in-mongodb-without-explicitly-naming-it if search_for != None and search_for != [] and search_for != ['']: search_words = ["\"" + word + "\"" for word in search_for] q_search_for = { "$text": { "$search": u" ".join(search_words) } # doable because text fields are indexed at main.py } query.update(q_search_for) return query
def wait_for_tray_event(mygui, server, username, hash_login, hash_msg): """ Check for config modification while waiting for action in systray """ log.debug("Main Loop\n") log.debug(f"{server} - {username} - {hash_login} - {hash_msg}") api = Api(server, username, hash_login, hash_msg) while True: if Config.was_configfile_modified(): log.info("Configfile was modified.") self_restart() event = mygui.tray.read() deal_with_tray_event(mygui, api, event)
def ping(cam_address): try: log.debug('Pinging camera with IP = (%s)' % cam_address) ping_resp = pyping.ping(cam_address) ping_status = ping_resp.ret_code log.debug('Ping return code for %s = %d' % (cam_address, ping_status)) if ping_status == 0: return CAM_STATUS_OK else: return CAM_STATUS_NOT_CONNECTED except BaseException as e: log.error('PYPING library error: (%s)' % e.message) return CAM_STATUS_CON_ERROR
def wrapper(*args, **kwargs): log.debug("-@- current_user checker") verify_jwt_in_request() claims = get_jwt_claims() log.debug("claims : \n %s", pformat(claims)) log.debug("kwargs : \n %s", pformat(kwargs)) ### check in kwargs user_oid = kwargs["usr_id"] log.debug("user_oid : %s", user_oid) ### check if oid sent is the same as the claim "_id" if user_oid != claims["_id"]: ### authorize if user is an admin if claims["auth"]["role"] == 'admin': return func(*args, **kwargs) ### stops if user is neither an admin nor the current user else: return { "msg": "Admins or your own user only !!! ".format(user_oid) }, 403 else: return func(*args, **kwargs)
def main(): """ Make sure config is valid and start main loop """ mygui = Gui() while not Config.is_configfile_valid(): log.debug("No valid config file") get_cred(mygui) wait_for_tray_event( mygui, Config.SERVER, Config.USER, Config.PW_HASH_LOGIN, Config.PW_HASH_MSG, )
def ask_for_cred(self): """ Create Windows asking for credentials """ window = sg.Window( title=f"{Config.APP_NAME} - Enter credentials for registration / login", layout=self.create_cred_layout(), size=(500, 150), icon=Config.ICON_B64, ) while not self.check_cred(window): pass log.debug("Ask for creds loop exited") window.close() return True
def concat_dsi_list(headers_dso, df_mapper_dsi_to_dmf, dsi_raw_data_list): """ concat and reindex several sets of data """ print() print("-+- " * 40) log.debug("... concat_dsi_list ...") # ### prj_dsi_mapping to df + index # df_mapper_dsi_to_dmf = pd.DataFrame(prj_dsi_mapping) # df_mapper_dsi_to_dmf = df_mapper_dsi_to_dmf.set_index(["oid_dsi","oid_dmf"]) # print() # log.debug("... df_mapper_dsi_to_dmf ...") # print(df_mapper_dsi_to_dmf) ### headers_dso to df + index df_mapper_col_headers = pd.DataFrame(headers_dso) df_mapper_col_headers = df_mapper_col_headers.set_index("oid_dmf") print() log.debug("... df_mapper_col_headers ...") print(df_mapper_col_headers) ### store remapped each dsi's f_data in a df_list df_list = [] for dsi_data in dsi_raw_data_list: print() log.debug("... dsi_data['oid_dsi'] : %s", dsi_data['oid_dsi']) df_dsi = dsi_remap(dsi_data, df_mapper_dsi_to_dmf, df_mapper_col_headers) print(df_dsi.head(3)) df_list.append(df_dsi) log.debug("... df_list is composed ...") ### concat df_list df_data_concat = pd.concat(df_list, ignore_index=True, sort=False) log.debug("... df_data_concat is composed ...") ### convert Nan to None # df_data_concat = df_data_concat.replace({np.nan:None}) # print() # log.debug("... df_data_concat : Nan values converted to None ...") # log.debug("... df_data_concat.dtypes : \n%s", pformat(df_data_concat.dtypes)) # print(df_data_concat.head(3)) return df_data_concat
def function_test(debug): loglevelset(debug) com = serial_setup() p = [i for i in ports.values()] p.sort() log.debug(str(p)) for k, v in enumerate(p): log.info('Switching to port: {0}'.format(k + 1)) log.info('Using hex code: {0}'.format(v.encode('hex'))) com.write(v) log.info(com.in_waiting) log.info(com.read(com.in_waiting).encode('hex')) log.info(com.read(com.in_waiting).encode('hex')) time.sleep(2) com.close()
def geocode_with_Ban( address, time_out = dft_timeout, ) : geocoder_ban = BANFrance(user_agent="solidata_app_to_BAN") log.debug("- geocode_with_Ban - ") try: loc = geocoder_ban.geocode( query=address, timeout=time_out, # exactly_one=True, ) log.debug("- loc : \n%s ", loc) return loc except GeocoderTimedOut: return geocode_with_Ban(address) except : pass
def main(): log.debug('Opening: %s' % DBUS_FILE_NAME_OMXPLAYER) for i in range(0, 4): try: log.debug('stream-%d connecting...' % i) action_result = send_dbus_action(i, ACTION_POS) log.debug('[stream-%d] action finished. %s' % (i, action_result if action_result is not None else "")) except OMXPlayerStopError: log.debug('[stream-%d] action failed.' % i)
def my_expired_token_callback(): """ Using the expired_token_loader decorator, we will now call this function whenever an expired token attempts to access an endpoint but otherwise valid access """ log.debug("-@- expired token checker") ### if user is not confirmed, delete user from DB ### otherwise return a link to refresh refresh_token return jsonify({ 'msg': 'The token has expired', 'status': 401, 'sub_status': 42, }), 401