Commits

Anonymous committed 9821b10

Add a download all assets command

Comments (0)

Files changed (2)

 PACKAGE_NAME = 'Shopify'
 
 def load():
-    global stores, current_store, current_theme, cache_data, cache_data_file, bad_values
+    global stores, current_store, current_theme, cache_data, cache_data_file, bad_values, show_themes_continue
     store_template = """{
     "Human Readable Shop Name":{
         "API_KEY": "API-KEY-HERE",
     stores        = None
     current_store = None
     current_theme = None
+    
+    show_themes_continue = 'shopify_show_assets'
 
     store_file = os.path.join(sublime.packages_path(), 'User', 'ShopifyStores.json')
     cache_data_file = os.path.join(PACKAGE_DIR, 'cache_data.json')
         asset_url = "%s/themes/%s/assets.json?asset[key]=%s" % (self.admin_url, theme_id, key)
         return self.load_shopify_json(asset_url)['asset']
     
+    def download_all_assets(self):
+        print "Going to download all!"
+        for x in range(0, len(current_theme['assets'])):
+            self.download_asset(x)
+
+        print "Download all assets is done."
+
     def download_asset(self, asset_index):
         key             = current_theme['assets'][asset_index]['key']
         asset_file_name = os.path.join(self.name, str(current_theme['id']), key)
 
         save_timestamp_to_cache_data(asset_file_name)
     
-        sublime.status_message('Downloading asset')
+        sublime.set_timeout(lambda:sublime.status_message('Downloading asset'),0)
+
+        print "Downloading asset: " + asset_file_name
         asset = current_store.pull_asset_data(key, theme_id = current_theme['id'])
         if ('attachment' in asset.keys()):
             value = base64.b64decode(asset['attachment'])
 
         with open(asset_full_name, 'wb') as f:
             f.write(value)
-        sublime.status_message('')
+        sublime.set_timeout(lambda:sublime.status_message(''),0)
         return asset_full_name
 
     def push_asset(self, key, theme_id, value):
             all_assets = current_store.refresh_assets(theme_id = current_theme['id'])
             sublime.status_message('')
 
-            self.window.run_command('shopify_show_assets')
+            self.window.run_command(show_themes_continue)
         self.window.show_quick_panel(commands, on_theme_select)
 
 class ShopifyShowAssetsCommand(sublime_plugin.WindowCommand):
+    global show_themes_continue
     def run(self):
         if (current_theme == None):
+            show_themes_continue = 'shopify_show_assets'
             self.window.run_command('shopify_show_themes')
             return
 
 
         self.window.show_quick_panel(commands, on_asset_select)
 
+class ShopifyDownloadAllCommand(sublime_plugin.WindowCommand):
+    def run(self):
+        global show_themes_continue
+        if (current_theme == None):
+            show_themes_continue = 'shopify_download_all'
+            self.window.run_command('shopify_show_themes')
+            return
+
+        sublime.status_message('Downloading All Assets')
+        ShopifyPuller().start()
+        sublime.status_message('')
+
+
+
 class UTC(tzinfo):
     """UTC"""
 
             sublime.set_timeout(lambda:sublime.status_message("Upload to %s has failed" % store.display_name),0)
         self.result = True
 
+class ShopifyPuller(threading.Thread):
+    def run(self):
+        current_store.download_all_assets()
+
+
 load()

Shopify.sublime-commands

     {
         "caption": "Shopify: Edit asset",
         "command": "shopify_show_assets"
+    },
+    {
+        "caption": "Shopify: Download all assets",
+        "command": "shopify_download_all"
     }
 
 ]