1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
|
import os
import unittest
from jc.exceptions import ParseError
import jc.parsers.asciitable
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
class MyTests(unittest.TestCase):
def test_asciitable_nodata(self):
"""
Test 'asciitable' with no data
"""
self.assertEqual(jc.parsers.asciitable.parse('', quiet=True), [])
def test_asciitable_m_pure_ascii(self):
"""
Test 'asciitable' with a pure ASCII table
"""
input = '''
+========+========+========+========+========+========+========+
| type | tota | used | fr ee | shar | buff | avai |
+========+========+========+========+========+========+========+
| Mem | 3861 | 2228 | 3364 | 1183 | 2743 | 3389 |
+--------+--------+--------+--------+--------+--------+--------+
| | | | | test 2 | | |
+--------+--------+--------+--------+--------+--------+--------+
| last | last | last | ab cde | | | final |
+========+========+========+========+========+========+========+
'''
expected = [
{
"type": "Mem",
"tota": "3861",
"used": "2228",
"fr_ee": "3364",
"shar": "1183",
"buff": "2743",
"avai": "3389"
},
{
"type": None,
"tota": None,
"used": None,
"fr_ee": None,
"shar": "test 2",
"buff": None,
"avai": None
},
{
"type": "last",
"tota": "last",
"used": "last",
"fr_ee": "ab cde",
"shar": None,
"buff": None,
"avai": "final"
}
]
self.assertEqual(jc.parsers.asciitable.parse(input, quiet=True), expected)
def test_asciitable_m_unicode(self):
"""
Test 'asciitable' with a unicode table
"""
input = '''
╒════════╤════════╤════════╤════════╤════════╤════════╤════════╕
│ type │ total │ used │ fr ee │ shar │ buff │ avai │
╞════════╪════════╪════════╪════════╪════════╪════════╪════════╡
│ Mem │ 3861 │ 2228 │ 3364 │ 1183 │ 2743 │ 3389 │
├────────┼────────┼────────┼────────┼────────┼────────┼────────┤
│ Swap │ 2097 │ 0 │ 2097 │ │ │ │
├────────┼────────┼────────┼────────┼────────┼────────┼────────┤
│ last │ last │ last │ ab cde │ │ │ final │
╘════════╧════════╧════════╧════════╧════════╧════════╧════════╛
'''
expected = [
{
"type": "Mem",
"total": "3861",
"used": "2228",
"fr_ee": "3364",
"shar": "1183",
"buff": "2743",
"avai": "3389"
},
{
"type": "Swap",
"total": "2097",
"used": "0",
"fr_ee": "2097",
"shar": None,
"buff": None,
"avai": None
},
{
"type": "last",
"total": "last",
"used": "last",
"fr_ee": "ab cde",
"shar": None,
"buff": None,
"avai": "final"
}
]
self.assertEqual(jc.parsers.asciitable.parse(input, quiet=True), expected)
def test_asciitable_pure_ascii_extra_spaces(self):
"""
Test 'asciitable' with a pure ASCII table that has heading and
trailing spaces and newlines.
"""
input = '''
+========+========+========+========+========+========+========+
| type | total | used | fr ee | shar | buff | avai
+========+========+========+========+========+========+========+
| Mem | 3861 | 2228 | 3364 | 1183 | 2743 | 3389
+--------+--------+--------+--------+--------+--------+--------+
| | | | test 2 | |
+--------+--------+--------+--------+--------+--------+--------+
| last | last | last | ab cde | | | final |
+========+========+========+========+========+========+========+
'''
expected = [
{
"type": "Mem",
"total": "3861",
"used": "2228",
"fr_ee": "3364",
"shar": "1183",
"buff": "2743",
"avai": "3389"
},
{
"type": None,
"total": None,
"used": None,
"fr_ee": None,
"shar": "test 2",
"buff": None,
"avai": None
},
{
"type": "last",
"total": "last",
"used": "last",
"fr_ee": "ab cde",
"shar": None,
"buff": None,
"avai": "final"
}
]
self.assertEqual(jc.parsers.asciitable.parse(input, quiet=True), expected)
def test_asciitable_unicode_extra_spaces(self):
"""
Test 'asciitable' with a pure ASCII table that has heading and
trailing spaces and newlines.
"""
input = '''
╒════════╤════════╤════════╤════════╤════════╤════════╤════════╕
type │ total │ used │ free │ shar │ buff │ avai
╞════════╪════════╪════════╪════════╪════════╪════════╪════════╡
Mem │ 3861 │ 2228 │ 3364 │ 1183 │ 2743 │ 3389
├────────┼────────┼────────┼────────┼────────┼────────┼────────┤
Swap │ 2097 │ 0 │ 2097 │ │ │
╘════════╧════════╧════════╧════════╧════════╧════════╧════════╛
'''
expected = [
{
"type": "Mem",
"total": "3861",
"used": "2228",
"free": "3364",
"shar": "1183",
"buff": "2743",
"avai": "3389"
},
{
"type": "Swap",
"total": "2097",
"used": "0",
"free": "2097",
"shar": None,
"buff": None,
"avai": None
}
]
self.assertEqual(jc.parsers.asciitable.parse(input, quiet=True), expected)
def test_asciitable_markdown(self):
"""
Test 'asciitable' with a markdown table
"""
input = '''
| type | total | used | free | shared | buff cache | available |
|--------|---------|--------|---------|----------|--------------|-------------|
| Mem | 3861332 | 222820 | 3364176 | 11832 | 274336 | 3389588 |
| Swap | 2097148 | 0 | 2097148 | | | |
'''
expected = [
{
"type": "Mem",
"total": "3861332",
"used": "222820",
"free": "3364176",
"shared": "11832",
"buff_cache": "274336",
"available": "3389588"
},
{
"type": "Swap",
"total": "2097148",
"used": "0",
"free": "2097148",
"shared": None,
"buff_cache": None,
"available": None
}
]
self.assertEqual(jc.parsers.asciitable.parse(input, quiet=True), expected)
def test_asciitable_simple(self):
"""
Test 'asciitable' with a simple table
"""
input = '''
type total used free shared buff cache available
------ ------- ------ ------- -------- ------------ -----------
Mem 3861332 222820 3364176 11832 274336 3389588
Swap 2097148 0 2097148
'''
expected = [
{
"type": "Mem",
"total": "3861332",
"used": "222820",
"free": "3364176",
"shared": "11832",
"buff_cache": "274336",
"available": "3389588"
},
{
"type": "Swap",
"total": "2097148",
"used": "0",
"free": "2097148",
"shared": None,
"buff_cache": None,
"available": None
}
]
self.assertEqual(jc.parsers.asciitable.parse(input, quiet=True), expected)
def test_asciitable_pretty_ansi(self):
"""
Test 'asciitable' with a pretty table with ANSI codes
"""
input = '''┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓ \n ┃\x1b[1m \x1b[0m\x1b[1mReleased \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1mTitle \x1b[0m\x1b[1m \x1b[0m┃\x1b[1m \x1b[0m\x1b[1m Box Office\x1b[0m\x1b[1m \x1b[0m┃ \n ┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━┩ \n │\x1b[36m \x1b[0m\x1b[36mDec 20, 2019\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mStar Wars: The Rise of Skywalker \x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m $952,110,690\x1b[0m\x1b[32m \x1b[0m│ \n │\x1b[36m \x1b[0m\x1b[36mMay 25, 2018\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mSolo: A Star Wars Story \x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m $393,151,347\x1b[0m\x1b[32m \x1b[0m│ \n │\x1b[36m \x1b[0m\x1b[36mDec 15, 2017\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mStar Wars Ep. V111: The Last Jedi\x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m$1,332,539,889\x1b[0m\x1b[32m \x1b[0m│ \n │\x1b[36m \x1b[0m\x1b[36mDec 16, 2016\x1b[0m\x1b[36m \x1b[0m│\x1b[35m \x1b[0m\x1b[35mRogue One: A Star Wars Story \x1b[0m\x1b[35m \x1b[0m│\x1b[32m \x1b[0m\x1b[32m$1,332,439,889\x1b[0m\x1b[32m \x1b[0m│ \n └──────────────┴───────────────────────────────────┴────────────────┘ \n'''
expected = [
{
"released": "Dec 20, 2019",
"title": "Star Wars: The Rise of Skywalker",
"box_office": "$952,110,690"
},
{
"released": "May 25, 2018",
"title": "Solo: A Star Wars Story",
"box_office": "$393,151,347"
},
{
"released": "Dec 15, 2017",
"title": "Star Wars Ep. V111: The Last Jedi",
"box_office": "$1,332,539,889"
},
{
"released": "Dec 16, 2016",
"title": "Rogue One: A Star Wars Story",
"box_office": "$1,332,439,889"
}
]
self.assertEqual(jc.parsers.asciitable.parse(input, quiet=True), expected)
def test_asciitable_special_chars_in_header(self):
"""
Test 'asciitable' with a pure ASCII table that has special
characters in the header. These should be converted to underscores
and no trailing or consecutive underscores should end up in the
resulting key names.
"""
input = '''
Protocol Address Age (min) Hardware Addr Type Interface
Internet 10.12.13.1 98 0950.5785.5cd1 ARPA FastEthernet2.13
Internet 10.12.13.3 131 0150.7685.14d5 ARPA GigabitEthernet2.13
Internet 10.12.13.4 198 0950.5C8A.5c41 ARPA GigabitEthernet2.17
'''
expected = [
{
"protocol": "Internet",
"address": "10.12.13.1",
"age_min": "98",
"hardware_addr": "0950.5785.5cd1",
"type": "ARPA",
"interface": "FastEthernet2.13"
},
{
"protocol": "Internet",
"address": "10.12.13.3",
"age_min": "131",
"hardware_addr": "0150.7685.14d5",
"type": "ARPA",
"interface": "GigabitEthernet2.13"
},
{
"protocol": "Internet",
"address": "10.12.13.4",
"age_min": "198",
"hardware_addr": "0950.5C8A.5c41",
"type": "ARPA",
"interface": "GigabitEthernet2.17"
}
]
self.assertEqual(jc.parsers.asciitable.parse(input, quiet=True), expected)
def test_asciitable_no_lower_raw(self):
"""
Test 'asciitable' with a pure ASCII table that has special
characters and mixed case in the header. These should be converted to underscores
and no trailing or consecutive underscores should end up in the
resulting key names. Using `raw` in this test to preserve case. (no lower)
"""
input = '''
Protocol Address Age (min) Hardware Addr Type Interface
Internet 10.12.13.1 98 0950.5785.5cd1 ARPA FastEthernet2.13
Internet 10.12.13.3 131 0150.7685.14d5 ARPA GigabitEthernet2.13
Internet 10.12.13.4 198 0950.5C8A.5c41 ARPA GigabitEthernet2.17
'''
expected = [
{
"Protocol": "Internet",
"Address": "10.12.13.1",
"Age_min": "98",
"Hardware_Addr": "0950.5785.5cd1",
"Type": "ARPA",
"Interface": "FastEthernet2.13"
},
{
"Protocol": "Internet",
"Address": "10.12.13.3",
"Age_min": "131",
"Hardware_Addr": "0150.7685.14d5",
"Type": "ARPA",
"Interface": "GigabitEthernet2.13"
},
{
"Protocol": "Internet",
"Address": "10.12.13.4",
"Age_min": "198",
"Hardware_Addr": "0950.5C8A.5c41",
"Type": "ARPA",
"Interface": "GigabitEthernet2.17"
}
]
self.assertEqual(jc.parsers.asciitable.parse(input, raw=True, quiet=True), expected)
def test_asciitable_centered_col_header(self):
"""
Test 'asciitable' with long centered column header which can break
column alignment
"""
input = '''
+---------+--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------+------------------+
| fdc_id | data_type | description | food_category_id | publication_date |
+---------+--------------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------+------------------+
| 167512 | sr_legacy_food | Pillsbury Golden Layer Buttermilk Biscuits, Artificial Flavor, refrigerated dough | | 2019-04-01 |
| 167513 | sr_legacy_food | Pillsbury, Cinnamon Rolls with Icing, refrigerated dough | | 2019-04-01 |
| 167514 | sr_legacy_food | Kraft Foods, Shake N Bake Original Recipe, Coating for Pork, dry | | 2019-04-01 |
| 167515 | sr_legacy_food | George Weston Bakeries, Thomas English Muffins | | 2019-04-01 |
| 167516 | sr_legacy_food | Waffles, buttermilk, frozen, ready-to-heat | | 2019-04-01 |
| 167517 | sr_legacy_food | Waffle, buttermilk, frozen, ready-to-heat, toasted | | 2019-04-01 |
| 167518 | sr_legacy_food | Waffle, buttermilk, frozen, ready-to-heat, microwaved | | 2019-04-01 |
'''
expected = [
{
"fdc_id": "167512",
"data_type": "sr_legacy_food",
"description": "Pillsbury Golden Layer Buttermilk Biscuits, Artificial Flavor, refrigerated dough",
"food_category_id": None,
"publication_date": "2019-04-01"
},
{
"fdc_id": "167513",
"data_type": "sr_legacy_food",
"description": "Pillsbury, Cinnamon Rolls with Icing, refrigerated dough",
"food_category_id": None,
"publication_date": "2019-04-01"
},
{
"fdc_id": "167514",
"data_type": "sr_legacy_food",
"description": "Kraft Foods, Shake N Bake Original Recipe, Coating for Pork, dry",
"food_category_id": None,
"publication_date": "2019-04-01"
},
{
"fdc_id": "167515",
"data_type": "sr_legacy_food",
"description": "George Weston Bakeries, Thomas English Muffins",
"food_category_id": None,
"publication_date": "2019-04-01"
},
{
"fdc_id": "167516",
"data_type": "sr_legacy_food",
"description": "Waffles, buttermilk, frozen, ready-to-heat",
"food_category_id": None,
"publication_date": "2019-04-01"
},
{
"fdc_id": "167517",
"data_type": "sr_legacy_food",
"description": "Waffle, buttermilk, frozen, ready-to-heat, toasted",
"food_category_id": None,
"publication_date": "2019-04-01"
},
{
"fdc_id": "167518",
"data_type": "sr_legacy_food",
"description": "Waffle, buttermilk, frozen, ready-to-heat, microwaved",
"food_category_id": None,
"publication_date": "2019-04-01"
}
]
self.assertEqual(jc.parsers.asciitable.parse(input, quiet=True), expected)
if __name__ == '__main__':
unittest.main()
|