From e34d1d396d276d9658022b39a63b18724f11868e Mon Sep 17 00:00:00 2001 From: Oliver Smith Date: Mon, 20 Oct 2014 15:12:22 -0700 Subject: [PATCH] v4.5.1 Issue #39 Use script path to locate data directory. When starting up, change current directory to match the path of trade.py - so if you do "trade/trade.py" it will look for "trade/data/..." Also added --dir (aka -C) to change directory --- README.txt | 8 ++++++++ trade.py | 13 +++++++++++++ 2 files changed, 21 insertions(+) diff --git a/README.txt b/README.txt index f39f6a1c..d1e54786 100644 --- a/README.txt +++ b/README.txt @@ -23,6 +23,14 @@ files from other commanders to fill out your database. == CHANGE LOG ============================================================================== +v4.5.1 Oct 20/2014 +. (kfsone) Added --dir (-C) command line for specifying which directory you + want trade.py to look in for it's files. +. (kfsone) trade.py will now default to trying to look for files relative + to where the "trade.py" script was called from. So if you run + "c:\tradedangerous\trade.py" it will look for the data directory as + "c:\tradedangerous\data\" (Issue #39) + v4.5.0 Oct 20/2014 . (Smacker65/Community) Smacker brings the star database up to 567 systems. diff --git a/trade.py b/trade.py index 061a42ba..30d06a91 100755 --- a/trade.py +++ b/trade.py @@ -924,6 +924,7 @@ def main(): commonArgs.add_argument('--detail', '-v', help='Increase level of detail in output.', default=0, required=False, action='count') commonArgs.add_argument('--quiet', '-q', help='Reduce level of detail in output.', default=0, required=False, action='count') commonArgs.add_argument('--db', help='Specify location of the SQLite database. Default: {}'.format(TradeDB.defaultDB), type=str, default=str(TradeDB.defaultDB)) + commonArgs.add_argument('--cwd', '-C', help='Change the directory relative to which TradeDangerous will try to access files such as the .db, etc.', type=str, required=False) subparsers = parser.add_subparsers(dest='subparser', title='Commands') @@ -1019,6 +1020,18 @@ def main(): if args.detail and args.quiet: raise CommandLineError("'--detail' (-v) and '--quiet' (-q) are mutually exclusive.") + # If a directory was specified, relocate to it. + # Otherwise, try to chdir to + if args.dir: + os.chdir(args.dir) + else: + if sys.argv[0]: + cwdPath = pathlib.Path('.').resolve() + exePath = pathlib.Path(sys.argv[0]).parent.resolve() + if cwdPath != exePath: + if args.debug: print("# cwd at launch was: {}, changing to {} to match trade.py".format(cwdPath, exePath)) + os.chdir(str(exePath)) + # load the database tdb = TradeDB(debug=args.debug, dbFilename=args.db)