Extracted conversion between objects and json data into class JsonClass

master
Felix Stupp 6 years ago
parent 7cf491a37f
commit d1dfaa2201
Signed by: zocker
GPG Key ID: 93E1BD26F6B02FB7

@ -12,10 +12,29 @@ import xdg.BaseDirectory
APP_NAME = "tinytinypy" # For default config directories APP_NAME = "tinytinypy" # For default config directories
# Helpers # Helper Class
def jsonToObj(cls, dataDict):
transDict = cls.TRANS class JsonClass:
return cls(**{transDict.get(key, key): val for key, val in dataDict.items() if transDict.get(key, key) is not None}) TRANS_EXP = {
"same_key": True,
"class_key": "json_key",
"ignore_class_key": None,
}
@classmethod
def getClassKey(cls, jsonKey):
for key, val in cls.TRANS.items():
if val == jsonKey or (key == jsonKey and val == True):
return key
return None
@classmethod
def getJsonKey(cls, classKey):
val = cls.TRANS.get(classKey, None)
return classKey if val == True else val
@classmethod
def fromJson(cls, dataDict):
return cls(**{cls.getClassKey(key): val for key, val in dataDict.items() if cls.getClassKey(key) is not None and val is not None})
def toJson(self):
return {self.__class__.getJsonKey(key): val for key, val in self.__dict__.items() if self.__class__.getJsonKey(key) is not None and val is not None}
# Class Invalid # Class Invalid
class TtRssCounters: class TtRssCounters:
@ -25,29 +44,28 @@ class TtRssCounters:
self.categories = categories self.categories = categories
self.tags = tags self.tags = tags
class Category: class Category(JsonClass):
TRANS = { TRANS = {
"id": "catId", "catId": "id",
"order_id": "orderId", "title": True,
"unread": True,
"orderId": "order_id",
} }
def __init__(self, catId, title=None, unread=None, orderId=None): def __init__(self, catId, title=None, unread=None, orderId=None):
self.catId = catId self.catId = catId
self.title = title self.title = title
self.unread = unread self.unread = unread
self.orderId = orderId self.orderId = orderId
@classmethod
def fromJson(cls, jsonData):
#return cls(**jsonToInput(cls.TRANS, jsonData))
return jsonToObj(cls, jsonData)
class Feed: class Feed(JsonClass):
TRANS = { TRANS = {
"id": "feedId", "feedId": "id",
"feed_url": "url", "title": True,
"cat_id": "catId", "url": True,
"last_updated": "lastUpdated", "catId": "cat_id",
"order_id": "orderId", "unread": True,
"has_icon": None, "lastUpdated": "last_updated",
"orderId": "order_id",
} }
def __init__(self, feedId, title=None, url=None, catId=None, unread=None, lastUpdated=None, orderId=None): def __init__(self, feedId, title=None, url=None, catId=None, unread=None, lastUpdated=None, orderId=None):
self.feedId = feedId self.feedId = feedId
@ -57,22 +75,10 @@ class Feed:
self.unread = unread self.unread = unread
self.lastUpdated = lastUpdated self.lastUpdated = lastUpdated
self.orderId = orderId self.orderId = orderId
@classmethod
def fromJson(cls, jsonData):
#return cls(**jsonToInput(cls.TRANS, jsonData))
return jsonToObj(cls, jsonData)
def toJson(self):
#return objToJson(self)
pass
class Headline: class Headline(JsonClass):
TRANS = { OLD_TRANS = { # TODO Add missing values to class
"id": "headlineId",
"guid": None, "guid": None,
"is_updated": "isUpdated",
"link": "url",
"feed_id": "feedId",
"feed_title": "feedTitle",
"comments_count": None, "comments_count": None,
"comments_link": None, "comments_link": None,
"always_display_attachments": None, "always_display_attachments": None,
@ -82,6 +88,22 @@ class Headline:
"flavor_image": None, "flavor_image": None,
"flavor_stream": None, "flavor_stream": None,
} }
TRANS = {
"headlineId": "id",
"unread": True,
"marked": True,
"published": True,
"updated": True,
"isUpdated": "is_updated",
"title": True,
"url": "link",
"feedId": "feed_id",
"tags": True,
"labels": True,
"feedTitle": "feed_title",
"author": True,
"score": True,
}
def __init__(self, headlineId, unread, marked, published, updated, isUpdated, title, url, feedId, tags, labels, feedTitle, author, score): def __init__(self, headlineId, unread, marked, published, updated, isUpdated, title, url, feedId, tags, labels, feedTitle, author, score):
self.headlineId = headlineId self.headlineId = headlineId
self.unread = unread self.unread = unread
@ -97,10 +119,6 @@ class Headline:
self.feedTitle = feedTitle self.feedTitle = feedTitle
self.author = author self.author = author
self.score = score self.score = score
@classmethod
def fromJson(cls, jsonData):
#return cls(**jsonToInput(cls.TRANS, jsonData))
return jsonToObj(cls, jsonData)
class TtRssInstance: class TtRssInstance:
@ -236,7 +254,7 @@ def func_articles(server, args):
for h in headlines: for h in headlines:
print(h.url) print(h.url)
else: else:
print(json.dumps([h.objToJson() for h in headlines])) print(json.dumps([h.toJson() for h in headlines]))
def parser_articles(sub): def parser_articles(sub):
p = sub.add_parser('articles', help="Get articles") p = sub.add_parser('articles', help="Get articles")

Loading…
Cancel
Save