Skip to content

Commit

Permalink
PR updates
Browse files Browse the repository at this point in the history
  • Loading branch information
lyonsil committed Sep 25, 2023
1 parent a0f4d2e commit acdadb2
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 62 deletions.
2 changes: 1 addition & 1 deletion c-sharp/JsonUtils/ProjectDataScopeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ out string errorMessage
JObject parsedArgs = JObject.Parse(jsonString);
dataScope = new ProjectDataScope()
{
ProjectID = Guid.Parse(Get(parsedArgs, PROJECT_ID)!),
ProjectID = Get(parsedArgs, PROJECT_ID),
ProjectName = Get(parsedArgs, PROJECT_NAME),
ExtensionName = Get(parsedArgs, EXTENSION_NAME),
DataType = Get(parsedArgs, DATA_TYPE),
Expand Down
4 changes: 2 additions & 2 deletions c-sharp/JsonUtils/ProjectMetadataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ out string errorMessage
try
{
JObject parsedArgs = JObject.Parse(jsonString);
Guid id = Guid.Parse(Get(parsedArgs, ID));
string id = Get(parsedArgs, ID);
string name = Get(parsedArgs, NAME);
string projectStorageType = Get(parsedArgs, STORAGE_TYPE);
string projectType = Get(parsedArgs, PROJECT_TYPE);
Expand Down Expand Up @@ -51,7 +51,7 @@ public static string ToJsonString(ProjectMetadata projectMetadata)
{
return new JObject
{
[ID] = projectMetadata.ID.ToString(),
[ID] = projectMetadata.ID,
[NAME] = projectMetadata.Name,
[STORAGE_TYPE] = projectMetadata.ProjectStorageType,
[PROJECT_TYPE] = projectMetadata.ProjectType
Expand Down
45 changes: 23 additions & 22 deletions c-sharp/Projects/LocalProjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace Paranext.DataProvider.Projects;
/// </summary>
internal static partial class LocalProjects
{
private static readonly ConcurrentDictionary<Guid, ProjectDetails> s_projectDetailsMap = new();
private static readonly ConcurrentDictionary<Guid, ScrText> s_paratextProjectMap = new();
private static readonly ConcurrentDictionary<string, ProjectDetails> s_projectDetailsMap =
new();

// All project directories are subdirectories of this
private static readonly string s_projectRootFolder;
Expand All @@ -39,7 +39,7 @@ static LocalProjects()

public static void Initialize()
{
if (!s_paratextProjectMap.IsEmpty || !s_projectDetailsMap.IsEmpty)
if (!s_projectDetailsMap.IsEmpty)
return;

if (!Directory.Exists(s_projectRootFolder))
Expand Down Expand Up @@ -72,14 +72,14 @@ public static IList<ProjectDetails> GetAllProjectDetails()
return s_projectDetailsMap.Values.ToList();
}

public static ProjectDetails GetProjectDetails(Guid projectId)
public static ProjectDetails GetProjectDetails(string projectId)
{
return s_projectDetailsMap[projectId];
return s_projectDetailsMap[projectId.ToUpperInvariant()];
}

public static ScrText GetParatextProject(Guid projectId)
public static ScrText GetParatextProject(string projectId)
{
return s_paratextProjectMap[projectId];
return ScrTextCollection.GetById(HexId.FromStr(projectId));
}

public static void SaveProjectMetadata(ProjectMetadata metadata, bool overwrite = false)
Expand All @@ -105,34 +105,35 @@ public static void SaveProjectMetadata(ProjectMetadata metadata, bool overwrite
AddProjectToMaps(new ProjectDetails(metadata, projectHomeDir));
}

public static void LoadProject(string projectName, Guid projectID)
public static void LoadProject(string projectName, string projectID)
{
var dir = GetProjectDir(projectName, projectID);
var projectMetadata =
LoadProjectMetadata(dir, out string errorMessage) ?? throw new Exception(errorMessage);
AddProjectToMaps(new ProjectDetails(projectMetadata, dir));
}

private static string GetProjectDir(string projectName, Guid projectID)
private static string GetProjectDir(string projectName, string projectID)
{
return Path.Join(s_projectRootFolder, $"{projectName}_{projectID}");
}

private static void AddProjectToMaps(ProjectDetails projectDetails)
{
ProjectName projectName =
new(
Path.Join(
projectDetails.Directory,
PROJECT_SUBDIRECTORY,
PARATEXT_DATA_SUBDIRECTORY
)
);
var projectPath = Path.Join(
projectDetails.HomeDirectory,
PROJECT_SUBDIRECTORY,
PARATEXT_DATA_SUBDIRECTORY
);
ProjectName projectName = new(projectPath);
var id = projectDetails.Metadata.ID;
if (s_projectDetailsMap.ContainsKey(id) || s_paratextProjectMap.ContainsKey(id))
Console.WriteLine($"Replacing Paratext project in map: {id}");
Console.WriteLine(
s_projectDetailsMap.ContainsKey(id)
? $"Replacing Paratext project in map: {id} = {projectPath}"
: $"Adding Paratext project in map: {id} = {projectPath}"
);
s_projectDetailsMap[id] = projectDetails;
s_paratextProjectMap[id] = new ScrText(projectName, RegistrationInfo.DefaultUser);
ScrTextCollection.Add(new ScrText(projectName, RegistrationInfo.DefaultUser));
}

/// <summary>
Expand Down Expand Up @@ -191,8 +192,8 @@ out string errorMessage
var matches = ProjectDirectoryRegex().Matches(finalDirectory);
if (
(matches.Count != 1)
|| Guid.Parse(matches[0].Groups["id"].Value) != metadata!.ID
|| matches[0].Groups["name"].Value != metadata.Name
|| (metadata == null)
|| !metadata.Contains(matches[0].Groups["id"].Value, matches[0].Groups["name"].Value)
)
{
errorMessage = $"Project directory does not match its metadata: {projectHomeDir}";
Expand Down
5 changes: 0 additions & 5 deletions c-sharp/Projects/ParatextProjectDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ protected override ResponseToRequest SetExtensionData(ProjectDataScope dataScope
return _paratextPsi.SetExtensionData(dataScope, data);
}

protected override ResponseToRequest SubscribeExtensionData(string jsonString)
{
return ResponseToRequest.Failed("Subscribing to extension data is not supported");
}

private ResponseToRequest Get(string dataType, string dataQualifier)
{
ProjectDataScope scope =
Expand Down
2 changes: 1 addition & 1 deletion c-sharp/Projects/ParatextProjectDataProviderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ string projectStorageInterpreterId
ProjectDetails details;
try
{
details = LocalProjects.GetProjectDetails(Guid.Parse(projectID));
details = LocalProjects.GetProjectDetails(projectID);
}
catch (Exception)
{
Expand Down
10 changes: 5 additions & 5 deletions c-sharp/Projects/ParatextProjectStorageInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public override ResponseToRequest CreateProject(ProjectDataScope scope)

ProjectMetadata metadata =
new(
scope.ProjectID.Value,
scope.ProjectID,
scope.ProjectName,
ProjectStorageType.ParatextFolders,
ProjectType.Paratext
Expand All @@ -68,7 +68,7 @@ public override ResponseToRequest GetProjectSettings(ProjectDataScope scope)
if (scope.ProjectID == null)
return ResponseToRequest.Failed("Must provide a project ID");

var scrText = LocalProjects.GetParatextProject(scope.ProjectID.Value);
var scrText = LocalProjects.GetParatextProject(scope.ProjectID);
return ResponseToRequest.Succeeded(JsonConvert.SerializeObject(scrText.Settings));
}

Expand All @@ -84,7 +84,7 @@ public override ResponseToRequest GetProjectData(ProjectDataScope scope)
// Not making it mandatory that all calls provide a VerseRef since there might be some types that don't use it
VerseRefConverter.TryCreateVerseRef(scope.DataQualifier, out var verseRef, out var error);

var scrText = LocalProjects.GetParatextProject(scope.ProjectID.Value);
var scrText = LocalProjects.GetParatextProject(scope.ProjectID);
return scope.DataType.ToUpperInvariant() switch
{
Book
Expand Down Expand Up @@ -115,7 +115,7 @@ public override ResponseToRequest SetProjectData(ProjectDataScope scope, string
// Not making it mandatory that all calls provide a VerseRef since there might be some types that don't use it
VerseRefConverter.TryCreateVerseRef(scope.DataQualifier, out var verseRef, out var error);

var scrText = LocalProjects.GetParatextProject(scope.ProjectID.Value);
var scrText = LocalProjects.GetParatextProject(scope.ProjectID);
switch (scope.DataType.ToUpperInvariant())
{
case Chapter:
Expand Down Expand Up @@ -178,7 +178,7 @@ public override ResponseToRequest SetExtensionData(ProjectDataScope scope, strin
if (scope.ProjectID == null)
return null;

var projectDetails = LocalProjects.GetProjectDetails(scope.ProjectID.Value);
var projectDetails = LocalProjects.GetProjectDetails(scope.ProjectID);
RawDirectoryProjectStreamManager extensionStreamManager = new(projectDetails);
return extensionStreamManager.GetDataStream(
$"extensions/{scope.ExtensionName}/{scope.DataQualifier}",
Expand Down
14 changes: 2 additions & 12 deletions c-sharp/Projects/ProjectDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Paranext.DataProvider.Projects;

/// <summary>
/// Manages reading/writing to a project
/// Subclasses are expected to define and add get/set/subscribe methods to the Getters/Setters/Subscribers
/// Subclasses are also expected to use a ProjectStorageInterpreter for implementing all get/set/subscribe methods as appropriate
/// Subclasses are expected to define and add get/set methods to the Getters/Setters
/// Subclasses are also expected to use a ProjectStorageInterpreter for implementing all get/set methods as appropriate
/// ProjectDataProviders are meant to be created/owned by ProjectDataProviderFactory instances
/// </summary>
internal abstract class ProjectDataProvider : NetworkObjects.DataProvider
Expand All @@ -19,14 +19,12 @@ protected ProjectDataProvider(string name, PapiClient papiClient, ProjectDetails
ProjectDetails = projectDetails;
Getters.Add("getExtensionData", GetExtensionData);
Setters.Add("setExtensionData", SetExtensionData);
Subscribers.Add("subscribeExtensionData", SubscribeExtensionData);
}

protected ProjectDetails ProjectDetails { get; }

protected Dictionary<string, Func<string, ResponseToRequest>> Getters { get; } = new();
protected Dictionary<string, Func<string, string, ResponseToRequest>> Setters { get; } = new();
protected Dictionary<string, Func<string, ResponseToRequest>> Subscribers { get; } = new();

protected ProjectDataScope ExtractDataScope(string jsonString)
{
Expand All @@ -51,9 +49,6 @@ protected override ResponseToRequest HandleRequest(string functionName, JsonArra
{
try
{
if (functionName.StartsWith("subscribe"))
return Subscribers[functionName](args[0]!.ToJsonString());

if (functionName.StartsWith("get"))
return Getters[functionName](args[0]!.ToJsonString());

Expand Down Expand Up @@ -102,9 +97,4 @@ private ResponseToRequest SetExtensionData(string jsonScope, string jsonData)
/// Set an extension's data in a project identified by <param name="scope"></param>.
/// </summary>
protected abstract ResponseToRequest SetExtensionData(ProjectDataScope scope, string jsonData);

/// <summary>
/// Subscribe to changes in an extension's data in a project
/// </summary>
protected abstract ResponseToRequest SubscribeExtensionData(string jsonString);
}
2 changes: 1 addition & 1 deletion c-sharp/Projects/ProjectDataScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal class ProjectDataScope
/// <summary>
/// Project ID
/// </summary>
public Guid? ProjectID { get; set; }
public string? ProjectID { get; set; }

/// <summary>
/// Project short name
Expand Down
16 changes: 10 additions & 6 deletions c-sharp/Projects/ProjectDetails.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ namespace Paranext.DataProvider.Projects;
/// </summary>
internal class ProjectDetails
{
public ProjectDetails(ProjectMetadata metadata, string directory)
public ProjectDetails(ProjectMetadata metadata, string homeDirectory)
{
Metadata = metadata;
Directory = directory;
HomeDirectory = homeDirectory;
}

public ProjectMetadata Metadata { get; }
public string Directory { get; }

/// <summary>
/// Directory where everything we know about a project is stored
/// </summary>
public string HomeDirectory { get; }

public override bool Equals(object? obj)
{
Expand All @@ -22,16 +26,16 @@ public override bool Equals(object? obj)

protected bool Equals(ProjectDetails other)
{
return Metadata.Equals(other.Metadata) && Directory == other.Directory;
return Metadata.Equals(other.Metadata) && HomeDirectory == other.HomeDirectory;
}

public override int GetHashCode()
{
return HashCode.Combine(Metadata, Directory);
return HashCode.Combine(Metadata, HomeDirectory);
}

public override string ToString()
{
return $"[{Directory}] = {Metadata}";
return $"[{HomeDirectory}] = {Metadata}";
}
}
15 changes: 11 additions & 4 deletions c-sharp/Projects/ProjectMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ namespace Paranext.DataProvider.Projects;
/// </summary>
public class ProjectMetadata
{
public ProjectMetadata(Guid id, string name, string projectStorageType, string projectType)
public ProjectMetadata(string id, string name, string projectStorageType, string projectType)
{
ID = id;
ID = id.ToUpperInvariant();
Name = name;
ProjectStorageType = projectStorageType;
ProjectType = projectType;
}

/// <summary>
/// ID of the project (must be unique)
/// ID of the project (must be unique and case insensitive)
/// </summary>
public Guid ID { get; }
public string ID { get; }

/// <summary>
/// Short name of the project (not necessarily unique)
Expand All @@ -33,6 +33,13 @@ public ProjectMetadata(Guid id, string name, string projectStorageType, string p
/// </summary>
public string ProjectType { get; }

public bool Contains(string id, string name, bool nameIsCaseSensitive = false)
{
return ID.Equals(id, StringComparison.InvariantCultureIgnoreCase) && nameIsCaseSensitive
? Name.Equals(name)
: Name.Equals(name, StringComparison.InvariantCultureIgnoreCase);
}

public override bool Equals(object? obj)
{
ProjectMetadata? that = obj as ProjectMetadata;
Expand Down
4 changes: 2 additions & 2 deletions c-sharp/Projects/RawDirectoryProjectStreamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ internal class RawDirectoryProjectStreamManager : IProjectStreamManager
public RawDirectoryProjectStreamManager(ProjectDetails projectDetails)
{
_projectDetails = projectDetails;
_writableRootDir = Path.Join(projectDetails.Directory, "project");
_writableRootDir = Path.Join(projectDetails.HomeDirectory, "project");
}

public void Initialize()
{
if (!Directory.Exists(_projectDetails.Directory))
if (!Directory.Exists(_projectDetails.HomeDirectory))
throw new Exception(
$"Project contents missing for {_projectDetails.Metadata.Name} ({_projectDetails.Metadata.ID})"
);
Expand Down
2 changes: 1 addition & 1 deletion src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ async function main() {
// Test a .NET data provider
setTimeout(async () => {
const paratextPdp = await getProjectDataProvider(
'499234e1-9965-452b-8175-138330893b6a',
'b4c501ad2538989d6fb723518e92408406e232d3',
'ParatextStandard',
'paratextFolders',
);
Expand Down

0 comments on commit acdadb2

Please sign in to comment.