Skip to content

Commit

Permalink
several minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfpackWilson committed Aug 25, 2020
1 parent bae124a commit 4a8fd5c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 73 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ optional arguments:
-r, --relative-path use the relative path to run catminer
```

The current supported output are dependent on [pyvba](https://pypi.org/project/pyvba/)
The current supported output are dependent on [pyvba](https://pypi.org/project/pyvba/).

## Developer Notes
Contributors are welcome! The project is [hosted on GitHub](https://github.com/WWU-CAD-Autograder/catminer). Report
Expand Down
3 changes: 0 additions & 3 deletions bin/catminer

This file was deleted.

134 changes: 70 additions & 64 deletions catminer/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,68 +37,74 @@ def check_dir(path: str) -> bool:
return os.path.exists(path)


# create parser and subparsers
parser = argparse.ArgumentParser(description='These are the available commands for catminer:',
epilog='Use "catminer <command> -h" to get more help on the command.')
subparsers = parser.add_subparsers(help='sub-commands', dest='command')

# 'run' command
run_parser = subparsers.add_parser('run', description='Run catminer using these commands:',
help='run the batch process')

run_parser.add_argument('-b', '--bat-file', nargs='?', const=os.getcwd(), default=os.getcwd(), type=str, metavar='path',
help='generate a .bat file for easier automation')
run_parser.add_argument('-i', '--in_dir', nargs=1, default=os.getcwd(), type=str, metavar='path',
help='set the run directory')
run_parser.add_argument('-o', '--out-dir', nargs=1, type=str, metavar='path', help='set the output directory')
run_parser.add_argument('-f', '--force-export', action='store_true', help='export previously exported files')
run_parser.add_argument('-t', '--file-type', nargs=1, default='xml', type=str, choices=['xml', 'json'],
help='choose the output file type (default: xml)')
run_parser.add_argument('-r', '--relative-path', action='store_true', help='use the relative path to run catminer')

# parse args
args = None

if 'pytest' in sys.argv[0]:
args = parser.parse_args([])
else:
args = parser.parse_args()

d_args = vars(args)

# bring up 'run' help if no command input
if len(vars(args)) == 0:
parser.parse_args(args=['run', '-h'])

# process arguments
if args.command == 'run':
# resolve out directory
if args.out_dir is None or args.in_dir == args.out_dir:
args.out_dir = os.path.join(os.getcwd(), 'catminer-output')

# check for list instances
for key, value in d_args.items():
if isinstance(value, list):
d_args[key] = d_args[key][0]

# check the entered directories
for i in ['in_dir', 'out_dir', 'bat_file']:
if d_args['out_dir'] == os.path.join(os.getcwd(), 'catminer-output'):
continue

check_dir(d_args[i])

# create bat file or start miner
if '-b' in sys.argv:
bat_str = f'python catminer run {"-f " if d_args.get("force_export", False) else ""}-t {args.file_type}^\n' + \
f' -i "{"." if "-r" in sys.argv else args.in_dir}"^\n' + \
f' -o "{os.path.join(".", "catminer") if "-r" in sys.argv else args.out_dir}"\n'

with open(os.path.join(args.bat_file, "catminer.bat"), 'w') as f:
f.write(bat_str)
f.close()
def main():
# create parser and subparsers
parser = argparse.ArgumentParser(description='These are the available commands for catminer:',
epilog='Use "catminer <command> -h" to get more help on the command.')
subparsers = parser.add_subparsers(help='sub-commands', dest='command')

# 'run' command
run_parser = subparsers.add_parser('run', description='Run catminer using these commands:',
help='run the batch process')

run_parser.add_argument('-b', '--bat-file', nargs='?', const=os.getcwd(), default=os.getcwd(), type=str, metavar='path',
help='generate a .bat file for easier automation')
run_parser.add_argument('-i', '--in_dir', nargs=1, default=os.getcwd(), type=str, metavar='path',
help='set the run directory')
run_parser.add_argument('-o', '--out-dir', nargs=1, type=str, metavar='path', help='set the output directory')
run_parser.add_argument('-f', '--force-export', action='store_true', help='export previously exported files')
run_parser.add_argument('-t', '--file-type', nargs=1, default='xml', type=str, choices=['xml', 'json'],
help='choose the output file type (default: xml)')
run_parser.add_argument('-r', '--relative-path', action='store_true', help='use the relative path to run catminer')

# parse args
args = None

if 'pytest' in sys.argv[0]:
args = parser.parse_args([])
else:
file_type = {'xml': catminer.XML, 'json': catminer.JSON}
miner = catminer.CATMiner(args.in_dir, args.out_dir, file_type[args.file_type],
force_export=d_args.get("force_export", False))
miner.begin()
args = parser.parse_args()

d_args = vars(args)

# bring up 'run' help if no command input
if 'run' not in sys.argv:
parser.parse_args(args=['run', '-h'])

# process arguments
if args.command == 'run':
# check for list instances
for key, value in d_args.items():
if isinstance(value, list):
d_args[key] = d_args[key][0]

# resolve out directory
if args.out_dir is None:
args.out_dir = os.path.join(os.getcwd(), 'catminer-output')
os.makedirs(args.out_dir, exist_ok=True)

# check the entered directories
for i in ['in_dir', 'out_dir', 'bat_file']:
if d_args['out_dir'] == os.path.join(os.getcwd(), 'catminer-output'):
continue

check_dir(d_args[i])

# create bat file or start miner
if '-b' in sys.argv:
bat_str = f'python catminer run {"-f " if d_args.get("force_export", False) else ""}-t {args.file_type}^\n' + \
f' -i "{"." if "-r" in sys.argv else args.in_dir}"^\n' + \
f' -o "{os.path.join(".", "catminer") if "-r" in sys.argv else args.out_dir}"\n'

with open(os.path.join(args.bat_file, "catminer.bat"), 'w') as f:
f.write(bat_str)
f.close()
else:
file_type = {'xml': catminer.XML, 'json': catminer.JSON}
miner = catminer.CATMiner(args.in_dir, args.out_dir, file_type[args.file_type],
force_export=d_args.get("force_export", False))
miner.begin()


if __name__ == '__main__':
main()
6 changes: 4 additions & 2 deletions catminer/catminer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import traceback
import zipfile as zf

# define static vars
DIR_PATH = os.path.dirname(__file__)

# define regular expressions
type_re = re.compile(r'(?<=\.CAT)[^.]*?$')
Expand Down Expand Up @@ -119,6 +117,10 @@ def _dir_crawl(self, path: str, out_dir: str) -> None:
for file in os.listdir(path):
file_path = os.path.join(path, file)

# skip if reading the output directory
if file_path == self._out_dir:
continue

# directory found -> scan it
if os.path.isdir(file_path):
dir_path = os.path.join(out_dir, file)
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import os

import setuptools

with open("README.md", "r") as fh:
Expand All @@ -23,6 +21,8 @@
"Programming Language :: Python :: 3",
],
install_requires=['pyvba', 'pywin32'],
scripts=['bin/catminer'],
entry_points={
'console_scripts': ['catminer = catminer.__main__:main']
},
python_requires='>=3',
)

0 comments on commit 4a8fd5c

Please sign in to comment.