Get a flat -- i.e., JSON-ish -- representation of a beets Item or Album object. For Albums, `expand` dictates whether tracks are included.
(obj, expand=False)
| 44 | |
| 45 | |
| 46 | def _rep(obj, expand=False): |
| 47 | """Get a flat -- i.e., JSON-ish -- representation of a beets Item or |
| 48 | Album object. For Albums, `expand` dictates whether tracks are |
| 49 | included. |
| 50 | """ |
| 51 | out = dict(obj) |
| 52 | |
| 53 | if isinstance(obj, beets.library.Item): |
| 54 | if app.config.get("INCLUDE_PATHS", False): |
| 55 | out["path"] = util.displayable_path(out["path"]) |
| 56 | else: |
| 57 | del out["path"] |
| 58 | |
| 59 | # Filter all bytes attributes and convert them to strings. |
| 60 | for key, value in out.items(): |
| 61 | if isinstance(out[key], bytes): |
| 62 | out[key] = base64.b64encode(value).decode("ascii") |
| 63 | |
| 64 | # Get the size (in bytes) of the backing file. This is useful |
| 65 | # for the Tomahawk resolver API. |
| 66 | try: |
| 67 | out["size"] = os.path.getsize(util.syspath(obj.path)) |
| 68 | except OSError: |
| 69 | out["size"] = 0 |
| 70 | |
| 71 | return out |
| 72 | |
| 73 | if isinstance(obj, beets.library.Album): |
| 74 | if app.config.get("INCLUDE_PATHS", False): |
| 75 | out["artpath"] = util.displayable_path(out["artpath"]) |
| 76 | else: |
| 77 | del out["artpath"] |
| 78 | if expand: |
| 79 | out["items"] = [_rep(item) for item in obj.items()] |
| 80 | return out |
| 81 | return None |
| 82 | |
| 83 | |
| 84 | def json_generator(items, root, expand=False): |
no test coverage detected