Пример #1
0
def checkResultAndExportStations(tdb, cmdenv, result):
    if not result:
        cmdenv.NOTE("No changes.")
        return None
    if cmdenv.noExport:
        cmdenv.DEBUG0("no-export set, not exporting stations")
        return None

    lines, csvPath = csvexport.exportTableToFile(tdb, cmdenv, "Station")
    cmdenv.NOTE("{} updated.", csvPath)

    if cmdenv.remove:
        if cmdenv.stationItemCount:
            cmdenv.NOTE("Station had items, regenerating .prices file")
            cache.regeneratePricesFile(tdb, cmdenv)

    return None
Пример #2
0
    def run(self):
        tdb, tdenv = self.tdb, self.tdenv

        #Create the /eddb folder for downloading the source files if it doesn't exist.
        try:
           Path(str(self.dataPath)).mkdir()
        except FileExistsError:
            pass
        
        # Run 'listings' by default:
        # If no options, or if only 'progbar', 'force', 'skipvend', and/or 'fallback', 
        # have been passed, enable 'listings'.
        default = True
        for option in self.options:
            if not ((option == 'force') or (option == 'fallback') or (option == 'skipvend') or (option == 'progbar')):
                default = False
        if default:
            self.options["listings"] = True
        
        # We can probably safely assume that the plugin has never been run if
        # the prices file doesn't exist, since the plugin always generates it.
        firstRun = not (tdb.dataPath / Path("TradeDangerous.prices")).exists()

        if firstRun:
            self.options["clean"] = True
        
        try:
            self.execute("ALTER TABLE Station ADD type_id INTEGER DEFAULT 0 NOT NULL")
        except sqlite3.OperationalError:
            pass
        except sqlite3.DatabaseError as e:
            self.options['clean'] = True
            tdenv.NOTE("Cleaning database: ",str(e))
        
        if self.getOption("clean"):
            # Rebuild the tables from scratch. Must be done on first run of plugin.
            # Can be done at anytime with the "clean" option.
            for name in [
                "Category",
                "Item",
                "Ship",
                "ShipVendor",
                "Station",
                "System",
                "Upgrade",
                "UpgradeVendor",
            ]:
                file = tdb.dataPath / Path(name + ".csv")
                try:
                    os.remove(str(file))
                except FileNotFoundError:
                    pass
                
            try:
                os.remove(str(tdb.dataPath) + "/TradeDangerous.db")
            except FileNotFoundError:
                pass
            try:
                os.remove(str(tdb.dataPath) + "/TradeDangerous.prices")
            except FileNotFoundError:
                pass

            # Because this is a clean run, we need to temporarily rename the RareItem.csv,
            # otherwise TD will crash trying to insert the rare items to the database,
            # because there's nothing in the Station table it tries to pull from.
            ri_path = tdb.dataPath / Path("RareItem.csv")
            rib_path = ri_path.with_suffix(".tmp")
            if rib_path.exists() and ri_path.exists():
                rib_path.unlink()
            if ri_path.exists():
                ri_path.rename(rib_path)

            tdb.reloadCache()
            
            # Now it's safe to move RareItems back.
            if ri_path.exists():
                ri_path.unlink()
            if rib_path.exists():
                rib_path.rename(ri_path)
                        
            self.options["all"] = True
            self.options["force"] = True

        tdenv.ignoreUnknown = True
        
        success = False
        while not success:
            try:
                tdb.load(maxSystemLinkLy = tdenv.maxSystemLinkLy)
                success = True
            except sqlite3.OperationalError:
                print("Database is locked, waiting for access.", end = "\r")
                time.sleep(1)
        
        #Select which options will be updated
        if self.getOption("listings"):
            self.options["item"] = True
            self.options["station"] = True

        if self.getOption("shipvend"):
            self.options["ship"] = True
            self.options["station"] = True

        if self.getOption("upvend"):
            self.options["upgrade"] = True
            self.options["station"] = True

        if self.getOption("station"):
            self.options["system"] = True

        if self.getOption("all"):
            self.options["item"] = True
            self.options["ship"] = True
            self.options["shipvend"] = True
            self.options["station"] = True
            self.options["system"] = True
            self.options["upgrade"] = True
            self.options["upvend"] = True
            self.options["listings"] = True

        if self.getOption("solo"):
            self.options["listings"] = False
            self.options["skipvend"] = True
        
        if self.getOption("skipvend"):
            self.options["shipvend"] = False
            self.options["upvend"] = False
        
        # Download required files and update tables.
        if self.getOption("upgrade"):
            if self.downloadFile(UPGRADES, self.upgradesPath) or self.getOption("force"):
                self.importUpgrades()

        if self.getOption("ship"):
            if self.downloadFile(SHIPS_URL, self.shipsPath) or self.getOption("force"):
                self.importShips()

        if self.getOption("system"):
            if self.downloadFile(SYSTEMS, self.systemsPath) or self.getOption("force"):
                self.importSystems()

        if self.getOption("station"):
            if self.downloadFile(STATIONS, self.stationsPath) or self.getOption("force"):
                self.importStations()

        if self.getOption("item"):
            if self.downloadFile(COMMODITIES, self.commoditiesPath) or self.getOption("force"):
                self.importCommodities()
        
        success = False
        while not success:
            try:
                tdb.getDB().commit()
                success = True
            except sqlite3.OperationalError:
                print("(commit) Database is locked, waiting for access.", end = "\r")
                time.sleep(1)
        
        #Remake the .csv files with the updated info.
        self.regenerate()

        if self.getOption("listings"):
            if self.downloadFile(LISTINGS, self.listingsPath) or self.getOption("force"):
                self.importListings(self.listingsPath)
            if self.downloadFile(LIVE_LISTINGS, self.liveListingsPath) or self.getOption("force"):
                self.importListings(self.liveListingsPath)

        success = False
        while not success:
            try:
                tdb.getDB().commit()
                success = True
            except sqlite3.OperationalError:
                print("(commit) Database is locked, waiting for access.", end = "\r")
                time.sleep(1)
        
        tdb.close()
        
        if self.updated['Listings']:
            tdenv.NOTE("Regenerating .prices file.")
            cache.regeneratePricesFile(tdb, tdenv)

        tdenv.NOTE("Import completed.")

        # TD doesn't need to do anything, tell it to just quit.
        return False
