-
Notifications
You must be signed in to change notification settings - Fork 1
Only send necessary files #37
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,6 +55,7 @@ def get_extra_params(self): | |
|
|
||
| def run(self, edit): | ||
| self.url = get_url(self.settings) + self.COMMAND_URL | ||
| use_pertinent_load = self.settings.get('pertinent_load', False) | ||
|
|
||
| template_filename = self.view.file_name() | ||
| if not template_filename: | ||
|
|
@@ -66,13 +67,6 @@ def run(self, edit): | |
|
|
||
| self.dissect_filename(template_filename) | ||
|
|
||
| # get file names | ||
| file_names = json.dumps(self.generate_file_list()) | ||
| use_cache = self.settings.get('use_cache', DEFAULT_USE_CACHE_SETTING) | ||
|
|
||
| print("Attempting to render %s for %s" % (self.action, self.partner)) | ||
| print("url is %s" % self.url) | ||
|
|
||
| params = dict(product_count=self.settings.get("product_count", 3), | ||
| partner=self.partner, | ||
| action=self.action, | ||
|
|
@@ -85,11 +79,23 @@ def run(self, edit): | |
| use_dev='dev.' in template_filename, | ||
| generation=getattr(self, 'generation', 0), | ||
| variant_id=getattr(self, 'variant_id', ''), | ||
| subaction=getattr(self, 'subaction', ''), | ||
| file_names=file_names) | ||
| subaction=getattr(self, 'subaction', '')) | ||
|
|
||
| print(params) | ||
| if not use_cache: | ||
| params["templates"] = json.dumps(self.generate_file_map()) | ||
|
|
||
| if use_pertinent_load: | ||
| file_names, file_map = self.generate_pertinent_file_names_and_map(template_filename) | ||
| params['file_names'] = json.dumps(file_names) | ||
| params['templates'] = json.dumps(file_map) | ||
| else: | ||
| params['file_names'] = json.dumps(self.generate_file_list()) | ||
| use_cache = self.settings.get('use_cache', DEFAULT_USE_CACHE_SETTING) | ||
| if not use_cache: | ||
| params["templates"] = json.dumps(self.generate_file_map()) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Both branches assign This would reduce duplication on the two branches |
||
|
|
||
| print("Attempting to render %s for %s" % (self.action, self.partner)) | ||
| print("url is %s" % self.url) | ||
|
|
||
| try: | ||
| nqe = self.settings.get("nqe") | ||
| assert nqe | ||
|
|
@@ -128,6 +134,53 @@ def dissect_filename(self, template_filename): | |
| self.partner = self.settings.get("partner", self.partner) or self.partner | ||
| self.partner = self.partner.replace("_templates", "") | ||
|
|
||
| def generate_pertinent_file_names_and_map(self, template_filename): | ||
| template_filename = template_filename.replace(self.path + '/', '') | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this trying to get just the file name part of this? It seems a bit weird to me to use |
||
| image_regex = "/img/" + self.partner + "/" + "(.*?)['\"]" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Combine the also to be anal-retentive: self.partner might include characters that are treated specially in regexes; I know it isn't supposed to, but if it does will this break in some catastrophic way? It might be fine. You could use |
||
| file_map = dict() | ||
| all_file_names_required = [template_filename] | ||
| all_images_required = [] | ||
| # go through all files this template requires | ||
| # we get all the filenames, their contents, and images | ||
| for file_name in all_file_names_required: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you get rid of this loop? It only ever contains |
||
| # some templates include html files that don't exist. Just because they don't exist, doesn't | ||
| # mean it should automatically fail the command, they are most likely in jinja "if" statements | ||
| try: | ||
| contents = read_file(os.path.join(self.path, file_name)) | ||
| except: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you catch the specific exception, instead of all exceptions? This catch all will hide bugs. Additionally I'm not sure if it is useful for a sublime plugin, but logging the exception with a message that you are ignoring it is frequently useful when debugging |
||
| continue | ||
| file_map[file_name] = contents | ||
| # this regex finds all the other files the html file depends on. Because we're thenadding to | ||
| # the loop we're cycling through, this can be recursive | ||
| files_required_by_file = [file_name for _,file_name in \ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. anal retentive suggestion of |
||
| re.findall("{%\s*(include|extends|import)\s*['\"](.*?)['\"]", contents)] | ||
| all_file_names_required += [x for x in files_required_by_file if x not in all_file_names_required] | ||
| images_required_by_file = re.findall(image_regex, contents) | ||
| all_images_required += [img for img in re.findall(image_regex, contents) if img not in all_images_required] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would theoretically be better for all_images_required to be a
|
||
|
|
||
| for img in all_images_required: | ||
| img_path = os.path.abspath(os.path.join(self.image_path, img)) | ||
| contents = encode_image(img_path) | ||
| file_map[img] = contents | ||
|
|
||
| all_file_names_required += all_images_required | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need to even do this? I think |
||
|
|
||
| # lastly, we need to go through the other campaign files that accompany the html | ||
| template_base_name = template_filename.split('.')[0] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might want to prefer |
||
| for postfix in ['.tracking', '.txt', '.yaml']: | ||
| if postfix == '.txt': | ||
| file_name = "_".join([self.action, "subject", self.subaction]) + postfix | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could just be |
||
| else: | ||
| file_name = template_base_name+postfix | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spaces around + |
||
| try: | ||
| all_file_names_required.append(file_name) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you get rid of |
||
| contents = read_file(os.path.join(self.path, file_name)) | ||
| file_map[file_name] = contents | ||
| except: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again I would only catch the specific exception: this should "crash" if you have a typo or something. |
||
| sublime.error_message("trouble loading file " + file_name) | ||
|
|
||
| return (all_file_names_required, file_map) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can just return file_map, right? (see above re: all_file_names_required) |
||
|
|
||
| def generate_file_map(self): | ||
| # Read all the files in the given folder. | ||
| # We gather them all and then send them up to GAE. | ||
|
|
||
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 think this variable only gets used once; any reason to not just inline it in the if statement below? Or define it immediately before it gets used? It will be easier to understand what controls this if statement when reading it, if the two parts are close together.