Skip to content

Commit

Permalink
Updates to allow login from google docs. Fixes unicode errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanc1 committed Nov 20, 2016
1 parent d12f2b2 commit 230a2db
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ ENV/
.ropeproject
*.sublime-workspace
*.sublime-project
client_secret.json
36 changes: 36 additions & 0 deletions google2xmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from __future__ import print_function
import httplib2

from apiclient import discovery
from xmd.google import get_credentials
from oauth2client import tools

try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None


def main():
"""Shows basic usage of the Google Drive API.
Creates a Google Drive API service object and outputs the names and IDs
for up to 10 files.
"""
credentials = get_credentials(flags)
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v3', http=http)

results = service.files().list(
pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
if not items:
print('No files found.')
else:
print('Files:')
for item in items:
print('{0} ({1})'.format(item['name'], item['id']))

if __name__ == '__main__':
main()
14 changes: 11 additions & 3 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

figures = """
[>figure('a_figure.png', numbered=True, a=(1,))]{the figure caption}
[>figure('a_figure.png', numbered=True)]{the figure caption}
"""

weird = "\r\n## DC resistivity \r\n [>sidenote]{\r\n[>figure(https://raw.githubusercontent.com/simpeg/tle-finitevolume/master/images/DCSurvey.png)]{\r\n Setup of a DC resistivity survey.\r\n }\r\n}\r\n\r\n\r\nDC resistivity surveys obtain"


class Basic(unittest.TestCase):

Expand All @@ -47,11 +49,17 @@ def test_paragraph(self):
assert '<{}>'.format(tag) in render
assert '</{}>'.format(tag) in render

def test_figures(self):
render = xmd.render(figures)
# def test_figures(self):
# render = xmd.render(figures)
# print(render)


def test_returns(self):
render = xmd.render(weird)
print(render)




if __name__ == '__main__':
unittest.main()
5 changes: 5 additions & 0 deletions xmd/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
from parser import transform, render, parse_file
from . import google

from .ipython import set_style
set_style()
del set_style
56 changes: 56 additions & 0 deletions xmd/google.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from __future__ import print_function
import os
import httplib2

from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage

# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/google2xmd.json
privilege = ['', '.appdata', '.file', '.metadata', '.readonly']
location = 'https://www.googleapis.com/auth/drive'
SCOPES = [location + priv for priv in privilege]

CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'XMD'


def get_credentials(flags=None):
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'google2xmd.json')

store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
if flags:
credentials = tools.run_flow(flow, store, flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials


def get_service():
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
return discovery.build('drive', 'v3', http=http)


def get_file(fileId, mimeType='text/plain'):
req = get_service().files().export_media(fileId=fileId, mimeType=mimeType)
return req.execute().decode('utf-8').strip(u'\ufeff')
25 changes: 25 additions & 0 deletions xmd/ipython.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import os


def in_ipython():
try:
__IPYTHON__
return True
except NameError:
return False


def set_style():
if not in_ipython():
return

from IPython.display import HTML, display

with open(os.sep.join(__file__.split(os.sep)[:-1] + ['xmd.css'])) as f:
style = (
'<style type="text/css">'
'{}'
'</style>'.format(f.read())
)

display(HTML(style))
12 changes: 8 additions & 4 deletions xmd/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ def context(self, val):
self._context = val

def parseArguments(self):
print(self.args)
# print(self.args)
inner = self.args[0].strip('(').strip(')')
args = [_.strip() for _ in inner.split(',')]
# print(args)
return args


class Command(XmdNode):
Expand All @@ -69,16 +73,16 @@ def render(self):
class Figure(Command):

def render(self):
self.parseArguments()
args = self.parseArguments()
fignum = self.context.count(self.__class__.__name__)
src = self.args[0].strip('"')
src = args[0].strip('"').strip("'")
inner = ' \n'.join([x.render() for x in self.content])
inner = inner.strip('<p>').strip('</p>')
html = (
'<div class="figure">'
'<img src="{src}">'
'<div class="caption">'
'<strong>Figure {num}:</strong>{html}'
'<strong>Figure {num}:</strong> {html}'
'</div>'
'</div>'
)
Expand Down
23 changes: 21 additions & 2 deletions xmd/parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sys
import pyparsing as pp
import nodes

Expand Down Expand Up @@ -60,8 +61,25 @@ def recurse(s, b, c):
pp.Suppress('{{') + pp.Word(pp.alphas) + pp.Suppress('}}')
)

unicodePrintables = u''.join(
unichr(c) for c in xrange(sys.maxunicode)
if not (
unichr(c).isspace() or
unichr(c) == '[' or
unichr(c) == ']'
)
)

chars = (
"0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"!\"#$%&'(){}*+,-./:;<=>?@\^_`|~ \r\n" +
unicodePrintables
)

text_block = pp.Group(
pp.OneOrMore(pp.Word("""0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'(){}*+,-./:;<=>?@\^_`|~ \n"""))
pp.OneOrMore(pp.Word(chars))
).setParseAction(nodes.Markdown)

mdObject << pp.ZeroOrMore(pp.MatchFirst([Command, injector, text_block]))
Expand All @@ -81,7 +99,8 @@ def render(text):
context = nodes.Context()
for t in T:
t.context = context
return '\n'.join([x.render() for x in T.asList()])
out = '\n'.join([x.render() for x in T.asList()])
return u"""<div class="xmd-container">\n{}\n</div>""".format(out)


def parse_file(name):
Expand Down
28 changes: 28 additions & 0 deletions xmd/xmd.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.xmd-container{
position: relative;
width: calc(100% - 250px);
}

.sidenote{
position:absolute;
left: calc(100% + 15px);
width: 230px;
margin-top: -30px;
font-size: 13px;
color: #aaa;
font-family: "Helvetica";
}

.figure{
margin: 15px 0 30px 0;
}

.figure img{
width:100%;
}

.figure .caption{
margin-top:15px;
text-align: center;
color: #aaa;
}

0 comments on commit 230a2db

Please sign in to comment.