Пример #3
0
def run(results, cmdenv, tdb):
    # If we're using a plugin, initialize that first.
    if cmdenv.plug:
        if cmdenv.pluginOptions:
            cmdenv.pluginOptions = chain.from_iterable(
                opt.split(',') for opt in cmdenv.pluginOptions)
        try:
            pluginClass = plugins.load(cmdenv.plug, "ImportPlugin")
        except plugins.PluginException as e:
            raise CommandLineError("Plugin Error: " + str(e))

        # Initialize the plugin
        plugin = pluginClass(tdb, cmdenv)

        # Run the plugin. If it returns False, then it did everything
        # that needs doing and we can stop now.
        # If it returns True, it is returning control to the module.
        if not plugin.run():
            return None

    tdb.reloadCache()
    tdb.close()

    if cmdenv.filename:
        if re.match(r"^https?://", cmdenv.filename, re.IGNORECASE):
            cmdenv.url, cmdenv.filename = cmdenv.filename, None

    if cmdenv.url:
        cmdenv.filename = cmdenv.filename or "import.prices"
        transfers.download(cmdenv, cmdenv.url, cmdenv.filename)
        if cmdenv.download:
            return None

    # If the filename specified was "-" or None, then go ahead
    # and present the user with an open file dialog.
    if not cmdenv.filename and hasTkInter:
        tk = tkinter.Tk()
        tk.withdraw()
        filetypes = (
            ("TradeDangerous '.prices' Files", "*.prices"),
            ("All Files", "*.*"),
        )
        filename = tkfd.askopenfilename(
            title="Select the file to import",
            initialfile="TradeDangerous.prices",
            filetypes=filetypes,
            initialdir='.',
        )
        if not filename:
            raise SystemExit("Aborted")
        cmdenv.filename = filename

    # check the file exists.
    if cmdenv.filename != "-":
        fh = None
        filePath = Path(cmdenv.filename)
        if not filePath.is_file():
            raise CommandLineError("File not found: {}".format(str(filePath)))
    else:
        filePath = "stdin"
        fh = sys.stdin

    if cmdenv.plug:
        if not plugin.finish():
            cache.regeneratePricesFile()
            return None

    cache.importDataFromFile(tdb,
                             cmdenv,
                             filePath,
                             pricesFh=fh,
                             reset=cmdenv.reset)

    return None
Пример #4
0
def run(results, cmdenv, tdb):
    # If we're using a plugin, initialize that first.
    if cmdenv.plug:
        if cmdenv.pluginOptions:
            cmdenv.pluginOptions = chain.from_iterable(
                opt.split(',') for opt in cmdenv.pluginOptions
            )
        try:
            pluginClass = plugins.load(cmdenv.plug, "ImportPlugin")
        except plugins.PluginException as e:
            raise CommandLineError("Plugin Error: "+str(e))

        # Initialize the plugin
        plugin = pluginClass(tdb, cmdenv)

        # Run the plugin. If it returns False, then it did everything
        # that needs doing and we can stop now.
        # If it returns True, it is returning control to the module.
        if not plugin.run():
            return None

    tdb.reloadCache()
    tdb.close()

    if cmdenv.filename:
        if re.match(r"^https?://", cmdenv.filename, re.IGNORECASE):
            cmdenv.url, cmdenv.filename = cmdenv.filename, None

    if cmdenv.url:
        cmdenv.filename = cmdenv.filename or "import.prices"
        transfers.download(cmdenv, cmdenv.url, cmdenv.filename)
        if cmdenv.download:
            return None

    # If the filename specified was "-" or None, then go ahead
    # and present the user with an open file dialog.
    if not cmdenv.filename and hasTkInter:
        tk = tkinter.Tk()
        tk.withdraw()
        filetypes = (
                ("TradeDangerous '.prices' Files", "*.prices"),
                ("All Files", "*.*"),
                )
        filename = tkfd.askopenfilename(
                    title="Select the file to import",
                    initialfile="TradeDangerous.prices",
                    filetypes=filetypes,
                    initialdir='.',
                )
        if not filename:
            raise SystemExit("Aborted")
        cmdenv.filename = filename

    # check the file exists.
    if cmdenv.filename != "-":
        fh = None
        filePath = Path(cmdenv.filename)
        if not filePath.is_file():
            raise CommandLineError("File not found: {}".format(
                        str(filePath)
                    ))
    else:
        filePath = "stdin"
        fh = sys.stdin

    if cmdenv.plug:
        if not plugin.finish():
            cache.regeneratePricesFile()
            return None

    cache.importDataFromFile(tdb, cmdenv, filePath, pricesFh=fh, reset=cmdenv.reset)

    return None