-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Updating Streamline branch #89
Open
Dreamersoul
wants to merge
10
commits into
SwiftKitz:streamline
Choose a base branch
from
Dreamersoul:streamline
base: streamline
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c5827e3
following pep8 naming convention
Dreamersoul af4abd1
pep8 and Param class serialization
Dreamersoul 1caa380
function definition is now being generated
Dreamersoul 16d5915
forgot to add the file to the commit
Dreamersoul 8d5a162
finishing up the models
Dreamersoul f22d116
changed config structure and now swift files are being generated
Dreamersoul ee12110
fixed indentation, generation is now outputted as a file
Dreamersoul de3b60f
refactored action class should be readable and maintainable now
Dreamersoul 805136e
made the template loader global
Dreamersoul 3ce37e4
tests place holder
Dreamersoul File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [:] | ||
) | ||
) | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 likeparameter_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.
There was a problem hiding this comment.
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.