Package jsondata
[hide private]
[frames] | no frames]

Source Code for Package jsondata

  1  """Modular processing of JSON data by trees and branches, pointers and patches. 
  2  """ 
  3   
  4  import sys 
  5  import os 
  6   
  7  import filesysobjects.userdata 
  8  import filesysobjects.osdata 
  9   
 10  __author__ = 'Arno-Can Uestuensoez' 
 11  __maintainer__ = 'Arno-Can Uestuensoez' 
 12  __license__ = "Artistic-License-2.0 + Forced-Fairplay-Constraints" 
 13  __copyright__ = "Copyright (C) 2015-2016 Arno-Can Uestuensoez" \ 
 14                  " @Ingenieurbuero Arno-Can Uestuensoez" 
 15  __version__ = '0.2.21' 
 16  __uuid__ = '63b597d6-4ada-4880-9f99-f5e0961351fb' 
 17   
 18   
 19  # 
 20  # used for default names 
 21  appname = 'jsondata' 
 22   
23 -class JSONDataError(Exception):
24 """ base Exception.""" 25
26 - def __init__(self, *arg):
27 """To be replaced by derived Exceptions. 28 29 Fetch standard parameters and forward message to base class 'Exception'. 30 """ 31 self.fetch(*arg) 32 Exception.__init__(self, self.s)
33
34 - def fetch(self, *arg):
35 """Fetch arguments. 36 37 Args: 38 *args: 39 The following order is expected: 40 41 0. Reason of exception. 42 43 1. Name of object that caused the exception. 44 45 2. Value of the object. 46 47 Returns: 48 None. 49 50 Raises: 51 None. 52 """ 53 self.s = "" 54 for a in arg: 55 self.s += ":" + str(a) 56 self.s = self.s[1:]
57
58 - def __repr__(self):
59 """Cause: <reason>:<object>:<value>""" 60 return self.s
61
62 - def __str__(self):
63 """Cause with additional header text.""" 64 return "ERROR::" + self.s
65 66 # Sets display for inetractive JSON/JSONschema design. 67 _interactive = False 68 69 70 V3K = False #: Python3.5+ 71 if sys.version_info[:2] > (3, 4,): 72 V3K = True 73 ISSTR = (str,) 74 """string and unicode""" 75 76 elif sys.version_info[:2] > (2, 6,) and sys.version_info[:2][0] < 3: 77 ISSTR = (str, unicode,) # @UndefinedVariable 78 """string and unicode""" 79 80 else: 81 raise JSONDataError( 82 "Requires Python 2.7+, or 3.5+:" + 83 str(sys.version_info[:2])) 84 85 # 86 # generic exceptions 87 # 88 89
90 -class JSONDataIndexError(JSONDataError, IndexError):
91 """ Error on key.""" 92
93 - def __init__(self, *arg):
94 JSONDataError.__init__(self, *arg) 95 JSONDataError.fetch(self, *arg) 96 IndexError.__init__(self, self.s)
97
98 - def __str__(self):
99 return "JSONDataIndexError:" + self.s
100 101
102 -class JSONDataKeyError(JSONDataError, KeyError):
103 """ Error on key.""" 104
105 - def __init__(self, *arg):
106 JSONDataError.__init__(self, *arg) 107 JSONDataError.fetch(self, *arg) 108 KeyError.__init__(self, self.s)
109
110 - def __str__(self):
111 return "JSONDataKeyError:" + self.s
112 113
114 -class JSONDataPathError(JSONDataError, KeyError):
115 """ Error on key.""" 116
117 - def __init__(self, *arg):
118 JSONDataError.__init__(self, *arg) 119 JSONDataError.fetch(self, *arg) 120 KeyError.__init__(self, self.s)
121
122 - def __str__(self):
123 return "JSONDataPathError:" + self.s
124 125
126 -class JSONDataNodeError(JSONDataError):
127 """ Error on node, slightly different from key.""" 128
129 - def __str__(self):
130 return "JSONDataNodeError:" + self.s
131 132
133 -class JSONDataNodeTypeError(JSONDataError):
134 """ Error on NodeTypes.""" 135
136 - def __str__(self):
137 return "JSONDataNodeTypeError:" + self.s
138 139
140 -class JSONDataParameterError(JSONDataError):
141 """ Erroneous parameters.""" 142
143 - def __str__(self):
144 return "JSONDataParameterError:" + self.s
145 146
147 -class JSONDataSourceFileError(JSONDataError):
148 """ Error on read of a source file.""" 149
150 - def __str__(self):
151 return "JSONDataSourceFileError:" + self.s
152 153
154 -class JSONDataModeError(JSONDataError):
155 """ Type error of source file content.""" 156
157 - def __str__(self):
158 return "JSONDataModeError:" + self.s
159 160
161 -class JSONDataTargetFileError(JSONDataError):
162 """ Error on writing a file.""" 163
164 - def __str__(self):
165 return "JSONDataTargetFileError:" + self.s
166 167
168 -class JSONDataValueError(JSONDataError):
169 """ Error on a value.""" 170
171 - def __str__(self):
172 return "JSONDataValueError:" + self.s
173 174
175 -class JSONDataAmbiguityError(Exception):
176 """ Error ambiguity of provided parameters.""" 177
178 - def __init__(self, requested, *sources):
179 if _interactive: 180 self.s = "Ambiguious input for:\n " + str(requested) 181 for sx in sources: 182 self.s += "\n " + str(sx) 183 else: 184 self.s = "Ambiguious input for:" + str(requested) 185 for sx in sources: 186 self.s += ":" + str(sx) 187 Exception.__init__(self, self.s)
188
189 - def __str__(self):
190 return "JSONDataAmbiguityError:" + self.s
191 192
193 -class JSONPointerError(JSONDataError):
194 """ Pointer error.""" 195 pass
196 197
198 -class JSONPointerTypeError(JSONDataError):
199 """ Pointer type error, the JSON pointer syntax does not represent a valid pointer.""" 200 pass
201 202
203 -class JSONTypeError(JSONDataError):
204 """ Pointer error.""" 205 pass
206 207
208 -class JSONDiffError(JSONDataError):
209 """Error in JSONDiff.""" 210 pass
211 212
213 -class JSONSearchError(JSONDataError):
214 """Error in JSONSearch.""" 215 pass
216 217
218 -class JSONDataPatchError(JSONDataError):
219 pass
220 221
222 -class JSONDataPatchItemError(JSONDataPatchError):
223 pass
224 225 226 # 227 # mode of operations 228 # 229 MJ_RFC4627 = 1 #: The first JSON RFC. 230 MJ_RFC7493 = 2 #: The IJSON RFC. 231 MJ_RFC7159 = 2 #: The JSON RFC by 'now'. 232 MJ_RFC8259 = 4 #: The JSON RFC by 'now'. 233 MJ_ECMA404 = 16 #: The first JSON EMCMA standard. 234 MJ_RFC6901 = 32 #: JSONPointer first IETF RFC. 235 MJ_RELPOINTERD1 = 64 #: JSONPointer - relative pointer Draft-1. 236 MJ_RFC6902 = 128 #: JSONPatch first IETF RFC. 237 MJ_DEFAULT = MJ_RFC7159 238 239 # 240 # validation of schemes 241 # 242 MS_OFF = 40 #: No validation. 243 MS_DRAFT3 = 43 #: The first supported JSONSchema IETF-Draft. 244 MS_DRAFT4 = 44 #: The current supported JSONSchema IETF-Draft. 245 MS_ON = MS_DRAFT4 #: The current when the default is activated. 246 MODE_SCHEMA_DEFAULT = MS_OFF #: The current default validation mode. 247 248 str2mj = { 249 "rfc4627": MJ_RFC4627, 250 "rfc7493": MJ_RFC7493, 251 "rfc7159": MJ_RFC7159, 252 "rfc8259": MJ_RFC8259, 253 "relpointerD1": MJ_RELPOINTERD1, 254 "ecma404": MJ_ECMA404, 255 "rfc6901": MJ_RFC6901, 256 "rfc6902": MJ_RFC6902, 257 "oss": MS_OFF, 258 str(MJ_RFC4627): MJ_RFC4627, 259 str(MJ_RFC7493): MJ_RFC7493, 260 str(MJ_RFC7159): MJ_RFC7159, 261 str(MJ_RFC8259): MJ_RFC8259, 262 str(MJ_ECMA404): MJ_ECMA404, 263 str(MJ_RFC6901): MJ_RFC6901, 264 str(MJ_RELPOINTERD1): MJ_RELPOINTERD1, 265 str(MJ_RFC6902): MJ_RFC6902, 266 str(MS_OFF): MS_OFF, 267 MJ_RFC4627: MJ_RFC4627, 268 MJ_RFC7493: MJ_RFC7493, 269 MJ_RFC7159: MJ_RFC7159, 270 MJ_ECMA404: MJ_ECMA404, 271 MJ_RFC6901: MJ_RFC6901, 272 MJ_RFC6902: MJ_RFC6902, 273 MS_OFF: MS_OFF, 274 } 275 mj2str = { 276 MJ_RFC4627: "rfc4627", 277 MJ_RFC7493: "rfc7493", 278 MJ_RFC7159: "rfc7159", 279 MJ_RFC8259: "rfc8259", 280 MJ_RELPOINTERD1: "relpointerD1", 281 MJ_ECMA404: "ecma404", 282 MJ_RFC6901: "rfc6901", 283 MJ_RFC6902: "rfc6902", 284 MS_OFF: "off", 285 } 286 287 288 289 290 # 291 # match criteria for node comparison 292 # 293 MATCH_INSERT = 0 #: for dicts 294 MATCH_NO = 1 #: negates the whole set 295 MATCH_KEY = 2 #: for dicts 296 MATCH_CHLDATTR = 3 #: for dicts and lists 297 MATCH_INDEX = 4 #: for lists 298 MATCH_MEM = 5 #: for dicts(value) and lists 299 MATCH_NEW = 6 #: If not present create a new, else ignore and keep present untouched. 300 MATCH_PRESENT = 7 #: Check all are present, else fails. 301 302 303 # 304 # application constraints and types for branch operations :: 305 # res = a OP b 306 # 307 B_ALL = 0 #: OP-On-Branches: process in any case. 308 B_AND = 1 #: OP-On-Branches: process only when both present. 309 B_OR = 2 #: OP-On-Branches: process if one at all is present. 310 B_XOR = 4 #: OP-On-Branches: process if only one is present. 311 312 B_ADD = 8 #: OP-On-Branches: add in accordance to RFC6902 313 B_MOD = 16 #: OP-On-Branches: modulo of branches. 314 B_SUB = 32 #: OP-On-Branches: subtract branches. 315 316 # 317 # copy property 318 # 319 C_REF = 0 #: OP-Copy: Copy reference. 320 C_DEEP = 1 #: OP-Copy: Copy deep. 321 C_SHALLOW = 2 #: OP-Copy: Copy shallow. 322 C_DEFAULT = C_REF #: Default value. 323 324 325 # 326 # application-scope 327 # 328 SC_DATA = 0 #: OP-Scope: the managed JSON data only 329 SC_SCHEMA = 1 #: OP-Scope: the managed JSON schema only 330 SC_JSON = 2 #: OP-Scope: the managed JSON data and schema only. 331 SC_OBJ = 3 #: OP-Scope: the attributes of current instance. 332 SC_ALL = 4 #: OP-Scope: the complete object, including data. 333 334 335 # 336 # data-scope 337 # 338 SD_BOTH = 0 #: Apply on mixed input and output data. 339 SD_INPUT = 1 #: Apply on input data. 340 SD_OUTPUT = 2 #: Apply on output data. 341 342 343 # 344 # sort order 345 # 346 S_NONE = 0 # no sort 347 S_SIMPLE = 1 # goups upper lower 348 349 350 # 351 # return types 352 # 353 R_OBJ = 0 #: Return object of type self. 354 R_DATA = 1 #: Return self.data. 355 R_JDATA = 2 #: Return object of type JSONData. 356 357 # 358 # types of return values 359 # 360 RT_STR = 1 #: string - 'str' or 'unicode' 361 RT_DICT = 2 #: dict 362 RT_LST = 4 #: list 363 RT_JSONPOINTER = 8 #: JSONPointer 364 RT_JSONPATCH= 16 #: JSONPatch 365 RT_JSONDATA = 32 #: JSONData 366 RT_DEFAULT = RT_JSONPOINTER #: default 367 rtypes2num = { 368 'RT_DICT': RT_DICT, 369 'RT_JSONDATA': RT_JSONDATA, 370 'RT_JSONPATCH': RT_JSONPATCH, 371 'RT_JSONPOINTER': RT_JSONPOINTER, 372 'RT_LST': RT_LST, 373 'RT_STR': RT_STR, 374 'default': RT_DEFAULT, 375 'dict': RT_DICT, 376 'jdata': RT_JSONPATCH, 377 'jpatch': RT_JSONDATA, 378 'jpointer': RT_JSONPOINTER, 379 'list': RT_LST, 380 'str': RT_STR, 381 None: RT_DEFAULT, 382 RT_DEFAULT: RT_DEFAULT, 383 RT_DICT: RT_DICT, 384 RT_JSONDATA: RT_JSONDATA, 385 RT_JSONPATCH: RT_JSONPATCH, 386 RT_JSONPOINTER: RT_JSONPOINTER, 387 RT_LST: RT_LST, 388 RT_STR: RT_STR, 389 } 390 391 # 392 # match sets 393 # 394 M_FIRST = 1 #: First match only. 395 M_LAST = 2 #: Last match only. 396 M_ALL = 4 #: All matches. 397 398 399 # 400 # verify pointers against jsondata 401 # 402 V_NONE = 1 #: no checks at all 403 V_FINAL = 2 #: checks final result only 404 V_STEPS = 4 #: checks each intermediate directory 405 V_DEFAULT = V_NONE #: default 406 verify2num = { 407 'V_DEFAULT': V_DEFAULT, 408 'V_FINAL': V_FINAL, 409 'V_NONE': V_NONE, 410 'V_STEPS': V_STEPS, 411 'default': V_DEFAULT, 412 'final': V_FINAL, 413 'none': V_NONE, 414 'steps': V_STEPS, 415 None: V_DEFAULT, 416 V_DEFAULT: V_DEFAULT, 417 V_FINAL: V_FINAL, 418 V_NONE: V_NONE, 419 V_STEPS: V_STEPS, 420 } 421 422 # 423 # display formats for JSONDiff 424 # 425 DF_SUMUP = 0 # short list 426 DF_CSV = 1 # csv, for now semicolon only 427 DF_JSON = 3 # JSON struture 428 DF_TABLE = 4 # table, for now fixed 429 DF_REVIEW = 5 # short for quick review format 430 DF_REPR = 6 # repr() - raw string, Python syntax 431 DF_STR = 7 # str() - formatted string, Python syntax 432 433 str2df = { 434 'sumup': DF_SUMUP, 435 'csv': DF_CSV, 436 'json': DF_JSON, 437 'review': DF_REVIEW, 438 'repr': DF_REPR, 439 'str': DF_STR, 440 'tabble': DF_TABLE, 441 str(DF_SUMUP): DF_SUMUP, 442 str(DF_CSV): DF_CSV, 443 str(DF_JSON): DF_JSON, 444 str(DF_REVIEW): DF_REVIEW, 445 str(DF_REPR): DF_REPR, 446 str(DF_STR): DF_STR, 447 str(DF_TABLE): DF_TABLE, 448 DF_SUMUP: DF_SUMUP, 449 DF_CSV: DF_CSV, 450 DF_JSON: DF_JSON, 451 DF_REVIEW: DF_REVIEW, 452 DF_REPR: DF_REPR, 453 DF_STR: DF_STR, 454 DF_TABLE: DF_TABLE, 455 } 456 df2str = { 457 DF_SUMUP: 'sumup', 458 DF_CSV: 'csv', 459 DF_JSON: 'json', 460 DF_REVIEW: 'review', 461 DF_REPR: 'repr', 462 DF_STR: 'str', 463 DF_TABLE: 'tabble', 464 } 465 466 # 467 # display formats for JSONData 468 # 469 PJ_TREE = 0 #: tree view JSON syntax 470 PJ_FLAT = 1 #: flat print JSON syntax 471 PJ_PYTREE = 2 #: tree view Python syntax 472 PJ_PYFLAT = 3 #: flat print Python syntax 473 PJ_REPR = 4 #: repr() - raw string, Python syntax 474 PJ_STR = 5 #: str() - formatted string, Python syntax 475 str2pj = { 476 'tree': PJ_TREE, 477 'flat': PJ_FLAT, 478 'pytree': PJ_PYTREE, 479 'pyflat': PJ_PYFLAT, 480 'repr': PJ_REPR, 481 'str': PJ_STR, 482 str(PJ_TREE): PJ_TREE, 483 str(PJ_FLAT): PJ_FLAT, 484 str(PJ_PYTREE): PJ_PYTREE, 485 str(PJ_PYFLAT): PJ_PYFLAT, 486 str(PJ_REPR): PJ_REPR, 487 str(PJ_STR): PJ_STR, 488 PJ_TREE: PJ_TREE, 489 PJ_FLAT: PJ_FLAT, 490 PJ_PYTREE: PJ_PYTREE, 491 PJ_PYFLAT: PJ_PYFLAT, 492 PJ_REPR: PJ_REPR, 493 PJ_STR: PJ_STR, 494 } 495 pj2str = { 496 PJ_TREE: 'tree', 497 PJ_FLAT: 'flat', 498 PJ_PYTREE: 'pytree', 499 PJ_PYFLAT: 'pyflat', 500 PJ_REPR: 'repr', 501 PJ_STR: 'str', 502 } 503 504 505 # 506 # Notation at the API - in/out. 507 # 508 NOTATION_NATIVE = 0 #: JSON notation in accordance to RFC7159 509 NOTATION_JSON = 1 #: JSON notation in accordance to RFC7159 510 NOTATION_JSON_REL = 2 #: JSON notation as relative pointer 511 NOTATION_HTTP_FRAGMENT = 3 #: JSON notation in accordance to RFC7159 with RFC3986. 512 513 514 # 515 # character display 516 # 517 CHARS_RAW = 0 #: display character set as raw 518 CHARS_STR = 1 #: display character set as str 519 CHARS_UTF = 2 #: display character set as utf 520 521 522 # 523 # line handling for overflow 524 # 525 LINE_CUT = 0 #: force line fit 526 LINE_WRAP = 1 #: wrap line in order to fit to length 527 528 529 # 530 # search parameters 531 # 532 SEARCH_FIRST = 0 #: Break display after first match. 533 SEARCH_ALL = 1 #: List all matches. 534 535 536 # 537 # pointer style 538 # 539 PT_PATH = 0 #: Displays a list of items. 540 PT_RFC6901 = 1 #: Displays rfc6901 strings. 541 PT_NODE = 2 #: Displays the node. 542 543 544 # 545 # json syntax 546 # 547 JSYN_NATIVE = 1 #: Literally in accordance to standards. 548 JSYN_PYTHON = 2 #: Python in-memory syntax representation. 549 550 551 552 # maps match strings and enums onto match-enums 553 match2match ={ 554 'child_attr_list': MATCH_CHLDATTR, 555 'index': MATCH_INDEX, 556 'key': MATCH_KEY, 557 'mem': MATCH_MEM, 558 'new': MATCH_NEW, 559 'no': MATCH_NO, 560 'present': MATCH_PRESENT, 561 MATCH_CHLDATTR: MATCH_CHLDATTR, 562 MATCH_INDEX: MATCH_INDEX, 563 MATCH_KEY: MATCH_KEY, 564 MATCH_MEM: MATCH_MEM, 565 MATCH_NEW: MATCH_NEW, 566 MATCH_NO: MATCH_NO, 567 MATCH_PRESENT: MATCH_PRESENT, 568 } 569 570 571 # maps mode strings and enums onto mde-enums 572 mode2mj = { 573 'default': MJ_RFC7159, 574 'ecma404': MJ_RFC8259, 575 'rfc4627': MJ_RFC4627, 576 'rfc7159': MJ_RFC7159, 577 'rfc7493': MJ_RFC7493, 578 'rfc8259': MJ_RFC8259, 579 MJ_ECMA404: MJ_RFC8259, 580 MJ_RFC4627: MJ_RFC4627, 581 MJ_RFC7159: MJ_RFC7159, 582 MJ_RFC7493: MJ_RFC7493, 583 MJ_RFC8259: MJ_RFC8259, 584 } 585 586 587 # maps validator strings and enums onto validator-enums 588 validator2ms = { 589 'default': MS_DRAFT4, 590 'draft3': MS_DRAFT3, 591 'off': MS_OFF, 592 MS_DRAFT3: MS_DRAFT3, 593 MS_DRAFT4: MS_DRAFT4, 594 MS_OFF: MS_OFF, 595 } 596 597 598 # maps copy strings and enums onto copy-enums 599 copy2c = { 600 'deep': C_DEEP, 601 'default': C_REF, 602 'ref': C_REF, 603 'shallow': C_SHALLOW, 604 C_DEEP: C_DEEP, 605 C_REF: C_REF, 606 C_SHALLOW: C_SHALLOW, 607 } 608 609 610 # 611 # for now hard-coded 612 # 613 consolewidth = 80 614 615 # 616 # misc 617 # 618 _verbose = 0 # common verbose variable 619 _debug = 0 # common verbose variable 620