Example #1
0
	def _do_auth(self, token=None):
		# Authenticate to github, save some login info (user/pass, or oauth token)
		conf = config.getConfig()
		auth = conf.getboolean('gist.auth', False) or token is not None
		if auth: # User/Pass Pair
			logger.info('auth: oauth token')
			if token is None:
				token = conf.require('gist.token')
			logger.debug('auth: test token usability')
			# Try authenticate
			self.req.headers['Authorization'] = 'token ' + token
			# Get a time in future (1 year)
			fmt_time = (datetime.datetime.now() + datetime.timedelta(days=365)).strftime('%Y-%m-%dT%H:%M:%SZ')
			test_url = _api_base + '/gists?since=' + fmt_time
			logger.debug('test url: ' + test_url)
			try:
				resp = self.req.get(test_url)
			except exceptions.RequestException as e:
				logger.warn('http error, assume token is good.')
				logger.info('[%s] %s' % (e.__class__.__name__, e.message))
				return
			logger.debug('http ok, response: %d %s' % (resp.status_code, resp.reason))
			if resp.status_code == 401: # Invalid token
				logger.warn('invalid token')
				return False
			elif resp.status_code == 200:
				logger.info('token ok.')
				return True
			else:
				logger.warn('unknown response status: %d %s' % (resp.status_code, resp.reason))
				raise exception.ServerException('Server responsed with unknown status: %d %s' % (resp.status_code, resp.reason))
		logger.info('auth: none')
		return None
Example #2
0
	def run(self):
		global _actions
		conf = config.getConfig()
		action = conf.require('action')
		if action not in _actions:
			logger.fatal('No function for action: ' + action)
		_actions[action](self)
Example #3
0
	def run(self):
		global _actions
		conf = config.getConfig()
		action = conf.require('action')
		if action not in _actions:
			logger.fatal('No function for action: ' + action)
		_actions[action](self)
Example #4
0
	def write_auth(self):
		# TODO: Implements auth
		conf = config.getConfig()
		fileconf = config.getGlobalConfig() if conf.require('global') else config.getUserConfig()
		remove = conf.require('remove')
		if remove:
			fileconf.remove('gist.auth')
			fileconf.remove('gist.token')
			print 'Authentication removed, you may delete the token from your user panel.'
			return
		if fileconf.get('gist.auth', False) and not conf.get('force', False):
			logger.info('check current token')
			try:
				token = fileconf.require('gist.token')
			except exception.NoSuchOption:
				fileconf.remove('gist.auth')
				return self.write_auth()
			result = self._do_auth(token=token)
			if result:
				print 'Current token is valid, no auth required.'
				return
			print 'Current token is invalid, requesting a new token.'
		token = self._perform_auth()
		logger.info('auth ok.')
		fileconf.set('gist.auth', True)
		fileconf.set('gist.token', token)
		logger.debug('saving to config file.')
		fileconf.save()
		print 'Done!'
Example #5
0
def run():
	parser = argparse.ArgumentParser(prog='paste.py', description='Push to or pull from paste pads!',
                                     conflict_handler='resolve', add_help=False,
                                     formatter_class=_Smart_formatter)
	opt_common = parser.add_argument_group('Common Options')
	opt_common.add_argument('-h', '--help', action='help',
                            help='Print this help message and exit.\n'
                            'Use `paste.py provider -h` for specific information.')
	opt_common.add_argument('-V', '--version', action='version', version='%(prog)s ' + _version)
	opt_log = parser.add_argument_group('Logging Options')
	opt_log.add_argument('--verbose', '-v', action='store_const', dest='log.level', const=logger.Level.INFO,
						 default=logger.Level.WARN, help='Enable verbose output.')
	opt_log.add_argument('--debug', '-g', action='store_const', dest='log.level', const=logger.Level.DEBUG,
						 help='Enable debug output. (VERY VERBOSE!)')
	opt_log.add_argument('--quiet', '-q', action='store_const', dest='log.level', const=logger.Level.ERROR,
						 help='Just be quiet, output only error message.')
	opt_log.add_argument('--simple-log', action='store_const', dest='log.format', const='{message}',
						 default=None, help='Output just simple message without timestamp, log level etc.')
	opt_log.add_argument('--no-color', action='store_const', dest='log.colorize', const=False,
						 default=True, help='Disable colorful output. Note: colorful is always false if output file is not a terminal.')
	opt_action = parser.add_subparsers(title='Paste pads', help='introduction', metavar='provider', dest='provider')
	__import__('providers', globals(), locals())
	for provider in ProviderBase.__subclasses__():
		ins = provider()
		opt_ins = opt_action.add_parser(ins._name, help=ins._info, conflict_handler='resolve')
		ins.add_args(opt_ins)
	args = parser.parse_args()
	conf = config.getConfig()
	for arg in args._get_kwargs():
		conf.set(arg[0], arg[1])
	logger.init(colorize=conf.getboolean('log.colorize'), level=conf.getint('log.level'), log_format=conf.get('log.format'))
	getProvider(conf.get('provider')).run()
