Skip to content

Commit

Permalink
fix: add keys=True to jsonpickle
Browse files Browse the repository at this point in the history
Signed-off-by: zjgemi <[email protected]>
  • Loading branch information
zjgemi committed Apr 7, 2024
1 parent fed4697 commit 3a0b4de
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 36 deletions.
3 changes: 2 additions & 1 deletion src/dflow/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from .code_gen import gen_code
from .common import (CustomArtifact, LineageClient, LocalArtifact, S3Artifact,
import_func)
from .common import jsonpickle
from .config import config, s3_config, set_config, set_s3_config
from .context import Context
from .dag import DAG
Expand Down Expand Up @@ -44,7 +45,7 @@
"LineageClient", "Secret", "query_workflows",
"query_archived_workflows", "ContainerExecutor", "ArgoStep",
"ArgoWorkflow", "argo_enumerate", "path_object_of_artifact",
"CustomArtifact", "gen_code"]
"CustomArtifact", "gen_code", "jsonpickle"]


if os.environ.get("DFLOW_LINEAGE"):
Expand Down
3 changes: 1 addition & 2 deletions src/dflow/argo_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
from copy import deepcopy
from typing import Any, List, Union

import jsonpickle

from .common import jsonpickle
from .config import config, s3_config
from .io import S3Artifact
from .op_template import get_k8s_client
Expand Down
7 changes: 3 additions & 4 deletions src/dflow/code_gen.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import inspect
import json

import jsonpickle

from .common import (input_artifact_pattern, input_parameter_pattern,
step_output_artifact_pattern,
step_output_parameter_pattern,
task_output_artifact_pattern,
task_output_parameter_pattern)
from .common import jsonpickle
from .config import config
from .dag import DAG
from .io import InputArtifact, InputParameter, OutputArtifact, OutputParameter
Expand Down Expand Up @@ -118,7 +117,7 @@ def render_steps(self, var_name, template):
"%s.outputs.parameters['%s']" % (
step_dict[match.group(1)], match.group(2)))
elif isinstance(par, dict) and "py/object" in par:
self.imports.add((None, "jsonpickle"))
self.imports.add(("dflow", "jsonpickle"))
kwargs["parameters"][name] = Variable(
"jsonpickle.loads(%s)" % repr(json.dumps(par)))
for name, art in kwargs.get("artifacts", {}).items():
Expand Down Expand Up @@ -254,7 +253,7 @@ def render_dag(self, var_name, template):
task_dict[match.group(1)], match.group(2)))
dependencies_dict[task["name"]].append(match.group(1))
elif isinstance(par, dict) and "py/object" in par:
self.imports.add((None, "jsonpickle"))
self.imports.add(("dflow", "jsonpickle"))
kwargs["parameters"][name] = Variable(
"jsonpickle.loads(%s)" % repr(json.dumps(par)))
for name, art in kwargs.get("artifacts", {}).items():
Expand Down
17 changes: 15 additions & 2 deletions src/dflow/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from importlib import import_module
from typing import Any, Dict, List, Union

import jsonpickle
import jsonpickle as jp

from .config import config as global_config
from .config import s3_config
Expand Down Expand Up @@ -45,7 +45,7 @@
r"^{{tasks\.(.*?)\.outputs\.artifacts\.(.*?)}}$")


class CustomHandler(jsonpickle.handlers.BaseHandler):
class CustomHandler(jp.handlers.BaseHandler):
def flatten(self, obj, data):
data.update(obj.to_dict())
return data
Expand Down Expand Up @@ -253,3 +253,16 @@ def download(self, name: str, path: str):

def render(self, template, name: str):
return template


class CustomPickler:
__path__ = jp.__path__

def dumps(self, obj, **kwargs):
return jp.dumps(obj, keys=True, **kwargs)

def loads(self, s, **kwargs):
return jp.loads(s, keys=True, **kwargs)


jsonpickle = CustomPickler()
6 changes: 2 additions & 4 deletions src/dflow/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
from copy import copy, deepcopy
from typing import Any, Dict, List, Optional, Union

import jsonpickle

