Commits

David Warburton committed 7610ec3

improve robustness and communication with user.

Comments (0)

Files changed (1)

 PACKAGE_NAME = 'Shopify'
 
 def load():
-    global stores, current_store, current_theme, cache_data, cache_data_file
+    global stores, current_store, current_theme, cache_data, cache_data_file, bad_values
     store_template = """{
     "Human Readable Shop Name":{
         "API_KEY": "API-KEY-HERE",
     }
 }
 """
+    stores        = None
+    current_store = None
+    current_theme = None
+
     store_file = os.path.join(sublime.packages_path(), 'User', 'ShopifyStores.json')
     cache_data_file = os.path.join(sublime.packages_path(), PACKAGE_NAME, 'cache_data.json')
     cache_data = {}
         with open(cache_data_file) as f:
             cache_data = json.load(f)
 
-    stores = None
-
     if (not os.path.exists(store_file)):
         with open(store_file, 'w') as f:
             f.write(store_template)
+            return
 
     with open(store_file) as f:
         store_data = json.load(f)
-    
-    if (len(store_data.keys()) == 1 and "Human Readable Shop Name" in store_data.keys()):
+
+    bad_values = []
+    if (len(store_data) == 1 and False):
+        only_store = store_data.values()[0]
+        if (only_store["API_KEY"]      == "API-KEY-HERE"):
+            bad_values.append("API_KEY")
+        if (only_store["API_PASSWORD"] == "API-PASSWORD-HERE"):
+            bad_values.append("API_PASSWORD")
+        if (only_store["SHOP_NAME"]    == "SHOP-NAME-AS-IT-APPEARS-IN-URLS" ):
+            bad_values.append("SHOP_NAME")
         return
     
     stores = []
     for name, data in store_data.iteritems():
         stores.append(Shop(name, data))
-    
-    current_store = None
-    current_theme = None
+
 
 class Shop:
-    def __init__(self, name, shop):
-        self.name         = shop['SHOP_NAME']
-        self.api_key      = shop['API_KEY']
-        self.api_password = shop['API_PASSWORD']
+    def __init__(self, name, shop_data):
+        self.name         = shop_data['SHOP_NAME']
+        self.api_key      = shop_data['API_KEY']
+        self.api_password = shop_data['API_PASSWORD']
         self.admin_url    = admin_url_template % (self.api_key, self.api_password, self.name)
         self.display_name = name
 
+    def load_shopify_json(self, address):
+        response = urllib.urlopen(address)
+        if( response.getcode() >= 400 ):
+            sublime.set_timeout(lambda: sublime.error_message("Shopify authentication failed."), 0)
+            open_stores_file()
+            raise Exception("Shopify authentication failed")
+
+        return json.load(response)
+
     def download_themes(self):
-        self.themes = json.load(urllib.urlopen("%s/themes.json" % self.admin_url))['themes']
+        self.themes = self.load_shopify_json("%s/themes.json" % self.admin_url)['themes']
         return self.themes
 
     def refresh_assets(self, theme_id):
 
         theme           = [t for t in self.themes if t['id'] == theme_id][0]
         assets_url      = "%s/themes/%s/assets.json" % (self.admin_url, theme_id)
-        all_assets      = json.load(urllib.urlopen(assets_url))['assets']
+        all_assets      = self.load_shopify_json(assets_url)['assets']
         keys            = map(lambda a: a['key'], all_assets)
         theme['assets'] = [a for a in all_assets if "%s.liquid" % a['key'] not in keys and not re.search("(\.png|\.gif|\.jpg)$", a['key'])]
 
     
     def pull_asset_data(self, key, theme_id):
         asset_url = "%s/themes/%s/assets.json?asset[key]=%s" % (self.admin_url, theme_id, key)
-        return json.load(urllib.urlopen(asset_url))['asset']
+        return self.load_shopify_json(asset_url)['asset']
     
     def download_asset(self, asset_index):
         key             = current_theme['assets'][asset_index]['key']
             success = False
         return success
 
+def open_stores_file():
+    sublime.active_window().run_command('open_file', {"file": "${packages}/User/ShopifyStores.json"})
+
 class ShopifyShowStoresCommand(sublime_plugin.WindowCommand):
     def run(self):
         if (not stores):
-            self.window.run_command('open_file', {"file": "${packages}/User/ShopifyStores.json"})
+            if (len(bad_values) > 0):
+                error_message = "Please correct these values: "
+                comma = False
+                for bvalue in bad_values:
+                    if (comma):
+                        error_message += ", "
+                    else:
+                        comma = True
+                    error_message += bvalue
+                sublime.error_message(error_message)    
+            open_stores_file()
             return
 
         commands = ["Shopify: Browse Themes - %s" % x.display_name for x in stores]
 
 class ShopifyShowThemesCommand(sublime_plugin.WindowCommand):
     def run(self):
-        if (not stores):
-            self.window.run_command('open_file', {"file": "${packages}/User/ShopifyStores.json"})
-            return
-
         if (current_store == None):
             self.window.run_command('shopify_show_stores')
             return
 
 class ShopifyShowAssetsCommand(sublime_plugin.WindowCommand):
     def run(self):
-        if (not stores):
-            self.window.run_command('open_file', {"file": "${packages}/User/ShopifyStores.json"})
-            return
-
-        if (current_store == None):
-            self.window.run_command('shopify_show_stores')
+        if (current_theme == None):
+            self.window.run_command('shopify_show_themes')
             return
 
         commands = ["%s: %s" % (current_theme['name'], a['key']) for a in current_theme['assets']]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.