Example #6
0
	def push_content(self):
		logger.debug('call: ubuntupaste.push_content')
		conf = config.getConfig()
		post_target	= 'http://paste.ubuntu.com/'
		logger.debug('post target: ' + post_target)
		poster		= conf.get('ubuntu.user', getpass.getuser())
		logger.debug('poster: ' + poster)
		# Get Filename for highlight.
		filename	= conf.require('src').name
		if filename == '-':
			print 'Type your content here, end with EOF'
			print 'Use Ctrl-C to interrupt, if you have mistyped something.'
		try:
			content = conf.require('src').read()
		except KeyboardInterrupt:
			logger.warn('Ctrl-C received, interrpted...')
			sys.exit(1)
		lines = content.count('\n')
		bytes = len(content)
		logger.info('content: %d lines, %d bytes' % (lines, bytes))
		lang = conf.get('ubuntu.lang', _get_language(filename))
		logger.debug('highlight: ' + lang)
		post_data	= {
			'poster'	: poster,
			'syntax'	: lang,
			'content'	: content,
		}
		try:
			resp = self.req.post(post_target, data=post_data, allow_redirects=False)
		except exceptions.RequestException as e:
			logger.info('Exception: ' + e.__class__.__name__)
			logger.error('Something went wrong when communicating with paste.ubuntu.com!')
			raise exception.ServerException(e)
			
		logger.debug('HTTP OK')
		logger.info('HTTP Status: %d %s' % (resp.status_code, resp.reason))
		
		if resp.status_code == 302:
			pastepad = resp.headers['location']
			logger.debug('location: ' + pastepad)
			pattern	= re.compile(r'^http:\/\/paste.ubuntu.com/(?P<paste_id>\d+)/$')
			res = pattern.match(pastepad)
			if not res:
				raise exception.ServerException('Unknown location: ' + pastepad)
			paste_id = res.group('paste_id')
			logger.info('paste_id: ' + paste_id)
			# return paste_id
			print 'Paste ID: ' + str(paste_id)
			print 'HTTP Link: ' + pastepad
			return
		
		if resp.status_code == 200:
			data = resp.content
			err_start_flag = '<ul class="errorlist"><li>'
			err_stop_flag = '</li></ul>'
			msg = self.html2text(self.fetch_between(resp.content, err_start_flag, err_stop_flag))
			raise exception.ServerException('Server refused our paste: ' + msg)
			
		raise exception.ServerException('Server responsed with unknown status %d %s' % (resp.status_code, resp.reason))
Example #7
0
from lib import config
import psycopg2
import psycopg2.extras
import json
import time
import sys
import traceback

connString = config.getConfig()['connString'];

def getConnection():
    try:
        return psycopg2.connect(connString);
    except:
        print('Unable to connect to database!')

def backUpCardSet(setId, filename, conn):
        card_set = getCardSet (setId, conn);
        
        #Remove Ids which we don't want while backing up to JSON
        for card in card_set['black_cards']:
            card.pop('id', None)
        for card in card_set['white_cards']:
            card.pop('id', None)

        encoded = json.dumps(card_set, separators=(',',':'));

        backup_file = open(filename, 'w');
        backup_file.write(encoded);
        backup_file.close();
                             
