Exemple #1
0
        private async Task <InterpreterAnswerDto> GetInterpreter(InterpreterAnswerModel interpreterModel, int brokerId)
        {
            if (interpreterModel.InterpreterId == Constants.DeclineInterpreterId)
            {
                return(new InterpreterAnswerDto
                {
                    Accepted = false,
                    DeclineMessage = interpreterModel.DeclineMessage
                });
            }
            var newInterpreterInformation = new InterpreterInformation
            {
                FirstName             = interpreterModel.NewInterpreterFirstName,
                LastName              = interpreterModel.NewInterpreterLastName,
                Email                 = interpreterModel.NewInterpreterEmail,
                PhoneNumber           = interpreterModel.NewInterpreterPhoneNumber,
                OfficialInterpreterId = interpreterModel.NewInterpreterOfficialInterpreterId
            };
            var interpreter = await _interpreterService.GetInterpreter(interpreterModel.InterpreterId.Value, newInterpreterInformation, brokerId);

            var requirementAnswers = interpreterModel.RequiredRequirementAnswers?.Select(ra => new OrderRequirementRequestAnswer
            {
                OrderRequirementId    = ra.OrderRequirementId,
                Answer                = ra.Answer,
                CanSatisfyRequirement = ra.CanMeetRequirement
            }).ToList() ?? new List <OrderRequirementRequestAnswer>();

            if (interpreterModel.DesiredRequirementAnswers != null)
            {
                requirementAnswers.AddRange(interpreterModel.DesiredRequirementAnswers.Select(ra => new OrderRequirementRequestAnswer
                {
                    OrderRequirementId    = ra.OrderRequirementId,
                    Answer                = ra.Answer,
                    CanSatisfyRequirement = ra.CanMeetRequirement
                }).ToList());
            }
            //Collect the interpreter information
            return(new InterpreterAnswerDto
            {
                Accepted = true,
                CompetenceLevel = interpreterModel.InterpreterCompetenceLevel.Value,
                ExpectedTravelCosts = interpreterModel.ExpectedTravelCosts,
                ExpectedTravelCostInfo = interpreterModel.ExpectedTravelCostInfo,
                RequirementAnswers = requirementAnswers,
                Interpreter = interpreter
            });
        }
Exemple #2
0
        public async Task <InterpreterBroker> GetInterpreter(int interpreterId, InterpreterInformation interpreterInformation, int brokerId)
        {
            NullCheckHelper.ArgumentCheckNull(interpreterInformation, nameof(GetInterpreter), nameof(OrderService));
            if (interpreterId == Constants.NewInterpreterId)
            {
                if (!IsUniqueOfficialInterpreterId(interpreterInformation.OfficialInterpreterId, brokerId))
                {
                    throw new ArgumentException("Er förmedling har redan registrerat en tolk med detta tolknummer (Kammarkollegiets) i tjänsten.", nameof(interpreterId));
                }
                var interpreter = new InterpreterBroker(
                    interpreterInformation.FirstName,
                    interpreterInformation.LastName,
                    brokerId,
                    interpreterInformation.Email,
                    interpreterInformation.PhoneNumber,
                    interpreterInformation.OfficialInterpreterId
                    );
                await _dbContext.AddAsync(interpreter);

                return(interpreter);
            }
            return(await _dbContext.InterpreterBrokers.GetInterpreterBrokerById(interpreterId));
        }
Exemple #3
0
        private bool TryRegisterInterpreter(HashSet <string> registeredPaths, HashSet <string> registeredIds, bool ignoreRegisteredPaths, RegistryKey vendorKey, string key, ProcessorArchitecture?arch)
        {
            Version version             = null;
            ProcessorArchitecture?arch2 = null;

            using (var interpKey = vendorKey.OpenSubKey(key)) {
                if (interpKey == null)
                {
                    // the key unexpectedly disappeared
                    return(false);
                }
                string id           = key;
                var    versionValue = interpKey.GetValue("SysVersion") as string;
                if ((versionValue == null || !Version.TryParse(versionValue, out version)) &&
                    !TryParsePythonVersion(key, out version, out arch2, ref id))
                {
                    version = new Version(2, 7);
                }

                var archStr = interpKey.GetValue("Architecture") as string;
                switch (archStr)
                {
                case "x64": arch = ProcessorArchitecture.Amd64; break;

                case "x86": arch = ProcessorArchitecture.X86; break;
                }

                if (version.Major == 2 && version.Minor <= 4)
                {
                    // 2.4 and below not supported.
                    return(false);
                }

                var installPath = vendorKey.OpenSubKey(key + "\\InstallPath");
                if (installPath != null)
                {
                    var basePathObj = installPath.GetValue("");
                    if (basePathObj == null)
                    {
                        // http://pytools.codeplex.com/discussions/301384
                        // messed up install, we don't know where it lives, we can't use it.
                        return(false);
                    }
                    string basePath = basePathObj.ToString();
                    if (!PathUtils.IsValidPath(basePath))
                    {
                        // Invalid path in registry
                        return(false);
                    }
                    if (!registeredPaths.Add(basePath) && !ignoreRegisteredPaths)
                    {
                        // registered in both HCKU and HKLM (we allow duplicate paths in HKCU
                        // which is why we have ignoreRegisteredPaths)
                        return(false);
                    }

                    var actualArch = arch ?? arch2;
                    if (!actualArch.HasValue)
                    {
                        actualArch = NativeMethods.GetBinaryType(Path.Combine(basePath, CPythonInterpreterFactoryConstants.ConsoleExecutable));
                    }

                    string description = interpKey.GetValue("Description") as string;
                    if (description == null)
                    {
                        description = "Python";
                    }

                    string newId = CPythonInterpreterFactoryConstants.GetIntepreterId(GetVendorName(vendorKey), actualArch, id);
                    InterpreterInformation existing;

                    lock (_factories) {
                        _factories.TryGetValue(newId, out existing);
                        try {
                            var    interpPath  = installPath.GetValue("ExecutablePath") as string ?? Path.Combine(basePath, CPythonInterpreterFactoryConstants.ConsoleExecutable);
                            var    windowsPath = installPath.GetValue("WindowedExecutablePath") as string ?? Path.Combine(basePath, CPythonInterpreterFactoryConstants.WindowsExecutable);
                            var    libraryPath = Path.Combine(basePath, CPythonInterpreterFactoryConstants.LibrarySubPath);
                            string prefixPath  = Path.GetDirectoryName(interpPath);

                            registeredIds.Add(newId);
                            var newConfig = new InterpreterConfiguration(
                                newId,
                                description,
                                prefixPath,
                                interpPath,
                                windowsPath,
                                libraryPath,
                                CPythonInterpreterFactoryConstants.PathEnvironmentVariableName,
                                actualArch ?? ProcessorArchitecture.None,
                                version
                                );
                            if (existing == null || !newConfig.Equals(existing.Configuration))
                            {
                                _factories[newId] = new InterpreterInformation(newConfig);
                                return(true);
                            }
                        } catch (ArgumentException) {
                        }
                    }

                    return(false);
                }
            }
            return(false);
        }
