<commit_before>﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Threading;
using NuGet.Packaging;
using NuGet.Versioning;
using NuGetPackageExplorer.Types;
using NuGetPe;
using PackageExplorer.Properties;
using PackageExplorerViewModel;
using Constants = NuGetPe.Constants;
using LazyPackageCommand = System.Lazy<NuGetPackageExplorer.Types.IPackageCommand, NuGetPackageExplorer.Types.IPackageCommandMetadata>;
using StringResources = PackageExplorer.Resources;

namespace PackageExplorer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    [Export]
    public partial class MainWindow : Window
    {
        private readonly IMruManager _mruManager;

        [ImportingConstructor]
        public MainWindow(IMruManager mruManager)
        {
            InitializeComponent();

            RecentFilesMenuItem.DataContext = _mruManager = mruManager;
            RecentFilesContainer.Collection = _mruManager.Files;

            if (AppCompat.IsWindows10S)
            {
                pluginMenuItem.Visibility = Visibility.Collapsed;
                pluginMenuItem.IsEnabled = false;
                mnuPluginSep.Visibility = Visibility.Collapsed;
            }
        }

        [Import]
        public ISettingsManager SettingsManager { get; set; }

        [Import]
        public IUIServices UIServices { get; set; }

        [Import]
        public INuGetPackageDownloader PackageDownloader { get; set; }

        [Import]
        public IPluginManager PluginManager { get; set; }

        [Import]
        public IPackageChooser PackageChooser { get; set; }

        [Import]
        public IPackageViewModelFactory PackageViewModelFactory { get; set; }

        [ImportMany(AllowRecomposition = true)]
        public ObservableCollection<LazyPackageCommand> PackageCommands
        {
            get
            {
                return PackageCommandsContainer != null
                           ? (ObservableCollection<LazyPackageCommand>)PackageCommandsContainer.Collection
                           : null;
            }
            set
            {
                if (PackageCommandsContainer != null)
                {
                    PackageCommandsContainer.Collection = value;
                }
            }
        }

        [Export]
        public IPackageEditorService EditorService { get; set; }

        private string _tempFile;

        private bool HasUnsavedChanges
        {
            get
            {
                var viewModel = (PackageViewModel)DataContext;
                return (viewModel != null && viewModel.HasEdit);
            }
        }

        private bool IsInEditFileMode
        {
            get
            {
                var viewModel = (PackageViewModel)DataContext;
                return (viewModel != null && viewModel.IsInEditFileMode);
            }
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            try
            {
                LoadSettings();
            }
            catch (Exception)
            {
            }
        }

        internal async Task OpenLocalPackage(string packagePath)
        {
            if (!File.Exists(packagePath))
            {
                UIServices.Show("File not found at " + packagePath, MessageLevel.Error);
                return;
            }

            var oldContent = PackageSourceItem.Content;
            PackageSourceItem.SetCurrentValue(ContentProperty, "Loading " + packagePath + "...");

            var succeeded = await Dispatcher.InvokeAsync(
                () => OpenLocalPackageCore(packagePath), DispatcherPriority.Loaded);

            if (!succeeded)
            {
                // restore old content
                PackageSourceItem.SetCurrentValue(ContentProperty, oldContent);
            }
        }

        private bool OpenLocalPackageCore(string packagePath)
        {
            IPackage package = null;

            var tempFile = Path.GetTempFileName();
            try
            {
                File.Copy(packagePath, tempFile, overwrite: true);

                var extension = Path.GetExtension(packagePath);
                if (extension.Equals(Constants.PackageExtension, StringComparison.OrdinalIgnoreCase))
                {
                    package = new ZipPackage(tempFile);
                }
                else if (extension.Equals(Constants.ManifestExtension, StringComparison.OrdinalIgnoreCase))
                {
                    using (var str = ManifestUtility.ReadManifest(tempFile))
                    {
                        var builder = new PackageBuilder(str, Path.GetDirectoryName(tempFile));
                        package = builder.Build();
                    }
                }

                if (package != null)
                {
                    LoadPackage(package, packagePath, PackageType.LocalPackage);
                    _tempFile = tempFile;
                    return true;
                }
            }
            catch (Exception ex)
            {
                package = null;
                UIServices.Show(ex.Message, MessageLevel.Error);
                return false;
            }
            finally
            {
                if (package == null && File.Exists(tempFile))
                {
                    try
                    {
                        File.Delete(tempFile);
                    }
                    catch { /* ignore */ }
                }
            }

            return false;<commit_msg>fix open .nuspec files<commit_after>﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Threading;
using NuGet.Packaging;
using NuGet.Versioning;
using NuGetPackageExplorer.Types;
using NuGetPe;
using PackageExplorer.Properties;
using PackageExplorerViewModel;
using Constants = NuGetPe.Constants;
using LazyPackageCommand = System.Lazy<NuGetPackageExplorer.Types.IPackageCommand, NuGetPackageExplorer.Types.IPackageCommandMetadata>;
using StringResources = PackageExplorer.Resources;

namespace PackageExplorer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    [Export]
    public partial class MainWindow : Window
    {
        private readonly IMruManager _mruManager;

        [ImportingConstructor]
        public MainWindow(IMruManager mruManager)
        {
            InitializeComponent();

            RecentFilesMenuItem.DataContext = _mruManager = mruManager;
            RecentFilesContainer.Collection = _mruManager.Files;

            if (AppCompat.IsWindows10S)
            {
                pluginMenuItem.Visibility = Visibility.Collapsed;
                pluginMenuItem.IsEnabled = false;
                mnuPluginSep.Visibility = Visibility.Collapsed;
            }
        }

        [Import]
        public ISettingsManager SettingsManager { get; set; }

        [Import]
        public IUIServices UIServices { get; set; }

        [Import]
        public INuGetPackageDownloader PackageDownloader { get; set; }

        [Import]
        public IPluginManager PluginManager { get; set; }

        [Import]
        public IPackageChooser PackageChooser { get; set; }

        [Import]
        public IPackageViewModelFactory PackageViewModelFactory { get; set; }

        [ImportMany(AllowRecomposition = true)]
        public ObservableCollection<LazyPackageCommand> PackageCommands
        {
            get
            {
                return PackageCommandsContainer != null
                           ? (ObservableCollection<LazyPackageCommand>)PackageCommandsContainer.Collection
                           : null;
            }
            set
            {
                if (PackageCommandsContainer != null)
                {
                    PackageCommandsContainer.Collection = value;
                }
            }
        }

        [Export]
        public IPackageEditorService EditorService { get; set; }

        private string _tempFile;

        private bool HasUnsavedChanges
        {
            get
            {
                var viewModel = (PackageViewModel)DataContext;
                return (viewModel != null && viewModel.HasEdit);
            }
        }

        private bool IsInEditFileMode
        {
            get
            {
                var viewModel = (PackageViewModel)DataContext;
                return (viewModel != null && viewModel.IsInEditFileMode);
            }
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            try
            {
                LoadSettings();
            }
            catch (Exception)
            {
            }
        }

        internal async Task OpenLocalPackage(string packagePath)
        {
            if (!File.Exists(packagePath))
            {
                UIServices.Show("File not found at " + packagePath, MessageLevel.Error);
                return;
            }

            var oldContent = PackageSourceItem.Content;
            PackageSourceItem.SetCurrentValue(ContentProperty, "Loading " + packagePath + "...");

            var succeeded = await Dispatcher.InvokeAsync(
                () => OpenLocalPackageCore(packagePath), DispatcherPriority.Loaded);

            if (!succeeded)
            {
                // restore old content
                PackageSourceItem.SetCurrentValue(ContentProperty, oldContent);
            }
        }

        private bool OpenLocalPackageCore(string packagePath)
        {
            IPackage package = null;

            var tempFile = Path.GetTempFileName();
            try
            {
                File.Copy(packagePath, tempFile, overwrite: true);

                var extension = Path.GetExtension(packagePath);
                if (extension.Equals(Constants.PackageExtension, StringComparison.OrdinalIgnoreCase))
                {
                    package = new ZipPackage(tempFile);
                }
                else if (extension.Equals(Constants.ManifestExtension, StringComparison.OrdinalIgnoreCase))
                {
                    using (var str = ManifestUtility.ReadManifest(tempFile))
                    {
                        var builder = new PackageBuilder(str, Path.GetDirectoryName(packagePath));
                        package = builder.Build();
                    }
                }

                if (package != null)
                {
                    LoadPackage(package, packagePath, PackageType.LocalPackage);
                    _tempFile = tempFile;
                    return true;
                }
            }
            catch (Exception ex)
            {
                package = null;
                UIServices.Show(ex.Message, MessageLevel.Error);
                return false;
            }
            finally
            {
                if (package == null && File.Exists(tempFile))
                {
                    try
                    {
                        File.Delete(tempFile);
                    }
                    catch { /* ignore */ }
                }
            }

            return false;
{'fraction_non_alphanumeric': 0.07302718832891247, 'fraction_numerical': 0.0008289124668435014, 'mean_word_length': 1.5996552467140703, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '107622', 'n_tokens_mistral': 2901, 'n_tokens_neox': 2735, 'n_words': 578}