Skip to content

Commit

Permalink
MAINT: Various bugfixing, style fixing, incorporating review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aberges-SLAC committed May 17, 2024
1 parent 07eb6f7 commit fa19600
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 70 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,28 @@ usage: grep_ioc KEYWORD [hutch]<br/>
</td>
</tr>

<tr>
<td>grep_more_ioc</td>
<td>
usage: grep_more_ioc [-h] [-d] patt hutch {print,search} <br/>
positional arguments: <br/>
patt Regex str to search through iocmanager.cfg<br/>
hutch hutch to search<br/>
-h, --help Show help message and exit<br/>
-d, --ignore_disabled Exclude IOCs based on disabled state <br/>
{print, search}<br/>
print Prints all the matching IOCs in a dataframe<br/>
-h, --help Show help message and exit<br/>
-c, --skip_comments Prints IOC.cfg file with comments skipped<br/>
-r, --release Includes the parent IOC release in the dataframe<br/>
-s, --print_dirs Dump child & parent directors to the terminal
search Regex-like search of child IOCs<br/>
PATT The regex str to use in the search<br/>
-h, --help Show help message and exit<br/>
-q, --quiet Surpresses file warning for paths that do not exist<br/>
-o, --only_search Skip printing dataframe, only print search results<br/>
</tr>

<tr>
<td>grep_pv</td>
<td>
Expand Down
27 changes: 27 additions & 0 deletions scripts/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
"""
Constants used in grep_more_ioc and getPVAliases
"""
###############################################################################
# %% Imports
###############################################################################

import glob as gb

###############################################################################
# %% Constants
###############################################################################

# Check the directories for the iocmanager config file
VALID_HUTCH = sorted([d for d in gb.glob('/cds/group/pcds/pyps/config'
+ '/*/')
if gb.glob(d + 'iocmanager.cfg')])
# Trim to 3 letter hutch code, include 'all' = '*'
VALID_HUTCH = ['all'] + [s.rsplit(r'/', maxsplit=2)[-2] for s in VALID_HUTCH]

# Keys from iocmanager. Found in /cds/group/pcds/config/*/iocmanager/utils.py
# Update this as needed
DEF_IMGR_KEYS = ['procmgr_config', 'hosts', 'dir', 'id', 'cmd',
'flags', 'port', 'host', 'disable', 'history',
'delay', 'alias', 'hard']

52 changes: 9 additions & 43 deletions scripts/getPVAliases.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
"""
A script for gathering alias associations from an IOC
@author: aberges
"""
###############################################################################
# %% Imports
Expand All @@ -18,29 +17,15 @@
search_file, simple_prompt)
from prettytable import PrettyTable

###############################################################################
# %% Constants
###############################################################################

VALID_HUTCH = sorted([s for s
in next(os.walk('/cds/group/pcds/pyps/config'))[1]
if '.' not in s]+['all'])
# Keys from iocmanager. Found in /cds/group/pcds/config/*/iocmanager/utils.py
# Update this as needed
DEF_IMGR_KEYS = ['procmgr_config', 'hosts', 'dir', 'id', 'cmd',
'flags', 'port', 'host', 'disable', 'history',
'delay', 'alias', 'hard']

###############################################################################
# %% Functions
###############################################################################


def request_file_dest(prompt: str,
default: str = os.getcwd(),) -> str:
def request_dir(prompt: str,
default: str = os.getcwd(),) -> str:
"""
Requests the user for a destination to save a file.
Tests if the resultant path exists and makes the directory if necessary.
Parameters
----------
Expand Down Expand Up @@ -70,25 +55,6 @@ def request_file_dest(prompt: str,
return result


def flatten_list(input_list: list[list]) -> list[str]:
"""
Flatten a 2D lists and find its unique values.
Note: loses order due to set() call. sear
Parameters
----------
input_list: list[list]
The 2D list to flatten.
Returns
-------
list
The list of unique elements from the 2D input_list
"""
_result = [e for lst in input_list for e in lst]
return list(set(_result))


def build_table(input_data: list[dict], columns: list[str] = None,
**kwargs) -> PrettyTable:
"""
Expand Down Expand Up @@ -226,16 +192,16 @@ def show_temp_table(input_data: list, col_list: list):
# parser obj configuration
parser = argparse.ArgumentParser(
prog='gatherPVAliases',
description="""gathers all record <-> alias associations from a child's
ioc.cfg, st.cmd, and parent ioc.cfg.""",
epilog='')
description="gathers all record <-> alias associations from a child's "
"ioc.cfg, st.cmd, and parent ioc.cfg.",
epilog='')
# main command arguments
parser.add_argument('patt', type=str)
parser.add_argument('hutch', type=str)
parser.add_argument('-d', '--dry_run', action='store_true',
default=False,
help='''Forces a dry run for the script.
No files are saved.''')
help="Forces a dry run for the script. "
"No files are saved.")

