Skip to content

Commit

Permalink
Introducing file versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
martinRenou committed Dec 2, 2024
1 parent 2a95faa commit cc280cb
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/schema/src/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as Y from 'yjs';

import { IJCadObject, IJCadOptions } from './_interface/jcad';
import {
CURRENT_SCHEMA_VERSION,
IDict,
IJcadObjectDocChange,
IJupyterCadDoc,
Expand Down Expand Up @@ -35,7 +36,7 @@ export class JupyterCadDoc
}

get version(): string {
return '0.1.0';
return CURRENT_SCHEMA_VERSION;
}

get objects(): Array<IJCadObject> {
Expand Down
2 changes: 2 additions & 0 deletions packages/schema/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import {
IJCadOptions
} from './_interface/jcad';

export const CURRENT_SCHEMA_VERSION = "3.0.0";

export interface IDict<T = any> {
[key: string]: T;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/schema/src/schema/jcad.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
"required": ["objects"],
"additionalProperties": false,
"properties": {
"schemaVersion": {
"type": "string",
"default": "3.0.0"
},
"objects": {
"$ref": "#/definitions/jcadModel"
},
Expand Down
1 change: 1 addition & 0 deletions python/jupytercad_core/jupytercad_core/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CURRENT_SCHEMA_VERSION = "3.0.0"
3 changes: 3 additions & 0 deletions python/jupytercad_core/jupytercad_core/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from jupyter_server.utils import url_path_join, ApiPath, to_os_path
import tornado

from .constants import CURRENT_SCHEMA_VERSION


class JCadExportHandler(APIHandler):
@tornado.web.authenticated
Expand All @@ -22,6 +24,7 @@ def post(self):
file_content = fobj.read()

jcad = dict(
schemaVersion=CURRENT_SCHEMA_VERSION,
objects=[
dict(
name=Path(export_name).stem,
Expand Down
12 changes: 10 additions & 2 deletions python/jupytercad_core/jupytercad_core/jcad_ydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pycrdt import Array, Map, Text
from jupyter_ydoc.ybasedoc import YBaseDoc

from .constants import CURRENT_SCHEMA_VERSION


class YJCad(YBaseDoc):
def __init__(self, *args, **kwargs):
Expand All @@ -17,7 +19,7 @@ def __init__(self, *args, **kwargs):
self.undo_manager.expand_scope(self._yobjects)

def version(self) -> str:
return "0.1.0"
return CURRENT_SCHEMA_VERSION

def get(self) -> str:
"""
Expand All @@ -30,7 +32,7 @@ def get(self) -> str:
meta = self._ymetadata.to_py()
outputs = self._youtputs.to_py()
return json.dumps(
dict(objects=objects, options=options, metadata=meta, outputs=outputs),
dict(schemaVersion=CURRENT_SCHEMA_VERSION, objects=objects, options=options, metadata=meta, outputs=outputs),
indent=2,
sort_keys=True,
)
Expand All @@ -42,6 +44,12 @@ def set(self, value: str) -> None:
:type value: Any
"""
valueDict = json.loads(value)

# Assuming file version 3.0.0 if the version is not specified
file_version = valueDict["schemaVersion"] if "schemaVersion" in valueDict else "3.0.0"
if file_version != CURRENT_SCHEMA_VERSION:
raise ValueError(f"Cannot load file version {file_version}")

newObj = []
for obj in valueDict["objects"]:
newObj.append(Map(obj))
Expand Down
4 changes: 3 additions & 1 deletion python/jupytercad_core/jupytercad_core/step_ydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
from pycrdt import Text
from jupyter_ydoc.ybasedoc import YBaseDoc

from .constants import CURRENT_SCHEMA_VERSION


class YSTEP(YBaseDoc):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._ydoc["source"] = self._ysource = Text()

def version(self) -> str:
return "0.1.0"
return CURRENT_SCHEMA_VERSION

def get(self) -> str:
"""
Expand Down
4 changes: 3 additions & 1 deletion python/jupytercad_core/jupytercad_core/stl_ydoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
from pycrdt import Text
from jupyter_ydoc.ybasedoc import YBaseDoc

from .constants import CURRENT_SCHEMA_VERSION


class YSTL(YBaseDoc):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._ydoc["source"] = self._ysource = Text()

def version(self) -> str:
return "0.1.0"
return CURRENT_SCHEMA_VERSION

def get(self) -> str:
"""
Expand Down
3 changes: 2 additions & 1 deletion python/jupytercad_core/src/jcadplugin/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from '@jupyter/collaborative-drive';
import { logoIcon, CommandIDs as BaseCommandIDs } from '@jupytercad/base';
import {
CURRENT_SCHEMA_VERSION,
IAnnotationModel,
IAnnotationToken,
IJCadExternalCommandRegistry,
Expand Down Expand Up @@ -146,7 +147,7 @@ const activate = (
format: 'text',
size: undefined,
content:
'{\n\t"objects": [],\n\t"options": {},\n\t"metadata": {},\n\t"outputs": {}}'
`{\n\t"schemaVersion": "${CURRENT_SCHEMA_VERSION}",\n\t"objects": [],\n\t"options": {},\n\t"metadata": {},\n\t"outputs": {}}`
});

// Open the newly created file with the 'Editor'
Expand Down
3 changes: 2 additions & 1 deletion python/jupytercad_core/src/stepplugin/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CURRENT_SCHEMA_VERSION,
IJCadObject,
IJcadObjectDocChange,
IJupyterCadDoc,
Expand All @@ -22,7 +23,7 @@ export class JupyterCadStepDoc extends JupyterCadDoc {
}

get version(): string {
return '0.1.0';
return CURRENT_SCHEMA_VERSION;
}

get objectsChanged(): ISignal<IJupyterCadDoc, IJcadObjectDocChange> {
Expand Down
3 changes: 2 additions & 1 deletion python/jupytercad_core/src/stlplugin/model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CURRENT_SCHEMA_VERSION,
IJCadObject,
IJcadObjectDocChange,
IJupyterCadDoc,
Expand All @@ -22,7 +23,7 @@ export class JupyterCadStlDoc extends JupyterCadDoc {
}

get version(): string {
return '0.1.0';
return CURRENT_SCHEMA_VERSION;
}

get objectsChanged(): ISignal<IJupyterCadDoc, IJcadObjectDocChange> {
Expand Down

0 comments on commit cc280cb

Please sign in to comment.