Skip to content
52 changes: 52 additions & 0 deletions appz_scripts/Twitter.swift
Original file line number Diff line number Diff line change
@@ -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: [:]
)
)

}
}
}
206 changes: 175 additions & 31 deletions appz_scripts/appz_models.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,186 @@
#!/usr/bin/env python
class Param:
def __init__(self, paramsDict):
self.type = paramsDict["type"]
self.name = paramsDict["name"]
self.isOptional = paramsDict["isOptional"]
import re


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 tailing ',' character
definition += ")"
return definition

def create_action(self):
action = "case .{}(".format(self.name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not exactly sure why you aren't using templates here? I think it's much cleaner if you create smaller template files, like action_template.txt or something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am planning on refactoring this code since it's really looking awful, your suggestion is way better than what I had in mind, gonna work on it next time I can.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the actions are now being generated using a jinja template, should be way better now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Dreamersoul Definitely starting to look a lot more organized.

I also imagined that every part would be a template, and then we can have a template loader globally, TemplateLoader.get("blah"). Something like parameter_enum_template.txt, parameter_arg_template.txt, action_enum_template.txt, action_definition_template.txt, ... etc. This will make sure the script doesn't deal with any string manipulation, and trust Jinja for all that stuff.

This might be just a burden, so I leave it up to you whether it is worth it or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love how this is going, I'll try to make it work after writing tests.

for parameter in self.parameters:
action += "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 ,
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)
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\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 ,
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)
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\t\t\t\t\t)\n\t\t\t\t\t\t\t)"
return action

# 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)
5 changes: 4 additions & 1 deletion appz_scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading