Skip to content

Commit

Permalink
* fix all violations of Roslyn analyzer rule and ReSharper inspection…
Browse files Browse the repository at this point in the history
… @ c#/crawler
  • Loading branch information
n0099 committed Dec 8, 2024
1 parent c7bbc83 commit e285646
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 30 deletions.
1 change: 1 addition & 0 deletions c#/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
[assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1122:Use string.Empty for empty strings")]
[assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1127:Generic type constraints should be on their own line")]
[assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1128:Put constructor initializers on their own line")]
[assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1133:Each attribute should be placed on its own line of code")]
[assembly: SuppressMessage("StyleCop.CSharp.ReadabilityRules", "SA1134:Attributes should not share line")]
[assembly: SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1008:Opening parenthesis should be spaced correctly")]
[assembly: SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1009:Closing parenthesis should be spaced correctly")]
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/SonicPusher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public SonicPusher(ILogger<SonicPusher> logger, IConfiguration config)
_config.GetValue("Hostname", "localhost"),
_config.GetValue("Port", 1491),
_config.GetValue("Secret", "SecretPassword"));
CollectionPrefix = _config.GetValue<string>("CollectionPrefix") ?? "tbm_";
CollectionPrefix = _config.GetValue<string>("CollectionPrefix", "tbm_");
}

public ISonicIngestConnection Ingest { get; }
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/ClientRequester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private async Task<HttpResponseMessage> PostJson(
return acc;
}) + "tiebaclient!!!";
#pragma warning disable CA5351 // Do Not Use Broken Cryptographic Algorithms
var signatureMd5 = BitConverter.ToString(MD5.HashData(Encoding.UTF8.GetBytes(signature))).Replace("-", "");
var signatureMd5 = Convert.ToHexString(MD5.HashData(Encoding.UTF8.GetBytes(signature)));
#pragma warning restore CA5351 // Do Not Use Broken Cryptographic Algorithms
postParamPairs.Add(KeyValuePair.Create("sign", signatureMd5));

Expand Down
5 changes: 2 additions & 3 deletions c#/crawler/src/Tieba/ClientRequesterTcs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ public sealed class ClientRequesterTcs : WithLogTrace
private readonly ConcurrentQueue<TaskCompletionSource> _queue = new();
private readonly Timer _timer = new() {Enabled = true};
private readonly Stopwatch _stopwatch = new();
private double _maxRps;
private uint _requestCounter;

public ClientRequesterTcs(ILogger<ClientRequesterTcs> logger, IConfiguration config)
Expand All @@ -28,10 +27,10 @@ public ClientRequesterTcs(ILogger<ClientRequesterTcs> logger, IConfiguration con
private float AverageRps => _requestCounter / (float)_stopwatch.Elapsed.TotalSeconds;
private double MaxRps
{
get => _maxRps;
get;
set
{
_maxRps = value;
field = value;
if ((uint)_timer.Interval != (uint)(1000 / value))
{ // only update interval with a truncated integer to prevent frequently change it
// which will cause the increment of real rps can't keep up with _maxRps with long queue length
Expand Down
5 changes: 3 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Crawler/CrawlerLocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,11 @@ public IReadOnlyDictionary<LockId, IReadOnlyDictionary<Page, FailureCount>> Retr
{
lock (_failed)
{
var failedClone = _failed.ToDictionary(pair => pair.Key, pair =>
var failedClone = _failed.ToDictionary(pair => pair.Key,
IReadOnlyDictionary<Page, FailureCount> (pair) =>
{
lock (pair.Value)
return (IReadOnlyDictionary<Page, FailureCount>)new Dictionary<Page, FailureCount>(pair.Value);
return new Dictionary<Page, FailureCount>(pair.Value);
});
_failed.Clear();
return failedClone;
Expand Down
5 changes: 3 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Facade/CrawlFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public abstract class CrawlFacade<TPostEntity, TParsedPost, TResponse, TPostProt
{
private readonly HashSet<Page> _lockingPages = [];
private readonly ConcurrentDictionary<Uid, User.Parsed> _users = new();
private UserParser? _userParser;
private ICrawlFacade<TPostEntity, TParsedPost>.ExceptionHandler _exceptionHandler = _ => { };

// ReSharper disable UnusedAutoPropertyAccessor.Global
Expand All @@ -31,7 +30,9 @@ public required ILogger<CrawlFacade<TPostEntity, TParsedPost, TResponse, TPostPr
// ReSharper restore UnusedAutoPropertyAccessor.Global
protected Fid Fid { get; } = fid;
protected ConcurrentDictionary<PostId, TParsedPost> Posts { get; } = new();
protected UserParser UserParser => _userParser ??= userParserFactory(_users);

[field: AllowNull, MaybeNull]
protected UserParser UserParser => field ??= userParserFactory(_users);

public virtual void Dispose()
{
Expand Down
5 changes: 3 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/IRevisionProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public interface IRevisionProperties
[typeof(ThreadRevision), typeof(ReplyRevision), typeof(SubReplyRevision), typeof(UserRevision)]);

private static IReadOnlyDictionary<Type, IReadOnlyDictionary<string, PropertyInfo>> GetPropsKeyByType(IEnumerable<Type> types) =>
types.ToDictionary(type => type, type =>
(IReadOnlyDictionary<string, PropertyInfo>)type.GetProperties().ToDictionary(prop => prop.Name));
types.ToDictionary(type => type,
IReadOnlyDictionary<string, PropertyInfo> (type) =>
type.GetProperties().ToDictionary(prop => prop.Name));
}
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/ReplySaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public partial class ReplySaver(
}
public partial class ReplySaver
{
private Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>? _addSplitRevisionsDelegatesKeyByEntityType;
[field: AllowNull, MaybeNull]
protected override Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>
AddSplitRevisionsDelegatesKeyByEntityType =>
_addSplitRevisionsDelegatesKeyByEntityType ??= new(() => new()
field ??= new(() => new()
{
{typeof(ReplyRevision.SplitFloor), AddRevisionsWithDuplicateIndex<ReplyRevision.SplitFloor>},
{typeof(ReplyRevision.SplitSubReplyCount), AddRevisionsWithDuplicateIndex<ReplyRevision.SplitSubReplyCount>},
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/SubReplySaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public partial class SubReplySaver(
}
public partial class SubReplySaver
{
private Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>? _addSplitRevisionsDelegatesKeyByEntityType;
[field: AllowNull, MaybeNull]
protected override Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>
AddSplitRevisionsDelegatesKeyByEntityType =>
_addSplitRevisionsDelegatesKeyByEntityType ??= new(() => new()
field ??= new(() => new()
{
{typeof(SubReplyRevision.SplitAgreeCount), AddRevisionsWithDuplicateIndex<SubReplyRevision.SplitAgreeCount>},
{typeof(SubReplyRevision.SplitDisagreeCount), AddRevisionsWithDuplicateIndex<SubReplyRevision.SplitDisagreeCount>}
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public partial class ThreadSaver(
}
public partial class ThreadSaver
{
private Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>? _addSplitRevisionsDelegatesKeyByEntityType;
[field: AllowNull, MaybeNull]
protected override Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>
AddSplitRevisionsDelegatesKeyByEntityType =>
_addSplitRevisionsDelegatesKeyByEntityType ??= new(() => new()
field ??= new(() => new()
{
{typeof(ThreadRevision.SplitViewCount), AddRevisionsWithDuplicateIndex<ThreadRevision.SplitViewCount>}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private Action Save<TEntity, TRevision, TValue>(
// locking key only using AuthorRevision.Fid and Uid, ignoring TriggeredBy
// this prevents inserting multiple entities with similar time and other fields with the same values
// ReSharper disable NotAccessedPositionalProperty.Global
public record UniqueAuthorRevision(Fid Fid, Uid Uid);
private sealed record UniqueAuthorRevision(Fid Fid, Uid Uid);

// ReSharper restore NotAccessedPositionalProperty.Global
private sealed class LatestAuthorRevisionProjection<TValue>
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/UserSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public void SaveParentThreadLatestReplierUid(CrawlerDbContext db, Tid tid) =>
}
public partial class UserSaver
{
private Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>? _addSplitRevisionsDelegatesKeyByEntityType;
[field: AllowNull, MaybeNull]
protected override Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>
AddSplitRevisionsDelegatesKeyByEntityType =>
_addSplitRevisionsDelegatesKeyByEntityType ??= new(() => new()
field ??= new(() => new()
{
{typeof(UserRevision.SplitDisplayName), AddRevisionsWithDuplicateIndex<UserRevision.SplitDisplayName>},
{typeof(UserRevision.SplitPortraitUpdatedAt), AddRevisionsWithDuplicateIndex<UserRevision.SplitPortraitUpdatedAt>},
Expand Down
2 changes: 1 addition & 1 deletion c#/imagePipeline/src/Consumer/MetadataConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private static class ExifGpsTagValuesParser
: null;
}

private static double ConvertDmsToDd(IReadOnlyList<double> dms)
private static double ConvertDmsToDd(List<double> dms)
{ // https://stackoverflow.com/questions/3249700/convert-degrees-minutes-seconds-to-decimal-coordinates
if (dms.Count != 3) throw new ArgumentException(
"Unexpected length for DMS, expecting three doubles.", nameof(dms));
Expand Down
2 changes: 1 addition & 1 deletion c#/imagePipeline/src/Consumer/QrCodeConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public QrCodeConsumer(IConfiguration config, FailedImageHandler failedImageHandl
{
_failedImageHandler = failedImageHandler;
var modelsPath = config.GetSection("QrCodeConsumer")
.GetValue("ModelPath", "./OpenCvWechatModels") ?? "./OpenCvWechatModels";
.GetValue("ModelPath", "./OpenCvWechatModels");
_qrCode = WeChatQRCode.Create(
$"{modelsPath}/detect.prototxt",
$"{modelsPath}/detect.caffemodel",
Expand Down
2 changes: 1 addition & 1 deletion c#/imagePipeline/src/ImageBatchConsumingWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override async Task DoWork(CancellationToken stoppingToken)
}

private async Task Consume(
IReadOnlyCollection<ImageWithBytes> imagesWithBytes,
List<ImageWithBytes> imagesWithBytes,
CancellationToken stoppingToken = default)
{
await using var dbFactory = dbContextDefaultFactory();
Expand Down
3 changes: 1 addition & 2 deletions c#/imagePipeline/src/Ocr/PaddleOcrProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ public PaddleOcrProvider(IConfiguration config, string script,
{
_config = config.GetSection("OcrConsumer:PaddleOcr");
_script = script;
Settings.GlobalModelDirectory =
_config.GetValue("ModelPath", "./PaddleOcrModels") ?? "./PaddleOcrModels";
Settings.GlobalModelDirectory = _config.GetValue("ModelPath", "./PaddleOcrModels");

Check failure on line 21 in c#/imagePipeline/src/Ocr/PaddleOcrProvider.cs

View workflow job for this annotation

GitHub Actions / runs-on (ubuntu-latest) / build (imagePipeline)

Possible null reference assignment.

Check failure on line 21 in c#/imagePipeline/src/Ocr/PaddleOcrProvider.cs

View workflow job for this annotation

GitHub Actions / runs-on (ubuntu-latest) / build (imagePipeline)

Possible null reference assignment.

Check failure on line 21 in c#/imagePipeline/src/Ocr/PaddleOcrProvider.cs

View workflow job for this annotation

GitHub Actions / runs-on (macos-latest) / build (imagePipeline)

Possible null reference assignment.

Check failure on line 21 in c#/imagePipeline/src/Ocr/PaddleOcrProvider.cs

View workflow job for this annotation

GitHub Actions / runs-on (macos-latest) / build (imagePipeline)

Possible null reference assignment.

Check failure on line 21 in c#/imagePipeline/src/Ocr/PaddleOcrProvider.cs

View workflow job for this annotation

GitHub Actions / runs-on (windows-latest) / build (imagePipeline)

Possible null reference assignment.

Check failure on line 21 in c#/imagePipeline/src/Ocr/PaddleOcrProvider.cs

View workflow job for this annotation

GitHub Actions / runs-on (windows-latest) / build (imagePipeline)

Possible null reference assignment.
_paddleOcrDetectorFactory = paddleOcrDetectorFactory;
_paddleOcrRecognizerFactory = paddleOcrRecognizerFactory;
}
Expand Down
10 changes: 5 additions & 5 deletions c#/imagePipeline/src/Ocr/TesseractRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ namespace tbm.ImagePipeline.Ocr;
public sealed partial class TesseractRecognizer(IConfiguration config, string script) : IDisposable
{
private readonly IConfigurationSection _config = config.GetSection("OcrConsumer:Tesseract");
private Lazy<OCRTesseract>? _tesseractInstanceHorizontal;
private Lazy<OCRTesseract>? _tesseractInstanceVertical;

public delegate TesseractRecognizer New(string script);

[SuppressMessage("ReSharper", "StringLiteralTypo")]
private Lazy<OCRTesseract> TesseractInstanceHorizontal => _tesseractInstanceHorizontal ??= new(script switch
[field: AllowNull, MaybeNull]
private Lazy<OCRTesseract> TesseractInstanceHorizontal => field ??= new(script switch
{ // https://en.wikipedia.org/wiki/Template:ISO_15924_script_codes_and_related_Unicode_data
"Hans" => CreateTesseract("best/chi_sim+best/eng"),
"Hant" => CreateTesseract("best/chi_tra+best/eng"),
Expand All @@ -21,7 +20,8 @@ public sealed partial class TesseractRecognizer(IConfiguration config, string sc
});

[SuppressMessage("ReSharper", "StringLiteralTypo")]
private Lazy<OCRTesseract> TesseractInstanceVertical => _tesseractInstanceVertical ??= new(script switch
[field: AllowNull, MaybeNull]
private Lazy<OCRTesseract> TesseractInstanceVertical => field ??= new(script switch
{
"Hans" => CreateTesseract("best/chi_sim_vert", isVertical: true),
"Hant" => CreateTesseract("best/chi_tra_vert", isVertical: true),
Expand All @@ -43,7 +43,7 @@ public void Dispose()
// https://github.com/shimat/opencvsharp/issues/873#issuecomment-1458868153
// https://pyimagesearch.com/2021/11/15/tesseract-page-segmentation-modes-psms-explained-how-to-improve-your-ocr-accuracy/
private OCRTesseract CreateTesseract(string scripts, bool isVertical = false) =>
OCRTesseract.Create(_config.GetValue("DataPath", "") ?? "",
OCRTesseract.Create(_config.GetValue("DataPath", ""),
scripts, charWhitelist: "", oem: 1, psmode: isVertical ? 5 : 7);
}
public sealed partial class TesseractRecognizer
Expand Down

0 comments on commit e285646

Please sign in to comment.