Skip to content

Commit

Permalink
feat: add --rules-dir option
Browse files Browse the repository at this point in the history
breakthewall committed Jun 22, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent c2e6740 commit 9a392bb
Showing 3 changed files with 30 additions and 9 deletions.
20 changes: 17 additions & 3 deletions rrparser/Args.py
Original file line number Diff line number Diff line change
@@ -6,8 +6,14 @@
"""

from argparse import ArgumentParser
from os import path as os_path
from rrparser._version import __version__

__PACKAGE_FOLDER = os_path.dirname(
os_path.realpath(__file__)
)
DEFAULT_RULES_DIR = __PACKAGE_FOLDER
DEFAULT_RULES_FILE = 'retrorules'

def build_args_parser():
parser = ArgumentParser(prog='rrparser', description='Python wrapper to fetch RetroRules')
@@ -19,9 +25,12 @@ def _add_arguments(parser):

## Positional arguments
#
parser.add_argument('rules_file', # must be '_' otherwise it will be touchy in 'args' Namespace
type=str,
help="rules file to parse. If set to 'retrorules', RetroRules are considered as input file, either locally or fetched over Internet.")
parser.add_argument(
'--rules_file', # must be '_' otherwise it will be touchy in 'args' Namespace
type=str,
default=DEFAULT_RULES_FILE,
help="rules file to parse. If set to 'retrorules', RetroRules are considered as input file, either locally or fetched over Internet."
)

## Optional arguments
#
@@ -63,5 +72,10 @@ def _add_arguments(parser):
parser.add_argument('--version', action='version',
version='%(prog)s {}'.format(__version__),
help='show the version number and exit')
parser.add_argument(
'--rules-dir',
default=__PACKAGE_FOLDER,
help=('Path to the rules directory (default in package directory).')
)

return parser
18 changes: 12 additions & 6 deletions rrparser/Parser.py
Original file line number Diff line number Diff line change
@@ -32,14 +32,19 @@
getLogger
)

from .Args import (
DEFAULT_RULES_FILE,
DEFAULT_RULES_DIR
)


RETRORULES_URL = 'https://zenodo.org/record/5828017/files/retrorules_rr02_rp2_hs.tar.gz'
RETRORULES_PATH = os_path.dirname(os_path.abspath( __file__ ))


def parse_rules(
rules_file: str,
outfile: str,
rules_file: str = DEFAULT_RULES_FILE,
rules_dir: str = DEFAULT_RULES_DIR,
input_format: str = 'csv',
rule_type: str = 'all',
diameters: str = '2,4,6,8,10,12,14,16',
@@ -82,7 +87,7 @@ def parse_rules(

# If 'rules_file' is set to RetroRules, then fetch RetroRules on Internet
if rules_file == 'retrorules':
rules_file = fetch_retrorules()
rules_file = fetch_retrorules(rules_dir)

logger.debug('Reading values...')
# Read 'csv' as 'tsv' by specying separator
@@ -146,8 +151,9 @@ def filter(


def fetch_retrorules(
rules_dir: str = DEFAULT_RULES_DIR,
logger: Logger = getLogger(__name__)
) -> str:
) -> str:
"""
Fetch RetroRules over Internet if not already on disk.
@@ -162,13 +168,13 @@ def fetch_retrorules(
Path to downoaded filename.
"""
filename = 'retrorules_rr02_rp2_hs/retrorules_rr02_rp2_flat_all.csv'
rules_file = os_path.join(RETRORULES_PATH, filename)
rules_file = os_path.join(rules_dir, filename)

if not os_path.exists(rules_file):
logger.info('Downloading retrorules file...')
download_and_extract_tar_gz(
RETRORULES_URL,
RETRORULES_PATH,
rules_dir,
filename
)

1 change: 1 addition & 0 deletions rrparser/__main__.py
Original file line number Diff line number Diff line change
@@ -32,6 +32,7 @@ def entry_point():
try:
results = parse_rules(
rules_file=args.rules_file,
rules_dir=args.rules_dir,
outfile=args.outfile,
input_format=args.input_format,
rule_type=args.rule_type,

0 comments on commit 9a392bb

Please sign in to comment.