コード例 #1
0
        public void Activate()
        {
            using (ProgressDialog progressDlg = new ProgressDialog())
            {
                progressDlg.StartPosition = FormStartPosition.CenterScreen;
                progressDlg.Show();

                ProgressItem piPrepare = progressDlg.Add("Prepare ...", "get taxon with images", 0, 2);
                // retrouve la liste de toutes les especes / sous especes
                List <TaxonDesc> Nodes = new List <TaxonDesc>();
                TaxonUtils.OriginalRoot.ParseNodeDesc((d) => { if (d.HasImage)
                                                               {
                                                                   Nodes.Add(d);
                                                               }
                                                      });
                piPrepare.Update(1, "all images name (can be long)");
                // et la liste des fichiers dans le répertoires d'images
                string PathImages  = TaxonImages.Manager.Path;
                int    TotalImages = 0;
                Dictionary <string, bool> FileUsed = new Dictionary <string, bool>();
                Directory.GetFiles(PathImages, "*.jpg", SearchOption.AllDirectories).ToList().ForEach(f => { FileUsed[f.ToLower()] = false; TotalImages++; });
                piPrepare.Update(2, "init done");

                ProgressItem piTaxonDesc = progressDlg.Add("Parse Taxon with images ...", "", 0, Nodes.Count);
                int          count       = 0;
                foreach (TaxonDesc desc in Nodes)
                {
                    piTaxonDesc.Update(++count);
                    foreach (TaxonImageDesc imageDesc in desc.Images)
                    {
                        if (imageDesc.IsALink)
                        {
                            continue;
                        }
                        string path = imageDesc.GetPath(desc).ToLower();
                        if (FileUsed.ContainsKey(path))
                        {
                            FileUsed[path] = true;
                        }
                    }
                }

                List <string> unusedfiles = FileUsed.Where(p => !p.Value).Select(p => p.Key).ToList();
                ProgressItem  piFinder    = progressDlg.Add("Init finder...", "", 0, 1);
                TaxonSearch   finder      = new TaxonSearch(TaxonUtils.OriginalRoot, true, true);
                piFinder.Update(1);

                int noTaxonFound       = 0;
                int severalTaxonFound  = 0;
                int renamedFiles       = 0;
                int sameName           = 0;
                int errorWhileRenaming = 0;

                string logfile = Path.Combine(TaxonUtils.GetLogPath(), "ImageRenameSynonyms.log");
                if (File.Exists(logfile))
                {
                    File.Delete(logfile);
                }
                using (StreamWriter log = new StreamWriter(logfile))
                {
                    ProgressItem piUnused = progressDlg.Add("Treat unused images ...", "", 0, unusedfiles.Count);
                    foreach (string filename in unusedfiles)
                    {
                        piUnused.Inc();
                        TaxonImages.SplitImageFilenameResult splitResult = TaxonImages.Manager.SplitImageFilename(filename);
                        if (splitResult == null)
                        {
                            continue;
                        }

                        List <TaxonTreeNode> nodes = finder.FindAll(splitResult.TaxonName);
                        string textline            = "[" + (nodes == null ? 0 : nodes.Count) + "] " + splitResult.TaxonName + ": ";
                        if (nodes == null || nodes.Count == 0)
                        {
                            noTaxonFound++;
                            textline += "no taxon found";
                        }
                        else if (nodes.Count > 1)
                        {
                            severalTaxonFound++;
                            textline += "too many taxons found";
                        }
                        else if (filename.ToLower() == splitResult.Desc.GetPath(nodes[0].Desc).ToLower())
                        {
                            sameName++;
                            textline += "same name, probably unused due to rank";
                        }
                        else
                        {
                            TaxonTreeNode  node = nodes[0];
                            TaxonImageDesc desc = splitResult.Desc;
                            desc.FillForNewFile(node.Desc);
                            string newFilename = desc.GetPath(node.Desc);
                            textline += " rename " + System.IO.Path.GetFileName(filename) + " in " + System.IO.Path.GetFileName(newFilename);
                            try
                            {
                                System.IO.File.Move(filename, newFilename);
                                renamedFiles++;
                            }
                            catch (System.Exception e)
                            {
                                textline += "[error] " + e.Message;
                                errorWhileRenaming++;
                            }
                        }
                        log.WriteLine(textline);
                    }
                }

                string message = "Rename unused file using synonyms name : \n\n";
                message += String.Format("    Total treated files: {0}\n", unusedfiles.Count);
                message += String.Format("    No match: {0}\n", noTaxonFound);
                message += String.Format("    Several matches: {0}\n", severalTaxonFound);
                message += String.Format("    New name is same as unused: {0}\n", sameName);
                message += String.Format("    Renamed files: {0}\n", renamedFiles);
                message += String.Format("    Error renaming: {0}\n", errorWhileRenaming);
                message += String.Format("Same renaming occurs, do not forget to update images\n");
                message += String.Format("for more details, look at ImageRenameSynonyms.log file");
                Loggers.WriteInformation(LogTags.Image, message);
            }
        }
