Commit e200c0b6f7f12006d43ad63f49467644911780f1

Authored by Quillot Mathias
1 parent 65cfb375ff
Exists in master

Now we can import relatively to the executed module even if we use -m option of python command

Showing 2 changed files with 12 additions and 1 deletions Inline Diff

1 """ Just a simple way to import relative modules when calling
2 modules with the option -m
3 exemple : python -m volia.script
4 if script import a module in the same namespace
5 this will not work because python will try to
6 find a module in the directory of execution, not those of the
7 module file.
8 """
9 import sys
10 import os
11 sys.path.append(os.path.dirname(__file__))
12
1 import argparse 1 import argparse
2 import sys 2 import sys
3 3
4 from volia.utils import SubCommandRunner 4 from utils import SubCommandRunner
5 5
6 # Main parser 6 # Main parser
7 parser = argparse.ArgumentParser(description="manage files") 7 parser = argparse.ArgumentParser(description="manage files")
8 subparsers = parser.add_subparsers(title="actions") 8 subparsers = parser.add_subparsers(title="actions")
9 9
10 10
11 11
12 # Filter ids 12 # Filter ids
13 parser_filter = subparsers.add_parser("filter", help="filter a file") 13 parser_filter = subparsers.add_parser("filter", help="filter a file")
14 parser_filter.add_argument("--file", type=str, help="path of the file to filter") 14 parser_filter.add_argument("--file", type=str, help="path of the file to filter")
15 parser_filter.add_argument("--filter", type=str, help="filter file [id_from] [id_to]") 15 parser_filter.add_argument("--filter", type=str, help="filter file [id_from] [id_to]")
16 parser_filter.set_defaults(which="filter") 16 parser_filter.set_defaults(which="filter")
17 17
18 # Convert 18 # Convert
19 parser_convert = subparsers.add_parser("convert", help="convert a file") 19 parser_convert = subparsers.add_parser("convert", help="convert a file")
20 parser_convert.add_argument("--file", type=str, help="...") 20 parser_convert.add_argument("--file", type=str, help="...")
21 parser_convert.add_argument("--type-from", type=str, choices=["old-masseffect", "new-masseffect"], help="...") 21 parser_convert.add_argument("--type-from", type=str, choices=["old-masseffect", "new-masseffect"], help="...")
22 parser_convert.add_argument("--type", choices=["old-masseffect", "new-masseffect"]) 22 parser_convert.add_argument("--type", choices=["old-masseffect", "new-masseffect"])
23 parser_convert.set_defaults(which="convert") 23 parser_convert.set_defaults(which="convert")
24 24
25 25
26 # Parse 26 # Parse
27 args = parser.parse_args() 27 args = parser.parse_args()
28 28
29 29
30 30
31 def filter_file(file_path, filter_path): 31 def filter_file(file_path, filter_path):
32 pass 32 pass
33 33
34 34
35 def convert(file, type_from, type): 35 def convert(file, type_from, type):
36 pass 36 pass
37 37
38 38
39 if __name__ == "__main__": 39 if __name__ == "__main__":
40 runner = SubCommandRunner({ 40 runner = SubCommandRunner({
41 "convert" : convert, 41 "convert" : convert,
42 "filter": filter_file, 42 "filter": filter_file,
43 }) 43 })
44 44
45 runner.run(args.which, args.__dict__, remove="which") 45 runner.run(args.which, args.__dict__, remove="which")
46 46