Skip to content
This repository has been archived by the owner on Oct 5, 2020. It is now read-only.

Commit

Permalink
auto-detect filepath if not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
cmccandless committed Mar 1, 2018
1 parent e8d3d76 commit 847598b
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 49 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
@@ -1,5 +1,9 @@
Changelog
=========
v1.1.1
------
- ParserDefinition.__init__() and get_parser() no longer require an explicit filepath

v1.1.0
------
- API: abstract parser definition functionality behind class ParserDefinition
Expand Down
75 changes: 30 additions & 45 deletions argutil/argutil.py
Expand Up @@ -11,13 +11,15 @@
from .working_directory import WorkingDirectory
from . import defaults
import json
import inspect
import os
import shutil
from sys import exit
from .deepcopy import deepcopy
from .primitives import primitives
import logging

VERSION = '1.1.0'
VERSION = '1.1.1'

logger = logging.getLogger('argutil')
logger.setLevel(logging.ERROR)
Expand All @@ -28,59 +30,31 @@
FileNotFoundError = IOError


primitives = {
'int': int,
'float': float,
'complex': complex,
'str': str,
'list': list,
'tuple': tuple,
'bytes': bytes,
'memoryview': memoryview,
'bytearray': bytearray,
'range': range,
'set': set,
'frozenset': frozenset,
'dict': dict,
}

# Python 2 compatibility
try:
primitives['long'] = long
except NameError:
pass

try:
primitives['unicode'] = unicode
except NameError:
pass

try:
primitives['buffer'] = buffer
except NameError:
pass

try:
primitives['xrange'] = xrange
except NameError:
primitives['xrange'] = range


class RawWithDefaultsFormatter(
RawTextHelpFormatter,
ArgumentDefaultsHelpFormatter
):
pass


def get_file(**kwargs):
stackdepth = kwargs.get('__stackdepth__', kwargs.get('__stackdepth__', 1))
return inspect.stack()[stackdepth].filename


class ParserDefinition(object):
@staticmethod
def create(
filepath,
filepath=None,
definitions_file=defaults.DEFINITIONS_FILE,
defaults_file=defaults.DEFAULTS_FILE,
fail_if_exists=False,
**kwargs
):
if filepath is None:
filepath = get_file(
__stackdepth__=kwargs.get('_-stackdepth__', 1) + 1
)
module = get_module(filepath)
if not os.path.isfile(filepath):
argutil_path = os.path.abspath(__file__)
Expand All @@ -104,10 +78,15 @@ def create(

def __init__(
self,
filepath,
filepath=None,
definitions_file=defaults.DEFINITIONS_FILE,
defaults_file=defaults.DEFAULTS_FILE
defaults_file=defaults.DEFAULTS_FILE,
**kwargs
):
if filepath is None:
filepath = get_file(
__stackdepth__=kwargs.get('__stackdepth__', 1) + 1
)
self.filepath = filepath
self.module = get_module(filepath)
self.definitions_file = definitions_file
Expand Down Expand Up @@ -416,9 +395,15 @@ def get_module(filepath):


def get_parser(
filepath,
filepath=None,
env=None,
definitions_file=defaults.DEFINITIONS_FILE,
defaults_file=defaults.DEFAULTS_FILE
defaults_file=defaults.DEFAULTS_FILE,
**kwargs
):
return ParserDefinition(filepath).get_parser(env)
return ParserDefinition(
filepath,
definitions_file,
defaults_file,
__stackdepth__=kwargs.get('__stackdepth__', 1) + 1
).get_parser(env)
36 changes: 36 additions & 0 deletions argutil/primitives.py
@@ -0,0 +1,36 @@
primitives = {
'int': int,
'float': float,
'complex': complex,
'str': str,
'list': list,
'tuple': tuple,
'bytes': bytes,
'memoryview': memoryview,
'bytearray': bytearray,
'range': range,
'set': set,
'frozenset': frozenset,
'dict': dict,
}

# Python 2 compatibility
try:
primitives['long'] = long
except NameError:
pass

try:
primitives['unicode'] = unicode
except NameError:
pass

try:
primitives['buffer'] = buffer
except NameError:
pass

try:
primitives['xrange'] = xrange
except NameError:
primitives['xrange'] = range
3 changes: 1 addition & 2 deletions examples/simple.py
Expand Up @@ -18,8 +18,7 @@
from __future__ import print_function
import argutil

parser_def = argutil.ParserDefinition(__file__)
argparser = parser_def.get_parser()
argparser = argutil.get_parser()
opts = argparser.parse_args()
print('foo:', opts.foo)
print('bar:', opts.bar)
3 changes: 1 addition & 2 deletions examples/submodules.py
Expand Up @@ -63,7 +63,6 @@ def that(opts):
'this': this,
'that': that
}
parser_def = argutil.ParserDefinition(__file__)
parser = parser_def.get_parser(env)
parser = argutil.get_parser(env=env)
opts = parser.parse_args()
opts.func(opts)

0 comments on commit 847598b

Please sign in to comment.