diff --git a/shopify/base.py b/shopify/base.py index 520dbbb2..c2e439f5 100644 --- a/shopify/base.py +++ b/shopify/base.py @@ -15,7 +15,8 @@ class ShopifyConnection(pyactiveresource.connection.Connection): def _open(self, *args, **kwargs): self.response = None try: - self.response = super(ShopifyConnection, self)._open(*args, **kwargs) + self.response = super(ShopifyConnection, self)._open(*args, + **kwargs) except pyactiveresource.connection.ConnectionError, err: self.response = err.response raise @@ -28,7 +29,7 @@ def connection(cls): """HTTP connection for the current thread""" local = cls._threadlocal if not getattr(local, 'connection', None): - # Make sure these variables are no longer affected by other threads. + # Make sure these variables are no longer affected by other threads local.user = cls.user local.password = cls.password local.site = cls.site @@ -118,19 +119,23 @@ def set_primary_key(cls, value): cls._primary_key = value primary_key = property(get_primary_key, set_primary_key, None, - 'Name of attribute that uniquely identies the resource') + 'Name of attribute that uniquely identies the ' + 'resource') class ShopifyResource(ActiveResource, mixins.Countable): __metaclass__ = ShopifyResourceMeta _primary_key = "id" _threadlocal = threading.local() - _headers = { 'User-Agent': 'ShopifyPythonAPI/%s Python/%s' % (shopify.VERSION, sys.version.split(' ', 1)[0]) } + _headers = { 'User-Agent': 'ShopifyPythonAPI/%s Python/%s' % ( + shopify.VERSION, sys.version.split(' ', 1)[0]) } def __init__(self, attributes=None, prefix_options=None): if attributes is not None and prefix_options is None: - prefix_options, attributes = self.__class__._split_options(attributes) - return super(ShopifyResource, self).__init__(attributes, prefix_options) + prefix_options, attributes = self.__class__._split_options( + attributes) + return super(ShopifyResource, self).__init__(attributes, + prefix_options) def is_new(self): return not self.id diff --git a/shopify/resources.py b/shopify/resources.py index 80e4a8e8..fb835e38 100644 --- a/shopify/resources.py +++ b/shopify/resources.py @@ -13,7 +13,8 @@ def metafields(self): def add_metafield(self, metafield): if self.is_new(): - raise ValueError("You can only add metafields to a resource that has been saved") + raise ValueError("You can only add metafields to a resource that " + "has been saved") metafield.save() return metafield @@ -26,10 +27,12 @@ def products(self): return Product.find(collection_id=self.id) def add_product(self, product): - return Collect.create(dict(collection_id=self.id, product_id=product.id)) + return Collect.create(dict(collection_id=self.id, + product_id=product.id)) def remove_product(self, product): - collect = Collect.find_first(collection_id=self.id, product_id=product.id) + collect = Collect.find_first(collection_id=self.id, + product_id=product.id) if collect: collect.destroy() @@ -71,13 +74,14 @@ def open(self): self._load_attributes_from_response(self.post("open")) def cancel(self, **kwargs): - self._load_attributes_from_response(self.post("cancel"), **kwargs) + self._load_attributes_from_response(self.post("cancel", **kwargs)) def transactions(self): return Transaction.find(order_id=self.id) def capture(self, amount=""): - return Transaction.create(amount=amount, kind="capture", order_id=self.id) + return Transaction.create(amount=amount, kind="capture", + order_id=self.id) class Product(ShopifyResource): @@ -110,14 +114,16 @@ class Variant(ShopifyResource): @classmethod def _prefix(cls, options={}): product_id = options.get("product_id") - return "/admin/" if product_id is None else "/admin/products/%s" % (product_id) + return ("/admin/" if product_id is None else + "/admin/products/%s" % (product_id)) class Image(ShopifyResource): _prefix_source = "/admin/products/$product_id/" def __getattr__(self, name): - if name in ["pico", "icon", "thumb", "small", "compact", "medium", "large", "grande", "original"]: + if name in ["pico", "icon", "thumb", "small", "compact", "medium", + "large", "grande", "original"]: return re.sub(r"/(.*)\.(\w{2,4})", r"/\1_%s.\2" % (name), self.src) else: return super(Image, self).__getattr__(name) @@ -161,7 +167,8 @@ class Metafield(ShopifyResource): @classmethod def _prefix(cls, options={}): - return "/admin/" if options.get("resource") is None else "/admin/%s/%s" % (options["resource"], options["resource_id"]) + return ("/admin/" if options.get("resource") is None else + "/admin/%s/%s" % (options["resource"], options["resource_id"])) class Comment(ShopifyResource): @@ -204,7 +211,9 @@ class Event(ShopifyResource): @classmethod def _prefix(cls, options={}): - return "/admin/" if options.get("resource") is None else "/admin/%s/%s/" % (options["resource"], options["resource_id"]) + return ("/admin/" if options.get("resource") is None else + "/admin/%s/%s/" % (options["resource"], + options["resource_id"])) class Customer(ShopifyResource): @@ -246,14 +255,16 @@ class Asset(ShopifyResource): @classmethod def _prefix(cls, options={}): - return "/admin/" if options.get("theme_id") is None else "/admin/themes/%s/" % (options["theme_id"]) + return ("/admin/" if options.get("theme_id") is None else + "/admin/themes/%s/" % (options["theme_id"])) @classmethod def _element_path(cls, id, prefix_options={}, query_options=None): if query_options is None: prefix_options, query_options = cls._split_options(prefix_options) return "%s%s.%s%s" % (cls._prefix(prefix_options), cls.plural, - cls.format.extension, cls._query_string(query_options)) + cls.format.extension, + cls._query_string(query_options)) @classmethod def find(cls, key=None, **kwargs): @@ -267,7 +278,8 @@ def find(cls, key=None, **kwargs): params.update(kwargs) theme_id = params.get("theme_id") path_prefix = "/admin/themes/%s" % (theme_id) if theme_id else "/admin" - resource = cls.find_one("%s/assets.%s" % (path_prefix, cls.format.extension), **params) + resource = cls.find_one("%s/assets.%s" % ( + path_prefix, cls.format.extension), **params) if theme_id and resource: resource._prefix_options["theme_id"] = theme_id return resource @@ -284,7 +296,8 @@ def __set_value(self, data): self.__wipe_value_attributes() self.attributes["value"] = data - value = property(__get_value, __set_value, None, "The asset's value or attachment") + value = property(__get_value, __set_value, None, + "The asset's value or attachment") def attach(self, data): self.attachment = base64.b64encode(data) @@ -292,7 +305,8 @@ def attach(self, data): def destroy(self): options = {"asset[key]": self.key} options.update(self._prefix_options) - return self.__class__.connection.delete(self._element_path(self.key, options), self.__class__.headers) + return self.__class__.connection.delete( + self._element_path(self.key, options), self.__class__.headers) def is_new(self): return False @@ -363,8 +377,10 @@ class TaxLine(ShopifyResource): pass -METAFIELD_ENABLED_CLASSES = (Article, Blog, CustomCollection, Customer, Order, Page, Product, SmartCollection, Variant) -EVENT_ENABLED_CLASSES = (Order, Product, CustomCollection, SmartCollection, Page, Blog, Article) +METAFIELD_ENABLED_CLASSES = (Article, Blog, CustomCollection, Customer, Order, + Page, Product, SmartCollection, Variant) +EVENT_ENABLED_CLASSES = (Order, Product, CustomCollection, SmartCollection, + Page, Blog, Article) for cls in METAFIELD_ENABLED_CLASSES: cls.__bases__ += (mixins.Metafields,)