{"action":"created","comment":{"url":"https://api.github.com/repos/google/openhtf/pulls/comments/67462520","id":67462520,"diff_hunk":"@@ -0,0 +1,160 @@\n+# coding: iso-8859-1\n+\n+# Copyright 2016 Google Inc. All Rights Reserved.\n+\n+# Licensed under the Apache License, Version 2.0 (the \"License\");\n+# you may not use this file except in compliance with the License.\n+# You may obtain a copy of the License at\n+\n+#     http://www.apache.org/licenses/LICENSE-2.0\n+\n+# Unless required by applicable law or agreed to in writing, software\n+# distributed under the License is distributed on an \"AS IS\" BASIS,\n+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+# See the License for the specific language governing permissions and\n+# limitations under the License.\n+\n+\n+\"\"\"Read in a .xls file and generate OpenHTF Unit objects.\n+\n+UNECE, the United Nations Economic Commision for Europe, publishes a set of\n+unit codes for international trade in the form of Excel spreadsheets (.xls\n+files) here:\n+\n+http://www.unece.org/cefact/codesfortrade/codes_index.html\n+\n+This tool is used to parse those spreadsheets and turn the published standard\n+code set into a .py file that OpenHTF can import.\n+\n+Typical usage looks like:\n+\n+python ./bin/units_from_xls.py --infile ./Rec20_Rev6e_2009.xls \\\n+--outfile ./__init__.py\n+\n+\"\"\"\n+\n+\n+import argparse\n+import os\n+import shutil\n+import sys\n+from tempfile import mkstemp\n+\n+import xlrd\n+\n+\n+# Column names for the columns we care about. This list must be populated in\n+# the expected order: [<name label>, <code label>, <suffix label>].\n+COLUMN_NAMES = ['Name',\n+                'Common\\nCode',\n+                'Symbol']\n+\n+# The contents of the lines of the output file between which the generated code\n+# will be inserted.\n+START_DELIMITER = '######## BEGIN_UNITS ########'\n+END_DELIMITER = '######## END_UNITS ########'\n+\n+SHEET_NAME = 'Annex I'\n+REPLACEMENTS = {' ': '_',\n+                '[': '',\n+                ']': '',\n+                '(': '',\n+                ')': '',\n+                ',': '.',\n+                u'°': 'DEG_',\n+               }\n+\n+\n+def main():\n+  \"\"\"Main entry point for UNECE code .xls parsing.\"\"\"\n+  parser = argparse.ArgumentParser(description='Units From XLS',\n+                                   prog='python units_from_xls.py')\n+  parser.add_argument('--infile', type=str,\n+                      help='A .xls file to parse.')\n+  parser.add_argument('--outfile', type=str, default='__init__.py',\n+                      help='Where to put the generated .py file.')\n+  args = parser.parse_args()\n+\n+  code = code_generator(\n+      xlrd.open_workbook(args.infile).sheet_by_name(SHEET_NAME))\n+\n+  insert_into_file(args.outfile, code, START_DELIMITER, END_DELIMITER)\n+\n+\n+def code_generator(sheet):\n+  \"\"\"A generator that parses a worksheet containing UNECE code definitions.\n+\n+  Args:\n+    sheet: An xldr.sheet object representing a UNECE code worksheet.\n+  Yields: An iterable of lines of Python source code defining OpenHTF Units.\n+  \"\"\"\n+  seen = set()\n+  try:\n+    col_indices = {}\n+    rows = sheet.get_rows()\n+    \n+    # Find the indices for the columns we care about.\n+    for idx, cell in enumerate(rows.next()):\n+      if cell.value in COLUMN_NAMES:\n+        col_indices[cell.value] = idx\n+\n+    # loop over all remaining rows and pull out units.\n+    for row in rows:\n+      name = row[col_indices[COLUMN_NAMES[0]]].value\n+      code = row[col_indices[COLUMN_NAMES[1]]].value\n+      suffix = multi_replace(row[col_indices[COLUMN_NAMES[2]]].value,\n+                             {\"'\": \"\\\\\\'\", '\"': '\\\\\\\"'})\n+      if name in seen:\n+        continue\n+      seen.add(name)\n+\n+      yield 'UNITS[\\'%s\\'] = Unit(\\'%s\\', \\'%s\\', \\'%s\\')\\n' % (\n+          multi_replace(name.upper(), REPLACEMENTS), name, code, suffix)\n+\n+  except xlrd.XLRDError:\n+    sys.stdout.write('Unable to process the .xls file.')\n+\n+\n+def multi_replace(string, replacements=None):\n+  \"\"\"Return the string in uppercase with the specified substitutions.\"\"\"\n+  replacements = replacements or {}\n+  result = string\n+\n+  for old, new in replacements.iteritems():\n+    result = result.replace(old, new)\n+\n+  return result\n+\n+\n+def insert_into_file(filepath, code, start, end):","path":"openhtf/util/units/bin/units_from_xls.py","position":129,"original_position":129,"commit_id":"5b7ca3e8a6d97993d4769714c459ca1f750ec3e6","original_commit_id":"5b7ca3e8a6d97993d4769714c459ca1f750ec3e6","user":{"login":"grybmadsci","id":14081283,"avatar_url":"https://avatars.githubusercontent.com/u/14081283?v=3","gravatar_id":"","url":"https://api.github.com/users/grybmadsci","html_url":"https://github.com/grybmadsci","followers_url":"https://api.github.com/users/grybmadsci/followers","following_url":"https://api.github.com/users/grybmadsci/following{/other_user}","gists_url":"https://api.github.com/users/grybmadsci/gists{/gist_id}","starred_url":"https://api.github.com/users/grybmadsci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grybmadsci/subscriptions","organizations_url":"https://api.github.com/users/grybmadsci/orgs","repos_url":"https://api.github.com/users/grybmadsci/repos","events_url":"https://api.github.com/users/grybmadsci/events{/privacy}","received_events_url":"https://api.github.com/users/grybmadsci/received_events","type":"User","site_admin":false},"body":"Also, the header should have in caps at the very top of the file \"THIS FILE IS AUTOMATICALLY GENERATED, DO NOT EDIT\"","created_at":"2016-06-17T05:36:17Z","updated_at":"2016-06-17T05:36:17Z","html_url":"https://github.com/google/openhtf/pull/334#discussion_r67462520","pull_request_url":"https://api.github.com/repos/google/openhtf/pulls/334","_links":{"self":{"href":"https://api.github.com/repos/google/openhtf/pulls/comments/67462520"},"html":{"href":"https://github.com/google/openhtf/pull/334#discussion_r67462520"},"pull_request":{"href":"https://api.github.com/repos/google/openhtf/pulls/334"}}},"pull_request":{"url":"https://api.github.com/repos/google/openhtf/pulls/334","id":74174092,"html_url":"https://github.com/google/openhtf/pull/334","diff_url":"https://github.com/google/openhtf/pull/334.diff","patch_url":"https://github.com/google/openhtf/pull/334.patch","issue_url":"https://api.github.com/repos/google/openhtf/issues/334","number":334,"state":"open","locked":false,"title":"Programmatically generate unit codes.","user":{"login":"jettisonjoe","id":8150422,"avatar_url":"https://avatars.githubusercontent.com/u/8150422?v=3","gravatar_id":"","url":"https://api.github.com/users/jettisonjoe","html_url":"https://github.com/jettisonjoe","followers_url":"https://api.github.com/users/jettisonjoe/followers","following_url":"https://api.github.com/users/jettisonjoe/following{/other_user}","gists_url":"https://api.github.com/users/jettisonjoe/gists{/gist_id}","starred_url":"https://api.github.com/users/jettisonjoe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jettisonjoe/subscriptions","organizations_url":"https://api.github.com/users/jettisonjoe/orgs","repos_url":"https://api.github.com/users/jettisonjoe/repos","events_url":"https://api.github.com/users/jettisonjoe/events{/privacy}","received_events_url":"https://api.github.com/users/jettisonjoe/received_events","type":"User","site_admin":false},"body":"Here's a first attempt at generating UNECE codes from the published spec (a .xls file :-\\). What do folks think of this approach?","created_at":"2016-06-17T00:36:34Z","updated_at":"2016-06-17T05:36:17Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e63fb6c33e637208db1a18792d49bd0036c8173f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/google/openhtf/pulls/334/commits","review_comments_url":"https://api.github.com/repos/google/openhtf/pulls/334/comments","review_comment_url":"https://api.github.com/repos/google/openhtf/pulls/comments{/number}","comments_url":"https://api.github.com/repos/google/openhtf/issues/334/comments","statuses_url":"https://api.github.com/repos/google/openhtf/statuses/5b7ca3e8a6d97993d4769714c459ca1f750ec3e6","head":{"label":"jettisonjoe:units","ref":"units","sha":"5b7ca3e8a6d97993d4769714c459ca1f750ec3e6","user":{"login":"jettisonjoe","id":8150422,"avatar_url":"https://avatars.githubusercontent.com/u/8150422?v=3","gravatar_id":"","url":"https://api.github.com/users/jettisonjoe","html_url":"https://github.com/jettisonjoe","followers_url":"https://api.github.com/users/jettisonjoe/followers","following_url":"https://api.github.com/users/jettisonjoe/following{/other_user}","gists_url":"https://api.github.com/users/jettisonjoe/gists{/gist_id}","starred_url":"https://api.github.com/users/jettisonjoe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jettisonjoe/subscriptions","organizations_url":"https://api.github.com/users/jettisonjoe/orgs","repos_url":"https://api.github.com/users/jettisonjoe/repos","events_url":"https://api.github.com/users/jettisonjoe/events{/privacy}","received_events_url":"https://api.github.com/users/jettisonjoe/received_events","type":"User","site_admin":false},"repo":{"id":41824189,"name":"openhtf","full_name":"jettisonjoe/openhtf","owner":{"login":"jettisonjoe","id":8150422,"avatar_url":"https://avatars.githubusercontent.com/u/8150422?v=3","gravatar_id":"","url":"https://api.github.com/users/jettisonjoe","html_url":"https://github.com/jettisonjoe","followers_url":"https://api.github.com/users/jettisonjoe/followers","following_url":"https://api.github.com/users/jettisonjoe/following{/other_user}","gists_url":"https://api.github.com/users/jettisonjoe/gists{/gist_id}","starred_url":"https://api.github.com/users/jettisonjoe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jettisonjoe/subscriptions","organizations_url":"https://api.github.com/users/jettisonjoe/orgs","repos_url":"https://api.github.com/users/jettisonjoe/repos","events_url":"https://api.github.com/users/jettisonjoe/events{/privacy}","received_events_url":"https://api.github.com/users/jettisonjoe/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jettisonjoe/openhtf","description":null,"fork":true,"url":"https://api.github.com/repos/jettisonjoe/openhtf","forks_url":"https://api.github.com/repos/jettisonjoe/openhtf/forks","keys_url":"https://api.github.com/repos/jettisonjoe/openhtf/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jettisonjoe/openhtf/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jettisonjoe/openhtf/teams","hooks_url":"https://api.github.com/repos/jettisonjoe/openhtf/hooks","issue_events_url":"https://api.github.com/repos/jettisonjoe/openhtf/issues/events{/number}","events_url":"https://api.github.com/repos/jettisonjoe/openhtf/events","assignees_url":"https://api.github.com/repos/jettisonjoe/openhtf/assignees{/user}","branches_url":"https://api.github.com/repos/jettisonjoe/openhtf/branches{/branch}","tags_url":"https://api.github.com/repos/jettisonjoe/openhtf/tags","blobs_url":"https://api.github.com/repos/jettisonjoe/openhtf/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jettisonjoe/openhtf/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jettisonjoe/openhtf/git/refs{/sha}","trees_url":"https://api.github.com/repos/jettisonjoe/openhtf/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jettisonjoe/openhtf/statuses/{sha}","languages_url":"https://api.github.com/repos/jettisonjoe/openhtf/languages","stargazers_url":"https://api.github.com/repos/jettisonjoe/openhtf/stargazers","contributors_url":"https://api.github.com/repos/jettisonjoe/openhtf/contributors","subscribers_url":"https://api.github.com/repos/jettisonjoe/openhtf/subscribers","subscription_url":"https://api.github.com/repos/jettisonjoe/openhtf/subscription","commits_url":"https://api.github.com/repos/jettisonjoe/openhtf/commits{/sha}","git_commits_url":"https://api.github.com/repos/jettisonjoe/openhtf/git/commits{/sha}","comments_url":"https://api.github.com/repos/jettisonjoe/openhtf/comments{/number}","issue_comment_url":"https://api.github.com/repos/jettisonjoe/openhtf/issues/comments{/number}","contents_url":"https://api.github.com/repos/jettisonjoe/openhtf/contents/{+path}","compare_url":"https://api.github.com/repos/jettisonjoe/openhtf/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jettisonjoe/openhtf/merges","archive_url":"https://api.github.com/repos/jettisonjoe/openhtf/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jettisonjoe/openhtf/downloads","issues_url":"https://api.github.com/repos/jettisonjoe/openhtf/issues{/number}","pulls_url":"https://api.github.com/repos/jettisonjoe/openhtf/pulls{/number}","milestones_url":"https://api.github.com/repos/jettisonjoe/openhtf/milestones{/number}","notifications_url":"https://api.github.com/repos/jettisonjoe/openhtf/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jettisonjoe/openhtf/labels{/name}","releases_url":"https://api.github.com/repos/jettisonjoe/openhtf/releases{/id}","deployments_url":"https://api.github.com/repos/jettisonjoe/openhtf/deployments","created_at":"2015-09-02T19:50:25Z","updated_at":"2016-01-12T23:54:07Z","pushed_at":"2016-06-17T00:34:18Z","git_url":"git://github.com/jettisonjoe/openhtf.git","ssh_url":"git@github.com:jettisonjoe/openhtf.git","clone_url":"https://github.com/jettisonjoe/openhtf.git","svn_url":"https://github.com/jettisonjoe/openhtf","homepage":null,"size":24115,"stargazers_count":0,"watchers_count":0,"language":"Python","has_issues":false,"has_downloads":false,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"google:master","ref":"master","sha":"c7cb9fae487a5a221bb3a6861cca6f40cd971a6e","user":{"login":"google","id":1342004,"avatar_url":"https://avatars.githubusercontent.com/u/1342004?v=3","gravatar_id":"","url":"https://api.github.com/users/google","html_url":"https://github.com/google","followers_url":"https://api.github.com/users/google/followers","following_url":"https://api.github.com/users/google/following{/other_user}","gists_url":"https://api.github.com/users/google/gists{/gist_id}","starred_url":"https://api.github.com/users/google/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/google/subscriptions","organizations_url":"https://api.github.com/users/google/orgs","repos_url":"https://api.github.com/users/google/repos","events_url":"https://api.github.com/users/google/events{/privacy}","received_events_url":"https://api.github.com/users/google/received_events","type":"Organization","site_admin":false},"repo":{"id":41519483,"name":"openhtf","full_name":"google/openhtf","owner":{"login":"google","id":1342004,"avatar_url":"https://avatars.githubusercontent.com/u/1342004?v=3","gravatar_id":"","url":"https://api.github.com/users/google","html_url":"https://github.com/google","followers_url":"https://api.github.com/users/google/followers","following_url":"https://api.github.com/users/google/following{/other_user}","gists_url":"https://api.github.com/users/google/gists{/gist_id}","starred_url":"https://api.github.com/users/google/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/google/subscriptions","organizations_url":"https://api.github.com/users/google/orgs","repos_url":"https://api.github.com/users/google/repos","events_url":"https://api.github.com/users/google/events{/privacy}","received_events_url":"https://api.github.com/users/google/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/google/openhtf","description":null,"fork":false,"url":"https://api.github.com/repos/google/openhtf","forks_url":"https://api.github.com/repos/google/openhtf/forks","keys_url":"https://api.github.com/repos/google/openhtf/keys{/key_id}","collaborators_url":"https://api.github.com/repos/google/openhtf/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/google/openhtf/teams","hooks_url":"https://api.github.com/repos/google/openhtf/hooks","issue_events_url":"https://api.github.com/repos/google/openhtf/issues/events{/number}","events_url":"https://api.github.com/repos/google/openhtf/events","assignees_url":"https://api.github.com/repos/google/openhtf/assignees{/user}","branches_url":"https://api.github.com/repos/google/openhtf/branches{/branch}","tags_url":"https://api.github.com/repos/google/openhtf/tags","blobs_url":"https://api.github.com/repos/google/openhtf/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/google/openhtf/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/google/openhtf/git/refs{/sha}","trees_url":"https://api.github.com/repos/google/openhtf/git/trees{/sha}","statuses_url":"https://api.github.com/repos/google/openhtf/statuses/{sha}","languages_url":"https://api.github.com/repos/google/openhtf/languages","stargazers_url":"https://api.github.com/repos/google/openhtf/stargazers","contributors_url":"https://api.github.com/repos/google/openhtf/contributors","subscribers_url":"https://api.github.com/repos/google/openhtf/subscribers","subscription_url":"https://api.github.com/repos/google/openhtf/subscription","commits_url":"https://api.github.com/repos/google/openhtf/commits{/sha}","git_commits_url":"https://api.github.com/repos/google/openhtf/git/commits{/sha}","comments_url":"https://api.github.com/repos/google/openhtf/comments{/number}","issue_comment_url":"https://api.github.com/repos/google/openhtf/issues/comments{/number}","contents_url":"https://api.github.com/repos/google/openhtf/contents/{+path}","compare_url":"https://api.github.com/repos/google/openhtf/compare/{base}...{head}","merges_url":"https://api.github.com/repos/google/openhtf/merges","archive_url":"https://api.github.com/repos/google/openhtf/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/google/openhtf/downloads","issues_url":"https://api.github.com/repos/google/openhtf/issues{/number}","pulls_url":"https://api.github.com/repos/google/openhtf/pulls{/number}","milestones_url":"https://api.github.com/repos/google/openhtf/milestones{/number}","notifications_url":"https://api.github.com/repos/google/openhtf/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/google/openhtf/labels{/name}","releases_url":"https://api.github.com/repos/google/openhtf/releases{/id}","deployments_url":"https://api.github.com/repos/google/openhtf/deployments","created_at":"2015-08-28T01:14:17Z","updated_at":"2016-06-15T20:42:21Z","pushed_at":"2016-06-17T00:36:34Z","git_url":"git://github.com/google/openhtf.git","ssh_url":"git@github.com:google/openhtf.git","clone_url":"https://github.com/google/openhtf.git","svn_url":"https://github.com/google/openhtf","homepage":null,"size":24232,"stargazers_count":29,"watchers_count":29,"language":"Python","has_issues":true,"has_downloads":false,"has_wiki":true,"has_pages":false,"forks_count":19,"mirror_url":null,"open_issues_count":77,"forks":19,"open_issues":77,"watchers":29,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/google/openhtf/pulls/334"},"html":{"href":"https://github.com/google/openhtf/pull/334"},"issue":{"href":"https://api.github.com/repos/google/openhtf/issues/334"},"comments":{"href":"https://api.github.com/repos/google/openhtf/issues/334/comments"},"review_comments":{"href":"https://api.github.com/repos/google/openhtf/pulls/334/comments"},"review_comment":{"href":"https://api.github.com/repos/google/openhtf/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/google/openhtf/pulls/334/commits"},"statuses":{"href":"https://api.github.com/repos/google/openhtf/statuses/5b7ca3e8a6d97993d4769714c459ca1f750ec3e6"}}}}
{"actor":{"display_login":"grybmadsci"}}