from .common import (CustomArtifact, LocalArtifact, S3Artifact, param_errmsg,
param_regex)
from .common import (CustomArtifact, LocalArtifact, S3Artifact, jsonpickle,
param_errmsg, param_regex)
from .config import config
from .utils import randstr, s3_config, upload_s3

Expand Down
4 changes: 1 addition & 3 deletions src/dflow/python/opio.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, Set, Union

import jsonpickle

from ..common import CustomHandler, S3Artifact
from ..common import CustomHandler, S3Artifact, jsonpickle
from ..config import config
from ..io import PVC, type_to_str

Expand Down
10 changes: 4 additions & 6 deletions src/dflow/python/python_op_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@
from pathlib import Path
from typing import Any, Dict, List, Optional, Type, Union

import jsonpickle

from .. import __path__
from ..common import S3Artifact
from ..common import S3Artifact, jsonpickle
from ..config import config
from ..io import (PVC, InputArtifact, InputParameter, Inputs, OutputArtifact,
OutputParameter, Outputs)
Expand Down Expand Up @@ -449,8 +447,8 @@ def render_script(self):
script += handle_packages_script(
"%s/inputs/artifacts/dflow_python_packages" % self.tmp_root)

script += "import json, jsonpickle\n"
script += "from dflow import config, s3_config\n"
script += "import json\n"
script += "from dflow import config, jsonpickle, s3_config\n"
script += "config.update(jsonpickle.loads(r'''%s'''))\n" % \
jsonpickle.dumps(config)
script += "s3_config.update(jsonpickle.loads(r'''%s'''))\n" % \
Expand Down Expand Up @@ -483,7 +481,7 @@ def render_script(self):
script += "%s = cloudpickle.loads(%s)\n" % \
(class_name, cloudpickle.dumps(op_class))

script += "import os, sys, traceback, jsonpickle\n"
script += "import os, sys, traceback\n"
script += "from dflow.python import OPIO, TransientError, FatalError\n"
script += "from dflow.python.utils import handle_input_artifact," \
" handle_input_parameter\n"
Expand Down
3 changes: 1 addition & 2 deletions src/dflow/python/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
from pathlib import Path
from typing import Dict, List, Set

import jsonpickle

from ..common import jsonpickle
from ..config import config
from ..utils import (artifact_classes, assemble_path_object,
convert_dflow_list, copy_file, expand, flatten, randstr,
Expand Down
5 changes: 2 additions & 3 deletions src/dflow/step.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@
from copy import copy, deepcopy
from typing import Any, Dict, List, Optional, Union

import jsonpickle

from .common import (CustomArtifact, HTTPArtifact, LocalArtifact, S3Artifact,
field_errmsg, field_regex, key_errmsg, key_regex)
field_errmsg, field_regex, jsonpickle, key_errmsg,
key_regex)
from .config import config, s3_config
from .context_syntax import GLOBAL_CONTEXT
from .executor import Executor
Expand Down
4 changes: 1 addition & 3 deletions src/dflow/util_ops.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import jsonpickle

from . import __path__
from .common import S3Artifact
from .common import S3Artifact, jsonpickle
from .config import config
from .io import (InputArtifact, InputParameter, Inputs, OutputArtifact,
OutputParameter)
Expand Down
4 changes: 1 addition & 3 deletions src/dflow/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
from pathlib import Path, PosixPath, WindowsPath
from typing import Dict, List, Optional, Set, Tuple, Union

import jsonpickle

from .common import LocalArtifact, S3Artifact
from .common import LocalArtifact, S3Artifact, jsonpickle
from .config import config, s3_config

try:
Expand Down
4 changes: 1 addition & 3 deletions src/dflow/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
from copy import deepcopy
from typing import Any, Dict, List, Optional, Union

import jsonpickle

from .argo_objects import ArgoStep, ArgoWorkflow
from .common import subdomain_errmsg, subdomain_regex
from .common import jsonpickle, subdomain_errmsg, subdomain_regex
from .config import config, s3_config
from .context import Context
from .context_syntax import GLOBAL_CONTEXT
Expand Down

0 comments on commit 3a0b4de

Please sign in to comment.