From c5827e3ba22ac8a2a8bfe7b40b5ffa1a9c826641 Mon Sep 17 00:00:00 2001 From: Hamad alghanim Date: Mon, 26 Sep 2016 23:34:15 +0300 Subject: [PATCH 01/10] following pep8 naming convention --- appz_scripts/appz_models.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/appz_scripts/appz_models.py b/appz_scripts/appz_models.py index 3ddae29..7e67947 100644 --- a/appz_scripts/appz_models.py +++ b/appz_scripts/appz_models.py @@ -1,10 +1,22 @@ #!/usr/bin/env python + + class Param: def __init__(self, paramsDict): self.type = paramsDict["type"] self.name = paramsDict["name"] self.isOptional = paramsDict["isOptional"] + def __str__(self): + return unicode(self) + + def __unicode__(self): + return "type: {}\nname: {}\nOptional: {}".format( + self.type, + self.name, + self.isOptional + ) + class Action: # TODO: finish actions @@ -35,7 +47,7 @@ def __str__(self): def __unicode__(self): # TODO: serialize actions - return "[INFO] name: {}\nurl: {}\nshceme: {}\nappstore Id: {}".format( + return "name: {}\nurl: {}\nshceme: {}\nappstore Id: {}".format( self.name, self.fallbackURL, self.scheme, From af4abd153c7a980199c9b60e7cbfdb62c07c40f4 Mon Sep 17 00:00:00 2001 From: Hamad alghanim Date: Mon, 26 Sep 2016 23:34:50 +0300 Subject: [PATCH 02/10] pep8 and Param class serialization --- appz_scripts/appz_models.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/appz_scripts/appz_models.py b/appz_scripts/appz_models.py index 7e67947..3af4f09 100644 --- a/appz_scripts/appz_models.py +++ b/appz_scripts/appz_models.py @@ -12,20 +12,20 @@ def __str__(self): def __unicode__(self): return "type: {}\nname: {}\nOptional: {}".format( - self.type, - self.name, - self.isOptional - ) + self.type, + self.name, + self.isOptional + ) class Action: # TODO: finish actions def __init__(self, actionDict): self.name = actionDict["name"] - self.params = self.createParams(actionDict["params"]) + self.params = self.create_params(actionDict["params"]) self.paths = actionDict["paths"] - def createParams(self, params): + def create_params(self, params): return [Param(param) for param in params] @@ -36,9 +36,9 @@ def __init__(self, jsonFile): self.fallbackURL = jsonFile["fallbackURL"] self.scheme = jsonFile["scheme"] self.appStoreId = jsonFile["appStoreId"] - self.actions = self.createActions(jsonFile["actions"]) + self.actions = self.create_actions(jsonFile["actions"]) - def createActions(self, actions): + def create_actions(self, actions): return [Action(action) for action in actions] # serlizaiton function @@ -48,7 +48,7 @@ def __str__(self): def __unicode__(self): # TODO: serialize actions return "name: {}\nurl: {}\nshceme: {}\nappstore Id: {}".format( - self.name, - self.fallbackURL, - self.scheme, - self.appStoreId) + self.name, + self.fallbackURL, + self.scheme, + self.appStoreId) From 1caa38084189517bc4dc840fe2b8919f0b9006f5 Mon Sep 17 00:00:00 2001 From: Hamad alghanim Date: Mon, 26 Sep 2016 23:58:20 +0300 Subject: [PATCH 03/10] function definition is now being generated --- appz_scripts/appz_models.py | 60 ++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/appz_scripts/appz_models.py b/appz_scripts/appz_models.py index 3af4f09..a027777 100644 --- a/appz_scripts/appz_models.py +++ b/appz_scripts/appz_models.py @@ -1,35 +1,61 @@ #!/usr/bin/env python -class Param: - def __init__(self, paramsDict): - self.type = paramsDict["type"] - self.name = paramsDict["name"] - self.isOptional = paramsDict["isOptional"] +class Parameter: + def __init__(self, paramters_dict): + self.type = paramters_dict["type"] + self.name = paramters_dict["name"] + self.isOptional = paramters_dict["isOptional"] + + def create_paramter_for_function(self): + optinal = "?" if self.isOptional else "" + return "{}: {}{}".format(self.name, self.type, optinal) def __str__(self): return unicode(self) def __unicode__(self): - return "type: {}\nname: {}\nOptional: {}".format( - self.type, - self.name, - self.isOptional - ) + return "name: {}\ntype: {}\nOptional: {}".format( + self.name, + self.type, + self.isOptional + ) class Action: # TODO: finish actions - def __init__(self, actionDict): - self.name = actionDict["name"] - self.params = self.create_params(actionDict["params"]) - self.paths = actionDict["paths"] + def __init__(self, action_dict): + self.name = action_dict["name"] + self.parameters = self.create_params(action_dict["params"]) + self.paths = action_dict["paths"] def create_params(self, params): - return [Param(param) for param in params] + return [Parameter(param) for param in params] + + def create_function_definition(self): + definition = "case {}(".format(self.name) + for parameter in self.parameters: + definition += parameter.create_paramter_for_function() + definition += "," + definition = definition[:-1] # remove tailing ',' character + definition += ")" + return definition + + # serlizaiton functions + def __str__(self): + return unicode(self) + + def __unicode__(self): + parameters = "\n".join(self.parameters) + paths = "\n".join(self.paths) + return "name: {}\nparameters: {}\npaths: {}".format( + self.name, + parameters, + paths + ) -class App: +class Application: # jsonFile: is a dictionary created from json file def __init__(self, jsonFile): self.name = jsonFile["name"] @@ -41,7 +67,7 @@ def __init__(self, jsonFile): def create_actions(self, actions): return [Action(action) for action in actions] - # serlizaiton function + # serlizaiton functions def __str__(self): return unicode(self) From 16d5915a4e0316180c6803864eafa38f1369b92e Mon Sep 17 00:00:00 2001 From: Hamad alghanim Date: Tue, 27 Sep 2016 00:25:02 +0300 Subject: [PATCH 04/10] forgot to add the file to the commit --- appz_scripts/templates/app_template.swift | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/appz_scripts/templates/app_template.swift b/appz_scripts/templates/app_template.swift index c14491e..0db53b4 100644 --- a/appz_scripts/templates/app_template.swift +++ b/appz_scripts/templates/app_template.swift @@ -25,15 +25,7 @@ public extension Applications.{{ app.name }} { public enum Action { {% for action in app.actions %} - case status(id: String) - case userHandle(String) - case userId(String) - case list(handle: String, slug: String) - case post(message: String, repliedStatusId: String?) - case search(query: String) - case timeline - case mentions - case messages + {{ action.create_function_definition() }} {% endfor %} } } From 8d5a16226d2cf80136537662b0219089a83cc521 Mon Sep 17 00:00:00 2001 From: Hamad alghanim Date: Wed, 28 Sep 2016 00:55:37 +0300 Subject: [PATCH 05/10] finishing up the models --- appz_scripts/appz_models.py | 47 ++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/appz_scripts/appz_models.py b/appz_scripts/appz_models.py index a027777..b463e5c 100644 --- a/appz_scripts/appz_models.py +++ b/appz_scripts/appz_models.py @@ -1,4 +1,22 @@ #!/usr/bin/env python +class Path: + def __init__(self, paths_dict): + app_path = paths_dict["app"]["path"] + app_query = paths_dict["app"]["query"] + web_path = paths_dict["web"]["path"] + web_query = paths_dict["web"]["query"] + + def __str__(self): + return unicode(self) + + def __unicode__(self): + return "app_path: {}\napp_query: {}\nweb_path: {}\nweb_query: {}"\ + .format( + self.app_path, + self.app_query, + self.web_path, + self.web_query + ) class Parameter: @@ -23,11 +41,10 @@ def __unicode__(self): class Action: - # TODO: finish actions def __init__(self, action_dict): self.name = action_dict["name"] self.parameters = self.create_params(action_dict["params"]) - self.paths = action_dict["paths"] + self.paths = Path(action_dict["paths"]) def create_params(self, params): return [Parameter(param) for param in params] @@ -41,6 +58,18 @@ def create_function_definition(self): definition += ")" return definition + def create_action(self): + action = "case .{}(".format(self.name) + for parameter in self.parameters: + action += "let {},".format(parameter.name) + if len(parameters) > 0: + action = action[:-1] # remove tailing , + action += "):\n" # finished enum definition + # enum implementaion starts here + action += "return ActionPaths(" + action += "app: Path(" + action += "pathComponents:" + # serlizaiton functions def __str__(self): return unicode(self) @@ -56,13 +85,13 @@ def __unicode__(self): class Application: - # jsonFile: is a dictionary created from json file - def __init__(self, jsonFile): - self.name = jsonFile["name"] - self.fallbackURL = jsonFile["fallbackURL"] - self.scheme = jsonFile["scheme"] - self.appStoreId = jsonFile["appStoreId"] - self.actions = self.create_actions(jsonFile["actions"]) + # json_file: is a dictionary created from json file + def __init__(self, json_file): + self.name = json_file["name"] + self.fallbackURL = json_file["fallbackURL"] + self.scheme = json_file["scheme"] + self.appStoreId = json_file["appStoreId"] + self.actions = self.create_actions(json_file["actions"]) def create_actions(self, actions): return [Action(action) for action in actions] From f22d1165e1a426babc456857a7b400be33d0fe9e Mon Sep 17 00:00:00 2001 From: Hamad alghanim Date: Wed, 28 Sep 2016 23:04:54 +0300 Subject: [PATCH 06/10] changed config structure and now swift files are being generated --- appz_scripts/appz_models.py | 97 ++++++++++++++++-- appz_scripts/templates/app_template.swift | 118 +--------------------- appz_scripts/twitter-spec.json | 6 +- 3 files changed, 93 insertions(+), 128 deletions(-) diff --git a/appz_scripts/appz_models.py b/appz_scripts/appz_models.py index b463e5c..d3bf384 100644 --- a/appz_scripts/appz_models.py +++ b/appz_scripts/appz_models.py @@ -1,10 +1,13 @@ #!/usr/bin/env python +import re + + class Path: def __init__(self, paths_dict): - app_path = paths_dict["app"]["path"] - app_query = paths_dict["app"]["query"] - web_path = paths_dict["web"]["path"] - web_query = paths_dict["web"]["query"] + self.app_path = paths_dict["app"]["path"] + self.app_query = paths_dict["app"]["query"] + self.web_path = paths_dict["web"]["path"] + self.web_query = paths_dict["web"]["query"] def __str__(self): return unicode(self) @@ -44,11 +47,18 @@ class Action: def __init__(self, action_dict): self.name = action_dict["name"] self.parameters = self.create_params(action_dict["params"]) - self.paths = Path(action_dict["paths"]) + self.path = Path(action_dict["paths"]) def create_params(self, params): return [Parameter(param) for param in params] + def get_parameter(self, parameter_string): + regex_query = re.findall('^{(.+?)}', parameter_string) + if len(regex_query) > 0: + return regex_query[0] + else: + return None + def create_function_definition(self): definition = "case {}(".format(self.name) for parameter in self.parameters: @@ -62,14 +72,81 @@ def create_action(self): action = "case .{}(".format(self.name) for parameter in self.parameters: action += "let {},".format(parameter.name) - if len(parameters) > 0: + if len(self.parameters) > 0: action = action[:-1] # remove tailing , action += "):\n" # finished enum definition # enum implementaion starts here - action += "return ActionPaths(" - action += "app: Path(" - action += "pathComponents:" - + action += "\t\treturn ActionPaths(\n" + action += "\t\t\tapp: Path(\n" + action += "\t\t\t\tpathComponents:[" + for app_path in self.path.app_path: + parameter = self.get_parameter(app_path) + if parameter is not None: + parameter_names = [param.name for param in self.parameters] + if parameter in parameter_names: + action += "{},".format(parameter) + else: + action += '"{}",'.format(app_path) + else: + action += '"{}",'.format(app_path) + if len(self.path.app_path) > 0: + action = action[:-1] + "],\n" # remove tailing , + else: + action += "],\n" + # pathComponents for app done here + action += "\t\t\t\tqueryParameters: [" + for key, value in self.path.app_query.iteritems(): + action += '"{}" : '.format(key) + parameter = self.get_parameter(value) + if parameter is not None: + parameter_names = [param.name for param in self.parameters] + if parameter in parameter_names: + action += "{},".format(parameter) + else: + action += '"{}",'.format(value) + else: + action += '"{}",'.format(value) + if len(self.path.app_query) > 0: + action = action[:-1] + "]),\n" # remove tailing , + else: + action += ":]),\n" + # queryParameters for app done here + + action += "\t\t\tweb: Path(\n" + action += "\t\t\t\tpathComponents:[" + for web_path in self.path.web_path: + parameter = self.get_parameter(web_path) + if parameter is not None: + parameter_names = [param.name for param in self.parameters] + if parameter in parameter_names: + action += "{},".format(parameter) + else: + action += '"{}",'.format(web_path) + else: + action += '"{}",'.format(web_path) + if len(self.path.web_path) > 0: + action = action[:-1] + "],\n" # remove tailing , + else: + action += "],\n" + # pathComponents for web done here + action += "\t\t\t\tqueryParameters: [" + for key, value in self.path.web_query.iteritems(): + action += '"{}":'.format(key) + parameter = self.get_parameter(value) + if parameter is not None: + parameter_names = [param.name for param in self.parameters] + if parameter in parameter_names: + action += "{},".format(parameter) + else: + action += '"{}",'.format(value) + else: + action += '"{}",'.format(value) + if len(self.path.web_query) > 0: + action = action[:-1] + "])," # remove tailing , + else: + action += ":]\n\t\t\t)\n\t\t)\n" + return action + # serlizaiton functions def __str__(self): return unicode(self) diff --git a/appz_scripts/templates/app_template.swift b/appz_scripts/templates/app_template.swift index 0db53b4..11a25c3 100644 --- a/appz_scripts/templates/app_template.swift +++ b/appz_scripts/templates/app_template.swift @@ -35,121 +35,9 @@ extension Applications.{{ app.name }}.Action: ExternalApplicationAction { public var paths: ActionPaths { switch self { - case .status(let id): - return ActionPaths( - app: Path( - pathComponents: ["status"], - queryParameters: ["id": id]), - web: Path( - pathComponents: ["statuses", id], - queryParameters: [:] - ) - ) - - case .userHandle(let handle): - return ActionPaths( - app: Path( - pathComponents: ["user"], - queryParameters: ["screen_name": handle] - ), - web: Path( - pathComponents: [handle], - queryParameters: [:] - ) - ) - - case .userId(let id): - return ActionPaths( - app: Path( - pathComponents: ["user"], - queryParameters: ["id": id] - ), - web: Path( - pathComponents: ["intent", "user"], - queryParameters: ["user_id": id] - ) - ) - - case .post(let message, let statusId): - return ActionPaths( - app: Path( - pathComponents: ["post"], - queryParameters: [ - "message": message, - "in_reply_to_status_id": statusId ?? "", - ] - ), - web: Path( - pathComponents: ["intent", "tweet"], - queryParameters: [ - "text": message, - "in_reply_to": statusId ?? "", - ] - ) - ) - - case .list(let handle, let slug): - return ActionPaths( - app: Path( - pathComponents: ["list"], - queryParameters: [ - "screen_name": handle, - "slug": slug, - ] - ), - web: Path( - pathComponents: [handle, "lists", slug], - queryParameters: [:] - ) - ) - - case .search(let query): - return ActionPaths( - app: Path( - pathComponents: ["search"], - queryParameters: ["query": query] - ), - web: Path( - pathComponents: ["search"], - queryParameters: ["q": query] - ) - ) - - case .timeline: - return ActionPaths( - app: Path( - pathComponents: ["timeline"], - queryParameters: [:] - ), - web: Path( - pathComponents: [], - queryParameters: [:] - ) - ) - - case .mentions: - return ActionPaths( - app: Path( - pathComponents: ["mentions"], - queryParameters: [:] - ), - web: Path( - pathComponents: ["mentions"], - queryParameters: [:] - ) - ) - - case .messages: - return ActionPaths( - app: Path( - pathComponents: ["messages"], - queryParameters: [:] - ), - web: Path( - pathComponents: ["messages"], - queryParameters: [:] - ) - ) + {% for action in app.actions %} + {{ action.create_action() }} + {% endfor %} } } } diff --git a/appz_scripts/twitter-spec.json b/appz_scripts/twitter-spec.json index eb81097..ccf9613 100644 --- a/appz_scripts/twitter-spec.json +++ b/appz_scripts/twitter-spec.json @@ -10,11 +10,11 @@ "paths": { "app":{ "path": ["status"], - "query": [{"id":"{id}"}] + "query": {"id":"{id}"} }, "web":{ - "path": ["status", "{id}"], - "query": [] + "path": ["statuses", "{id}"], + "query": {} } }, "params": [ From ee12110f7b7945b4b2d1c6c163b3bfd6b25f5e51 Mon Sep 17 00:00:00 2001 From: Hamad alghanim Date: Wed, 28 Sep 2016 23:32:47 +0300 Subject: [PATCH 07/10] fixed indentation, generation is now outputted as a file --- appz_scripts/Twitter.swift | 52 +++++++++++++++++++++++++++++++++++++ appz_scripts/appz_models.py | 16 ++++++------ appz_scripts/main.py | 5 +++- 3 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 appz_scripts/Twitter.swift diff --git a/appz_scripts/Twitter.swift b/appz_scripts/Twitter.swift new file mode 100644 index 0000000..4a5aa53 --- /dev/null +++ b/appz_scripts/Twitter.swift @@ -0,0 +1,52 @@ +// +// Twitter.swift +// Appz +// +// Copyright © 2016 kitz. All rights reserved. +// + +public extension Applications { + + public struct Twitter: ExternalApplication { + + public typealias ActionType = Applications.Twitter.Action + + public let scheme = "twitter:" + public let fallbackURL = "https://twitter.com/" + public let appStoreId = "333903271" + + public init() {} + } +} + +// MARK: - Actions + +public extension Applications.Twitter { + + public enum Action { + + case status(id: String) + + } +} + +extension Applications.Twitter.Action: ExternalApplicationAction { + + public var paths: ActionPaths { + + switch self { + + case .status(let id): + return ActionPaths( + app: Path( + pathComponents:["status"], + queryParameters: ["id" : id]), + web: Path( + pathComponents:["statuses",id], + queryParameters: [:] + ) + ) + + } + } +} \ No newline at end of file diff --git a/appz_scripts/appz_models.py b/appz_scripts/appz_models.py index d3bf384..2702e93 100644 --- a/appz_scripts/appz_models.py +++ b/appz_scripts/appz_models.py @@ -76,9 +76,9 @@ def create_action(self): action = action[:-1] # remove tailing , action += "):\n" # finished enum definition # enum implementaion starts here - action += "\t\treturn ActionPaths(\n" - action += "\t\t\tapp: Path(\n" - action += "\t\t\t\tpathComponents:[" + action += "\t\t\t\t\t\t\treturn ActionPaths(\n" + action += "\t\t\t\t\t\t\t\tapp: Path(\n" + action += "\t\t\t\t\t\t\t\t\tpathComponents:[" for app_path in self.path.app_path: parameter = self.get_parameter(app_path) if parameter is not None: @@ -94,7 +94,7 @@ def create_action(self): else: action += "],\n" # pathComponents for app done here - action += "\t\t\t\tqueryParameters: [" + action += "\t\t\t\t\t\t\t\t\tqueryParameters: [" for key, value in self.path.app_query.iteritems(): action += '"{}" : '.format(key) parameter = self.get_parameter(value) @@ -112,8 +112,8 @@ def create_action(self): action += ":]),\n" # queryParameters for app done here - action += "\t\t\tweb: Path(\n" - action += "\t\t\t\tpathComponents:[" + action += "\t\t\t\t\t\t\t\tweb: Path(\n" + action += "\t\t\t\t\t\t\t\t\tpathComponents:[" for web_path in self.path.web_path: parameter = self.get_parameter(web_path) if parameter is not None: @@ -129,7 +129,7 @@ def create_action(self): else: action += "],\n" # pathComponents for web done here - action += "\t\t\t\tqueryParameters: [" + action += "\t\t\t\t\t\t\t\t\tqueryParameters: [" for key, value in self.path.web_query.iteritems(): action += '"{}":'.format(key) parameter = self.get_parameter(value) @@ -144,7 +144,7 @@ def create_action(self): if len(self.path.web_query) > 0: action = action[:-1] + "])," # remove tailing , else: - action += ":]\n\t\t\t)\n\t\t)\n" + action += ":]\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t)" return action # serlizaiton functions diff --git a/appz_scripts/main.py b/appz_scripts/main.py index f274faf..3c38463 100644 --- a/appz_scripts/main.py +++ b/appz_scripts/main.py @@ -16,4 +16,7 @@ app = Application(data) template = env.get_template("app_template.swift") outputFile = template.render(app=app) - print outputFile.encode('utf-8') + file_ = open("{}.swift".format(app.name), 'w') + file_.write(outputFile.encode('utf-8')) + file_.close() + print "Done" From de3b60f5a16acd89e6e57e3d61dfa633721cb542 Mon Sep 17 00:00:00 2001 From: Hamad alghanim Date: Thu, 29 Sep 2016 19:14:48 +0300 Subject: [PATCH 08/10] refactored action class should be readable and maintainable now --- appz_scripts/Twitter.swift | 20 ++-- appz_scripts/appz_models.py | 102 +++++++++------------ appz_scripts/templates/action_template.txt | 10 ++ 3 files changed, 61 insertions(+), 71 deletions(-) create mode 100644 appz_scripts/templates/action_template.txt diff --git a/appz_scripts/Twitter.swift b/appz_scripts/Twitter.swift index 4a5aa53..01e60f8 100644 --- a/appz_scripts/Twitter.swift +++ b/appz_scripts/Twitter.swift @@ -36,16 +36,16 @@ extension Applications.Twitter.Action: ExternalApplicationAction { switch self { - case .status(let id): - return ActionPaths( - app: Path( - pathComponents:["status"], - queryParameters: ["id" : id]), - web: Path( - pathComponents:["statuses",id], - queryParameters: [:] - ) - ) + case .status(let id): + return ActionPaths( + app: Path( + pathComponents:["status"], + queryParameters: ["id" : id]), + web: Path( + pathComponents:["statuses",id], + queryParameters: [:] + ) + ) } } diff --git a/appz_scripts/appz_models.py b/appz_scripts/appz_models.py index 2702e93..6678b6a 100644 --- a/appz_scripts/appz_models.py +++ b/appz_scripts/appz_models.py @@ -1,5 +1,6 @@ #!/usr/bin/env python import re +from jinja2 import Template, Environment, FileSystemLoader class Path: @@ -68,84 +69,63 @@ def create_function_definition(self): definition += ")" return definition - def create_action(self): - action = "case .{}(".format(self.name) + def parameter_generation(self): + parameter_string = "" for parameter in self.parameters: - action += "let {},".format(parameter.name) + parameter_string += "let {},".format(parameter.name) if len(self.parameters) > 0: - action = action[:-1] # remove tailing , - action += "):\n" # finished enum definition - # enum implementaion starts here - action += "\t\t\t\t\t\t\treturn ActionPaths(\n" - action += "\t\t\t\t\t\t\t\tapp: Path(\n" - action += "\t\t\t\t\t\t\t\t\tpathComponents:[" - for app_path in self.path.app_path: - parameter = self.get_parameter(app_path) - if parameter is not None: - parameter_names = [param.name for param in self.parameters] - if parameter in parameter_names: - action += "{},".format(parameter) - else: - action += '"{}",'.format(app_path) - else: - action += '"{}",'.format(app_path) - if len(self.path.app_path) > 0: - action = action[:-1] + "],\n" # remove tailing , + parameter_string = parameter_string[:-1] # remove tailing , + return parameter_string + + def path_components_generation(self, path_type): + if path_type == "app": + paths = self.path.app_path else: - action += "],\n" - # pathComponents for app done here - action += "\t\t\t\t\t\t\t\t\tqueryParameters: [" - for key, value in self.path.app_query.iteritems(): - action += '"{}" : '.format(key) - parameter = self.get_parameter(value) + paths = self.path.web_path + components = "" + for path in paths: + parameter = self.get_parameter(path) if parameter is not None: parameter_names = [param.name for param in self.parameters] if parameter in parameter_names: - action += "{},".format(parameter) + components += "{},".format(parameter) else: - action += '"{}",'.format(value) + components += '"{}",'.format(path) else: - action += '"{}",'.format(value) - if len(self.path.app_query) > 0: - action = action[:-1] + "]),\n" # remove tailing , - else: - action += ":]),\n" - # queryParameters for app done here + components += '"{}",'.format(path) + if len(paths) > 0: + components = components[:-1] # remove tailing , - action += "\t\t\t\t\t\t\t\tweb: Path(\n" - action += "\t\t\t\t\t\t\t\t\tpathComponents:[" - for web_path in self.path.web_path: - parameter = self.get_parameter(web_path) - if parameter is not None: - parameter_names = [param.name for param in self.parameters] - if parameter in parameter_names: - action += "{},".format(parameter) - else: - action += '"{}",'.format(web_path) - else: - action += '"{}",'.format(web_path) - if len(self.path.web_path) > 0: - action = action[:-1] + "],\n" # remove tailing , + return components + + def query_parameters_generation(self, query_type): + if query_type == "app": + query_items = self.path.app_query else: - action += "],\n" - # pathComponents for web done here - action += "\t\t\t\t\t\t\t\t\tqueryParameters: [" - for key, value in self.path.web_query.iteritems(): - action += '"{}":'.format(key) + query_items = self.path.web_query + query = "" + for key, value in query_items.iteritems(): + query += '"{}" : '.format(key) parameter = self.get_parameter(value) if parameter is not None: parameter_names = [param.name for param in self.parameters] if parameter in parameter_names: - action += "{},".format(parameter) + query += "{},".format(parameter) else: - action += '"{}",'.format(value) + query += '"{}",'.format(value) else: - action += '"{}",'.format(value) - if len(self.path.web_query) > 0: - action = action[:-1] + "])," # remove tailing , + query += '"{}",'.format(value) + if len(query_items) > 0: + query = query[:-1] # remove tailing , else: - action += ":]\n\t\t\t\t\t\t\t\t)\n\t\t\t\t\t\t\t)" - return action + query += ":" + return query + + def create_action(self): + env = Environment(loader=FileSystemLoader('templates')) + template = env.get_template("action_template.txt") + outputFile = template.render(action=self) + return outputFile.encode('utf-8') # serlizaiton functions def __str__(self): diff --git a/appz_scripts/templates/action_template.txt b/appz_scripts/templates/action_template.txt new file mode 100644 index 0000000..b513eac --- /dev/null +++ b/appz_scripts/templates/action_template.txt @@ -0,0 +1,10 @@ + case .{{ action.name }}({{ action.parameter_generation() }}): + return ActionPaths( + app: Path( + pathComponents:[{{ action.path_components_generation("app") }}], + queryParameters: [{{ action.query_parameters_generation("app") }}]), + web: Path( + pathComponents:[{{ action.path_components_generation("web") }}], + queryParameters: [{{ action.query_parameters_generation("web") }}] + ) + ) From 805136ea4efb1e66b73262e379f21d84ae94e2bf Mon Sep 17 00:00:00 2001 From: Hamad alghanim Date: Fri, 30 Sep 2016 16:23:08 +0300 Subject: [PATCH 09/10] made the template loader global --- appz_scripts/__init__.py | 5 +++++ appz_scripts/appz_models.py | 11 +++++------ appz_scripts/main.py | 4 +--- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/appz_scripts/__init__.py b/appz_scripts/__init__.py index e69de29..146e50c 100644 --- a/appz_scripts/__init__.py +++ b/appz_scripts/__init__.py @@ -0,0 +1,5 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +from jinja2 import Environment, FileSystemLoader + +env = Environment(loader=FileSystemLoader('templates')) diff --git a/appz_scripts/appz_models.py b/appz_scripts/appz_models.py index 6678b6a..e9eafe1 100644 --- a/appz_scripts/appz_models.py +++ b/appz_scripts/appz_models.py @@ -1,6 +1,6 @@ #!/usr/bin/env python import re -from jinja2 import Template, Environment, FileSystemLoader +from __init__ import env # i don't like this will change it in the future class Path: @@ -65,7 +65,7 @@ def create_function_definition(self): for parameter in self.parameters: definition += parameter.create_paramter_for_function() definition += "," - definition = definition[:-1] # remove tailing ',' character + definition = definition[:-1] # remove trailing ',' character definition += ")" return definition @@ -74,7 +74,7 @@ def parameter_generation(self): for parameter in self.parameters: parameter_string += "let {},".format(parameter.name) if len(self.parameters) > 0: - parameter_string = parameter_string[:-1] # remove tailing , + parameter_string = parameter_string[:-1] # remove trailing , return parameter_string def path_components_generation(self, path_type): @@ -94,7 +94,7 @@ def path_components_generation(self, path_type): else: components += '"{}",'.format(path) if len(paths) > 0: - components = components[:-1] # remove tailing , + components = components[:-1] # remove trailing , return components @@ -116,13 +116,12 @@ def query_parameters_generation(self, query_type): else: query += '"{}",'.format(value) if len(query_items) > 0: - query = query[:-1] # remove tailing , + query = query[:-1] # remove trailing , else: query += ":" return query def create_action(self): - env = Environment(loader=FileSystemLoader('templates')) template = env.get_template("action_template.txt") outputFile = template.render(action=self) return outputFile.encode('utf-8') diff --git a/appz_scripts/main.py b/appz_scripts/main.py index 3c38463..0d84076 100644 --- a/appz_scripts/main.py +++ b/appz_scripts/main.py @@ -2,16 +2,14 @@ # -*- coding: utf-8 -*- -from jinja2 import Template, Environment, FileSystemLoader import json from appz_models import Application +from __init__ import env # i don't like this will change it in the future if __name__ == "__main__": # TODO: iterate through a directory of json spec files data_file = open('twitter-spec.json') data = json.load(data_file) - # all template files should be inside the templates folder - env = Environment(loader=FileSystemLoader('templates')) # this is the application class that will parse the json file app = Application(data) template = env.get_template("app_template.swift") From 3ce37e4fd174121e5e9b447e991eda5c26e1ed4c Mon Sep 17 00:00:00 2001 From: Hamad alghanim Date: Fri, 30 Sep 2016 16:44:05 +0300 Subject: [PATCH 10/10] tests place holder --- .../tests/appz_scripts/appz_models_tests.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 appz_scripts/tests/appz_scripts/appz_models_tests.py diff --git a/appz_scripts/tests/appz_scripts/appz_models_tests.py b/appz_scripts/tests/appz_scripts/appz_models_tests.py new file mode 100644 index 0000000..ee9d853 --- /dev/null +++ b/appz_scripts/tests/appz_scripts/appz_models_tests.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +import unittest + + +def test_placeholder(): + assert (1+1 == 2) + + +# Here's our "unit tests". +class EqualityTests(unittest.TestCase): + + def test(self): + self.failIf(test_placeholder()) + + +def main(): + unittest.main() + + +if __name__ == '__main__': + main()