From 107c6b5f154980aaa478fa3f4723be542aee37b7 Mon Sep 17 00:00:00 2001 From: Nilesh Kapadia Date: Mon, 15 Jun 2009 00:22:06 -0400 Subject: [PATCH] Added ability to use a downloaded bundle as the source --- README | 16 +++++++++------- snippet_copier.py | 47 +++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/README b/README index 92eee96..9eb1641 100644 --- a/README +++ b/README @@ -18,13 +18,9 @@ I have added the following new features: having having to specify the bundle name. You can still do that, but now you can instead specify the URL of a Textmate bundle with the --url option. -TODO -==== - -I plan to add these features: - -* Ability to use a downloaded Textmate bundle as the source - +* Ability to use a downloaded Textmate bundle as the source (using the + --folder option) + Example ======= @@ -37,3 +33,9 @@ snippet_copier.py \ Note that currently the URL specified should have a trailing slash ("Bundle/" is appended to it). + +To use a downloaded bundle: + +snippet_copier.py \ + --folder ~/Downloads/Oracle.tmbundle \ + --path ~/Emacs/snippets/text-mode/plsql-mode diff --git a/snippet_copier.py b/snippet_copier.py index 81253a1..b77dcc8 100755 --- a/snippet_copier.py +++ b/snippet_copier.py @@ -145,6 +145,12 @@ def _create_soup(self, uri): sock.close() return soup + def _create_soup_from_file(self, filename): + snippet_file = open(filename) + soup = BeautifulSoup(snippet_file.read()) + snippet_file.close() + return soup + def _snippet_uris(self): if not self.bundle_url: self.bundle_url = self.URI_BASE % self.bundle @@ -156,7 +162,7 @@ def set_bundle_name(self, bundle): self.bundle = bundle.replace(' ', '%20') def set_bundle_url(self, bundle_url): - self.bundle_url = bundle_url + "Snippets/" + self.bundle_url = str(bundle_url) + "Snippets/" def download_snippets(self): snippets = [] @@ -167,9 +173,26 @@ def download_snippets(self): pass return snippets + def load_snippets(self, folder): + snippets = [] + snippet_folder = os.path.abspath(folder) + os.sep + "Snippets" + for filename in os.listdir(snippet_folder): + try: + snippets.append(self.create_snippet_from_file( + snippet_folder + os.sep + filename)) + except NotImplementedError: + pass + return snippets + def create_snippet(self, uri): soup = self._create_soup(uri) + return self.process_soup(soup) + def create_snippet_from_file(self, filename): + soup = self._create_soup_from_file(filename) + return self.process_soup(soup) + + def process_soup(self, soup): try: return Snippet( soup.find("key", text="tabTrigger").findNext("string").string, @@ -187,23 +210,31 @@ def create_snippet(self, uri): "bundle") p.add_option("--path", "-p", help="Path where snippets should be saved") p.add_option("--url", "-u", help="Specific URL of a Textmate Bundle") + p.add_option("--folder", "-f", help="Specific folder path where Textmate" + "Bundle is located") options, arguments = p.parse_args() # Validate everything is good - if not options.bundle and not options.url: - p.error("bundle option or URL not given") + if not options.bundle and not options.url and not options.folder: + p.error("bundle option name, URL, or folder path not given") if not options.path: p.error("path option not given") elif not os.path.exists(options.path) or not os.path.isdir(options.path): p.error("not a valid path") - + + if options.folder and not os.path.exists(options.folder): + p.error("Folder of bundle doesn't exist") + bndl = TextMateBundle() if options.bundle: bndl.set_bundle_name(options.bundle) - else: + elif options.url: bndl.set_bundle_url(options.url) - - for snippet in bndl.download_snippets(): - snippet.save_as_yasnippet(options.path) + if options.folder: + for snippet in bndl.load_snippets(options.folder): + snippet.save_as_yasnippet(options.path) + else: + for snippet in bndl.download_snippets(): + snippet.save_as_yasnippet(options.path)