Exemple #4
0
        private static void TryRegisterInterpreter(
            Dictionary<string, InterpreterInformation> factories,
            HashSet<string> registeredPaths,
            bool ignoreRegisteredPaths,
            RegistryKey vendorKey,
            string key,
            ProcessorArchitecture? arch
            )
        {
            Version version = null;
            ProcessorArchitecture? arch2 = null;

            var vendorName = GetVendorName(vendorKey);
            var vendorDescription = (vendorKey.GetValue("DisplayName") as string) ?? vendorName;
            var vendorSupport = vendorKey.GetValue("SupportUrl") as string;

            using (var interpKey = vendorKey.OpenSubKey(key)) {
                if (interpKey == null) {
                    // the key unexpectedly disappeared
                    return;
                }
                string id = key;
                var versionValue = interpKey.GetValue("SysVersion") as string;
                if ((versionValue == null || !Version.TryParse(versionValue, out version)) &&
                    !TryParsePythonVersion(key, out version, out arch2, ref id)) {
                    version = new Version(2, 7);
                }

                try {
                    var langVer = version.ToLanguageVersion();
                } catch (InvalidOperationException) {
                    // Version is not currently supported
                    return;
                }

                var archStr = interpKey.GetValue("Architecture") as string;
                switch (archStr) {
                    case "x64": arch = ProcessorArchitecture.Amd64; break;
                    case "x86": arch = ProcessorArchitecture.X86; break;
                }

                if (version.Major == 2 && version.Minor <= 4) {
                    // 2.4 and below not supported.
                    return;
                }

                var installPath = vendorKey.OpenSubKey(key + "\\InstallPath");
                if (installPath != null) {
                    var basePathObj = installPath.GetValue("");
                    if (basePathObj == null) {
                        // http://pytools.codeplex.com/discussions/301384
                        // messed up install, we don't know where it lives, we can't use it.
                        return;
                    }
                    string basePath = basePathObj.ToString();
                    if (!PathUtils.IsValidPath(basePath)) {
                        // Invalid path in registry
                        return;
                    }
                    if (!registeredPaths.Add(basePath) && !ignoreRegisteredPaths) {
                        // registered in both HCKU and HKLM (we allow duplicate paths in HKCU
                        // which is why we have ignoreRegisteredPaths)
                        return;
                    }

                    var actualArch = arch ?? arch2;
                    if (!actualArch.HasValue) {
                        actualArch = NativeMethods.GetBinaryType(Path.Combine(basePath, CPythonInterpreterFactoryConstants.ConsoleExecutable));
                    }

                    string description, supportUrl;
                    if (vendorName == "PythonCore") {
                        vendorDescription = "Python Software Foundation";
                        description = "Python";
                        supportUrl = "https://www.python.org/";
                    } else {
                        description = interpKey.GetValue("DisplayName") as string;
                        if (string.IsNullOrEmpty(description)) {
                            description = "{0} {1}".FormatInvariant(GetVendorName(vendorKey), key);
                        }

                        supportUrl = interpKey.GetValue("SupportUrl") as string ?? vendorSupport;
                    }

                    string newId = CPythonInterpreterFactoryConstants.GetInterpreterId(GetVendorName(vendorKey), actualArch, id);

                    try {
                        var interpPath = installPath.GetValue("ExecutablePath") as string ?? Path.Combine(basePath, CPythonInterpreterFactoryConstants.ConsoleExecutable);
                        var windowsPath = installPath.GetValue("WindowedExecutablePath") as string ?? Path.Combine(basePath, CPythonInterpreterFactoryConstants.WindowsExecutable);
                        var libraryPath = Path.Combine(basePath, CPythonInterpreterFactoryConstants.LibrarySubPath);
                        string prefixPath = Path.GetDirectoryName(interpPath);

                        var newConfig = new InterpreterConfiguration(
                            newId,
                            description,
                            prefixPath,
                            interpPath,
                            windowsPath,
                            libraryPath,
                            CPythonInterpreterFactoryConstants.PathEnvironmentVariableName,
                            actualArch ?? ProcessorArchitecture.None,
                            version
                        );
                        factories[newId] = new InterpreterInformation(newConfig, vendorDescription, supportUrl);
                    } catch (ArgumentException) {
                    }
                }
            }
        }