Skip to content

Commit

Permalink
Unbroke sub-editor modes
Browse files Browse the repository at this point in the history
  • Loading branch information
kfsone committed Sep 2, 2014
1 parent 226bc99 commit cd3bc18
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,24 @@ class EditAction(argparse.Action):
"""
argparse action that stores a value and also flags args._editing
"""
def __init__(self, *args, **kwargs):
if not 'type' in kwargs and not 'nargs' in kwargs:
kwargs['nargs'] = 0
super(EditAction, self).__init__(*args, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, "_editing", True)
setattr(namespace, self.dest, values or self.default)


class EditActionStoreTrue(argparse.Action):
"""
argparse action that stores True but also flags args._editing
"""
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError("nargs not allowed")
super(EditActionStoreTrue, self).__init__(option_strings, dest, nargs=0, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, "_editing", True)
setattr(namespace, self.dest, True)


def new_file_arg(string):
""" argparse action handler for specifying a file that does not already exist. """

Expand Down Expand Up @@ -444,7 +453,8 @@ def editUpdate(args, stationID):
# Create a temporary text file with a list of the price data.
tmpPath = pathlib.Path("prices.tmp")
if tmpPath.exists():
print("Temporary file ({}) already exists.".format(tmpPath))
print("ERROR: Temporary file ({}) already exists.".format(tmpPath))
sys.exit(1)
absoluteFilename = None
try:
# Open the file and dump data to it.
Expand All @@ -460,7 +470,7 @@ def editUpdate(args, stationID):

# Launch the editor
editorCommandLine = [ editor ] + editorArgs + [ absoluteFilename ]
if args.debug: print("# Invoking [{}]".format('::'.join(editorCommandLine)))
print("# Invoking [{}]".format(str(editorCommandLine)))
result = subprocess.call(editorCommandLine)
if result != 0:
print("NOTE: Edit failed ({}), nothing to import.".format(result))
Expand Down Expand Up @@ -568,8 +578,8 @@ def main():
updtOptArgs.add_argument('-h', '--help', help='Show this help message and exit.', action=HelpAction, nargs=0)
updtOptArgs.add_argument('--editor', help='Generates a text file containing the prices for the station and loads it into the specified editor.', required=False, default=None, type=str, action=EditAction)
editorGroup = updateParser.add_mutually_exclusive_group()
editorGroup.add_argument('--sublime', help='Like --editor but uses Sublime Text 3, which is nice.', required=False, default=False, action=EditAction)
editorGroup.add_argument('--notepad', help='Like --editor but uses Notepad.', required=False, default=False, action=EditAction)
editorGroup.add_argument('--sublime', help='Like --editor but uses Sublime Text 3, which is nice.', required=False, default=False, action=EditActionStoreTrue)
editorGroup.add_argument('--notepad', help='Like --editor but uses Notepad.', required=False, default=False, action=EditActionStoreTrue)
updateParser.set_defaults(proc=updateCommand)

args = parser.parse_args()
Expand Down

0 comments on commit cd3bc18

Please sign in to comment.