public void RegexIsValid_WithValidRegex_ReturnsTrue() { const string input = @"\d+"; var converter = new RegexConverter(); Assert.IsTrue(converter.IsValid(input)); }
/// <summary> /// Extracts season and episode number by using some regexes from config file /// </summary> /// <param name="ie">Candidate which should be processed</param> public static void ExtractSeasonAndEpisode(MediaFile ie) { string[] patterns = Helper.ReadProperties(AppProperties.EPISODE_IDENTIFIER_PATTERNS_KEY); for (int i = 0; i < patterns.Length; i++) { patterns[i] = RegexConverter.toRegex(patterns[i]); } ExtractSeasonAndEpisode(ie, patterns); }
private string GetXml(Regex reg) { var rc = new RegexConverter(reg); XmlMode xmlMode = cbWithSchema.Checked ? XmlMode.XmlWithEmbeddedSchema : XmlMode.XmlOnly; return(rc.GetXmlString(xmlMode)); }
public void ConvertFrom() { RegexConverter rc = new RegexConverter(); object regex = rc.ConvertFrom("[a-z]"); Assert.IsNotNull(regex); Assert.IsTrue(regex is Regex); Assert.IsFalse(((Regex)regex).IsMatch("2")); }
static Converters() { IPAddress = new IPAddressConverter (); Regex = new RegexConverter (); ZeroOneBoolean = new ZeroOneBooleanConverter (); TextualZeroOneBoolean = new ZeroOneBooleanConverter (true); Uri = new UriConverter (); Version = new VersionConverter (); XmlNode = new XmlNodeConverter (); }
public void ConvertRegex_WithRegularExpressionInput_ParsesRegex() { var expected = new Regex(@"\d+").ToString(); const string input = @"\d+"; var converter = new RegexConverter(); var actual = converter.ConvertFromInvariantString(input).ToString(); Assert.AreEqual(expected, actual); }
public void WriteJsonNull() { StringWriter sw = new StringWriter(); JsonTextWriter jsonWriter = new JsonTextWriter(sw); RegexConverter converter = new RegexConverter(); converter.WriteJson(jsonWriter, null, null); StringAssert.AreEqual(@"null", sw.ToString()); }
static Converters() { IPAddress = new IPAddressConverter(); Regex = new RegexConverter(); TextualZeroOneBoolean = new ZeroOneBooleanConverter(true); Type = new ReflectedTypeConverter(); Uri = new UriConverter(); Version = new VersionConverter(); XmlNode = new XmlNodeConverter(); ZeroOneBoolean = new ZeroOneBooleanConverter(); }
public static void ConvertWithRegex() { var converter = new RegexConverter(); var rx = (Regex)converter.ConvertFromInvariantString("th.s"); var match = rx.Match("send this home"); Console.WriteLine("Matched regex case sensitive: {0}", match.Value); match = rx.Match("send thus home"); Console.WriteLine("Matched regex case sensitive: {0}", match.Value); match = rx.Match("SEND THIS HOME"); Console.WriteLine("Matched regex case sensitive: {0}", match.Value); }
private void Matches(RegexGrammar g, string regex, string s) { var converter = new RegexConverter(); var c = converter.Convert(g.ParseExpression(regex)); var runner = PatternCompiler.Default.Compile(new Pattern() { Data = c }); var result = runner.Run(s); Assert.IsTrue(result.IsSuccessful && result.InputPosition >= s.Length, $"PEG from regex {regex} must match {s}. Matched {result.InputPosition} characters. Success: {result.IsSuccessful}"); }
public void ConvertStringToRegex() { var rString = ".+"; var converter = new RegexConverter(); var result = converter.ConvertFrom(rString); var rResult = result as Regex; Assert.NotNull(rResult); Assert.Equal(rString, rResult.ToString()); }
public void ConvertRegexToString() { var rString = ".+"; var regex = new Regex(rString); var converter = new RegexConverter(); var result = converter.ConvertTo(regex, typeof(string)); var sResult = result as string; Assert.False(string.IsNullOrEmpty(sResult)); Assert.Equal(rString, sResult); }
private static int GetNumCaptures(string regex, string strData) { var data = strData.ToCharArray(); var g = new RegexGrammar(PatternCompiler.Default); var converter = new RegexConverter(); var c = converter.Convert(g.ParseExpression(regex)); var matchPattern = new ZeroOrMore(new PrioritizedChoice(new CaptureGroup(0, c), new Any())); var p = new Pattern(null) { Data = matchPattern }; var runner = PatternCompiler.Default.Compile(p); var captures = new List <Capture>(); var result = runner.Run(data, 0, data.Length, captures); return(result.IsSuccessful ? captures.Count : -1); }
/// <summary> /// Extracts all downloaded archives and moves subtitles to the movie files with proper naming /// </summary> /// <param name="i">Index of the temporary directory in which subtitles are stored. Temp Directory PROVIDER_NAME_KEY is "TEMP"+i</param> public void ProcessSubtitles(int i) { string folder = Helper.ReadProperty(ConfigKeyConstants.LAST_DIRECTORY_KEY) + "TEMP" + i.ToString(); List <string> extensions = new List <string>(Helper.ReadProperties(ConfigKeyConstants.SUBTITLE_FILE_EXTENSIONS_KEY, true)); if (extensions == null) { Logger.Instance.LogMessage("No Subtitle Extensions found!", LogLevel.WARNING); return; } if (Directory.Exists(folder)) { extractArchives(folder, extensions); //now that everything is extracted, try to assign subtitles to episodes //first, figure out episode and season numbers from filenames //scan for subtitle files in temp folder List <FileSystemInfo> Files = new List <FileSystemInfo>(); int count = 0; foreach (string ex in extensions) { List <FileSystemInfo> fsi = Helper.GetAllFilesRecursively(folder, "*." + ex, ref count, null); Files.AddRange(fsi); } string[] patterns = Helper.ReadProperties(ConfigKeyConstants.EPISODE_IDENTIFIER_PATTERNS_KEY); foreach (FileSystemInfo file in Files) { int Season = -1; int Episode = -1; foreach (string str in patterns) { //replace %S and %E by proper regexps string pattern = RegexConverter.toRegex(str); Match m = Regex.Match(file.Name, pattern, RegexOptions.IgnoreCase | RegexOptions.RightToLeft); if (m.Success) { try { Season = Int32.Parse(m.Groups["Season"].Value); } catch (FormatException) { Logger.Instance.LogMessage("Cannot parse " + m.Groups["Season"].Value + " to an integer", LogLevel.WARNING); } try { Episode = Int32.Parse(m.Groups["Episode"].Value); } catch (FormatException) { Logger.Instance.LogMessage("Cannot parse " + m.Groups["Episode"].Value + " to an integer", LogLevel.WARNING); } break; } } //now that season and episode are known, assign the configurationFilePath to a SubtitleFile object bool contains = false; foreach (SubtitleFile s in this.subtitles) { if (Season != -1 && Episode != -1 && s.Episode == Episode && s.Season == Season) { s.Filenames.Add(file.Name); contains = true; } } if (!contains) { SubtitleFile sf = new SubtitleFile(); sf.Episode = Episode; sf.Season = Season; sf.Filenames.Add(file.Name); this.subtitles.Add(sf); } } int MatchedSubtitles = 0; //Move subtitle files to their video files foreach (MediaFile ie in MediaFileManager.Instance) { List <string> ext = new List <string>(Helper.ReadProperties(ConfigKeyConstants.VIDEO_FILE_EXTENSIONS_KEY)); for (int b = 0; b < ext.Count; b++) { ext[b] = ext[b].ToLower(); } if (ext.Contains(Path.GetExtension(ie.Filename).Substring(1).ToLower()) && ie.ProcessingRequested && ie.EpisodeNr != -1 && ie.SeasonNr != -1) { foreach (SubtitleFile sf in this.subtitles) { if (sf.Season == ie.SeasonNr && sf.Episode == ie.EpisodeNr) { bool move = false; string source = ""; string target = ie.FilePath.Path + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(ie.Filename) + Path.GetExtension(sf.Filenames[0]); if (sf.Filenames.Count == 1) { move = true; source = folder + Path.DirectorySeparatorChar + sf.Filenames[0]; } else { FileSelector fs = new FileSelector(sf.Filenames); if (fs.ShowDialog() == DialogResult.OK) { move = true; source = folder + Path.DirectorySeparatorChar + sf.Filenames[fs.selection]; } } if (File.Exists(target)) { if (MessageBox.Show(target + " already exists. Overwrite?", "Overwrite?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes) { File.Delete(target); } else { move = false; } } if (move) { try { File.Copy(source, target); MatchedSubtitles++; } catch (Exception ex) { Logger.Instance.LogMessage(source + " --> " + target + ": " + ex.Message, LogLevel.ERROR); } } } } } } Logger.Instance.LogMessage("Downloaded " + Files.Count + " subtitles and matched " + MatchedSubtitles + " of them.", LogLevel.INFO); //cleanup this.subtitles.Clear(); Directory.Delete(folder, true); //updateCandidateFiles(true); } }
/// <summary> /// Updatess list view and do lots of other connected stuff with it /// </summary> /// <param name="clear">if true, list is cleared first and unconnected subtitle files are scheduled to be renamed</param> public static void UpdateList(bool clear, BackgroundWorker worker, DoWorkEventArgs e) { MediaFileManager infoManager = MediaFileManager.Instance; // Clear list if desired, remove deleted files otherwise if (clear) { infoManager.clear(); } else { infoManager.removeMissingFileEntries(); } // read path from config && remove tailing slashes string path = Helper.ReadProperty(AppProperties.LAST_DIRECTORY_KEY); path = path.TrimEnd(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }); log.Debug("Opening folder " + path); bool CreateDirectoryStructure = Helper.ReadBool(AppProperties.CREATE_DIRECTORY_STRUCTURE_KEY); bool UseSeasonSubdirs = Helper.ReadBool(AppProperties.CREATE_SEASON_SUBFOLDERS_KEY); if (Directory.Exists(path)) { //scan for new files List <string> extensions = new List <string>(Helper.ReadProperties(AppProperties.VIDEO_FILE_EXTENSIONS_KEY)); extensions.AddRange(Helper.ReadProperties(AppProperties.SUBTITLE_FILE_EXTENSIONS_KEY)); //convert all extensions to lowercase for (int i = 0; i < extensions.Count; i++) { extensions[i] = extensions[i].ToLower(); } //read all files with matching extension List <FileSystemInfo> Files = new List <FileSystemInfo>(); int count = 0; foreach (string ex in extensions) { Files.AddRange(Helper.getAllFilesRecursively(path, "*." + ex, ref count, worker)); } if (worker.CancellationPending) { e.Cancel = true; log.Info("Cancelled opening folder."); return; } if (MainForm.Instance.InvokeRequired) { MainForm.Instance.Invoke(new EventHandler(delegate { MainForm.Instance.lblFileListingProgress.Visible = false; MainForm.Instance.taskProgressBar.Visible = true; })); } else { MainForm.Instance.lblFileListingProgress.Visible = false; MainForm.Instance.taskProgressBar.Visible = true; } //some declarations already for speed string[] patterns = Helper.ReadProperties(AppProperties.EPISODE_IDENTIFIER_PATTERNS_KEY); for (int i = 0; i < patterns.Length; i++) { patterns[i] = RegexConverter.toRegex(patterns[i]); } int DirectorySeason = -1; MediaFile ie = null; bool contains = false; DateTime dt; string currentpath = ""; string MovieIndicator = String.Join("|", Helper.ReadProperties(AppProperties.MOVIE_INDICATOR_STRINGS_KEY)); //Form1.Instance.taskProgressBar.Maximum = Files.Count; for (int f = 0; f < Files.Count; f++) { if (worker.CancellationPending) { e.Cancel = true; log.Info("Cancelled opening folder."); return; } //Form1.Instance.taskProgressBar.Value=f; worker.ReportProgress((int)((double)f / ((double)Files.Count) * 100)); FileSystemInfo file = Files[f]; //showname and season recognized from path DirectorySeason = -1; //Check if there is already an entry on this file, and if not, create one ie = null; currentpath = Path.GetDirectoryName(file.FullName); foreach (MediaFile i in MediaFileManager.Instance) { if (i.Filename == file.Name && i.FilePath.Path == currentpath) { ie = i; break; } } if (ie == null) { ie = new MediaFile(); } //test for movie path so we can skip all series recognition code if (Regex.Match(currentpath, MovieIndicator).Success) { ie.isMovie = true; } //Set basic values, by setting those values destination directory and configurationFilePath will be generated automagically ie.FilePath.Path = currentpath; ie.Filename = file.Name; ie.Extension = Path.GetExtension(file.FullName).ToLower().Replace(".", ""); if (!ie.isMovie) { //Get season number and showname from directory DirectorySeason = ExtractSeasonFromDirectory(Path.GetDirectoryName(file.FullName)); dt = DateTime.Now; //try to recognize season and episode from configurationFilePath /////////////////////////////////////////////////// ExtractSeasonAndEpisode(ie, patterns); /////////////////////////////////////////////////// //Need to do this for filenames which only contain episode numbers (But might be moved in a season dirArgument already) //if season number couldn't be extracted, try to get it from folder if (ie.SeasonNr <= 0 && DirectorySeason != -1) { ie.SeasonNr = DirectorySeason; } //if season recognized from directory name doesn't match season recognized from configurationFilePath, the file might be located in a wrong directory if (DirectorySeason != -1 && ie.SeasonNr != DirectorySeason) { log.Warn("File seems to be located inconsistently: " + ie.Filename + " was recognized as season " + ie.SeasonNr + ", but folder name indicates that it should be season " + DirectorySeason.ToString()); } } //if nothing could be recognized, assume that this is a movie if ((ie.SeasonNr < 1 && ie.EpisodeNr < 1) || ie.isMovie) { ie.RemoveVideoTags(); } //if not added yet, add it if (!contains) { MediaFileManager.Instance.Add(ie); } } //SelectSimilarFilesForProcessing(path,Helper.ReadProperties(AppProperties.LAST_SEARCHED_SERIES_TITLES_KEY)[0]); SelectRecognizedFilesForProcessing(); } //Recreate subtitle names so they can adjust to the video files they belong to foreach (MediaFile ie in MediaFileManager.Instance) { if (ie.IsSubtitle) { ie.createTargetName(); } } log.Info("Found " + MediaFileManager.Instance.CountMediaFiles + " Files"); if (Helper.ReadBool(AppProperties.REPORT_MISSING_EPISODES_KEY)) { FindMissingEpisodes(); } }
static void Main(string[] args) { //{ // var jitter = new CustomJitter("Regex.dll"); // var rg = new RegexGrammar(new PatternCompiler(new Compiler(), new DefaultOptimizer(), jitter)); // jitter.Save(); // rg.ParseExpression("abc"); //} var patternCompiler = new PatternCompiler(new Compiler(), null, new ILJitter()); var regexGrammar = new Lazy <RegexGrammar>(() => new RegexGrammar(patternCompiler)); var converter = new RegexConverter(); var helper = new PegHelper(patternCompiler); helper.EnsureExpressionBuilt(); //CompileAndWritePatternToFile("PegExpression", helper.GetExpressionPattern()); //var input = "AAA AAAas ntAar ".ToCharArray(); var input = GenerateInputData(1 << 20); //var pattern = new PointerImplementation(); //var patternStr = "([A-Za-z] 'awyer' [ \t] / [A-Za-z] 'inn' [ \t])"; //var patternStr = "([A-Za-z] 'x')"; //var patternStr = "([A-Za-z] 'awyer' [ \t] / [A-Za-z] 'inn' [ \t])"; //var patternStr = "'Tom' / 'Finn' / 'Sawyer' / 'Huckleberry'"; //var patternStr = "'Tom' / 'Sawyer' / 'Huckleberry' / 'Finn' "; //var patternStr = "[ -z][ -z]([ -z][ -z]('Tom' / 'Sawyer' / 'Huckleberry' / 'Finn') / [ -z]('Tom' / 'Sawyer' / 'Huckleberry' / 'Finn') / ('Tom' / 'Sawyer' / 'Huckleberry' / 'Finn'))"; //var patternStr = "[ -z][ -z]([ -z][ -z]('T' / 'S') / [ -z]('T' / 'Sawye' / 'Huck') / 'Huckleberry')"; //var patternStr = "[ -z][ -z]([ -z][ -z]('Tom' / 'Sawyer' / 'Huckleberry' / 'Finn') / [ -z]('Tom' / 'Sawyer' / 'Huckleberry' / 'Finn') / ('Tom' / 'Sawyer' / 'Huckleberry' / 'Finn'))"; //var patternStr = "[ -z][ -z]([ -z][ -z]('T' / 'S' / 'H') / [ -z]('T' / 'S' / 'H') / ('T' / 'S'))"; //var patternStr = $"[ -{char.MaxValue}][ -z]([ -z][ -z]('T' / 'S') / [ -z]('T'))"; //var patternStr = ".. ('T' / 'SS' / 'HHH' / 'FFFF')"; //var patternStr = "('T' / 'SS' / 'HHH' / 'FFFF')"; //var patternStr = ".. ('TT' / 'FFF')"; //var patternStr = "'Twain'"; //var patternStr = "[a-z] 'shing'"; //var patternStr = "[a-z]+"; //var patternStr = "('Huck'[a-zA-Z]+) / ('Saw'[a-zA-Z]+)"; //var m = $"[{char.MinValue}-uz-{char.MaxValue}]"; //var patternStr = $"[a-q]{m}{m}{m}{m}{m}{m}{m}{m}{m}{m}{m}{m}{m} 'x'"; //var pattern = CompileAndWritePatternToFile("SimpleMatch", new Pattern("SimpleMatch") { Data = helper.ParseExpression("[a-z]*") }); //var p = converter.Convert(regexGrammar.Value.ParseExpression("Twain")); //var p = converter.Convert(regexGrammar.Value.ParseExpression("river.{20,50}Tom|Tom.{20,50}river")); //var p = converter.Convert(regexGrammar.Value.ParseExpression("river.{10,25}Tom|Tom.{10,25}river")); //var a = new Pattern("A"); //a.Data = new PrioritizedChoice(new Sequence(letters, a), new Empty()); //var p = new Sequence(letters, a); //var p = new Sequence(new PrioritizedChoice('T', 'R'), "om");//Operator.EndingWithGreedy(capitalsAndNonCapitals, CharacterClass.String("ing")); //var ws = new Pattern { Data = new ZeroOrMore(new CharacterClass(' ')) }; //var p1 = new Pattern { Data = new Sequence(ws, CharacterClass.String("abc")) }; //var p2 = new Pattern { Data = new Sequence(ws, CharacterClass.String("xyz")) }; //var p = new PrioritizedChoice(p1, p2); var p = new ZeroOrMore(new PrioritizedChoice(new CaptureGroup(0, converter.Convert(regexGrammar.Value.ParseExpression("([A-Za-z]awyer|[A-Za-z]inn)\\s"))), new Any())); var s2 = new Stopwatch(); s2.Start(); var peg = new Pattern("SimpleMatch") { Data = p, //Data = new ZeroOrMore(new PrioritizedChoice(new CaptureGroup(0, p), new Any())) }; var pattern = CompileAndWritePatternToFile("SimpleMatch", peg); Console.WriteLine($"Saved ({s2.ElapsedMilliseconds}ms)"); var text = "Tom..Huckleberry Finn Tom Tom Huck\nFinn,"; var capts = new List <Capture>(); var runResult = pattern.Run(text, capts); if (runResult.IsSuccessful && runResult.InputPosition == text.Length) { Console.WriteLine($"Successful match on '{text}'"); } //for (var n = 0; n < 10; n++) //{ // for (var x = 0; x < 25; x++) // { // //var pegGrammar = new PegGrammar(new ILInterpreterFactory()); // //pegGrammar.EnsureExpressionBuilt(); // //var expression = pegGrammar.ParseExpression("'th' [a-z]+"); // //var compiler = (new ILCompilerFactory()).Create(new Pattern // //{ // // Data = new ZeroOrMore(new PrioritizedChoice(new CaptureGroup(0, expression), new Any())) // //}); // Stopwatch s = new Stopwatch(); // s.Start(); // var result = default(RunResult); // var captures = new List<Capture>(); // for (var i = 0; i < 1000; i++) // { // captures = new List<Capture>(); // result = pattern.Run(input, 0, input.Length, captures); // if (!result.IsSuccessful) // { // Console.WriteLine("Match fail"); // } // } // s.Stop(); // Console.WriteLine($"That took {s.ElapsedMilliseconds}ms ({captures.Count})"); // } //} Console.ReadKey(); }
private static Regex GetReg(string xml) { Regex reg = RegexConverter.GetRegex(xml); return(reg); }
public void ConvertFromNonSupportedOptionBails() { RegexConverter rc = new RegexConverter(); rc.ConvertFrom(12); }
public void ConvertFromNullReference() { RegexConverter rc = new RegexConverter(); rc.ConvertFrom(null); }
private static void BeginRequestHandler(object sender, EventArgs e) { var pluginHelper = ServiceLocator.Current.GetInstance <IPluginHelper>(); var installedPlugins = pluginHelper.GetInstalledPlugins(); //find any http handlers registered in modules and check if request is qualified for any handler.) foreach (PluginHttpHandler handler in installedPlugins.Where(plugin => plugin.PluginSetting.HttpHandlers != null).SelectMany( plugin => plugin.PluginSetting.HttpHandlers)) { if (handler.Verb.Trim().Equals(AsterixExpression) || handler.Verb.ToLower().Contains(HttpContext.Current.Request.RequestType.ToLower())) { if (Regex.IsMatch(ApplicationUtility.GetUrlPath(HttpContext.Current.Request.RawUrl), RegexConverter.FilterToRegex(handler.Path), RegexOptions.IgnoreCase)) { //get handler's type and check if this type inherited from IHttpHandler interface. var handlerType = Type.GetType(handler.HandlerType); if (handlerType.GetInterfaces().Contains(typeof(IHttpHandler))) { HttpContext.Current.RemapHandler((IHttpHandler)Activator.CreateInstance(handlerType)); } } } } }