Example #8
0
	def pull_content(self):
		logger.debug('call: ubuntupaste.pull_content')
		conf = config.getConfig()
		fn = conf.require('dest')
		if fn == '-':
			fo = sys.stdout
		else:
			if os.path.exists(fn):
				if not conf.get('overwrite', False):
					raise exception.FileExists('File %s already exists.' % fn)
			fo = open(fn, 'w')
		_uri = conf.require('src')
		res = uri.parse(_uri)
		if res is None:
			raise exception.InvalidURI('Invalid URI: ' + _uri)
		logger.debug('uri format ok.')
		logger.debug('scheme: ' + res.scheme)
		if res.scheme == 'ubuntu':
			logger.info('using ubuntu:// style uri')
			pid = res.path
			logger.debug('path: ' + pid)
			for ch in pid:
				if not ch.isdigit():
					raise exception.InvalidURI('UbuntuPaste should only contains digits!')
			
			if pid != str(int(pid)):
				raise exception.InvalidURI('No leading zero allowed.')
		
			url = 'http://paste.ubuntu.com/{pid}/'.format(pid=pid)
			logger.info('to http url: ' + url)
		elif res.scheme == 'http':
			logger.info('using http:// style uri')
			if '/' not in res.path:
				raise exception.InvalidURI('Invalid http url: ' + _uri)
			host, path = map(lambda x : x.lower(), res.path.split('/', 1))
			# NOTE: Leading / in path is removed when using split.
			logger.debug('http host: ' + host)
			logger.debug('http path: ' + path)
			if host != 'paste.ubuntu.com':
				raise exception.InvalidURI('HTTP Host should be paste.ubuntu.com!')
			pattern = re.compile(r'^[1-9](?:\d+)(?:/?)')
			if not pattern.match(path):
				raise exception.InvalidURI('Invalid path for ubuntu paste!')
			
			# url validated.
			url = _uri
		else:
			raise exception.InvalidURI('Unknown scheme: ' + res.scheme)
		
		# Check if pad exists
		try:
			res = self.req.get(url)
		except exceptions.RequestException as e:
			logger.info('Exception: ' + e.__class__.__name__)
			logger.warn('Something wrong when communicating with paste.ubuntu.com, assume paste pad exists.')
			return url
		
		logger.debug('HTTP OK.')
		logger.info('Server response: %d %s' % (res.status_code, res.reason))
		
		if res.status_code == 200: # OK
			# Q = pyquery.PyQuery(res.content)
			# content	= pyquery.PyQuery(Q('.code').html().replace('\n', '<br />')).text()#
			start_flag	= '<td class="code"><div class="paste"><pre>'
			end_flag	= '</pre></div>'

			content		= self.html2text(self.fetch_between(res.content, start_flag, end_flag))
			logger.debug('content: %d lines, %d bytes' % (content.count('\n') + 1, len(content)))
			# return content
			fo.write(content)
			return
		
		if res.status_code >= 400 and res.status_code < 500:
			raise exception.NoSuchPad('No such pad: %s. Server responsed with status code %d' % (_uri, res.status_code))
		
		raise exception.ServerException('Server responsed with status code %d' % res.status_code)
Example #9
0
	def push(self):
		# TODO: Implements push.
		conf = config.getConfig()
		res = self._do_auth()
		if res is not None:
			if not res:
				if conf.getboolean('check', True):
					print 'Token is invalid, please use paste.py gist auth to get a new token.'
					sys.exit(1)
				else:
					del self.req.headers['Authorization']
		files = conf.require('files')
		if files.count(sys.stdin) > 1:
			raise exception.InvalidValue('stdin was listed more than once!')
		logger.debug('private: ' + ('yes' if conf.require('private') else 'no'))
		logger.debug('description: ' + conf.require('description'))
		logger.debug('files: ' + str(len(files)))
		post_data = {
			'public'		: not conf.require('private'),
			'description'	: conf.require('description'),
		}
		file_data = dict()
		try:
			for file in files:
				logger.info('reading file ' + file.name)
				if file is sys.stdin:
					print 'Type your content here, end with EOF'
					print 'Use Ctrl-C to interrupt, if you have mistyped something.'
				content = file.read()
				logger.debug('file ' + file.name + ': %d lines, %d bytes' % (content.count('\n'), len(content)))
				fname = os.path.basename(file.name)
				now = 2
				if fname in file_data:
					if '.' in fname:
						name, ext = fname.rsplit('.', 1)
					else:
						name, ext = fname, ''
					while (name + '-' + str(now) + '.' + ext) in file_data:
						now += 1
					fname = (name + '-' + str(now) + '.' + ext)
				logger.debug('final filename: ' + fname)
				file_data[fname] = {
					'content'	: content,
				}
		except KeyboardInterrupt:
			logger.warn('Ctrl-C received, exiting.')
			sys.exit(1)
		post_data['files'] = file_data
		post_str = json.dumps(post_data)
		post_url = _api_base + '/gists'
		logger.debug('post url: ' + post_url)
		try:
			resp = self.req.post(post_url, data=post_str, headers={
				'Content-Type'	: 'application/json',
			})
		except exceptions.RequestException as e:
			logger.error('Post error: ' + e.message)
			raise exception.ServerException(e)
		logger.debug('http ok.')
		logger.info('server response: %d %s' % (resp.status_code, resp.reason))
		if resp.status_code == 201:
			logger.info('gist created')
			url = resp.json()[u'html_url']
			gistid = url.rsplit('/', 1)[1]
			print 'HTTP Link: ' + url
			print 'Paste.py uri: gist://' + gistid
		else:
			raise exception.ServerException('Server responsed with unknown status: %d %s ' % (resp.status_code, resp.reason))