###############################################################################
# %% Main
Expand Down Expand Up @@ -356,8 +322,8 @@ def main():

# write to file, else do nothing
if (len(final_output) > 0) & (args.dry_run is False):
dest = request_file_dest('Choose base file destination',
default_dest)
dest = request_dir('Choose base file destination',
default_dest)
# make sure the destination exists and mkdir if it doesn't
if os.path.exists(dest) is False:
print(Fore.LIGHTBLUE_EX
Expand Down
4 changes: 3 additions & 1 deletion scripts/getPVAliases.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/bash

# execute python script
/cds/group/pcds/pyps/conda/py39/envs/pcds-5.8.2/bin/python "$ENG_TOOLS_SCRIPTS/getPVAliases.py" "$@"
THIS_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"

/cds/group/pcds/pyps/conda/py39/envs/pcds-5.8.2/bin/python "${THIS_DIR}/getPVAliases.py" "$@"
42 changes: 17 additions & 25 deletions scripts/grep_more_ioc.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""A tool for enhancing the grep_ioc CLI tool for ECS-Delivery team
enginer = aberges
version = 1.0.6
# -*- coding: utf-8 -*-
"""
A tool for enhancing the grep_ioc CLI tool for ECS-Delivery team
"""
###############################################################################
# %% Imports
Expand All @@ -15,6 +15,7 @@
from shutil import get_terminal_size

import pandas as pd
from constants import (DEF_IMGR_KEYS, VALID_HUTCH)
from colorama import Fore, Style

###############################################################################
Expand All @@ -25,19 +26,6 @@

pd.set_option("display.max_rows", 1000)

###############################################################################
# %% Constants
###############################################################################

VALID_HUTCH = sorted([s for s
in next(os.walk('/cds/group/pcds/pyps/config'))[1]
if '.' not in s]+['all'])
# Keys from iocmanager. Found in /cds/group/pcds/config/*/iocmanager/utils.py
# Update this as needed
DEF_IMGR_KEYS = ['procmgr_config', 'hosts', 'dir', 'id', 'cmd',
'flags', 'port', 'host', 'disable', 'history',
'delay', 'alias', 'hard']

###############################################################################
# %% Functions
###############################################################################
Expand Down Expand Up @@ -90,10 +78,14 @@ def search_file(*, file: str, output: list = None,

def print_skip_comments(file: str):
"""Prints contents of a file while ignoring comments"""
with open(file, 'r', encoding='utf_8') as _f:
for line in _f.readlines():
if not line.strip().startswith('#'):
print(line.strip())
try:
with open(file, 'r', encoding='utf_8') as _f:
for line in _f.readlines():
if not line.strip().startswith('#'):
print(line.strip())
except FileNotFoundError:
print(f'{Fore.RED}Could not open {Style.RESET_ALL}{file}'
+ f' {Fore.RED}does not exist{Style.RESET_ALL}')


def simple_prompt(prompt: str, default: str = 'N'):
Expand Down Expand Up @@ -323,9 +315,9 @@ def build_parser():
help='Flag for excluding based'
+ ' on the "disabled" state.')
# subparsers
subparsers = (parser
.add_subparsers(help='Follow-up commands after grep_ioc '
+ 'executes'))
subparsers = parser.add_subparsers(
help='Follow-up commands after grep_ioc executes'
)
# --------------------------------------------------------------------------- #
# print subarguments
# --------------------------------------------------------------------------- #
Expand All @@ -350,7 +342,7 @@ def build_parser():
print_frame.add_argument('-s', '--print_dirs', action='store_true',
default=False,
help='Prints the child & parent IOC'
+ 'directories as the final output')
+ ' directories as the final output')
# --------------------------------------------------------------------------- #
# search subarguments
# --------------------------------------------------------------------------- #
Expand All @@ -365,7 +357,7 @@ def build_parser():
metavar='PATT')
search.add_argument('-q', '--quiet', action='store_true', default=False,
help='Surpresses file warning for paths that do not'
+ 'exist.')
+ ' exist.')
search.add_argument('-o', '--only_search', action='store_true',
default=False,
help="Don't print the dataframe, just search results.")
Expand Down
4 changes: 3 additions & 1 deletion scripts/grep_more_ioc.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/usr/bin/bash

# execute python script
/cds/group/pcds/pyps/conda/py39/envs/pcds-5.8.4/bin/python "$ENG_TOOLS_SCRIPTS/grep_more_ioc.py" "$@"
THIS_DIR="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"

/cds/group/pcds/pyps/conda/py39/envs/pcds-5.8.4/bin/python "${THIS_DIR}/grep_more_ioc.py" "$@"

0 comments on commit fa19600

Please sign in to comment.