Skip to content
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

Конина Анастасия #181

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions ErrorHandling/Result.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using FluentAssertions;

namespace ResultOfTask
{
Expand Down Expand Up @@ -58,21 +59,29 @@ public static Result<TOutput> Then<TInput, TOutput>(
this Result<TInput> input,
Func<TInput, TOutput> continuation)
{
throw new NotImplementedException();
return input.Then(n => Of(()=> continuation(n)));
}

public static Result<TOutput> Then<TInput, TOutput>(
this Result<TInput> input,
Func<TInput, Result<TOutput>> continuation)
{
throw new NotImplementedException();
if (input.IsSuccess)
{
return continuation(input.Value);
}

return Fail<TOutput>(input.Error);
}

public static Result<TInput> OnFail<TInput>(
this Result<TInput> input,
Action<string> handleError)
{
throw new NotImplementedException();
if (!input.IsSuccess)
handleError(input.Error);

return input;
}
}
}
10 changes: 5 additions & 5 deletions FileSenderRailway/Dependencies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface ISender
void Send(Document document);
}

public class Document
public class Document //сделать апгрейд до 6 версии(можно будет написать record), в папке solved всегда есть решение.
{
public Document(string name, byte[] content, DateTime created, string format)
{
Expand All @@ -30,10 +30,10 @@ public Document(string name, byte[] content, DateTime created, string format)
Content = content;
}

public string Name { get; set; }
public DateTime Created { get; set; }
public string Format { get; set; }
public byte[] Content { get; set; }
public string Name { get; private set; }
public DateTime Created { get; private set; }
public string Format { get; private set; }
public byte[] Content { get; private set; }
}

public class FileContent
Expand Down
19 changes: 13 additions & 6 deletions FileSenderRailway/FileSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,7 @@ public IEnumerable<FileSendResult> SendFiles(FileContent[] files, X509Certificat
string errorMessage = null;
try
{
Document doc = recognizer.Recognize(file);
if (!IsValidFormatVersion(doc))
throw new FormatException("Invalid format version");
if (!IsValidTimestamp(doc))
throw new FormatException("Too old document");
doc.Content = cryptographer.Sign(doc.Content, certificate);
var doc = PrepareFileToSend(certificate, file);
sender.Send(doc);
}
catch (FormatException e)
Expand All @@ -50,6 +45,18 @@ public IEnumerable<FileSendResult> SendFiles(FileContent[] files, X509Certificat
}
}

private Document PrepareFileToSend(X509Certificate certificate, FileContent file)
{
Document doc = recognizer.Recognize(file);
if (!IsValidFormatVersion(doc))
throw new FormatException("Invalid format version");
if (!IsValidTimestamp(doc))
throw new FormatException("Too old document");
doc = new Document(file.Name, doc.Content, DateTime.Now, certificate.ToString());
doc.Content = cryptographer.Sign(doc.Content, certificate);
return doc;
}

