def get_ipython_module_path(module_str): warn( "get_ipython_module_path has moved to the IPython.paths module since IPython 4.0.", DeprecationWarning, stacklevel=2) from IPython.paths import get_ipython_module_path return get_ipython_module_path(module_str)
def img_to_slack(imagefile): token_file = os.path.join( os.path.dirname(get_ipython_module_path('BMM.functions')), 'image_uploader_token') try: with open(token_file, "r") as f: token = f.read().replace('\n', '') except: post_to_slack(f'failed to post image: {imagefile}') return () client = WebClient(token=token) #client = WebClient(token=os.environ['SLACK_API_TOKEN']) try: response = client.files_upload(channels='#beamtime', file=imagefile) assert response["file"] # the uploaded file except SlackApiError as e: post_to_slack('failed to post image: {imagefile}') # You will get a SlackApiError if "ok" is False assert e.response["ok"] is False assert e.response[ "error"] # str like 'invalid_auth', 'channel_not_found' print(f"Got an error: {e.response['error']}") except Exception as em: print("EXCEPTION: " + str(em)) report(f'failed to post image: {imagefile}', level='bold', slack=True)
def initialize_secrets(): '''Check that the Slack secret files are in their expected locations. If not, copy them from the NAS server NFS mounted at /mnt/nfs/nas1. ''' STARTUP = os.path.dirname(get_ipython_module_path('BMM.functions')) for fname in SECRET_FILES: if os.path.isfile(os.path.join(STARTUP, fname)): print(f'{TAB}Found {fname} file: {CHECK}') else: try: shutil.copyfile(os.path.join(SECRETS, fname), os.path.join(STARTUP, fname)) print(f'{TAB}Copied {fname} file') except Exception as e: print(e) print( error_msg( f'{TAB}Failed to copy {os.path.join(SECRETS, fname)}!') )
def get_ipython_module_path(module_str): warn("get_ipython_module_path has moved to the IPython.paths module") from IPython.paths import get_ipython_module_path return get_ipython_module_path(module_str)
def test_get_ipython_module_path(): ipapp_path = paths.get_ipython_module_path('IPython.terminal.ipapp') assert_isfile(ipapp_path)
def get_ipython_module_path(module_str): warn("get_ipython_module_path has moved to the IPython.paths module since IPython 4.0.", stacklevel=2) from IPython.paths import get_ipython_module_path return get_ipython_module_path(module_str)
def test_get_ipython_module_path(): ipapp_path = paths.get_ipython_module_path('IPython.terminal.ipapp') nt.assert_true(os.path.isfile(ipapp_path))
def BMM_log_info(message): chmod(BMM_log_master_file, 0o644) chmod(BMM_nas_log_file, 0o644) entry = '' for line in message.split('\n'): entry += ' ' + line + '\n' BMM_logger.info(entry) chmod(BMM_log_master_file, 0o444) chmod(BMM_nas_log_file, 0o444) ## small effort to obfuscate the web hook URL, which is secret-ish. See: ## https://api.slack.com/messaging/webhooks#create_a_webhook ## in the future, this could be an ini with per-user channel URLs... slack_secret = os.path.join( os.path.dirname(get_ipython_module_path('BMM.functions')), 'slack_secret') try: with open(slack_secret, "r") as f: default_slack_channel = f.read().replace('\n', '') except: print(error_msg('\t\t\tslack_secret file not found!')) def post_to_slack(text): try: channel = BMMuser.slack_channel except: channel = default_slack_channel if channel is None: channel = default_slack_channel post = {"text": "{0}".format(text)}