Skip to content

Commit

Permalink
use file-scoped namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
rkm committed Jan 2, 2025
1 parent 81e8f13 commit 52a195f
Show file tree
Hide file tree
Showing 337 changed files with 23,128 additions and 23,465 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ dotnet_diagnostic.CA1861.severity = none

# SYSLIB1045: Use GeneratedRegexAttribute to generate the regular expression implementation at compile time.
dotnet_diagnostic.SYSLIB1045.severity = none

csharp_style_namespace_declarations = file_scoped:error
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,38 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace SmiServices.Applications.DicomDirectoryProcessor
namespace SmiServices.Applications.DicomDirectoryProcessor;

/// <summary>
/// Command line program to process a directory and write an Accession
/// Directory message to the message exchange for each directory found
/// that contains DICOM (*.dcm) files.
/// </summary>
public static class DicomDirectoryProcessor
{
/// <summary>
/// Command line program to process a directory and write an Accession
/// Directory message to the message exchange for each directory found
/// that contains DICOM (*.dcm) files.
/// Main program.
/// </summary>
public static class DicomDirectoryProcessor
/// <param name="args">
/// Arguments. There should be exactly one argument that specified the
/// path to the top level directory that is be searched.
/// </param>
[ExcludeFromCodeCoverage]
public static int Main(IEnumerable<string> args)
{
/// <summary>
/// Main program.
/// </summary>
/// <param name="args">
/// Arguments. There should be exactly one argument that specified the
/// path to the top level directory that is be searched.
/// </param>
[ExcludeFromCodeCoverage]
public static int Main(IEnumerable<string> args)
{
int ret = SmiCliInit
.ParseAndRun<DicomDirectoryProcessorCliOptions>(
args,
nameof(DicomDirectoryProcessor),
OnParse
);
return ret;
}
int ret = SmiCliInit
.ParseAndRun<DicomDirectoryProcessorCliOptions>(
args,
nameof(DicomDirectoryProcessor),
OnParse
);
return ret;
}

private static int OnParse(GlobalOptions globals, DicomDirectoryProcessorCliOptions parsedOptions)
{
var bootstrapper = new MicroserviceHostBootstrapper(() => new DicomDirectoryProcessorHost(globals, parsedOptions));
int ret = bootstrapper.Main();
return ret;
}
private static int OnParse(GlobalOptions globals, DicomDirectoryProcessorCliOptions parsedOptions)
{
var bootstrapper = new MicroserviceHostBootstrapper(() => new DicomDirectoryProcessorHost(globals, parsedOptions));
int ret = bootstrapper.Main();
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,44 @@
using System.Collections.Generic;
using System.IO;

namespace SmiServices.Applications.DicomDirectoryProcessor
namespace SmiServices.Applications.DicomDirectoryProcessor;

public class DicomDirectoryProcessorCliOptions : CliOptions
{
public class DicomDirectoryProcessorCliOptions : CliOptions
{
[Option('d', "to-process", Required = true, HelpText = "The directory to process")]
public string ToProcess { get; set; } = null!;
[Option('d', "to-process", Required = true, HelpText = "The directory to process")]
public string ToProcess { get; set; } = null!;

[Option('f', "directory-format", Required = false, HelpText = "The specific directory search format to use (case insensitive). Options include PACS,LIST,ZIPS and DEFAULT", Default = "Default")]
public string? DirectoryFormat { get; set; }
[Option('f', "directory-format", Required = false, HelpText = "The specific directory search format to use (case insensitive). Options include PACS,LIST,ZIPS and DEFAULT", Default = "Default")]
public string? DirectoryFormat { get; set; }


public DirectoryInfo? ToProcessDir
public DirectoryInfo? ToProcessDir
{
get
{
get
{
return ToProcess == null
? null
: new DirectoryInfo(ToProcess);
}
set => ToProcess = value?.FullName ?? throw new ArgumentNullException(nameof(value));
return ToProcess == null
? null
: new DirectoryInfo(ToProcess);
}
set => ToProcess = value?.FullName ?? throw new ArgumentNullException(nameof(value));
}

[Usage]
public static IEnumerable<Example> Examples
[Usage]
public static IEnumerable<Example> Examples
{
get
{
get
{
yield return
new Example("Normal Scenario", new DicomDirectoryProcessorCliOptions { ToProcess = @"c:\temp\bob" });
yield return
new Example("Override Yaml File", new DicomDirectoryProcessorCliOptions { ToProcess = @"c:\temp\bob", YamlFile = "myconfig.yaml" });
yield return
new Example("Search using the PACS directory structure", new DicomDirectoryProcessorCliOptions { ToProcess = @"c:\temp\bob", DirectoryFormat = "PACS" });
}
yield return
new Example("Normal Scenario", new DicomDirectoryProcessorCliOptions { ToProcess = @"c:\temp\bob" });
yield return
new Example("Override Yaml File", new DicomDirectoryProcessorCliOptions { ToProcess = @"c:\temp\bob", YamlFile = "myconfig.yaml" });
yield return
new Example("Search using the PACS directory structure", new DicomDirectoryProcessorCliOptions { ToProcess = @"c:\temp\bob", DirectoryFormat = "PACS" });
}
}

public override string ToString()
{
return base.ToString() + "ToProcess: \"" + ToProcess + ", DirectoryFormat" + DirectoryFormat + "\"\n";
}
public override string ToString()
{
return base.ToString() + "ToProcess: \"" + ToProcess + ", DirectoryFormat" + DirectoryFormat + "\"\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,102 +5,101 @@
using System.Globalization;
using System.IO;

namespace SmiServices.Applications.DicomDirectoryProcessor
namespace SmiServices.Applications.DicomDirectoryProcessor;

/// <summary>
/// Processes directories to find those that contain DICOM files
/// </summary>
public class DicomDirectoryProcessorHost : MicroserviceHost
{
private readonly DicomDirectoryProcessorCliOptions _cliOptions;
private readonly IDicomDirectoryFinder _ddf;

/// <summary>
/// Processes directories to find those that contain DICOM files
/// Constructor
/// </summary>
public class DicomDirectoryProcessorHost : MicroserviceHost
/// <param name="cliOptions">Common microservices options. Must contain details for an message exchange labelled as "accessionDirectories"</param>
/// <param name="globals">Configuration settings for the program</param>
public DicomDirectoryProcessorHost(GlobalOptions globals, DicomDirectoryProcessorCliOptions cliOptions)
: base(globals)
{
private readonly DicomDirectoryProcessorCliOptions _cliOptions;
private readonly IDicomDirectoryFinder _ddf;
_cliOptions = cliOptions;

/// <summary>
/// Constructor
/// </summary>
/// <param name="cliOptions">Common microservices options. Must contain details for an message exchange labelled as "accessionDirectories"</param>
/// <param name="globals">Configuration settings for the program</param>
public DicomDirectoryProcessorHost(GlobalOptions globals, DicomDirectoryProcessorCliOptions cliOptions)
: base(globals)
if (!cliOptions.DirectoryFormat!.ToLower().Equals("list"))
{
_cliOptions = cliOptions;

if (!cliOptions.DirectoryFormat!.ToLower().Equals("list"))
{
// TODO(rkm 2020-02-12) I think we want to check this regardless of the mode
// (bp 2020-02-13) By not doing this check on list means that the list of paths is not required to be in PACS and can be imported from anywhere
if (!Directory.Exists(globals.FileSystemOptions!.FileSystemRoot))
throw new ArgumentException("Cannot find the FileSystemRoot specified in the given MicroservicesOptions (" + globals.FileSystemOptions.FileSystemRoot + ")");
// TODO(rkm 2020-02-12) I think we want to check this regardless of the mode
// (bp 2020-02-13) By not doing this check on list means that the list of paths is not required to be in PACS and can be imported from anywhere
if (!Directory.Exists(globals.FileSystemOptions!.FileSystemRoot))
throw new ArgumentException("Cannot find the FileSystemRoot specified in the given MicroservicesOptions (" + globals.FileSystemOptions.FileSystemRoot + ")");

if (!cliOptions.ToProcessDir!.Exists)
throw new ArgumentException("Could not find directory " + cliOptions.ToProcessDir.FullName);
if (!cliOptions.ToProcessDir!.Exists)
throw new ArgumentException("Could not find directory " + cliOptions.ToProcessDir.FullName);

if (!cliOptions.ToProcessDir.FullName.StartsWith(globals.FileSystemOptions.FileSystemRoot, true, CultureInfo.CurrentCulture))
throw new ArgumentException("Directory parameter (" + cliOptions.ToProcessDir.FullName + ") must be below the FileSystemRoot (" + globals.FileSystemOptions.FileSystemRoot + ")");
}
else
{
if (!File.Exists(cliOptions.ToProcessDir!.FullName))
throw new ArgumentException("Could not find accession directory list file (" + cliOptions.ToProcessDir.FullName + ")");
if (!cliOptions.ToProcessDir.FullName.StartsWith(globals.FileSystemOptions.FileSystemRoot, true, CultureInfo.CurrentCulture))
throw new ArgumentException("Directory parameter (" + cliOptions.ToProcessDir.FullName + ") must be below the FileSystemRoot (" + globals.FileSystemOptions.FileSystemRoot + ")");
}
else
{
if (!File.Exists(cliOptions.ToProcessDir!.FullName))
throw new ArgumentException("Could not find accession directory list file (" + cliOptions.ToProcessDir.FullName + ")");

if (!Path.GetExtension(cliOptions.ToProcessDir.FullName).Equals(".csv"))
throw new ArgumentException("When in 'list' mode, path to accession directory file of format .csv expected (" + cliOptions.ToProcessDir.FullName + ")");
}
if (!Path.GetExtension(cliOptions.ToProcessDir.FullName).Equals(".csv"))
throw new ArgumentException("When in 'list' mode, path to accession directory file of format .csv expected (" + cliOptions.ToProcessDir.FullName + ")");
}

switch (cliOptions.DirectoryFormat.ToLower())
{
case "pacs":
Logger.Info("Creating PACS directory finder");
switch (cliOptions.DirectoryFormat.ToLower())
{
case "pacs":
Logger.Info("Creating PACS directory finder");

_ddf = new PacsDirectoryFinder(globals.FileSystemOptions!.FileSystemRoot!,
globals.FileSystemOptions.DicomSearchPattern!, MessageBroker.SetupProducer(globals.ProcessDirectoryOptions!.AccessionDirectoryProducerOptions!, isBatch: false));
break;
case "list":
Logger.Info("Creating accession directory lister");
_ddf = new PacsDirectoryFinder(globals.FileSystemOptions!.FileSystemRoot!,
globals.FileSystemOptions.DicomSearchPattern!, MessageBroker.SetupProducer(globals.ProcessDirectoryOptions!.AccessionDirectoryProducerOptions!, isBatch: false));
break;
case "list":
Logger.Info("Creating accession directory lister");

_ddf = new AccessionDirectoryLister(globals.FileSystemOptions!.FileSystemRoot!,
globals.FileSystemOptions.DicomSearchPattern!, MessageBroker.SetupProducer(globals.ProcessDirectoryOptions!.AccessionDirectoryProducerOptions!, isBatch: false));
break;
case "default":
Logger.Info("Creating basic directory finder");
_ddf = new AccessionDirectoryLister(globals.FileSystemOptions!.FileSystemRoot!,
globals.FileSystemOptions.DicomSearchPattern!, MessageBroker.SetupProducer(globals.ProcessDirectoryOptions!.AccessionDirectoryProducerOptions!, isBatch: false));
break;
case "default":
Logger.Info("Creating basic directory finder");

_ddf = new BasicDicomDirectoryFinder(globals.FileSystemOptions!.FileSystemRoot!,
globals.FileSystemOptions.DicomSearchPattern!, MessageBroker.SetupProducer(globals.ProcessDirectoryOptions!.AccessionDirectoryProducerOptions!, isBatch: false));
break;
case "zips":
Logger.Info("Creating zip directory finder");
_ddf = new BasicDicomDirectoryFinder(globals.FileSystemOptions!.FileSystemRoot!,
globals.FileSystemOptions.DicomSearchPattern!, MessageBroker.SetupProducer(globals.ProcessDirectoryOptions!.AccessionDirectoryProducerOptions!, isBatch: false));
break;
case "zips":
Logger.Info("Creating zip directory finder");

_ddf = new ZipDicomDirectoryFinder(globals.FileSystemOptions!.FileSystemRoot!,
globals.FileSystemOptions.DicomSearchPattern!, MessageBroker.SetupProducer(globals.ProcessDirectoryOptions!.AccessionDirectoryProducerOptions!, isBatch: false));
break;
default:
throw new ArgumentException(
$"Could not match directory format {cliOptions.DirectoryFormat} to an directory scan implementation");
}
_ddf = new ZipDicomDirectoryFinder(globals.FileSystemOptions!.FileSystemRoot!,
globals.FileSystemOptions.DicomSearchPattern!, MessageBroker.SetupProducer(globals.ProcessDirectoryOptions!.AccessionDirectoryProducerOptions!, isBatch: false));
break;
default:
throw new ArgumentException(
$"Could not match directory format {cliOptions.DirectoryFormat} to an directory scan implementation");
}
}

/// <summary>
/// Searches from the given directory to look for DICOM files and writes AccessionDirectoryMessages to the message exchange
/// </summary>
public override void Start()
/// <summary>
/// Searches from the given directory to look for DICOM files and writes AccessionDirectoryMessages to the message exchange
/// </summary>
public override void Start()
{
try
{
try
{
_ddf.SearchForDicomDirectories(_cliOptions.ToProcessDir!.FullName);
}
catch (Exception e)
{
Fatal(e.Message, e);
return;
}

Stop("Directory scan completed");
_ddf.SearchForDicomDirectories(_cliOptions.ToProcessDir!.FullName);
}

public override void Stop(string reason)
catch (Exception e)
{
_ddf.Stop();
base.Stop(reason);
Fatal(e.Message, e);
return;
}

Stop("Directory scan completed");
}

public override void Stop(string reason)
{
_ddf.Stop();
base.Stop(reason);
}
}
Loading

0 comments on commit 52a195f

Please sign in to comment.