diff --git a/appz_scripts/Twitter.swift b/appz_scripts/Twitter.swift new file mode 100644 index 0000000..01e60f8 --- /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/__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 3ddae29..e9eafe1 100644 --- a/appz_scripts/appz_models.py +++ b/appz_scripts/appz_models.py @@ -1,42 +1,165 @@ #!/usr/bin/env python -class Param: - def __init__(self, paramsDict): - self.type = paramsDict["type"] - self.name = paramsDict["name"] - self.isOptional = paramsDict["isOptional"] +import re +from __init__ import env # i don't like this will change it in the future + + +class Path: + def __init__(self, paths_dict): + 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) + + 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: + 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 "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.createParams(actionDict["params"]) - self.paths = actionDict["paths"] - - def createParams(self, params): - return [Param(param) for param in params] - - -class App: - # 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.createActions(jsonFile["actions"]) - - def createActions(self, actions): + def __init__(self, action_dict): + self.name = action_dict["name"] + self.parameters = self.create_params(action_dict["params"]) + 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: + definition += parameter.create_paramter_for_function() + definition += "," + definition = definition[:-1] # remove trailing ',' character + definition += ")" + return definition + + def parameter_generation(self): + parameter_string = "" + for parameter in self.parameters: + parameter_string += "let {},".format(parameter.name) + if len(self.parameters) > 0: + parameter_string = parameter_string[:-1] # remove trailing , + return parameter_string + + def path_components_generation(self, path_type): + if path_type == "app": + paths = self.path.app_path + else: + 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: + components += "{},".format(parameter) + else: + components += '"{}",'.format(path) + else: + components += '"{}",'.format(path) + if len(paths) > 0: + components = components[:-1] # remove trailing , + + return components + + def query_parameters_generation(self, query_type): + if query_type == "app": + query_items = self.path.app_query + else: + 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: + query += "{},".format(parameter) + else: + query += '"{}",'.format(value) + else: + query += '"{}",'.format(value) + if len(query_items) > 0: + query = query[:-1] # remove trailing , + else: + query += ":" + return query + + def create_action(self): + template = env.get_template("action_template.txt") + outputFile = template.render(action=self) + return outputFile.encode('utf-8') + + # 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 Application: + # 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] - # serlizaiton function + # serlizaiton functions def __str__(self): return unicode(self) def __unicode__(self): # TODO: serialize actions - return "[INFO] name: {}\nurl: {}\nshceme: {}\nappstore Id: {}".format( - self.name, - self.fallbackURL, - self.scheme, - self.appStoreId) + return "name: {}\nurl: {}\nshceme: {}\nappstore Id: {}".format( + self.name, + self.fallbackURL, + self.scheme, + self.appStoreId) diff --git a/appz_scripts/main.py b/appz_scripts/main.py index f274faf..0d84076 100644 --- a/appz_scripts/main.py +++ b/appz_scripts/main.py @@ -2,18 +2,19 @@ # -*- 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") 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" 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") }}] + ) + ) diff --git a/appz_scripts/templates/app_template.swift b/appz_scripts/templates/app_template.swift index c14491e..11a25c3 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 %} } } @@ -43,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/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() 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": [