Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 5c895504173298288778ae5619af44c29e6669fa 0 parents
@joar authored
Showing with 124 additions and 0 deletions.
  1. +6 −0 .gitignore
  2. +64 −0 automgtic/__init__.py
  3. +33 −0 automgtic/models.py
  4. +21 −0 run.py
6 .gitignore
@@ -0,0 +1,6 @@
+/automgtic.db
+/bin
+/include
+/lib
+/local
+.py[co]
64 automgtic/__init__.py
@@ -0,0 +1,64 @@
+from urllib2 import urlopen
+
+from oauthlib.oauth2.draft25 import WebApplicationClient
+from automgtic.models import Config, session
+
+
+def set_client_id(client_id):
+ client_id = client_id or raw_input('Client identifier: ')
+ set_config('client_id', client_id)
+
+
+def authorize():
+ client = get_client()
+ uri = client.prepare_request_uri(
+ get_config('mg_server') + '/oauth/authorize',
+ redirect_uri='http://foo.example/')
+
+ print 'Go to {0}, then paste the "?code=$CODE" $CODE part.'.format(uri)
+ code = raw_input('code: ')
+
+ token_uri = client.prepare_request_uri(
+ get_config('mg_server') + '/oauth/access_token',
+ code=code)
+
+ token_request = urlopen(token_uri)
+ token_response = token_request.read()
+
+ token_data = client.parse_request_body_response(token_response)
+
+ print 'Token data: {0}'.format(token_data)
+
+ set_config('access_token', token_data['access_token'])
+
+
+def get_client():
+ client = WebApplicationClient(get_config('client_id'))
+ return client
+
+
+def get_config(key, default=None):
+ conf = Config.query.filter(Config.key == unicode(key)).first()
+
+ if not conf and not default:
+ raise ValueError('{0} is not configured.'.format(key))
+ elif not conf:
+ return default
+ else:
+ return conf.value
+
+
+def set_config(key, value, checkfirst=False):
+ conf = Config.query.filter(Config.key == unicode(key)).first()
+
+ if checkfirst:
+ if conf:
+ raise NameError('{0} is already set in the configuration.'.format(key))
+
+ if conf:
+ conf.value = unicode(value)
+ else:
+ conf = Config(unicode(key), unicode(value))
+ session.add(conf)
+
+ session.commit()
33 automgtic/models.py
@@ -0,0 +1,33 @@
+from sqlalchemy import create_engine, Column, Integer, Unicode
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import sessionmaker, scoped_session
+
+engine = create_engine('sqlite:///automgtic.db', echo=True)
+
+Session = scoped_session(sessionmaker(bind=engine))
+session = Session()
+
+
+class AutomgticBase(object):
+ query = Session.query_property()
+
+Base = declarative_base(cls=AutomgticBase)
+
+
+class Config(Base):
+ __tablename__ = 'core__config'
+
+ id = Column(Integer, primary_key=True)
+ key = Column(Unicode, index=True, unique=True)
+ value = Column(Unicode, index=True)
+
+ def __init__(self, key, value):
+ self.key = key
+ self.value = value
+
+ def __repr__(self):
+ return '<{0} {1}: {2} = {3}>'.format(
+ self.__class__.__name__,
+ self.id,
+ self.key.decode('ascii', 'replace'),
+ self.value.decode('ascii', 'replace'))
21 run.py
@@ -0,0 +1,21 @@
+import argparse
+import automgtic
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(
+ description='automgtic - Autouploader for GNU MediaGoblin')
+ parser.add_argument(
+ '--authorize',
+ const=True,
+ action='store_const',
+ help='Initiate the configuration.')
+ parser.add_argument(
+ '--run',
+ const=True,
+ action='store_const',
+ help='Run the autouploader')
+
+ args = parser.parse_args()
+
+ if args.authorize:
+ automgtic.authorize()
Please sign in to comment.
Something went wrong with that request. Please try again.