GitHub PullRequestReviewCommentEvent at 2014-05-22 14:53:11
Repository: fjord
Actor: rlr
Payload: {"comment":{"url":"https://api.github.com/repos/mozilla/fjord/pulls/comments/12953994","id":12953994,"diff_hunk":"@@ -0,0 +1,190 @@\n+from functools import wraps\n+\n+from django.conf import settings\n+\n+import json\n+import requests\n+from gengo import Gengo, GengoError\n+\n+\n+# Cache of supported languages from Gengo. Theoretically, these don't\n+# change often, so the first time we request it, we cache it and then\n+# keep that until the next deployment.\n+GENGO_LANGUAGE_CACHE = None\n+\n+\n+class FjordGengoError(Exception):\n+    \"\"\"Superclass for all Gengo translation errors\"\"\"\n+    pass\n+\n+\n+class GengoConfigurationError(FjordGengoError):\n+    \"\"\"Raised when the Gengo-centric keys aren't set in settings\"\"\"\n+\n+\n+class GengoUnknownLanguage(FjordGengoError):\n+    \"\"\"Raised when the guesser can't guess the language\"\"\"\n+\n+\n+class GengoUnsupportedLanguage(FjordGengoError):\n+    \"\"\"Raised when the guesser guesses a language Gengo doesn't support\n+\n+    .. Note::\n+\n+       If you buy me a beer, I'll happily tell you how I feel about\n+       this.\n+\n+    \"\"\"\n+\n+\n+class GengoMachineTranslationFailure(FjordGengoError):\n+    \"\"\"Raised when machine translation didn't work\"\"\"\n+\n+\n+def requires_keys(fun):\n+    \"\"\"Throw GengoConfigurationError if keys aren't set\"\"\"\n+    @wraps(fun)\n+    def _requires_keys(self, *args, **kwargs):\n+        if not self.gengo_api:\n+            raise GengoConfigurationError()\n+        return fun(self, *args, **kwargs)\n+    return _requires_keys\n+\n+\n+class FjordGengo(object):\n+    def __init__(self):\n+        \"\"\"Constructs a FjordGengo wrapper around the Gengo class\n+\n+        We do this to make using the API a little easier in the\n+        context for Fjord as it includes the business logic around\n+        specific use cases we have.\n+\n+        Also, having all the Gengo API stuff in one place makes it\n+        easier for mocking when testing.\n+\n+        \"\"\"\n+        if settings.GENGO_PUBLIC_KEY and settings.GENGO_PRIVATE_KEY:\n+            gengo_api = Gengo(\n+                public_key=settings.GENGO_PUBLIC_KEY,\n+                private_key=settings.GENGO_PRIVATE_KEY\n+            )\n+        else:\n+            gengo_api = None\n+\n+        self.gengo_api = gengo_api\n+\n+    @requires_keys\n+    def get_balance(self):\n+        \"\"\"Returns the account balance as a float\"\"\"\n+        balance = self.gengo_api.getAccountBalance()\n+        return float(balance['response']['credits'])\n+\n+    @requires_keys\n+    def get_languages(self):\n+        \"\"\"Returns the list of supported language targets\n+\n+        .. Note::\n+\n+           This is cached until the next deployment.\n+\n+        \"\"\"\n+        global GENGO_LANGUAGE_CACHE\n+        if not GENGO_LANGUAGE_CACHE:","path":"fjord/translations/gengo_utils.py","position":92,"original_position":92,"commit_id":"6bded976b8f1b704670382eee8be5be13d4e243e","original_commit_id":"6bded976b8f1b704670382eee8be5be13d4e243e","user":{"login":"rlr","id":36629,"avatar_url":"https://avatars.githubusercontent.com/u/36629?","gravatar_id":"00e30f0c3219349c80f3ff7aacc9d34a","url":"https://api.github.com/users/rlr","html_url":"https://github.com/rlr","followers_url":"https://api.github.com/users/rlr/followers","following_url":"https://api.github.com/users/rlr/following{/other_user}","gists_url":"https://api.github.com/users/rlr/gists{/gist_id}","starred_url":"https://api.github.com/users/rlr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rlr/subscriptions","organizations_url":"https://api.github.com/users/rlr/orgs","repos_url":"https://api.github.com/users/rlr/repos","events_url":"https://api.github.com/users/rlr/events{/privacy}","received_events_url":"https://api.github.com/users/rlr/received_events","type":"User","site_admin":false},"body":"I don't think this is thread safe. Does it matter? Is everything OK if two threads enter this if statement at the same time?","created_at":"2014-05-22T14:53:11Z","updated_at":"2014-05-22T14:53:11Z","html_url":"https://github.com/mozilla/fjord/pull/289#discussion_r12953994","pull_request_url":"https://api.github.com/repos/mozilla/fjord/pulls/289","_links":{"self":{"href":"https://api.github.com/repos/mozilla/fjord/pulls/comments/12953994"},"html":{"href":"https://github.com/mozilla/fjord/pull/289#discussion_r12953994"},"pull_request":{"href":"https://api.github.com/repos/mozilla/fjord/pulls/289"}}}}