private bool IsValidFormatVersion(Document doc)
{
return doc.Format == "4.0" || doc.Format == "3.1";
Expand Down
28 changes: 28 additions & 0 deletions TagCloud/CloudDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Drawing;
namespace TagCloud;

public class CloudDrawer : ICloudDrawer
{
private readonly Size pictureSize;

public CloudDrawer(Size pictureSize)
{
this.pictureSize = pictureSize;
}

public Bitmap DrawCloud(IEnumerable<WordForCloud> wordsForCloud)
{
var bitmap = new Bitmap(pictureSize.Width, pictureSize.Height);
var gr = Graphics.FromImage(bitmap);
gr.FillRectangle(Brushes.White, 0, 0, pictureSize.Width, pictureSize.Height);
foreach (var wordForCloud in wordsForCloud)
{
gr.DrawString($"{wordForCloud.Word}", wordForCloud.Font,
new SolidBrush(wordForCloud.WordColor), wordForCloud.WordSize);
}

gr.Dispose();

return bitmap;
}
}
22 changes: 22 additions & 0 deletions TagCloud/ColorGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Drawing;

namespace TagCloud;

public class ColorGenerator : IColorGenerator
{
private readonly Color[] colors;
private int index;

public ColorGenerator(Color[] colors)
{
this.colors = colors;
}

public Color GetNextColor()
{
index++;
if (index >= colors.Length)
index = 0;
return colors[index];
}
}
133 changes: 133 additions & 0 deletions TagCloud/ConsoleInterface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System.Drawing;
using System.Drawing.Imaging;
using McMaster.Extensions.CommandLineUtils;

namespace TagCloud;

public class ConsoleInterface : IProgramInterface
{
private ITagCloudCreatorFactory tagCloudCreatorFactory;

public ConsoleInterface(ITagCloudCreatorFactory tagCloudCreatorFactory)
{
this.tagCloudCreatorFactory = tagCloudCreatorFactory;
}

public void Run(string[] args)
{
var tagCloudSettings = ParseArguments(args);
if (!tagCloudSettings.IsSuccess)
{
Console.WriteLine(tagCloudSettings.Error);
return;
}

if (tagCloudSettings.Value == null)
return;
Comment on lines +25 to +26

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Предполагается, что IsSuccess должно говорить о том, что настройки не null.


var result = CreateTagCloud(tagCloudCreatorFactory, tagCloudSettings.Value);
if (!result.IsSuccess)
Console.WriteLine(result.Error);
}

private static Result<TagCloudSettings> ParseArguments(string[] args)
{
var app = new CommandLineApplication();
app.HelpOption();

var pictureSizeArg = app.Option<int>("-s|--pictureSize <int,int>", "Picture size",
CommandOptionType.MultipleValue);
var cloudCenterArg = app.Option<int>("-c|--cloudCenter <int,int>", "Cloud center",
CommandOptionType.MultipleValue);
var colorsArgs =
app.Option<string>("-o|--colors <name,name,...>", "Colors", CommandOptionType.MultipleValue);
var fontNameArg = app.Option<string>("-f|--font <name>", "font", CommandOptionType.SingleValue);
var maxFontSizeArg =
app.Option<int>("-m|--maxFontSize <int>", "Max font size", CommandOptionType.SingleValue);
var inputFileArg =
app.Option<string>("-n|--inputFile <path>", "Input file .txt", CommandOptionType.SingleValue);
var outputFileArg =
app.Option<string>("-u|--outputFile <path>", "output file", CommandOptionType.SingleValue);
var boringWordsFileArg = app.Option<string>("-b|--boringWords <path>", "boring words file",
CommandOptionType.SingleValue);

TagCloudSettings tagCloudSettings = null;

app.OnExecute(() =>
{
var pictureSize = pictureSizeArg.HasValue() && pictureSizeArg.Values.Count == 2
? new Size(pictureSizeArg.ParsedValues[0], pictureSizeArg.ParsedValues[1])
: new Size(2000, 2000);

var cloudCenter = cloudCenterArg.HasValue() && cloudCenterArg.Values.Count == 2
? new Point(cloudCenterArg.ParsedValues[0], cloudCenterArg.ParsedValues[1])
: new Point(1000, 1000);

var colors = colorsArgs.HasValue()
? cloudCenterArg.Values.Select(Color.FromName).ToArray()
: new[] {Color.Black};

var fontName = fontNameArg.HasValue()
? fontNameArg.ParsedValue
: "Arial";

var maxFontSize = maxFontSizeArg.HasValue()
? maxFontSizeArg.ParsedValue
: 40;

var inputFile = inputFileArg.HasValue()
? inputFileArg.ParsedValue
: "in.txt";

var outputFile = outputFileArg.HasValue()
? outputFileArg.ParsedValue
: "out.png";

var boringWordsFile = boringWordsFileArg.HasValue()
? boringWordsFileArg.ParsedValue
: "boring.txt";

tagCloudSettings = new TagCloudSettings(pictureSize,
cloudCenter,
colors,
fontName,
maxFontSize,
inputFile,
boringWordsFile,
outputFile);
});

return Result.OfAction(() => app.Execute(args)).Then(q => tagCloudSettings);
}

private static Result<None> CreateTagCloud(ITagCloudCreatorFactory tagCloudCreatorFactory, TagCloudSettings tagCloudSettings)
{
var bitmap = GetCloudImage(tagCloudCreatorFactory,
tagCloudSettings.PictureSize,
tagCloudSettings.CloudCenter,
tagCloudSettings.Colors,
tagCloudSettings.FontName,
tagCloudSettings.MaxFontSize,
tagCloudSettings.InputFile,
tagCloudSettings.BoringWordsFile);

return bitmap.Then(b => b.Save(tagCloudSettings.OutputFile, ImageFormat.Png))
.ReplaceErrorIfEmpty($"Can't save file {tagCloudSettings.OutputFile}");
}

private static Result<Bitmap> GetCloudImage(ITagCloudCreatorFactory tagCloudCreatorFactory, Size pictureSize,
Point cloudCenter, Color[] colors, string fontName,
int maxFontSize, string inputFile, string boringWordsFile)
{
var tagCloudCreator = tagCloudCreatorFactory
.Get(pictureSize,
cloudCenter,
colors,
fontName,
maxFontSize,
inputFile,
boringWordsFile);
var bitmap = tagCloudCreator.GetCloud();
return bitmap;
}
}
10 changes: 10 additions & 0 deletions TagCloud/Factory/CloudDrawerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Drawing;
namespace TagCloud.Factory;

public class CloudDrawerFactory : ICloudDrawerFactory
{
public ICloudDrawer Get(Size pictureSize)
{
return new CloudDrawer(pictureSize);
}
}
10 changes: 10 additions & 0 deletions TagCloud/Factory/ColorGeneratorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Drawing;
namespace TagCloud.Factory;

public class ColorGeneratorFactory: IColorGeneratorFactory
{
public IColorGenerator Get(Color[] colors)
{
return new ColorGenerator(colors);
}
}
9 changes: 9 additions & 0 deletions TagCloud/Factory/SpiralPointsFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;
using TagsCloudVisualization;
namespace TagCloud.Factory;

public class SpiralPointsFactory: IPointsFactory
{
public IPoints Get(Point cloudCenter) =>
new SpiralPoints(cloudCenter);
}
42 changes: 42 additions & 0 deletions TagCloud/Factory/TagCloudCreatorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System.Drawing;
namespace TagCloud.Factory;

public class TagCloudCreatorFactory: ITagCloudCreatorFactory
{
private IWordsForCloudGeneratorFactory wordsForCloudGeneratorFactory;
private ITagCloudLayouterFactory tagCloudLayouteFactory;
private IColorGeneratorFactory colorGeneratorFactory;
private ICloudDrawerFactory cloudDrawerFactory;
private IPointsFactory pointsFactory;
private IWordsReader wordsReader;
private IWordsNormalizer wordsNormalizer;

public TagCloudCreatorFactory(IWordsForCloudGeneratorFactory wordsForCloudGeneratorFactory,
IColorGeneratorFactory colorGeneratorFactory,
ICloudDrawerFactory cloudDrawerFactory,
ITagCloudLayouterFactory tagCloudLayouteFactory,
IPointsFactory pointsFactory,
IWordsReader wordsReader,
IWordsNormalizer wordsNormalizer)
{
this.tagCloudLayouteFactory = tagCloudLayouteFactory;
this.wordsForCloudGeneratorFactory = wordsForCloudGeneratorFactory;
this.colorGeneratorFactory = colorGeneratorFactory;
this.cloudDrawerFactory = cloudDrawerFactory;
this.pointsFactory = pointsFactory;
this.wordsNormalizer = wordsNormalizer;
this.wordsReader = wordsReader;
}


public ITagCloudCreator Get(Size pictureSize, Point cloudCenter, Color[] colors, string fontName,
int maxFontSize,
string inputFile, string boringWordsFile)
{
return new TagCloudCreator(
wordsForCloudGeneratorFactory.Get(fontName, maxFontSize,
tagCloudLayouteFactory.Get(pointsFactory.Get(cloudCenter)),
colorGeneratorFactory.Get(colors)), wordsReader, wordsNormalizer,
cloudDrawerFactory.Get(pictureSize), inputFile, boringWordsFile);
}
}
10 changes: 10 additions & 0 deletions TagCloud/Factory/TagCloudLayouterFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using TagsCloudVisualization;
namespace TagCloud.Factory;

public class TagCloudLayouterFactory : ITagCloudLayouterFactory
{
public ITagCloudLayouter Get(IPoints points)
{
return new CircularCloudLayouter(points);
}
}
11 changes: 11 additions & 0 deletions TagCloud/Factory/WordsForCloudGeneratorFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using TagsCloudVisualization;
namespace TagCloud.Factory;

public class WordsForCloudGeneratorFactory : IWordsForCloudGeneratorFactory
{
public IWordsForCloudGenerator Get(string fontName, int maxFontSize, ITagCloudLayouter tagCloudLayouter,
IColorGenerator colorGenerator)
{
return new WordsForCloudGenerator(fontName, maxFontSize, tagCloudLayouter, colorGenerator);
}
}
8 changes: 8 additions & 0 deletions TagCloud/Interfaces/ICloudDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Collections.Generic;
using System.Drawing;
namespace TagCloud;

public interface ICloudDrawer
{
Bitmap DrawCloud(IEnumerable<WordForCloud> wordsForCloud);
}
Loading