Example #10
0
import argparse
import enum
from lib import config

config = config.getConfig()

class ImportDuplicateBehavior(enum.Enum):
    fail = 1
    askAndFail = 2
    askAndSkip = 3
    link = 4

def getArgs():
    parser = argparse.ArgumentParser(description='CLI tools for a PYX database')
    commandparser = parser.add_subparsers(help="Commands", dest='command')
    commandparser.required = True

    #Insert
    insertparser = commandparser.add_parser('insert', help='Insert a card')
    
    icardtype = insertparser.add_subparsers(help='Card colors', dest='cardcolor')
    
    iwhitecard = icardtype.add_parser('white', help='Insert a white card')
    iwhitecard.add_argument('-t', '--text', type=str, help='Card text', required = True)
    iwhitecard.add_argument('-w', '--watermark', type=str, default=config['defaultWatermark'], help='Card watermark.  Defaults to ' + config['defaultWatermark'])
    iwhitecard.add_argument('-s', '--cardset', type=int, default=config['defaultCardSet'], help='Card set ID to insert the card into.  Defaults to ' + str(config['defaultCardSet']))
    
    iblackcard = icardtype.add_parser('black', help='Insert a black card')
    iblackcard.add_argument('-t', '--text', type=str, help='Card text', required = True)
    iblackcard.add_argument('-p', '--pick', type=int, default=1, help='Number of blanks on the card/cards to pick.  Defaults to one')
    iblackcard.add_argument('-d', '--draw', type=int, default=0, help='Number of cards to draw before playing.  Defaults to zero')
