Snippets

Adam Labadorf python git integration

Updated by Adam Labadorf

File git_integrations.py Modified

  • Ignore whitespace
  • Hide word diff
+# this file has the shortened URL of 'http://bit.do/git_int'
+# to load on the fly:
+"""
+import urllib.request as ur
+exec(ur.urlopen('http://bit.do/git_int').read())
+"""
 import inspect
 import glob
 import json
Updated by Adam Labadorf

File git_integrations.py Modified

  • Ignore whitespace
  • Hide word diff
             cobj = compile(f.read(),pyfn,'exec')
             exec(cobj,globals(),locals())
             # things declared/defined in the module show up in co_consts like follows
-            #<code object d3coffeescriptmagic at 0x7f4cd408a6f0, file ".git_repos/x684a/jupyter_snippet.py", line 6>
+            #<code object (?P<varname>[^ ]+) at 0x7f4cd408a6f0, file "(?P<path_to>[^"]*)?{pyfn}", line 6>
             src_defines = []
             for const in cobj.co_consts :
                 const = repr(const)
Created by Adam Labadorf

File git_integrations.py Added

  • Ignore whitespace
  • Hide word diff
+import inspect
+import glob
+import json
+import os
+from pprint import pprint
+import re
+import subprocess
+import urllib.request as ur
+
+from IPython.core.magic import register_line_magic
+ 
+git_repo_dir = '.git_repos'
+not os.path.exists(git_repo_dir) and os.mkdir(git_repo_dir)
+
+@register_line_magic
+def bitbucket_snippet(line) :
+    line = line.split()
+    if len(line) != 2 :
+        print('Usage:\n'
+              '%bitbucket_snippet <username> <snippet id>\n'
+              'e.g. %bitbucket_snippet adamlabadorf x684a')
+        return
+    owner, snippet_id = line
+    get_bitbucket_snippet(owner,snippet_id)
+    
+def get_bitbucket_snippet(owner, snip_id) :
+    snippet_url = 'https://api.bitbucket.org/2.0/snippets/{owner}/{snip_id}'
+
+    params = dict(
+        owner = owner,
+        snip_id = snip_id
+    )
+
+    # fetch json obj
+    req = ur.urlopen(snippet_url.format(**params)).read()
+    req_json = json.loads(req.decode('utf-8'))
+
+    if not req_json.get('links') or not req_json.get('links').get('clone') :
+        raise Exception('unexpected JSON structure: {}'.format(
+                json.dumps(req_json,indent=2))
+        )
+
+    href = [_['href'] for _ in req_json['links']['clone'] if _['name'] == 'https']
+    if not href :
+        raise Exception('No https href was found for snippet')
+    href = href[0]
+
+    snip_id = req_json.get('id',params['snip_id'])
+    repo_dir = '{}/{}'.format(git_repo_dir,snip_id)
+    
+    git_clone_or_pull(href,repo_dir)
+    
+    exec_dir(repo_dir)
+
+def git_clone_or_pull(href,repo_dir) :
+    if os.path.exists(repo_dir) :
+        git_cmd = ('git','pull')
+        p = subprocess.Popen(git_cmd
+                             ,stderr=subprocess.PIPE
+                             ,stdout=subprocess.PIPE
+                             ,cwd=repo_dir
+                            )
+    else :
+        git_cmd = ('git','clone',href,repo_dir)
+        p = subprocess.Popen(git_cmd
+                             ,stderr=subprocess.PIPE
+                             ,stdout=subprocess.PIPE
+                            )
+    stdout, stderr = p.communicate()
+    if stderr.strip() :
+        print('git command: {}'.format(' '.join(git_cmd)))
+        print('Stderr: {}'.format(stderr))
+    return
+
+def exec_dir(py_dir) :
+    # exec all of the python code in the dir
+    # you'd better trust that code!
+    for pyfn in glob.glob('{}/*.py'.format(py_dir)) :
+        # http://stackoverflow.com/questions/436198/what-is-an-alternative-to-execfile-in-python-3-0
+        print('Sourcing {}'.format(pyfn))
+        with open(pyfn) as f :
+            cobj = compile(f.read(),pyfn,'exec')
+            exec(cobj,globals(),locals())
+            # things declared/defined in the module show up in co_consts like follows
+            #<code object d3coffeescriptmagic at 0x7f4cd408a6f0, file ".git_repos/x684a/jupyter_snippet.py", line 6>
+            src_defines = []
+            for const in cobj.co_consts :
+                const = repr(const)
+                if pyfn in const :
+                    patt = '<code object ([^ ]+).*'
+                    src_defines.append(re.match(patt,const).group(1))
+            print('    defined: {}'.format(', '.join(src_defines)))
  1. 1
  2. 2
  3. 3
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.