Skip to content

Commit bd9c53b

Browse files
Merge pull request #68 from zmap/phillip/update
Update code/test to work with latest Python 3.12 and add CI
2 parents e6615e0 + 31375a2 commit bd9c53b

11 files changed

+842
-671
lines changed

.github/workflows/ci.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Run Unit Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
workflow_dispatch:
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
# Step 1: Check out the code
16+
- name: Checkout code
17+
uses: actions/checkout@v3
18+
19+
# Step 2: Set up Python
20+
- name: Set up Python
21+
uses: actions/setup-python@v4
22+
with:
23+
python-version: '3.12' # Specify the Python version you want to use
24+
25+
# Step 3: Install dependencies
26+
- name: Install dependencies
27+
run: |
28+
python -m pip install --upgrade pip setuptools pytest
29+
pip install -r requirements.txt || true # In case requirements.txt doesn't exist
30+
31+
# Step 4: Run tests
32+
- name: Run tests
33+
run: |
34+
pip3 install ".[tests]"
35+
pytest

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
ZSchema
22
=======
33

4-
[![Build Status](https://travis-ci.org/zmap/zschema.svg?branch=master)](https://travis-ci.org/zmap/zschema)
5-
64
ZSchema is a generic (meta-)schema language for defining database schemas. It
75
facilitates (1) validating JSON documents against a schema definition and (2)
86
compilating a schema to multiple database engines. For example, if you wanted
@@ -188,8 +186,11 @@ https://github.com/zmap/zschema/blob/master/zschema/leaves.py#L25.
188186
Running Tests
189187
=============
190188

191-
Tests are run with [nose](http://nose.readthedocs.io/en/latest/). Run them via
192-
`python setup.py test`.
189+
Tests are run with [pytest](https://docs.pytest.org/en/stable/). Run them via:
190+
```zsh
191+
pip3 install ".[tests]"
192+
pytest
193+
```
193194

194195

195196
License and Copyright

pyproject.toml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[build-system]
2+
requires = ["setuptools>=42"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "zschema"
7+
description = "A schema language for JSON documents that allows validation and compilation into various database engines"
8+
version = "0.11.0"
9+
authors = [ { name = "ZMap Team"} ]
10+
license = { text = "Apache License, Version 2.0" } # Replace with the actual license
11+
keywords = ["python", "json", "schema", "bigquery", "elasticsearch"]
12+
13+
dependencies = [
14+
"future",
15+
"python-dateutil",
16+
"pytz",
17+
"six"
18+
]
19+
20+
[project.optional-dependencies]
21+
tests = [
22+
"pytest"
23+
]
24+
25+
[project.scripts]
26+
zschema = "zschema.__main__:main"

setup.py

-41
This file was deleted.

zschema/__main__.py

+66-42
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import sys
2+
import importlib.util
23
import os.path
34
import json
45
import zschema.registry
56
import argparse
67

7-
from imp import load_source
88
from importlib import import_module
99
from site import addsitedir
1010

11-
from leaves import *
12-
from keys import *
13-
from compounds import *
11+
from .leaves import *
12+
from .keys import *
13+
from .compounds import *
1414

1515
commands = [
1616
"bigquery",
@@ -20,43 +20,59 @@
2020
"docs-es",
2121
"validate",
2222
"flat",
23-
"json"
23+
"json",
2424
]
2525

2626
cmdList = ", ".join(commands)
2727

2828
parser = argparse.ArgumentParser(
2929
prog="zschema",
30-
description="Process a zschema definition. "
31-
"VERSION: %s" % zschema.__version__)
32-
33-
parser.add_argument("command",
34-
metavar="command", choices=commands,
35-
help="The command to execute; one of [ %s ]" % cmdList)
36-
37-
parser.add_argument("schema",
38-
help="The name of the schema in the zschema.registry. "
39-
"For backwards compatibility, a filename can be "
40-
"prefixed with a colon, as in 'schema.py:my-type'.")
41-
42-
parser.add_argument("target", nargs="?",
43-
help="Only used for the validate command. "
44-
"The input JSON file that will be checked against "
45-
"the schema.")
30+
description="Process a zschema definition. " "VERSION: %s" % zschema.__version__,
31+
)
32+
33+
parser.add_argument(
34+
"command",
35+
metavar="command",
36+
choices=commands,
37+
help="The command to execute; one of [ %s ]" % cmdList,
38+
)
39+
40+
parser.add_argument(
41+
"schema",
42+
help="The name of the schema in the zschema.registry. "
43+
"For backwards compatibility, a filename can be "
44+
"prefixed with a colon, as in 'schema.py:my-type'.",
45+
)
46+
47+
parser.add_argument(
48+
"target",
49+
nargs="?",
50+
help="Only used for the validate command. "
51+
"The input JSON file that will be checked against "
52+
"the schema.",
53+
)
4654

4755
parser.add_argument("--module", help="The name of a module to import.")
4856

49-
parser.add_argument("--validation-policy", help="What to do when a validation "
50-
"error occurs. This only overrides the top-level Record. It does not "
51-
"override subrecords. Default: error.", choices=["ignore", "warn", "error"],
52-
default=None)
53-
54-
parser.add_argument("--validation-policy-override", help="Override validation "
55-
"policy for all levels of the schema.", choices=["ignore", "warn", "error"],
56-
default=None)
57-
58-
parser.add_argument("--path", nargs="*",
59-
help="Additional PYTHONPATH directories to include.")
57+
parser.add_argument(
58+
"--validation-policy",
59+
help="What to do when a validation "
60+
"error occurs. This only overrides the top-level Record. It does not "
61+
"override subrecords. Default: error.",
62+
choices=["ignore", "warn", "error"],
63+
default=None,
64+
)
65+
66+
parser.add_argument(
67+
"--validation-policy-override",
68+
help="Override validation " "policy for all levels of the schema.",
69+
choices=["ignore", "warn", "error"],
70+
default=None,
71+
)
72+
73+
parser.add_argument(
74+
"--path", nargs="*", help="Additional PYTHONPATH directories to include."
75+
)
6076

6177
args = parser.parse_args()
6278

@@ -71,7 +87,7 @@ def main():
7187
# Backwards compatibility: given "file.py:schema", load file.py.
7288
if ":" in schema:
7389
path, recname = schema.split(":")
74-
load_source('module', path)
90+
load_source("module", path)
7591
schema = recname
7692

7793
if args.module:
@@ -82,31 +98,39 @@ def main():
8298
record.set("validation_policy", args.validation_policy)
8399
command = args.command
84100
if command == "bigquery":
85-
print json.dumps(record.to_bigquery())
101+
print(json.dumps(record.to_bigquery()))
86102
elif command == "elasticsearch":
87-
print json.dumps(record.to_es(recname))
103+
print(json.dumps(record.to_es(recname)))
88104
elif command == "proto":
89-
print record.to_proto(recname)
105+
print(record.to_proto(recname))
90106
elif command == "docs-es":
91-
print json.dumps(record.docs_es(recname))
107+
print(json.dumps(record.docs_es(recname)))
92108
elif command == "docs-bq":
93-
print json.dumps(record.docs_bq(recname))
109+
print(json.dumps(record.docs_bq(recname)))
94110
elif command == "json":
95-
print record.to_json()
111+
print(record.to_json())
96112
elif command == "flat":
97113
for r in record.to_flat():
98-
print json.dumps(r)
114+
print(json.dumps(r))
99115
elif command == "validate":
100116
if not os.path.exists(args.target):
101117
sys.stderr.write("Invalid test file. %s does not exist.\n" % args.target)
102118
sys.exit(1)
103119
with open(args.target) as fd:
104120
for line in fd:
105-
record.validate(json.loads(line.strip()),
106-
args.validation_policy_override)
121+
record.validate(
122+
json.loads(line.strip()), args.validation_policy_override
123+
)
107124
else:
108125
usage()
109126

110127

128+
def load_source(name, path):
129+
spec = importlib.util.spec_from_file_location(name, path)
130+
module = importlib.util.module_from_spec(spec)
131+
spec.loader.exec_module(module)
132+
return module
133+
134+
111135
if __name__ == "__main__":
112136
main()

0 commit comments

Comments
 (0)