Example #11
0
    def pull_content(self):
        logger.debug('call: ubuntupaste.pull_content')
        conf = config.getConfig()
        fn = conf.require('dest')
        if fn == '-':
            fo = sys.stdout
        else:
            if os.path.exists(fn):
                if not conf.get('overwrite', False):
                    raise exception.FileExists('File %s already exists.' % fn)
            fo = open(fn, 'w')
        _uri = conf.require('src')
        res = uri.parse(_uri)
        if res is None:
            raise exception.InvalidURI('Invalid URI: ' + _uri)
        logger.debug('uri format ok.')
        logger.debug('scheme: ' + res.scheme)
        if res.scheme == 'ubuntu':
            logger.info('using ubuntu:// style uri')
            pid = res.path
            logger.debug('path: ' + pid)
            for ch in pid:
                if not ch.isdigit():
                    raise exception.InvalidURI(
                        'UbuntuPaste should only contains digits!')

            if pid != str(int(pid)):
                raise exception.InvalidURI('No leading zero allowed.')

            url = 'http://paste.ubuntu.com/{pid}/'.format(pid=pid)
            logger.info('to http url: ' + url)
        elif res.scheme == 'http':
            logger.info('using http:// style uri')
            if '/' not in res.path:
                raise exception.InvalidURI('Invalid http url: ' + _uri)
            host, path = map(lambda x: x.lower(), res.path.split('/', 1))
            # NOTE: Leading / in path is removed when using split.
            logger.debug('http host: ' + host)
            logger.debug('http path: ' + path)
            if host != 'paste.ubuntu.com':
                raise exception.InvalidURI(
                    'HTTP Host should be paste.ubuntu.com!')
            pattern = re.compile(r'^[1-9](?:\d+)(?:/?)')
            if not pattern.match(path):
                raise exception.InvalidURI('Invalid path for ubuntu paste!')

            # url validated.
            url = _uri
        else:
            raise exception.InvalidURI('Unknown scheme: ' + res.scheme)

        # Check if pad exists
        try:
            res = self.req.get(url)
        except exceptions.RequestException as e:
            logger.info('Exception: ' + e.__class__.__name__)
            logger.warn(
                'Something wrong when communicating with paste.ubuntu.com, assume paste pad exists.'
            )
            return url

        logger.debug('HTTP OK.')
        logger.info('Server response: %d %s' % (res.status_code, res.reason))

        if res.status_code == 200:  # OK
            # Q = pyquery.PyQuery(res.content)
            # content	= pyquery.PyQuery(Q('.code').html().replace('\n', '<br />')).text()#
            start_flag = '<td class="code"><div class="paste"><pre>'
            end_flag = '</pre></div>'

            content = self.html2text(
                self.fetch_between(res.content, start_flag, end_flag))
            logger.debug('content: %d lines, %d bytes' %
                         (content.count('\n') + 1, len(content)))
            # return content
            fo.write(content)
            return

        if res.status_code >= 400 and res.status_code < 500:
            raise exception.NoSuchPad(
                'No such pad: %s. Server responsed with status code %d' %
                (_uri, res.status_code))

        raise exception.ServerException(
            'Server responsed with status code %d' % res.status_code)
Example #12
0
    def push_content(self):
        logger.debug('call: ubuntupaste.push_content')
        conf = config.getConfig()
        post_target = 'http://paste.ubuntu.com/'
        logger.debug('post target: ' + post_target)
        poster = conf.get('ubuntu.user', getpass.getuser())
        logger.debug('poster: ' + poster)
        # Get Filename for highlight.
        filename = conf.require('src').name
        if filename == '-':
            print 'Type your content here, end with EOF'
            print 'Use Ctrl-C to interrupt, if you have mistyped something.'
        try:
            content = conf.require('src').read()
        except KeyboardInterrupt:
            logger.warn('Ctrl-C received, interrpted...')
            sys.exit(1)
        lines = content.count('\n')
        bytes = len(content)
        logger.info('content: %d lines, %d bytes' % (lines, bytes))
        lang = conf.get('ubuntu.lang', _get_language(filename))
        logger.debug('highlight: ' + lang)
        post_data = {
            'poster': poster,
            'syntax': lang,
            'content': content,
        }
        try:
            resp = self.req.post(post_target,
                                 data=post_data,
                                 allow_redirects=False)
        except exceptions.RequestException as e:
            logger.info('Exception: ' + e.__class__.__name__)
            logger.error(
                'Something went wrong when communicating with paste.ubuntu.com!'
            )
            raise exception.ServerException(e)

        logger.debug('HTTP OK')
        logger.info('HTTP Status: %d %s' % (resp.status_code, resp.reason))

        if resp.status_code == 302:
            pastepad = resp.headers['location']
            logger.debug('location: ' + pastepad)
            pattern = re.compile(
                r'^http:\/\/paste.ubuntu.com/(?P<paste_id>\d+)/$')
            res = pattern.match(pastepad)
            if not res:
                raise exception.ServerException('Unknown location: ' +
                                                pastepad)
            paste_id = res.group('paste_id')
            logger.info('paste_id: ' + paste_id)
            # return paste_id
            print 'Paste ID: ' + str(paste_id)
            print 'HTTP Link: ' + pastepad
            return

        if resp.status_code == 200:
            data = resp.content
            err_start_flag = '<ul class="errorlist"><li>'
            err_stop_flag = '</li></ul>'
            msg = self.html2text(
                self.fetch_between(resp.content, err_start_flag,
                                   err_stop_flag))
            raise exception.ServerException('Server refused our paste: ' + msg)

        raise exception.ServerException(
            'Server responsed with unknown status %d %s' %
            (resp.status_code, resp.reason))