/// <summary> /// Initialization of the package; this method is called right after the package is sited, so this is the place /// where you can put all the initialization code that rely on services provided by VisualStudio. /// </summary> /// <param name="cancellationToken">A cancellation token to monitor for initialization cancellation, which can occur when VS is shutting down.</param> /// <param name="progress">A provider for progress updates.</param> /// <returns>A task representing the async work of package initialization, or an already completed task if there is none. Do not return null from this method.</returns> protected override void Initialize() { var root = Path_.Directory(Assembly.GetExecutingAssembly().Location); AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; Assembly?CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { var path = Path_.CombinePath(root, new AssemblyName(args.Name).Name + ".dll"); if (!Path_.FileExists(path)) { return(null); } return(Assembly.LoadFrom(path)); } // When initialized asynchronously, the current thread may be a background thread at this point. // Do any initialization that requires the UI thread after switching to the UI thread. //await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); base.Initialize(); // Add our command handlers for menu if (GetService <IMenuCommandService>() is OleMenuCommandService mcs) { mcs.AddCommand(new AlignMenuCommand(this)); mcs.AddCommand(new UnalignMenuCommand(this)); } }
/// <summary>Called when a new user account has been assigned</summary> private void HandleAcctChanged(object sender = null, EventArgs e = null) { // Ensure closed Util.Dispose(ref m_db); // If there is no account id, don't open a database if (Model == null || !Model.Acct.AccountId.HasValue()) { return; } // Open a database of the trade history and other user account related info var filepath = Path_.CombinePath(Settings.General.AcctDataCacheDir, "Acct_{0}.db".Fmt(Model.Acct.AccountId)); m_db = new Sqlite.Database(filepath); // Tweak some DB settings for performance m_db.Execute(Sqlite.Sql("PRAGMA synchronous = OFF")); m_db.Execute(Sqlite.Sql("PRAGMA journal_mode = MEMORY")); // Ensure the table of trades exists var sql = Sqlite.Sql("create table if not exists ", Table.TradeHistory, " (\n", "[", nameof(Trade.Id), "] text primary key,\n", "[", nameof(Trade.SymbolCode), "] text)"); m_db.Execute(sql); // Ensure the table of orders exists sql = Sqlite.Sql("create table if not exists ", Table.Orders, " (\n", "[", nameof(Order.Id), "] integer primary key,\n", "[", ("TradeId"), "] text,\n", "[", nameof(Order.SymbolCode), "] text,\n", "[", nameof(Order.TradeType), "] integer)"); m_db.Execute(sql); }
// Notes: // This is a base class for an entry signal trigger. // Derived types use various methods to guess where price is going. // Derived types add 'Feature' objects to the 'Features' collection, these are basically // values in the range [-1.0,+1.0] where -1.0 = strong sell, +1.0 = strong buy. public Predictor(Rylobot bot, string name) { Bot = bot; Name = name; CurrentIndex = 0; Instrument = new Instrument(bot); Features = new List <Feature>(); LogFilepath = Path_.CombinePath(@"P:\projects\Tradee\Rylobot\Rylobot\net\Data", "{0}.predictions.csv".Fmt(name)); }
public StrategyDataCollector(Rylobot bot) : base(bot, "StrategyDataCollector") { NNet = new PredictorNeuralNet(bot); WindowSize = 20; RtRThreshold = 2.0; // Generate the training and test data var outdir = @"P:\projects\Tradee\Rylobot\Rylobot\net\Data"; m_training = new StreamWriter(Path_.CombinePath(outdir, "training.txt")); m_testing = new StreamWriter(Path_.CombinePath(outdir, "testing.txt")); }
public void TestPathNames() { string path; //path = Path_.RelativePath(@"A:\dir\subdir\file.ext", @"A:\dir"); //Assert.Equal(@".\subdir\file.ext", path); path = Path_.CombinePath(@"A:\", @".\dir\subdir2", @"..\subdir\", "file.ext"); Assert.Equal(@"A:\dir\subdir\file.ext", path); path = Path_.SanitiseFileName("1_path@\"[{+\\!@#$%^^&*()\'/?", "@#$%", "A"); Assert.Equal("1_pathAA[{+A!AAAA^^&A()'AA", path); const string noquotes = "C:\\a b\\path.ext"; const string withquotes = "\"C:\\a b\\path.ext\""; Assert.Equal(withquotes, Path_.Quote(noquotes, true)); Assert.Equal(withquotes, Path_.Quote(withquotes, true)); Assert.Equal(noquotes, Path_.Quote(noquotes, false)); Assert.Equal(noquotes, Path_.Quote(withquotes, false)); }
public StartupOptions(string[] args) { var exe_dir = Util.ResolveAppPath(); if (!Path_.DirExists(exe_dir)) { throw new ArgumentException("Cannot determine the current executable directory"); } // Check the command line options for (int i = 0, iend = args.Length; i != iend; ++i) { var arg = args[i].ToLowerInvariant(); // No option character implies the file to load if (arg[0] != '-' && arg[0] != '/') { if (FileToLoad != null) { throw new ArgumentException("Command line should specify a single file path only. If the file path contains white space, remember to use quotes. e.g. RyLogViewer \"my file.txt\""); } FileToLoad = arg; continue; } // Helper for comparing option strings bool IsOption(string opt) => string.CompareOrdinal(arg, 0, opt, 0, opt.Length) == 0; // (order these by longest option first) if (IsOption(CmdLineOption.RDelim)) { RowDelim = arg.Substring(CmdLineOption.RDelim.Length); } else if (IsOption(CmdLineOption.CDelim)) { ColDelim = arg.Substring(CmdLineOption.CDelim.Length); } else if (IsOption(CmdLineOption.NoGUI)) { NoGUI = true; } else if (IsOption(CmdLineOption.Silent)) { Silent = true; } else if (IsOption(CmdLineOption.PatternSet)) { PatternSetFilepath = arg.Substring(CmdLineOption.PatternSet.Length); } else if (IsOption(CmdLineOption.SettingsPath)) { SettingsPath = arg.Substring(CmdLineOption.SettingsPath.Length); } else if (IsOption(CmdLineOption.LogFilePath)) { LogFilePath = arg.Substring(CmdLineOption.LogFilePath.Length); } else if (IsOption(CmdLineOption.Export)) { ExportPath = arg.Substring(CmdLineOption.Export.Length); } else if (IsOption(CmdLineOption.Portable)) { PortableMode = true; } else if (IsOption(CmdLineOption.ShowHelp)) { ShowHelp = true; } else if (IsOption(CmdLineOption.ShowHelp2)) { ShowHelp = true; } else { throw new ArgumentException("Unknown command line option '" + arg + "'."); } } // Determine whether to run the app in portable mode PortableMode |= Path_.FileExists(Path_.CombinePath(exe_dir, "portable")); // Set the UserDataDir based on whether we're running in portable mode or not UserDataDir = PortableMode ? Util.ResolveAppPath() : Util.ResolveUserDocumentsPath(Util.AppCompany, Util.AppProductName); // If we're in portable mode, check that we have write access to the local directory if (PortableMode) { if (!Path_.DirExists(UserDataDir) || (new DirectoryInfo(UserDataDir).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { throw new IOException("Unable to run in portable mode as the directory ('" + UserDataDir + "') is readonly."); } } // If not in portable mode, check the UserDataDir directory exists (or can be created) else { if (!Path_.DirExists(UserDataDir)) { Directory.CreateDirectory(UserDataDir); } } // If the export option is given, a 'FileToLoad' must also be given if (ExportPath != null && FileToLoad == null) { throw new ArgumentException("A file to export must be given if the '-e' option is used"); } // If a settings path has not been given, use the defaults if (SettingsPath == null) { SettingsPath = Path.Combine(UserDataDir, "settings2.xml"); } // Set the licence file path LicenceFilepath = PortableMode ? Path.Combine(exe_dir, "licence.xml") : Path.Combine(UserDataDir, "licence.xml"); // If no licence file exists, create the free one if (!Path_.FileExists(LicenceFilepath)) { new Licence().WriteLicenceFile(LicenceFilepath); } }
/// <summary>Pull 'filename' from EDDB. Returns true if the file was downloaded, and the output filepath</summary> public async Task <DownloadFileResult> DownloadFile(string file_url, string output_dir, TimeSpan?maximum_age = null) { var filename = Path_.FileName(file_url); var output_path = Path_.CombinePath(Path_.CreateDirs(output_dir), filename); using (StatusStack.NewStatusMessage($"Downloading '{filename}'...")) { try { HttpRequestMessage req; HttpResponseMessage resp; // Request the head information about the target file Log.Write(ELogLevel.Info, $"Checking size and timestamp of '{filename}'"); req = new HttpRequestMessage(HttpMethod.Head, file_url); resp = await Client.SendAsync(req, Shutdown); if (!resp.IsSuccessStatusCode) { Log.Write(ELogLevel.Error, $"Downloading information for '{filename}' failed: {resp.StatusCode} {resp.ReasonPhrase}"); throw new HttpRequestException($"{resp.ReasonPhrase} ({resp.StatusCode})"); } // Only download if the server version is newer. if (maximum_age != null && Path_.FileExists(output_path)) { var time_diff = new FileInfo(output_path).LastWriteTime - resp.Content.Headers.LastModified; if (time_diff > -maximum_age.Value) { Log.Write(ELogLevel.Info, $"Local copy of '{filename}' is less than {Settings.Instance.DataAge.ToPrettyString(trailing_zeros:false)} older than the latest version"); return(new DownloadFileResult(file_url, output_path, false)); } } // Get the download size (remember it might be compressed) var length = resp.Content.Headers.ContentLength; // The server version is newer, download the whole file Log.Write(ELogLevel.Info, $"Downloading '{filename}' ({length} bytes)"); using (Scope.Create(() => Downloading = true, () => Downloading = false)) { // Make the web request req = new HttpRequestMessage(HttpMethod.Get, file_url); resp = await Client.SendAsync(req, Shutdown); if (!resp.IsSuccessStatusCode) { Log.Write(ELogLevel.Error, $"Downloading '{filename}' failed: {resp.StatusCode} {resp.ReasonPhrase}"); throw new HttpRequestException($"{resp.ReasonPhrase} ({resp.StatusCode})"); } // Read the response content into a file using (var file = new FileStream(output_path, FileMode.Create, FileAccess.Write, FileShare.Read)) { // Decompress if the content is compressed if (resp.Content.Headers.ContentEncoding.Any(x => x == "gzip")) { using (var content = await resp.Content.ReadAsStreamAsync()) using (var gzip = new GZipStream(content, CompressionMode.Decompress)) await gzip.CopyToAsync(file); } else { await resp.Content.CopyToAsync(file); } Log.Write(ELogLevel.Info, $"Download complete '{filename}'"); return(new DownloadFileResult(file_url, output_path, true)); } } } catch { Log.Write(ELogLevel.Error, $"Data file '{filename}' was not available from {file_url}."); return(new DownloadFileResult(file_url, output_path, false)); } } }
// Notes: // - Parsed command line options public StartupOptions(string[] args) { FilesToLoad = new List <string>(); SettingsPath = null !; var exe_dir = Util.ResolveAppPath(); if (!Path_.DirExists(exe_dir)) { throw new ArgumentException("Cannot determine the current executable directory"); } // Check the command line options for (int i = 0, iend = args.Length; i != iend; ++i) { var arg = args[i].ToLowerInvariant(); // No option character implies the file to load if (arg[0] != '-' && arg[0] != '/') { FilesToLoad.Add(arg); continue; } // Helper for comparing option strings bool IsOption(string opt) => string.CompareOrdinal(arg, 0, opt, 0, opt.Length) == 0; // (order these by longest option first) if (IsOption(CmdLine.SettingsPath)) { SettingsPath = arg.Substring(CmdLine.SettingsPath.Length); } else if (IsOption(CmdLine.Portable)) { PortableMode = true; } else if (IsOption(CmdLine.ShowHelp)) { ShowHelp = true; } else if (IsOption(CmdLine.ShowHelp2)) { ShowHelp = true; } else { throw new ArgumentException($"Unknown command line option '{arg}'."); } } // Determine whether to run the app in portable mode PortableMode |= Path_.FileExists(Path_.CombinePath(exe_dir, "portable")); // Set the UserDataDir based on whether we're running in portable mode or not UserDataDir = Path.GetFullPath(PortableMode ? Path_.CombinePath(exe_dir, "UserData") : Util.ResolveUserDocumentsPath("Rylogic", "LDraw")); Path_.CreateDirs(UserDataDir); // Check that we have write access to the user data directory if (!Path_.DirExists(UserDataDir) || (new DirectoryInfo(UserDataDir).Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) { throw new IOException($"The user data directory ('{UserDataDir}') is readonly."); } // If a settings path has not been given, use the defaults SettingsPath ??= Path_.CombinePath(UserDataDir, "settings2.xml"); }
/// <summary> /// Smart copy from 'src' to 'dst'. Loosely like XCopy. /// 'src' can be a single file, a comma separated list of files, or a directory<para/> /// 'dst' can be a /// if 'src' is a directory, </summary> public static void Copy(string src, string dst, bool overwrite = false, bool only_if_modified = false, bool ignore_non_existing = false, Action <string>?feedback = null, bool show_unchanged = false) { var src_is_dir = Path_.IsDirectory(src); var dst_is_dir = Path_.IsDirectory(dst) || dst.EndsWith("/") || dst.EndsWith("\\") || src_is_dir; // Find the names of the source files to copy var files = new List <string>(); if (src_is_dir) { files = Path_.EnumFileSystem(src, SearchOption.AllDirectories).Select(x => x.FullName).ToList(); } else if (Path_.FileExists(src)) { files = new List <string>() { src } } ; else if (src.Contains('*') || src.Contains('?')) { files = Path_.EnumFileSystem(src, SearchOption.AllDirectories, new Pattern(EPattern.Wildcard, src).RegexString).Select(x => x.FullName).ToList(); } else if (!ignore_non_existing) { throw new FileNotFoundException($"'{src}' does not exist"); } // If the 'src' represents multiple files, 'dst' must be a directory if (src_is_dir || files.Count > 1) { // if 'dst' doesn't exist, assume it's a directory if (!Path_.DirExists(dst)) { dst_is_dir = true; } // or if it does exist, check that it is actually a directory else if (!dst_is_dir) { throw new FileNotFoundException($"'{dst}' is not a valid directory"); } } // Ensure that 'dstdir' exists. (Canonicalise fixes the case where 'dst' is a drive, e.g. 'C:\') var dstdir = Path_.Canonicalise((dst_is_dir ? dst : Path_.Directory(dst)).TrimEnd('/', '\\')); if (!Path_.DirExists(dstdir)) { Directory.CreateDirectory(dstdir); } // Copy the file(s) to 'dst' foreach (var srcfile in files) { // If 'dst' is a directory, use the same filename from 'srcfile' var dstfile = string.Empty; if (dst_is_dir) { var spath = src_is_dir ? Path_.RelativePath(src, srcfile) : Path_.FileName(srcfile); dstfile = Path_.CombinePath(dstdir, spath); } else { dstfile = dst; } // If 'srcfile' is a directory, ensure the directory exists at the destination if (Path_.IsDirectory(srcfile)) { if (!dst_is_dir) { throw new Exception($"ERROR: {dst} is not a directory"); } // Create the directory at the destination if (!Path_.DirExists(dstfile)) { System.IO.Directory.CreateDirectory(dstfile); } if (feedback != null) { feedback(srcfile + " --> " + dstfile); } } else { // Copy if modified or always based on the flag if (only_if_modified && !Path_.DiffContent(srcfile, dstfile)) { if (feedback != null && show_unchanged) { feedback(srcfile + " --> unchanged"); } continue; } // Ensure the directory path exists var d = Path_.Directory(dstfile); var f = Path_.FileName(dstfile); if (!Path_.DirExists(d)) { System.IO.Directory.CreateDirectory(d); } if (feedback != null) { feedback(srcfile + " --> " + dstfile); } File.Copy(srcfile, dstfile, overwrite); } } }
public static string CacheDBFilePath(Settings settings, string sym) { return(Path_.CombinePath(settings.General.PriceDataCacheDir, "PriceData_{0}.db".Fmt(sym))); }