-
Notifications
You must be signed in to change notification settings - Fork 303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Муканов Арман #203
Open
MNYOU
wants to merge
9
commits into
kontur-courses:master
Choose a base branch
from
MNYOU:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Муканов Арман #203
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
79fdcc4
feat: создал генератор облака тегов
MNYOU fe33b62
test: добавил тесты.
MNYOU 4cfd0e7
feat: добавил консольный клиент
MNYOU 938d4dd
docs: добавил примеры генерации
MNYOU 21df153
fix: исправил навзвание директории
MNYOU 9add474
refactor: рефакторинг
MNYOU 9aa937e
refactor: Декомпозировал TextPreprocessor.cs
MNYOU 731ed78
refactor: переделал регистрацию на assembly
MNYOU 4efacea
fix: Переделал object на IOptions
MNYOU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using CommandLine; | ||
using ConsoleApp.Handlers; | ||
using ConsoleApp.Options; | ||
|
||
namespace ConsoleApp; | ||
|
||
public class CommandLineParser: ICommandLineParser | ||
{ | ||
private readonly IOptionsHandler[] handlers; | ||
private readonly IOptions[] options; | ||
|
||
public CommandLineParser(IOptionsHandler[] handlers, IOptions[] options) | ||
{ | ||
this.handlers = handlers; | ||
this.options = options; | ||
} | ||
|
||
public void ParseFromConsole() | ||
{ | ||
var types = options | ||
.Select(opt => opt.GetType()) | ||
.ToArray(); | ||
|
||
Console.WriteLine("Доступные команды \"--help\""); | ||
while (true) | ||
{ | ||
var input = Console.ReadLine(); | ||
var args = input.Split(); | ||
Parser.Default.ParseArguments(args, types) | ||
.WithParsed<IOptions>(Parse); | ||
} | ||
} | ||
|
||
private void Parse<T>(T options) where T : IOptions | ||
{ | ||
var handler = handlers.FirstOrDefault(h => h.CanParse(options)); | ||
if (handler is null) | ||
throw new Exception("Обработчик параметров не найден."); | ||
|
||
var message = handler.WithParsed(options); | ||
Console.WriteLine(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\TagsCloudContainer\TagsCloudContainer.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Autofac" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using ConsoleApp.Options; | ||
|
||
namespace ConsoleApp.Handlers; | ||
|
||
public class ExitOptionsHandler : IOptionsHandler | ||
{ | ||
public bool CanParse(IOptions options) | ||
{ | ||
return options is ExitOptions; | ||
} | ||
|
||
public string WithParsed(IOptions options) | ||
{ | ||
Environment.Exit(0); | ||
return "Завершение выполнения программы."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using ConsoleApp.Options; | ||
using MyStemWrapper; | ||
using TagsCloudContainer; | ||
using TagsCloudContainer.Settings; | ||
|
||
namespace ConsoleApp.Handlers; | ||
|
||
public class GenerateCloudOptionsHandler : IOptionsHandler | ||
{ | ||
private readonly MyStem myStem; | ||
private readonly IAppSettings appSettings; | ||
private readonly IAnalyseSettings analyseSettings; | ||
private readonly ITagsCloudContainer cloudContainer; | ||
|
||
public GenerateCloudOptionsHandler(IAppSettings appSettings, MyStem myStem, IAnalyseSettings analyseSettings, | ||
ITagsCloudContainer cloudContainer) | ||
{ | ||
this.appSettings = appSettings; | ||
this.myStem = myStem; | ||
this.analyseSettings = analyseSettings; | ||
this.cloudContainer = cloudContainer; | ||
} | ||
|
||
public bool CanParse(IOptions options) | ||
{ | ||
return options is GenerateCloudOptions; | ||
} | ||
|
||
public string WithParsed(IOptions options) | ||
{ | ||
Map(options); | ||
return Execute(); | ||
} | ||
|
||
private void Map(IOptions options) | ||
{ | ||
if (options is GenerateCloudOptions opts) | ||
Map(opts); | ||
else | ||
throw new ArgumentException(nameof(options)); | ||
} | ||
|
||
private void Map(GenerateCloudOptions options) | ||
{ | ||
appSettings.InputFile = options.InputFile; | ||
appSettings.OutputFile = options.OutputFile; | ||
|
||
if (!string.IsNullOrWhiteSpace(options.AnalyseParameters)) | ||
myStem.Parameters = "-" + options.AnalyseParameters; | ||
if (options.ValidSpeechParts.Any()) | ||
analyseSettings.ValidSpeechParts = options.ValidSpeechParts.ToArray(); | ||
} | ||
|
||
private string Execute() | ||
{ | ||
cloudContainer.GenerateImageToFile(appSettings.InputFile, appSettings.OutputFile); | ||
return $"Успешно сохранено в файл - \"{appSettings.OutputFile}\"."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using ConsoleApp.Options; | ||
|
||
namespace ConsoleApp.Handlers; | ||
|
||
public interface IOptionsHandler | ||
{ | ||
public bool CanParse(IOptions options); | ||
|
||
public string WithParsed(IOptions options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
using ConsoleApp.Options; | ||
using SixLabors.ImageSharp; | ||
using TagsCloudContainer.Settings; | ||
|
||
namespace ConsoleApp.Handlers; | ||
|
||
public class SetImageOptionsHandler : IOptionsHandler | ||
{ | ||
private readonly IImageSettings imageSettings; | ||
|
||
public SetImageOptionsHandler(IImageSettings imageSettings) | ||
{ | ||
this.imageSettings = imageSettings; | ||
} | ||
|
||
private void Map(SetImageOptions options) | ||
{ | ||
if (options.PrimaryColor != default) | ||
imageSettings.PrimaryColor = options.PrimaryColor; | ||
if (options.BackgroundColor != default) | ||
imageSettings.BackgroundColor = options.BackgroundColor; | ||
if (options.Width != default) | ||
imageSettings.ImageSize = new Size(options.Width, imageSettings.ImageSize.Height); | ||
if (options.Height != default) | ||
imageSettings.ImageSize = new Size(imageSettings.ImageSize.Width, options.Height); | ||
if (options.Font is not null) | ||
imageSettings.TextOptions.Font = options.Font; | ||
} | ||
|
||
private void Map(IOptions options) | ||
{ | ||
if (options is SetImageOptions opts) | ||
Map(opts); | ||
else | ||
throw new ArgumentException(nameof(options)); | ||
} | ||
|
||
public bool CanParse(IOptions options) | ||
{ | ||
return options is SetImageOptions; | ||
} | ||
|
||
public string WithParsed(IOptions options) | ||
{ | ||
Map(options); | ||
return "Настройки изображения установлены."; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace ConsoleApp; | ||
|
||
public interface ICommandLineParser | ||
{ | ||
public void ParseFromConsole(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using CommandLine; | ||
|
||
namespace ConsoleApp.Options; | ||
|
||
[Verb("exit", HelpText = "Закончить выполнение программы")] | ||
public class ExitOptions: IOptions | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using CommandLine; | ||
|
||
namespace ConsoleApp.Options; | ||
|
||
[Verb("generate", HelpText = "Предобработка слов")] | ||
public class GenerateCloudOptions: IOptions | ||
{ | ||
[Option('i', "input", Required = true, HelpText = "Путь к файлу текста для анализа.")] | ||
public string InputFile { get; set; } | ||
|
||
[Option('o', "output", Required = true, HelpText = "Путь к сохранению изображения.")] | ||
public string OutputFile { get; set; } | ||
|
||
[Option('p', "params", HelpText = "Параметры вывода MyStem")] | ||
public string AnalyseParameters { get; set; } | ||
|
||
[Value(1, Max = 14, HelpText = "Части речи, которые буду задействованы при анализе.")] | ||
public IEnumerable<string> ValidSpeechParts { get; set; } = new string[0]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace ConsoleApp.Options; | ||
|
||
public interface IOptions | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using CommandLine; | ||
using SixLabors.Fonts; | ||
using SixLabors.ImageSharp; | ||
|
||
namespace ConsoleApp.Options; | ||
|
||
[Verb("image", HelpText = "Настройка изображения")] | ||
public class SetImageOptions: IOptions | ||
{ | ||
[Option('c', "color", HelpText = "Основной цвет")] | ||
public Color PrimaryColor { get; set; } | ||
|
||
[Option('b', "background", HelpText = "Цвет заднего фона")] | ||
public Color BackgroundColor { get; set; } | ||
|
||
[Option('w', "width", HelpText = "Ширина")] | ||
public int Width { get; set; } | ||
|
||
[Option('h', "height", HelpText = "Высота")] | ||
public int Height { get; set; } | ||
|
||
[Option('f', "font",HelpText = "Шрифт")] | ||
public Font Font { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System.Reflection; | ||
using Autofac; | ||
using MyStemWrapper; | ||
using TagsCloudContainer; | ||
using TagsCloudContainer.Settings; | ||
|
||
namespace ConsoleApp; | ||
|
||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
var builder = new ContainerBuilder(); | ||
ConfigureService(builder); | ||
var container = builder.Build(); | ||
|
||
using var scope = container.BeginLifetimeScope(); | ||
var commandLineReader = scope.Resolve<ICommandLineParser>(); | ||
commandLineReader.ParseFromConsole(); | ||
} | ||
|
||
public static void ConfigureService(ContainerBuilder builder) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
RegisterAssemblyTypes(builder, typeof(Tag).GetTypeInfo().Assembly); | ||
RegisterAssemblyTypes(builder, typeof(CommandLineParser).GetTypeInfo().Assembly); | ||
|
||
var location = Assembly.GetExecutingAssembly().Location; | ||
var path = Path.GetDirectoryName(location); | ||
var myStem = new MyStem | ||
{ | ||
PathToMyStem = $"{path}\\mystem.exe", | ||
Parameters = "-nli", | ||
}; | ||
builder.RegisterInstance(myStem).AsSelf().SingleInstance(); | ||
|
||
builder.RegisterType<AppSettings>().As<IAppSettings>().SingleInstance(); | ||
builder.RegisterType<AnalyseSettings>().As<IAnalyseSettings>().SingleInstance(); | ||
builder.RegisterType<ImageSettings>().As<IImageSettings>().SingleInstance(); | ||
} | ||
|
||
private static void RegisterAssemblyTypes(ContainerBuilder builder, Assembly assembly) | ||
{ | ||
builder.RegisterAssemblyTypes(assembly) | ||
.AsImplementedInterfaces(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
хорошо что выделил в отдельную сборку CUI