コード例 #2
0
        public void Activate()
        {
            DateTime ts_Start = DateTime.Now;

            using (ProgressDialog progressDlg = new ProgressDialog())
            {
                progressDlg.StartPosition = FormStartPosition.CenterScreen;
                progressDlg.Show();

                // retrouve la liste de toutes les especes / sous especes
                ProgressItem         piPrepare = progressDlg.Add("Prepare ...", "get species", 0, 4);
                List <TaxonTreeNode> Species   = new List <TaxonTreeNode>();
                TaxonUtils.OriginalRoot.GetAllChildrenRecursively(Species, ClassicRankEnum.Espece);
                piPrepare.Update(1, "get sub species");
                TaxonUtils.OriginalRoot.GetAllChildrenRecursively(Species, ClassicRankEnum.SousEspece);
                piPrepare.Update(2, "init");

                int beforeWith    = 0;
                int beforeImages  = 0;
                int beforeWithout = 0;

                DateTime ts_StartDico = DateTime.Now;
                Dictionary <string, TaxonTreeNode> dico = new Dictionary <string, TaxonTreeNode>();
                foreach (TaxonTreeNode node in Species)
                {
                    if (node.Desc.Images != null && node.Desc.Images.Count != 0)
                    {
                        beforeWith++;
                        beforeImages += node.Desc.Images.Count;
                    }
                    else
                    {
                        beforeWithout++;
                    }

                    dico[node.Desc.RefMultiName.Main.ToLower()] = node;
                }

                piPrepare.Update(3, "clear");
                TaxonUtils.OriginalRoot.ParseNodeDesc((d) => { d.Images = null; });
                piPrepare.Update(4, "end");
                piPrepare.End();

                DateTime ts_StartParseCollection = DateTime.Now;

                List <string> badFormatFilenames = new List <string>();

                ProgressItem piCollections = progressDlg.Add("Parse collections ...", "", 0, TaxonImages.Manager.CollectionsEnumerable().Count());
                foreach (ImageCollection collection in TaxonImages.Manager.CollectionsEnumerable())
                {
                    piCollections.Update(piCollections.Current + 1, collection.Name);

                    if (!Directory.Exists(collection.Path))
                    {
                        continue;
                    }
                    IEnumerable <string> files = Directory.EnumerateFiles(collection.Path, "*.jpg");

                    int  count        = 10000;
                    Task getCountTask = Task.Factory.StartNew(() => { count = files.Count(); });

                    int          counter  = 0;
                    ProgressItem piPhotos = progressDlg.Add("Parse " + collection.Name + " photos ...", "", 0, count - 1);
                    foreach (string file in files)
                    {
                        string name = Path.GetFileNameWithoutExtension(file);

                        counter++;
                        if (counter == 10)
                        {
                            counter = 0;
                            if (getCountTask != null)
                            {
                                if (getCountTask.IsCompleted)
                                {
                                    piPhotos.Max = count - 1;
                                    getCountTask = null;
                                }
                                else if (piPhotos.Max < piPhotos.Current + 10)
                                {
                                    piPhotos.Max += 10000;
                                }
                            }
                            piPhotos.Update(piPhotos.Current + 10, name);
                        }

                        TaxonImages.SplitImageFilenameResult result = TaxonImages.Manager.SplitImageFilename(file, false, collection);
                        if (result == null)
                        {
                            badFormatFilenames.Add(file);
                            continue;
                        }

                        name = result.TaxonName.ToLower();

                        if (dico.ContainsKey(name))
                        {
                            if (dico[name].Desc.Images == null)
                            {
                                dico[name].Desc.Images = new List <TaxonImageDesc>();
                            }
                            dico[name].Desc.Images.Add(result.Desc);
                        }
                    }
                    piPhotos.End();

                    // treat xmls
                    foreach (var pair in collection.AllLinks)
                    {
                        ImagesLinks list = pair.Value;

                        for (int i = 0; i < list.Count; i++)
                        {
                            ImageLink link = list[i];
                            if (dico.TryGetValue(link.Key.ToLower(), out TaxonTreeNode node))
                            {
                                TaxonImageDesc desc = new TaxonImageDesc
                                {
                                    CollectionId = collection.Id,
                                    Secondary    = false,
                                    LinksId      = pair.Key,
                                    Index        = i
                                };

                                if (node.Desc.Images == null)
                                {
                                    node.Desc.Images = new List <TaxonImageDesc>();
                                }
                                node.Desc.Images.Add(desc);
                            }
                        }
                    }

                    foreach (var reference in collection.DistantReferences)
                    {
                        if (dico.TryGetValue(reference.TaxonName.ToLower(), out TaxonTreeNode node))
                        {
                            TaxonImageDesc desc = new TaxonImageDesc
                            {
                                CollectionId = collection.Id,
                                Secondary    = false,
                                LinksId      = 0,
                                Index        = reference.Index
                            };

                            if (node.Desc.Images == null)
                            {
                                node.Desc.Images = new List <TaxonImageDesc>();
                            }
                            node.Desc.Images.Add(desc);
                        }
                    }
                }

                DateTime ts_StartFinalise = DateTime.Now;

                int afterWith    = 0;
                int afterImages  = 0;
                int afterWithout = 0;

                foreach (TaxonTreeNode node in Species)
                {
                    if (node.Desc.Images != null)
                    {
                        afterWith++;
                        afterImages += node.Desc.Images.Count;
                    }
                    else
                    {
                        afterWithout++;
                    }
                }

                DateTime ts_End = DateTime.Now;

                string message = "Update image flag : \n\n";
                message += String.Format("    Total with image     : {0} referencing {1} images, (before: {2} referencing {3} images)\n", afterWith, afterImages, beforeWith, beforeImages);
                message += String.Format("    Total without image  : {0}, (before: {1})\n\n", afterWithout, beforeWithout);
                message += String.Format("    Badly formatted files: {0}\n", badFormatFilenames.Count);
                message += String.Format("    {0} ms to get taxons\n", (ts_StartDico - ts_Start).Milliseconds);
                message += String.Format("    {0} ms to fill dico\n", (ts_StartParseCollection - ts_StartDico).Milliseconds);
                message += String.Format("    {0} ms to parse collections\n", (ts_StartFinalise - ts_StartParseCollection).Milliseconds);
                message += String.Format("    {0} ms to finalize taxons\n", (ts_End - ts_StartFinalise).Milliseconds);
                message += String.Format("Dumped in UpdateImages.log file");
                Loggers.WriteInformation(LogTags.Image, message);

                string logfile = Path.Combine(TaxonUtils.GetLogPath(), "UpdateImages.log");
                if (File.Exists(logfile))
                {
                    File.Delete(logfile);
                }
                using (StreamWriter log = new StreamWriter(logfile))
                {
                    log.WriteLine("UpdateImages result ( " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + " )\n");
                    log.WriteLine(message);
                    log.WriteLine("");
                    log.WriteLine("Files with bad format names detected:\n");
                    foreach (string filename in badFormatFilenames)
                    {
                        log.WriteLine("    " + filename);
                    }
                }
            }

            TaxonUtils.OriginalRoot.UpdateAvailableImages();
            TaxonControlList.OnAvailableImagesChanged();
        }