pylindas 0.4.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pylindas/__init__.py ADDED
@@ -0,0 +1,7 @@
1
+ from pylindas.cube import Cube
2
+ from pylindas.getter.get import get_cube, get_observations
3
+ from pylindas.lindas.namespaces import Namespaces
4
+ from pylindas.lindas.upload import upload_ttl
5
+ from pylindas.lindas.query import query_lindas
6
+
7
+ __version__ = "0.4.1"
pylindas/cli.py ADDED
@@ -0,0 +1,144 @@
1
+ import argparse
2
+ import os
3
+ import pandas as pd
4
+ import yaml
5
+ import py_cube
6
+ import logging
7
+
8
+ from py_cube.fetch import fetch
9
+ from py_cube.example import list_examples, load_example
10
+ from py_cube.cube.shared_dimension import convert_geojson_to_ttl
11
+
12
+
13
+ logger = logging.getLogger('pycube')
14
+
15
+
16
+ def serialize(input_directory: str, output_ttl: str, na_values: list[str], sep: str = ",", decimal: str = "."):
17
+ csv_path = os.path.join(input_directory, "data.csv")
18
+ yml_path = os.path.join(input_directory, "description.yml")
19
+ json_path = os.path.join(input_directory, "description.json")
20
+
21
+ if os.path.exists(yml_path):
22
+ with open(yml_path) as file:
23
+ cube_yaml = yaml.safe_load(file)
24
+ elif os.path.exists(json_path):
25
+ with open(json_path) as file:
26
+ cube_yaml = yaml.safe_load(file)
27
+ else:
28
+ raise FileNotFoundError("Neither description.yml nor description.json found in the directory")
29
+
30
+ df = pd.read_csv(csv_path, na_values=na_values, sep=sep, decimal=decimal)
31
+
32
+ cube = py_cube.Cube(dataframe=df, cube_yaml=cube_yaml, environment="TEST", local=True)
33
+ cube.prepare_data()
34
+ cube.write_cube()
35
+ cube.write_observations()
36
+ cube.write_shape()
37
+ cube.serialize(os.path.join(os.getcwd(), output_ttl))
38
+ print(cube)
39
+
40
+
41
+ def configure_logging(log_level):
42
+ class CustomFormatter(logging.Formatter):
43
+ """Custom logging formatter to add colors based on log level."""
44
+
45
+ COLORS = {
46
+ 'DEBUG': '\033[0m', # Normal
47
+ 'INFO': '\033[94m', # Blue
48
+ 'WARNING': '\033[93m', # Yellow
49
+ 'ERROR': '\033[91m', # Red
50
+ 'CRITICAL': '\033[91m', # Red
51
+ }
52
+
53
+ def format(self, record):
54
+ log_fmt = self.COLORS.get(record.levelname, '\033[0m') + '%(levelname)s: %(message)s\033[0m'
55
+ formatter = logging.Formatter(log_fmt)
56
+ return formatter.format(record)
57
+
58
+ console_handler = logging.StreamHandler()
59
+ logger.setLevel(log_level)
60
+ console_handler.setFormatter(CustomFormatter())
61
+ logger.addHandler(console_handler)
62
+
63
+
64
+ def main():
65
+ parser = argparse.ArgumentParser(description="Cube data operations")
66
+ subparsers = parser.add_subparsers(dest="operation", help="Operation to perform")
67
+
68
+ serialize_parser = subparsers.add_parser("serialize", help="Serialize cube data")
69
+ serialize_parser.add_argument("input_directory", help="Directory containing the data files")
70
+ serialize_parser.add_argument("output_ttl", help="Output TTL file")
71
+ serialize_parser.add_argument("--na_value", nargs="+", help="Values to treat as NA")
72
+ serialize_parser.add_argument("--sep", default=",", nargs="?", help="Separator for CSV file")
73
+ serialize_parser.add_argument("--decimal", default=".", nargs="?", help="Decimal separator")
74
+ serialize_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
75
+
76
+ fetch_parser = subparsers.add_parser("fetch", help="Fetches a dataset from a URL")
77
+ fetch_parser.add_argument("input_url", type=str, help="The URL of the dataset to fetch")
78
+ fetch_parser.add_argument("output", type=str, help="The directory to save the output files")
79
+ fetch_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
80
+
81
+ shared_parser = subparsers.add_parser("shared", help="Shared Dimension operations")
82
+ shared_subparsers = shared_parser.add_subparsers(dest="suboperation", help="Shared sub-operations")
83
+
84
+ convert_geojson_parser = shared_subparsers.add_parser("convert_geojson", help="Convert GeoJSON to TTL")
85
+ convert_geojson_parser.add_argument("input_geojson", type=str, help="Input GeoJSON file")
86
+ convert_geojson_parser.add_argument("output_ttl", type=str, help="Output TTL file")
87
+ convert_geojson_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
88
+
89
+ example_parser = subparsers.add_parser("example", help="Example operations")
90
+ example_subparsers = example_parser.add_subparsers(dest="suboperation", help="Example sub-operations")
91
+
92
+ list_parser = example_subparsers.add_parser("list", help="List all examples")
93
+ list_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
94
+
95
+ start_fuseki_parser = example_subparsers.add_parser("start-fuseki", help="Start a Fuseki database")
96
+ start_fuseki_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
97
+
98
+ load_parser = example_subparsers.add_parser("load", help="Load an example by name")
99
+ load_parser.add_argument("example_name", type=str, help="The name of the example to load", choices=[example["id"] for example in list_examples()])
100
+ # add optional base_uri argument to load parser
101
+ load_parser.add_argument("--base-uri", type=str, help="The base URI for a SPARQL database (Fuseki supported)", default="http://localhost:3030/dataset")
102
+ load_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
103
+
104
+ schema_parser = subparsers.add_parser("schema", help="Schema operations")
105
+ schema_subparsers = schema_parser.add_subparsers(dest="suboperation", help="Schema sub-operations")
106
+ schema_subparsers.add_parser("import", help="Import the description schema file")
107
+ schema_parser.add_argument("output", type=str, help="Output file")
108
+ schema_parser.add_argument("-v", "--verbose", action="count", default=0, help="Increase verbosity")
109
+
110
+
111
+ args = parser.parse_args()
112
+ log_level = logging.DEBUG if args.verbose == 1 else logging.INFO
113
+
114
+ configure_logging(log_level)
115
+
116
+ if args.operation == "serialize":
117
+ serialize(args.input_directory, args.output_ttl, args.na_value, args.sep, args.decimal)
118
+ elif args.operation == "fetch":
119
+ fetch(args.input_url, args.output)
120
+ elif args.operation == "example":
121
+ if args.suboperation == "list":
122
+ examples = list_examples()
123
+ for example in examples:
124
+ print(f'{example["id"]}: {example["name"]}')
125
+ elif args.suboperation == "load":
126
+ load_example(args.example_name, args.base_uri)
127
+ elif args.suboperation == "start-fuseki":
128
+ os.system("scripts/fuseki/start.sh")
129
+ elif args.operation == "shared":
130
+ if args.suboperation == "convert_geojson":
131
+ convert_geojson_to_ttl(args.input_geojson, args.output_ttl)
132
+ elif args.operation == 'schema':
133
+ if args.suboperation == "import-description":
134
+ description_schema_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'description.schema.json')
135
+ with open(description_schema_path, 'r') as f:
136
+ schema = f.read()
137
+ with open(args.output, 'w') as f:
138
+ f.write(schema)
139
+ logger.debug(f"Imported description into current directory: {args.output}")
140
+
141
+
142
+
143
+ if __name__ == "__main__":
144
+ main()
@@ -0,0 +1 @@
1
+ from pylindas.cube.cube import Cube