Ejemplo n.º 1
0
import getpass
import logging
import pprint
import string
import sys

version="2.4.3"

if sys.version_info < (2, 5):
    print 'This program requires Python 2.5 or newer.'
    sys.exit(1)

if sys.version_info >= (2, 7):
    # suppresses default logger.  Logging is setup in fanficfare/__init__.py so it works in calibre, too.
    rootlogger = logging.getLogger()
    loghandler = logging.NullHandler()
    loghandler.setFormatter(logging.Formatter('(=====)(levelname)s:%(message)s'))
    rootlogger.addHandler(loghandler)

try:
    # running under calibre
    from calibre_plugins.fanficfare_plugin.fanficfare import adapters, writers, exceptions
    from calibre_plugins.fanficfare_plugin.fanficfare.configurable import Configuration
    from calibre_plugins.fanficfare_plugin.fanficfare.epubutils import (
        get_dcsource_chaptercount, get_update_data, reset_orig_chapters_epub)
    from calibre_plugins.fanficfare_plugin.fanficfare.geturls import get_urls_from_page, get_urls_from_imap
except ImportError:
    from fanficfare import adapters, writers, exceptions
    from fanficfare.configurable import Configuration
    from fanficfare.epubutils import (
        get_dcsource_chaptercount, get_update_data, reset_orig_chapters_epub)
from qgis.gui import QgsMessageBar

import rasterio
from pat import LOGGER_NAME, PLUGIN_NAME, TEMPDIR, PLUGIN_SHORT
from util.custom_logging import errorCatcher, openLogPanel
from util.qgis_common import removeFileFromQGIS, addRasterFileToQGIS, save_as_dialog, get_layer_source
from util.settings import read_setting, write_setting

from pyprecag.raster_ops import rescale, normalise
from pyprecag import crs as pyprecag_crs

FORM_CLASS, _ = uic.loadUiType(
    os.path.join(os.path.dirname(__file__), 'rescaleNormalise_dialog_base.ui'))

LOGGER = logging.getLogger(LOGGER_NAME)
LOGGER.addHandler(logging.NullHandler(
))  # logging.StreamHandler()  # Handle logging, no logging has been configured


class RescaleNormaliseDialog(QDialog, FORM_CLASS):
    """Dialog for Rescaling or normalising a single band from an image"""

    toolKey = 'RescaleNormaliseDialog'

    def __init__(self, iface, parent=None):

        super(RescaleNormaliseDialog, self).__init__(parent)

        # Set up the user interface from Designer.
        self.setupUi(self)

        # The qgis interface
Ejemplo n.º 3
0
"""A class for a Qt-enabled python Thread."""
import threading
import logging
import traceback

from qtpy import QtCore

LOGGER = logging.getLogger(__name__)
LOGGER.addHandler(logging.NullHandler())


class Executor(QtCore.QObject, threading.Thread):
    """A thread of control that will emit a Qt signal when finished."""

    finished = QtCore.Signal()

    def __init__(self, target, args=None, kwargs=None):
        """Initialize the Executor object.

        Args:
            target (callable): A function or unbound method that should be
                called within the separate thread of control.
            args (iterable): An iterable of positional arguments that will
                be passed to ``target``.  If ``None``, positional
                arguments will be ignored.
            kwargs (dict): A dict mapping string parameter names to parameter
                values that should be passed to ``target``.  If ``None``,
                keyword arguments will be ignored.

        Returns:
            ``None``.
Ejemplo n.º 4
0
def main():
    '''Main program'''
    if not _ARGS.xml:
        if _ARGS.no_debug:
            log_handler = logging.NullHandler()
        else:
            if not os.access(
                    os.path.expanduser('~/.local/share/ibus-typing-booster'),
                    os.F_OK):
                os.system('mkdir -p ~/.local/share/ibus-typing-booster')
            logfile = os.path.expanduser(
                '~/.local/share/ibus-typing-booster/debug.log')
            log_handler = logging.handlers.TimedRotatingFileHandler(
                logfile,
                when='midnight',
                interval=1,
                backupCount=7,
                encoding='UTF-8',
                delay=False,
                utc=False,
                atTime=None)
        log_formatter = logging.Formatter(
            '%(asctime)s %(filename)s '
            'line %(lineno)d %(funcName)s %(levelname)s: '
            '%(message)s')
        log_handler.setFormatter(log_formatter)
        LOGGER.setLevel(logging.DEBUG)
        LOGGER.addHandler(log_handler)
        LOGGER.info('********** STARTING **********')

    if _ARGS.xml:
        from xml.etree.ElementTree import Element, SubElement, tostring

        egs = Element('engines')

        for language in ('t', ):
            _engine = SubElement(egs, 'engine')

            _name = SubElement(_engine, 'name')
            _name.text = 'typing-booster'

            _longname = SubElement(_engine, 'longname')
            _longname.text = 'Typing Booster'

            _language = SubElement(_engine, 'language')
            _language.text = language

            _license = SubElement(_engine, 'license')
            _license.text = 'GPL'

            _author = SubElement(_engine, 'author')
            _author.text = ('Mike FABIAN <*****@*****.**>' +
                            ', Anish Patil <*****@*****.**>')

            _icon = SubElement(_engine, 'icon')
            _icon.text = os.path.join(ICON_DIR, 'ibus-typing-booster.svg')

            _layout = SubElement(_engine, 'layout')
            _layout.text = 'default'

            _desc = SubElement(_engine, 'description')
            _desc.text = 'A completion input method to speedup typing.'

            _symbol = SubElement(_engine, 'symbol')
            _symbol.text = '🚀'

            _setup = SubElement(_engine, 'setup')
            _setup.text = SETUP_TOOL

            _icon_prop_key = SubElement(_engine, 'icon_prop_key')
            _icon_prop_key.text = 'InputMode'

        # now format the xmlout pretty
        indent(egs)
        egsout = tostring(egs, encoding='utf8', method='xml').decode('utf-8')
        patt = re.compile(r'<\?.*\?>\n')
        egsout = patt.sub('', egsout)
        sys.stdout.buffer.write((egsout + '\n').encode('utf-8'))
        return 0

    if _ARGS.daemon:
        if os.fork():
            sys.exit()

    ima = IMApp(_ARGS.ibus)
    signal(SIGTERM, lambda signum, stack_frame: cleanup(ima))
    signal(SIGINT, lambda signum, stack_frame: cleanup(ima))
    try:
        ima.run()
    except KeyboardInterrupt:
        ima.quit()
    return 0
Ejemplo n.º 5
0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
psq - a distributed task queue using Google Cloud Pubsub.

Homepage: https://github.com/GoogleCloudPlatform/psq
"""

from __future__ import absolute_import

import logging

from .globals import current_queue, current_task, queue_context, task_context
from .queue import BroadcastQueue, Queue
from .storage import DatastoreStorage, Storage
from .task import Retry, Task, TaskResult
from .worker import MultiprocessWorker, Worker

__all__ = [
    'Queue', 'BroadcastQueue', 'Task', 'TaskResult', 'Retry', 'Worker',
    'MultiprocessWorker', 'Storage', 'DatastoreStorage', 'current_queue',
    'queue_context', 'current_task', 'task_context'
]

# Set default logging handler to avoid "No handler found" warnings.
logging.getLogger(__name__).addHandler(logging.NullHandler())
Ejemplo n.º 6
0
#!/usr/bin/env python

import logging

# Create a module logger named after the module
module_logger = logging.getLogger(__name__)
# Add a NullHandler for the case if no logging is configured by the application
module_logger.addHandler(logging.NullHandler())

class MixsException(Exception):
    """
    Exception for the MIXS

    Attributes:
        key (str): The key for the exception.
        message (str): The error message to provide for when the exception is thrown.
    """
    def __init__(self, message, key=None):
        self.key = key
        self.message = message

    def __str__(self):
        if self.key is not None:
            return "Error [%s]: %s" % (self.key, self.message)
        else:
            return "Error: %s" % self.message

class MIXS(object):
    """
    The MIXS class. This class contains all required fields, as well as validation
    of the passed in dictionary to ensure all fields are there.
Ejemplo n.º 7
0
def init(folder=None, debug=False, config=None):
    global DEFAULT_FOLDER, THEMES, TEMP_THEMES, LOG_FILE, INI_FILE, INI_FILE, DB_PATH, CONFIG, FP_HASHES, POOL

    DEFAULT_FOLDER = folder or os.environ.get(
        'bw_plex_default_folder') or os.path.expanduser('~/.config/bw_plex')

    if os.path.isdir(DEFAULT_FOLDER) and not os.access(DEFAULT_FOLDER,
                                                       os.W_OK):
        print('You default folder is not writeable')
        sys.exit()

    THEMES = os.path.join(DEFAULT_FOLDER, 'themes')
    TEMP_THEMES = os.path.join(DEFAULT_FOLDER, 'temp_themes')
    FP_HASHES = os.path.join(DEFAULT_FOLDER, 'hashes.pklz')
    LOG_FILE = os.path.join(DEFAULT_FOLDER, 'log.txt')
    INI_FILE = config or os.path.join(DEFAULT_FOLDER, 'config.ini')
    DB_PATH = os.path.join(DEFAULT_FOLDER, 'media.db')

    os.makedirs(DEFAULT_FOLDER, exist_ok=True)
    os.makedirs(THEMES, exist_ok=True)
    os.makedirs(TEMP_THEMES, exist_ok=True)

    from bw_plex.config import read_or_make
    CONFIG = read_or_make(INI_FILE)
    POOL = Pool(int(CONFIG.get('thread_pool_number', 10)))

    from bw_plex.db import db_init
    db_init()

    handle = logging.NullHandler()
    frmt = logging.Formatter(
        CONFIG.get(
            'logformat',
            '%(asctime)s :: %(name)s :: %(levelname)s :: %(filename)s:%(lineno)d :: %(message)s'
        ))
    handle.setFormatter(frmt)
    LOG.addHandler(handle)

    stream_handle = logging.StreamHandler()
    stream_handle.setFormatter(frmt)
    LOG.addHandler(stream_handle)

    handle.setFormatter(frmt)
    LOG.addHandler(handle)

    # FILE
    rfh = RotatingFileHandler(LOG_FILE, 'a', 512000, 3)
    rfh.setFormatter(frmt)
    LOG.addHandler(rfh)

    log_to_lvl = {
        'debug': logging.DEBUG,
        'info': logging.INFO,
        'warning': logging.WARNING
    }

    if debug is True:
        LOG.setLevel(logging.DEBUG)
    else:
        try:
            lvl = log_to_lvl[CONFIG['general']['loglevel']]
            LOG.setLevel(lvl)
        except KeyError:
            LOG.error(
                'Invalid option for loglevel in fonfig file, defualting to level to debug'
            )
            LOG.setLevel(logging.DEBUG)

    # This import is slow
    import pkg_resources

    LOG.info('Using bw_plex version %s',
             pkg_resources.get_distribution("bw_plex").version)
    LOG.info('default folder set to %s', DEFAULT_FOLDER)

    FILTER.add_secret(CONFIG['server']['token'])
    FILTER.add_secret(CONFIG['server']['password'])
    secret_args = arg_extract(keys=['username', 'token', 'password']).values()
    for arg in secret_args:
        FILTER.add_secret(arg)

    if debug is None:
        if CONFIG['general']['debug'] is False:
            LOG.addFilter(FILTER)
    elif debug is False:
        LOG.addFilter(FILTER)
    else:
        LOG.info('Log is not sanitized!')

        packages = ['plexapi', 'requests', 'urllib3']
        for pack in packages:
            _pack = logging.getLogger(pack)
            _pack.setLevel(logging.DEBUG)
            _pack.addHandler(rfh)
            _pack.addHandler(stream_handle)
Ejemplo n.º 8
0
    def setup_method(self, method):
        self.logger = logging.getLogger('test')

        self.logger.addHandler(logging.NullHandler())
Ejemplo n.º 9
0
class Robinhood:
    """Wrapper class for fetching/parsing Robinhood endpoints """

    session = None
    username = None
    password = None
    headers = None
    auth_token = None
    oauth_token = None

    logger = logging.getLogger('Robinhood')
    logger.addHandler(logging.NullHandler())

    ###########################################################################
    #                       Logging in and initializing
    ###########################################################################

    def __init__(self):
        self.session = requests.session()
        self.session.proxies = getproxies()
        self.headers = {
            "Accept": "*/*",
            "Accept-Encoding": "gzip, deflate",
            "Accept-Language":
            "en;q=1, fr;q=0.9, de;q=0.8, ja;q=0.7, nl;q=0.6, it;q=0.5",
            "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
            "X-Robinhood-API-Version": "1.0.0",
            "Connection": "keep-alive",
            "User-Agent": "Robinhood/823 (iPhone; iOS 7.1.2; Scale/2.00)",
        }
        self.session.headers = self.headers
        self.auth_method = self.login_prompt

    def login_required(function):  # pylint: disable=E0213
        """ Decorator function that prompts user for login if they are not logged in already. Can be applied to any function using the @ notation. """
        def wrapper(self, *args, **kwargs):
            if 'Authorization' not in self.headers:
                self.auth_method()
            return function(self, *args, **kwargs)  # pylint: disable=E1102

        return wrapper

    def login_prompt(self):  # pragma: no cover
        """Prompts user for username and password and calls login() """

        username = input("Username: "******"""Save and test login info for Robinhood accounts

        Args:
            username (str): username
            password (str): password

        Returns:
            (bool): received valid auth token

        """

        self.username = username
        self.password = password
        payload = {
            'password': self.password,
            'username': self.username,
            'client_id': 'c82SH0WZOsabOXGP2sxqcj34FxkvfnWRZBKlBjFS',
            'grant_type': 'password'
        }

        if mfa_code:
            payload['mfa_code'] = mfa_code

        try:
            res = self.session.post(endpoints.login(),
                                    data=payload,
                                    timeout=15)
            res.raise_for_status()
            data = res.json()
        except requests.exceptions.HTTPError:
            raise RH_exception.LoginFailed()

        if 'mfa_required' in data.keys():  # pragma: no cover
            raise RH_exception.TwoFactorRequired(
            )  # requires a second call to enable 2FA

        if 'access_token' in data.keys():
            self.auth_token = data['access_token']
            self.headers['Authorization'] = 'Bearer ' + self.auth_token
            return True

        return False

    def logout(self):
        """Logout from Robinhood

        Returns:
            (:obj:`requests.request`) result from logout endpoint

        """

        try:
            req = self.session.post(endpoints.logout(), timeout=15)
            req.raise_for_status()
        except requests.exceptions.HTTPError as err_msg:
            warnings.warn('Failed to log out ' + repr(err_msg))

        self.headers['Authorization'] = None
        self.auth_token = None

        return req

    ###########################################################################
    #                               GET DATA
    ###########################################################################

    def investment_profile(self):
        """Fetch investment_profile """

        res = self.session.get(endpoints.investment_profile(), timeout=15)
        res.raise_for_status()  # will throw without auth
        data = res.json()

        return data

    def instruments(self, stock):
        """Fetch instruments endpoint

            Args:
                stock (str): stock ticker

            Returns:
                (:obj:`dict`): JSON contents from `instruments` endpoint
        """

        res = self.session.get(endpoints.instruments(),
                               params={'query': stock.upper()},
                               timeout=15)
        res.raise_for_status()
        res = res.json()

        # if requesting all, return entire object so may paginate with ['next']
        if (stock == ""):
            return res

        return res['results']

    def instrument(self, id):
        """Fetch instrument info

            Args:
                id (str): instrument id

            Returns:
                (:obj:`dict`): JSON dict of instrument
        """
        url = str(endpoints.instruments()) + str(id) + "/"

        try:
            req = requests.get(url, timeout=15)
            req.raise_for_status()
            data = req.json()
        except requests.exceptions.HTTPError:
            raise RH_exception.InvalidInstrumentId()

        return data

    def quote_data(self, stock=''):
        """Fetch stock quote

            Args:
                stock (str): stock ticker, prompt if blank

            Returns:
                (:obj:`dict`): JSON contents from `quotes` endpoint
        """

        url = None

        if stock.find(',') == -1:
            url = str(endpoints.quotes()) + str(stock) + "/"
        else:
            url = str(endpoints.quotes()) + "?symbols=" + str(stock)

        #Check for validity of symbol
        try:
            req = requests.get(url, timeout=15)
            req.raise_for_status()
            data = req.json()
        except requests.exceptions.HTTPError:
            raise RH_exception.InvalidTickerSymbol()

        return data

    # We will keep for compatibility until next major release
    def quotes_data(self, stocks):
        """Fetch quote for multiple stocks, in one single Robinhood API call

            Args:
                stocks (list<str>): stock tickers

            Returns:
                (:obj:`list` of :obj:`dict`): List of JSON contents from `quotes` endpoint, in the
                    same order of input args. If any ticker is invalid, a None will occur at that position.
        """

        url = str(endpoints.quotes()) + "?symbols=" + ",".join(stocks)

        try:
            req = requests.get(url, timeout=15)
            req.raise_for_status()
            data = req.json()
        except requests.exceptions.HTTPError:
            raise RH_exception.InvalidTickerSymbol()

        return data["results"]

    def get_quote_list(self, stock='', key=''):
        """Returns multiple stock info and keys from quote_data (prompt if blank)

            Args:
                stock (str): stock ticker (or tickers separated by a comma)
                , prompt if blank
                key (str): key attributes that the function should return

            Returns:
                (:obj:`list`): Returns values from each stock or empty list
                               if none of the stocks were valid

        """

        #Creates a tuple containing the information we want to retrieve
        def append_stock(stock):
            keys = key.split(',')
            myStr = ''
            for item in keys:
                myStr += stock[item] + ","

            return (myStr.split(','))

        #Prompt for stock if not entered
        if not stock:  # pragma: no cover
            stock = input("Symbol: ")

        data = self.quote_data(stock)
        res = []

        # Handles the case of multple tickers
        if stock.find(',') != -1:
            for stock in data['results']:
                if stock is None:
                    continue
                res.append(append_stock(stock))

        else:
            res.append(append_stock(data))

        return res

    def get_quote(self, stock=''):
        """Wrapper for quote_data """

        data = self.quote_data(stock)
        return data["symbol"]

    def get_historical_quotes(self,
                              stock,
                              interval,
                              span,
                              bounds=Bounds.REGULAR):
        """Fetch historical data for stock

            Note: valid interval/span configs
                interval = 5minute | 10minute + span = day, week
                interval = day + span = year
                interval = week
                TODO: NEEDS TESTS

            Args:
                stock (str): stock ticker
                interval (str): resolution of data
                span (str): length of data
                bounds (:enum:`Bounds`, optional): 'extended' or 'regular' trading hours

            Returns:
                (:obj:`dict`) values returned from `historicals` endpoint
        """
        if type(stock) is str:
            stock = [stock]

        if isinstance(bounds, str):  # recast to Enum
            bounds = Bounds(bounds)

        params = {
            'symbols': ','.join(stock).upper(),
            'interval': interval,
            'span': span,
            'bounds': bounds.name.lower()
        }

        res = self.session.get(endpoints.historicals(),
                               params=params,
                               timeout=15)
        return res.json()

    def get_news(self, stock):
        """Fetch news endpoint
            Args:
                stock (str): stock ticker

            Returns:
                (:obj:`dict`) values returned from `news` endpoint
        """

        return self.session.get(endpoints.news(stock.upper()),
                                timeout=15).json()

    def print_quote(self, stock=''):  # pragma: no cover
        """Print quote information
            Args:
                stock (str): ticker to fetch

            Returns:
                None
        """

        data = self.get_quote_list(stock, 'symbol,last_trade_price')
        for item in data:
            quote_str = item[0] + ": $" + item[1]
            print(quote_str)
            self.logger.info(quote_str)

    def print_quotes(self, stocks):  # pragma: no cover
        """Print a collection of stocks

            Args:
                stocks (:obj:`list`): list of stocks to pirnt

            Returns:
                None
        """

        if stocks is None:
            return

        for stock in stocks:
            self.print_quote(stock)

    def ask_price(self, stock=''):
        """Get asking price for a stock

            Note:
                queries `quote` endpoint, dict wrapper

            Args:
                stock (str): stock ticker

            Returns:
                (float): ask price
        """

        return self.get_quote_list(stock, 'ask_price')

    def ask_size(self, stock=''):
        """Get ask size for a stock

            Note:
                queries `quote` endpoint, dict wrapper

            Args:
                stock (str): stock ticker

            Returns:
                (int): ask size
        """

        return self.get_quote_list(stock, 'ask_size')

    def bid_price(self, stock=''):
        """Get bid price for a stock

            Note:
                queries `quote` endpoint, dict wrapper

            Args:
                stock (str): stock ticker

            Returns:
                (float): bid price
        """

        return self.get_quote_list(stock, 'bid_price')

    def bid_size(self, stock=''):
        """Get bid size for a stock

            Note:
                queries `quote` endpoint, dict wrapper

            Args:
                stock (str): stock ticker

            Returns:
                (int): bid size
        """

        return self.get_quote_list(stock, 'bid_size')

    def last_trade_price(self, stock=''):
        """Get last trade price for a stock

            Note:
                queries `quote` endpoint, dict wrapper

            Args:
                stock (str): stock ticker

            Returns:
                (float): last trade price
        """

        return self.get_quote_list(stock, 'last_trade_price')

    def previous_close(self, stock=''):
        """Get previous closing price for a stock

            Note:
                queries `quote` endpoint, dict wrapper

            Args:
                stock (str): stock ticker

            Returns:
                (float): previous closing price
        """

        return self.get_quote_list(stock, 'previous_close')

    def previous_close_date(self, stock=''):
        """Get previous closing date for a stock

            Note:
                queries `quote` endpoint, dict wrapper

            Args:
                stock (str): stock ticker

            Returns:
                (str): previous close date
        """

        return self.get_quote_list(stock, 'previous_close_date')

    def adjusted_previous_close(self, stock=''):
        """Get adjusted previous closing price for a stock

            Note:
                queries `quote` endpoint, dict wrapper

            Args:
                stock (str): stock ticker

            Returns:
                (float): adjusted previous closing price
        """

        return self.get_quote_list(stock, 'adjusted_previous_close')

    def symbol(self, stock=''):
        """Get symbol for a stock

            Note:
                queries `quote` endpoint, dict wrapper

            Args:
                stock (str): stock ticker

            Returns:
                (str): stock symbol
        """

        return self.get_quote_list(stock, 'symbol')

    def last_updated_at(self, stock=''):
        """Get last update datetime

            Note:
                queries `quote` endpoint, dict wrapper

            Args:
                stock (str): stock ticker

            Returns:
                (str): last update datetime
        """

        return self.get_quote_list(stock, 'last_updated_at')

    def last_updated_at_datetime(self, stock=''):
        """Get last updated datetime

            Note:
                queries `quote` endpoint, dict wrapper
                `self.last_updated_at` returns time as `str` in format: 'YYYY-MM-ddTHH:mm:ss:000Z'

            Args:
                stock (str): stock ticker

            Returns:
                (datetime): last update datetime

        """

        #Will be in format: 'YYYY-MM-ddTHH:mm:ss:000Z'
        datetime_string = self.last_updated_at(stock)
        result = dateutil.parser.parse(datetime_string)

        return result

    def get_account(self):
        """Fetch account information

            Returns:
                (:obj:`dict`): `accounts` endpoint payload
        """

        res = self.session.get(endpoints.accounts(), timeout=15)
        res.raise_for_status()  # auth required
        res = res.json()

        return res['results'][0]

    def get_url(self, url):
        """
            Flat wrapper for fetching URL directly
        """

        return self.session.get(url, timeout=15).json()

    def get_popularity(self, stock=''):
        """Get the number of robinhood users who own the given stock

            Args:
                stock (str): stock ticker

            Returns:
                (int): number of users who own the stock
        """
        stock_instrument = self.get_url(
            self.quote_data(stock)["instrument"])["id"]
        return self.get_url(
            endpoints.instruments(stock_instrument,
                                  "popularity"))["num_open_positions"]

    def get_tickers_by_tag(self, tag=None):
        """Get a list of instruments belonging to a tag

            Args: tag - Tags may include but are not limited to:
                * top-movers
                * etf
                * 100-most-popular
                * mutual-fund
                * finance
                * cap-weighted
                * investment-trust-or-fund

            Returns:
                (List): a list of Ticker strings
        """
        instrument_list = self.get_url(endpoints.tags(tag))["instruments"]
        return [
            self.get_url(instrument)["symbol"]
            for instrument in instrument_list
        ]

    ###########################################################################
    #                           GET OPTIONS INFO
    ###########################################################################

    def get_options(self, stock, expiration_dates, option_type):
        """Get a list (chain) of options contracts belonging to a particular stock

            Args: stock ticker (str), list of expiration dates to filter on (YYYY-MM-DD), and whether or not its a 'put' or a 'call' option type (str).

            Returns:
                Options Contracts (List): a list (chain) of contracts for a given underlying equity instrument
        """
        instrumentid = self.get_url(self.quote_data(stock)["instrument"])["id"]
        if (type(expiration_dates) == list):
            _expiration_dates_string = expiration_dates.join(",")
        else:
            _expiration_dates_string = expiration_dates
        chain_id = self.get_url(
            endpoints.chain(instrumentid))["results"][0]["id"]
        return [
            contract for contract in self.get_url(
                endpoints.options(chain_id, _expiration_dates_string,
                                  option_type))["results"]
        ]

    @login_required
    def get_option_market_data(self, optionid):
        """Gets a list of market data for a given optionid.

        Args: (str) option id

        Returns: dictionary of options market data.
        """
        if not self.oauth_token:
            res = self.session.post(endpoints.convert_token(), timeout=15)
            res.raise_for_status()
            res = res.json()
            self.oauth_token = res["access_token"]
            self.headers['Authorization'] = 'Bearer ' + self.oauth_token
        return self.get_url(endpoints.market_data(optionid))

    ###########################################################################
    #                           GET FUNDAMENTALS
    ###########################################################################

    def get_fundamentals(self, stock=''):
        """Find stock fundamentals data

            Args:
                (str): stock ticker

            Returns:
                (:obj:`dict`): contents of `fundamentals` endpoint
        """

        #Prompt for stock if not entered
        if not stock:  # pragma: no cover
            stock = input("Symbol: ")

        url = str(endpoints.fundamentals(str(stock.upper())))

        #Check for validity of symbol
        try:
            req = requests.get(url, timeout=15)
            req.raise_for_status()
            data = req.json()
        except requests.exceptions.HTTPError:
            raise RH_exception.InvalidTickerSymbol()

        return data

    def fundamentals(self, stock=''):
        """Wrapper for get_fundamentlals function """

        return self.get_fundamentals(stock)

    ###########################################################################
    #                           PORTFOLIOS DATA
    ###########################################################################

    def portfolios(self):
        """Returns the user's portfolio data """

        req = self.session.get(endpoints.portfolios(), timeout=15)
        req.raise_for_status()

        return req.json()['results'][0]

    def adjusted_equity_previous_close(self):
        """Wrapper for portfolios

            Returns:
                (float): `adjusted_equity_previous_close` value

        """

        return float(self.portfolios()['adjusted_equity_previous_close'])

    def equity(self):
        """Wrapper for portfolios

            Returns:
                (float): `equity` value
        """

        return float(self.portfolios()['equity'])

    def equity_previous_close(self):
        """Wrapper for portfolios

            Returns:
                (float): `equity_previous_close` value
        """

        return float(self.portfolios()['equity_previous_close'])

    def excess_margin(self):
        """Wrapper for portfolios

            Returns:
                (float): `excess_margin` value
        """

        return float(self.portfolios()['excess_margin'])

    def extended_hours_equity(self):
        """Wrapper for portfolios

            Returns:
                (float): `extended_hours_equity` value
        """

        try:
            return float(self.portfolios()['extended_hours_equity'])
        except TypeError:
            return None

    def extended_hours_market_value(self):
        """Wrapper for portfolios

            Returns:
                (float): `extended_hours_market_value` value
        """

        try:
            return float(self.portfolios()['extended_hours_market_value'])
        except TypeError:
            return None

    def last_core_equity(self):
        """Wrapper for portfolios

            Returns:
                (float): `last_core_equity` value
        """

        return float(self.portfolios()['last_core_equity'])

    def last_core_market_value(self):
        """Wrapper for portfolios

            Returns:
                (float): `last_core_market_value` value
        """

        return float(self.portfolios()['last_core_market_value'])

    def market_value(self):
        """Wrapper for portfolios

            Returns:
                (float): `market_value` value
        """

        return float(self.portfolios()['market_value'])

    @login_required
    def order_history(self, orderId=None):
        """Wrapper for portfolios
            Optional Args: add an order ID to retrieve information about a single order.
            Returns:
                (:obj:`dict`): JSON dict from getting orders
        """

        return self.session.get(endpoints.orders(orderId), timeout=15).json()

    def dividends(self):
        """Wrapper for portfolios

            Returns:
                (:obj: `dict`): JSON dict from getting dividends
        """

        return self.session.get(endpoints.dividends(), timeout=15).json()

    ###########################################################################
    #                           POSITIONS DATA
    ###########################################################################

    def positions(self):
        """Returns the user's positions data

            Returns:
                (:object: `dict`): JSON dict from getting positions
        """

        return self.session.get(endpoints.positions(), timeout=15).json()

    def securities_owned(self):
        """Returns list of securities' symbols that the user has shares in

            Returns:
                (:object: `dict`): Non-zero positions
        """

        return self.session.get(endpoints.positions() + '?nonzero=true',
                                timeout=15).json()

    ###########################################################################
    #                               PLACE ORDER
    ###########################################################################

    def place_order(self,
                    instrument,
                    quantity=1,
                    bid_price=0.0,
                    transaction=None,
                    trigger='immediate',
                    order='market',
                    time_in_force='gfd'):
        """Place an order with Robinhood

            Notes:
                OMFG TEST THIS PLEASE!

                Just realized this won't work since if type is LIMIT you need to use "price" and if
                a STOP you need to use "stop_price".  Oops.
                Reference: https://github.com/sanko/Robinhood/blob/master/Order.md#place-an-order

            Args:
                instrument (dict): the RH URL and symbol in dict for the instrument to be traded
                quantity (int): quantity of stocks in order
                bid_price (float): price for order
                transaction (:enum:`Transaction`): BUY or SELL enum
                trigger (:enum:`Trigger`): IMMEDIATE or STOP enum
                order (:enum:`Order`): MARKET or LIMIT
                time_in_force (:enum:`TIME_IN_FORCE`): GFD or GTC (day or until cancelled)

            Returns:
                (:obj:`requests.request`): result from `orders` put command
        """

        if isinstance(transaction, str):
            transaction = Transaction(transaction)

        if not bid_price:
            bid_price = self.quote_data(instrument['symbol'])['bid_price']

        payload = {
            'account': self.get_account()['url'],
            'instrument': unquote(instrument['url']),
            'price': float(bid_price),
            'quantity': quantity,
            'side': transaction.name.lower(),
            'symbol': instrument['symbol'],
            'time_in_force': time_in_force.lower(),
            'trigger': trigger,
            'type': order.lower()
        }

        #data = 'account=%s&instrument=%s&price=%f&quantity=%d&side=%s&symbol=%s#&time_in_force=gfd&trigger=immediate&type=market' % (
        #    self.get_account()['url'],
        #    urllib.parse.unquote(instrument['url']),
        #    float(bid_price),
        #    quantity,
        #    transaction,
        #    instrument['symbol']
        #)

        res = self.session.post(endpoints.orders(), data=payload, timeout=15)
        res.raise_for_status()

        return res

    def place_buy_order(self, instrument, quantity, bid_price=0.0):
        """Wrapper for placing buy orders

            Args:
                instrument (dict): the RH URL and symbol in dict for the instrument to be traded
                quantity (int): quantity of stocks in order
                bid_price (float): price for order

            Returns:
                (:obj:`requests.request`): result from `orders` put command

        """

        transaction = Transaction.BUY

        return self.place_order(instrument, quantity, bid_price, transaction)

    def place_sell_order(self, instrument, quantity, bid_price=0.0):
        """Wrapper for placing sell orders

            Args:
                instrument (dict): the RH URL and symbol in dict for the instrument to be traded
                quantity (int): quantity of stocks in order
                bid_price (float): price for order

            Returns:
                (:obj:`requests.request`): result from `orders` put command
        """

        transaction = Transaction.SELL

        return self.place_order(instrument, quantity, bid_price, transaction)

    # Methods below here are a complete rewrite for buying and selling
    # These are new. Use at your own risk!

    def place_market_buy_order(self,
                               instrument_URL=None,
                               symbol=None,
                               time_in_force=None,
                               quantity=None):
        """Wrapper for placing market buy orders

            Notes:
                If only one of the instrument_URL or symbol are passed as
                arguments the other will be looked up automatically.

            Args:
                instrument_URL (str): The RH URL of the instrument
                symbol (str): The ticker symbol of the instrument
                time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
                quantity (int): Number of shares to buy

            Returns:
                (:obj:`requests.request`): result from `orders` put command
        """
        return (self.submit_order(order_type='market',
                                  trigger='immediate',
                                  side='buy',
                                  instrument_URL=instrument_URL,
                                  symbol=symbol,
                                  time_in_force=time_in_force,
                                  quantity=quantity))

    def place_limit_buy_order(self,
                              instrument_URL=None,
                              symbol=None,
                              time_in_force=None,
                              price=None,
                              quantity=None):
        """Wrapper for placing limit buy orders

            Notes:
                If only one of the instrument_URL or symbol are passed as
                arguments the other will be looked up automatically.

            Args:
                instrument_URL (str): The RH URL of the instrument
                symbol (str): The ticker symbol of the instrument
                time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
                price (float): The max price you're willing to pay per share
                quantity (int): Number of shares to buy

            Returns:
                (:obj:`requests.request`): result from `orders` put command
        """
        return (self.submit_order(order_type='limit',
                                  trigger='immediate',
                                  side='buy',
                                  instrument_URL=instrument_URL,
                                  symbol=symbol,
                                  time_in_force=time_in_force,
                                  price=price,
                                  quantity=quantity))

    def place_stop_loss_buy_order(self,
                                  instrument_URL=None,
                                  symbol=None,
                                  time_in_force=None,
                                  stop_price=None,
                                  quantity=None):
        """Wrapper for placing stop loss buy orders

            Notes:
                If only one of the instrument_URL or symbol are passed as
                arguments the other will be looked up automatically.

            Args:
                instrument_URL (str): The RH URL of the instrument
                symbol (str): The ticker symbol of the instrument
                time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
                stop_price (float): The price at which this becomes a market order
                quantity (int): Number of shares to buy

            Returns:
                (:obj:`requests.request`): result from `orders` put command
        """
        return (self.submit_order(order_type='market',
                                  trigger='stop',
                                  side='buy',
                                  instrument_URL=instrument_URL,
                                  symbol=symbol,
                                  time_in_force=time_in_force,
                                  stop_price=stop_price,
                                  quantity=quantity))

    def place_stop_limit_buy_order(self,
                                   instrument_URL=None,
                                   symbol=None,
                                   time_in_force=None,
                                   stop_price=None,
                                   price=None,
                                   quantity=None):
        """Wrapper for placing stop limit buy orders

            Notes:
                If only one of the instrument_URL or symbol are passed as
                arguments the other will be looked up automatically.

            Args:
                instrument_URL (str): The RH URL of the instrument
                symbol (str): The ticker symbol of the instrument
                time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
                stop_price (float): The price at which this becomes a limit order
                price (float): The max price you're willing to pay per share
                quantity (int): Number of shares to buy

            Returns:
                (:obj:`requests.request`): result from `orders` put command
        """
        return (self.submit_order(order_type='limit',
                                  trigger='stop',
                                  side='buy',
                                  instrument_URL=instrument_URL,
                                  symbol=symbol,
                                  time_in_force=time_in_force,
                                  stop_price=stop_price,
                                  price=price,
                                  quantity=quantity))

    def place_market_sell_order(self,
                                instrument_URL=None,
                                symbol=None,
                                time_in_force=None,
                                quantity=None):
        """Wrapper for placing market sell orders

            Notes:
                If only one of the instrument_URL or symbol are passed as
                arguments the other will be looked up automatically.

            Args:
                instrument_URL (str): The RH URL of the instrument
                symbol (str): The ticker symbol of the instrument
                time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
                quantity (int): Number of shares to sell

            Returns:
                (:obj:`requests.request`): result from `orders` put command
        """
        return (self.submit_order(order_type='market',
                                  trigger='immediate',
                                  side='sell',
                                  instrument_URL=instrument_URL,
                                  symbol=symbol,
                                  time_in_force=time_in_force,
                                  quantity=quantity))

    def place_limit_sell_order(self,
                               instrument_URL=None,
                               symbol=None,
                               time_in_force=None,
                               price=None,
                               quantity=None):
        """Wrapper for placing limit sell orders

            Notes:
                If only one of the instrument_URL or symbol are passed as
                arguments the other will be looked up automatically.

            Args:
                instrument_URL (str): The RH URL of the instrument
                symbol (str): The ticker symbol of the instrument
                time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
                price (float): The minimum price you're willing to get per share
                quantity (int): Number of shares to sell

            Returns:
                (:obj:`requests.request`): result from `orders` put command
        """
        return (self.submit_order(order_type='limit',
                                  trigger='immediate',
                                  side='sell',
                                  instrument_URL=instrument_URL,
                                  symbol=symbol,
                                  time_in_force=time_in_force,
                                  price=price,
                                  quantity=quantity))

    def place_stop_loss_sell_order(self,
                                   instrument_URL=None,
                                   symbol=None,
                                   time_in_force=None,
                                   stop_price=None,
                                   quantity=None):
        """Wrapper for placing stop loss sell orders

            Notes:
                If only one of the instrument_URL or symbol are passed as
                arguments the other will be looked up automatically.

            Args:
                instrument_URL (str): The RH URL of the instrument
                symbol (str): The ticker symbol of the instrument
                time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
                stop_price (float): The price at which this becomes a market order
                quantity (int): Number of shares to sell

            Returns:
                (:obj:`requests.request`): result from `orders` put command
        """
        return (self.submit_order(order_type='market',
                                  trigger='stop',
                                  side='sell',
                                  instrument_URL=instrument_URL,
                                  symbol=symbol,
                                  time_in_force=time_in_force,
                                  stop_price=stop_price,
                                  quantity=quantity))

    def place_stop_limit_sell_order(self,
                                    instrument_URL=None,
                                    symbol=None,
                                    time_in_force=None,
                                    price=None,
                                    stop_price=None,
                                    quantity=None):
        """Wrapper for placing stop limit sell orders

            Notes:
                If only one of the instrument_URL or symbol are passed as
                arguments the other will be looked up automatically.

            Args:
                instrument_URL (str): The RH URL of the instrument
                symbol (str): The ticker symbol of the instrument
                time_in_force (str): 'GFD' or 'GTC' (day or until cancelled)
                stop_price (float): The price at which this becomes a limit order
                price (float): The max price you're willing to get per share
                quantity (int): Number of shares to sell

            Returns:
                (:obj:`requests.request`): result from `orders` put command
        """
        return (self.submit_order(order_type='limit',
                                  trigger='stop',
                                  side='sell',
                                  instrument_URL=instrument_URL,
                                  symbol=symbol,
                                  time_in_force=time_in_force,
                                  stop_price=stop_price,
                                  price=price,
                                  quantity=quantity))

    def submit_order(self,
                     instrument_URL=None,
                     symbol=None,
                     order_type=None,
                     time_in_force=None,
                     trigger=None,
                     price=None,
                     stop_price=None,
                     quantity=None,
                     side=None):
        """Submits order to Robinhood

            Notes:
                This is normally not called directly.  Most programs should use
                one of the following instead:

                    place_market_buy_order()
                    place_limit_buy_order()
                    place_stop_loss_buy_order()
                    place_stop_limit_buy_order()
                    place_market_sell_order()
                    place_limit_sell_order()
                    place_stop_loss_sell_order()
                    place_stop_limit_sell_order()

            Args:
                instrument_URL (str): the RH URL for the instrument
                symbol (str): the ticker symbol for the instrument
                order_type (str): 'MARKET' or 'LIMIT'
                time_in_force (:enum:`TIME_IN_FORCE`): GFD or GTC (day or
                                                       until cancelled)
                trigger (str): IMMEDIATE or STOP enum
                price (float): The share price you'll accept
                stop_price (float): The price at which the order becomes a
                                    market or limit order
                quantity (int): The number of shares to buy/sell
                side (str): BUY or sell

            Returns:
                (:obj:`requests.request`): result from `orders` put command
        """

        # Start with some parameter checks. I'm paranoid about $.
        if (instrument_URL is None):
            if (symbol is None):
                raise (ValueError(
                    'Neither instrument_URL nor symbol were passed to submit_order'
                ))
            instrument_URL = self.instruments(symbol)[0]['url']

        if (symbol is None):
            symbol = self.session.get(instrument_URL,
                                      timeout=15).json()['symbol']

        if (side is None):
            raise (ValueError(
                'Order is neither buy nor sell in call to submit_order'))

        if (order_type is None):
            if (price is None):
                if (stop_price is None):
                    order_type = 'market'
                else:
                    order_type = 'limit'

        symbol = str(symbol).upper()
        order_type = str(order_type).lower()
        time_in_force = str(time_in_force).lower()
        trigger = str(trigger).lower()
        side = str(side).lower()

        if (order_type != 'market') and (order_type != 'limit'):
            raise (ValueError('Invalid order_type in call to submit_order'))

        if (order_type == 'limit'):
            if (price is None):
                raise (ValueError(
                    'Limit order has no price in call to submit_order'))
            if (price <= 0):
                raise (ValueError(
                    'Price must be positive number in call to submit_order'))

        if (trigger == 'stop'):
            if (stop_price is None):
                raise (ValueError(
                    'Stop order has no stop_price in call to submit_order'))
            if (price <= 0):
                raise (ValueError(
                    'Stop_price must be positive number in call to submit_order'
                ))

        if (stop_price is not None):
            if (trigger != 'stop'):
                raise (ValueError(
                    'Stop price set for non-stop order in call to submit_order'
                ))

        if (price is None):
            if (order_type == 'limit'):
                raise (ValueError(
                    'Limit order has no price in call to submit_order'))

        if (price is not None):
            if (order_type.lower() == 'market'):
                raise (ValueError(
                    'Market order has price limit in call to submit_order'))

        price = float(price)

        if (quantity is None):
            raise (ValueError('No quantity specified in call to submit_order'))

        quantity = int(quantity)

        if (quantity <= 0):
            raise (ValueError(
                'Quantity must be positive number in call to submit_order'))

        payload = {}

        for field, value in [('account', self.get_account()['url']),
                             ('instrument', instrument_URL),
                             ('symbol', symbol), ('type', order_type),
                             ('time_in_force', time_in_force),
                             ('trigger', trigger), ('price', price),
                             ('stop_price', stop_price),
                             ('quantity', quantity), ('side', side)]:
            if (value is not None):
                payload[field] = value

        res = self.session.post(endpoints.orders(), data=payload, timeout=15)
        res.raise_for_status()

        return res

    ##############################
    #                          CANCEL ORDER
    ##############################

    def cancel_order(self, order_id):
        """
        Cancels specified order and returns the response (results from `orders` command). 
        If order cannot be cancelled, `None` is returned.

        Args:
            order_id (str): Order ID that is to be cancelled or order dict returned from
            order get.
        Returns:
            (:obj:`requests.request`): result from `orders` put command
        """
        if order_id is str:
            try:
                order = self.session.get(self.endpoints['orders'] + order_id,
                                         timeout=15).json()
            except (requests.exceptions.HTTPError) as err_msg:
                raise ValueError('Failed to get Order for ID: ' + order_id +
                                 '\n Error message: ' + repr(err_msg))
        else:
            raise ValueError(
                'Cancelling orders requires a valid order_id string')

        if order.get('cancel') is not None:
            try:
                res = self.session.post(order['cancel'], timeout=15)
                res.raise_for_status()
            except (requests.exceptions.HTTPError) as err_msg:
                raise ValueError('Failed to cancel order ID: ' + order_id +
                                 '\n Error message: ' + repr(err_msg))
                return None

        # Order type cannot be cancelled without a valid cancel link
        else:
            raise ValueError('Unable to cancel order ID: ' + order_id)

        return res
Ejemplo n.º 10
0
                        return

                #print(f"DEBUG: found on trunk port: {mac} -- {task.host.name} -- {dest_port}")
                return

        #print(f"DEBUG: On '{task.host.name}' MAC not found")
    except Exception as e:
        print(f"Something is wrong with '{task.host.name}'")


def main(mac):
    nr = InitNornir(config_file="inventory.yaml")
    nr.run(collect, mac=mac)
    if not mac_found:
        print("MAC not found")


if __name__ == '__main__':
    # Hide Nornir's tracebacks
    logging.getLogger("nornir").addHandler(logging.NullHandler())
    logging.getLogger("nornir").propagate = False

    if len(sys.argv) > 1:
        # assume that second argument is MAC
        mac = sys.argv[1]
    else:
        # no MAC in arguments, ask input here
        mac = input("Enter MAC: ")
        print()

    main(mac)
Ejemplo n.º 11
0
# -*- coding: utf-8 -*-

import logging
from .item import Item, item_dict, item_list
from .io import load_json, save_json
from .servant import Servant, servant_dict, servant_list, skill_dict
from .sound import Sound, sound_list
from .text import Dictionary, Text
from .merge import merge

logging.getLogger(__package__).addHandler(logging.NullHandler())
Ejemplo n.º 12
0
    def __init__(self, mpf_path, machine_path, args):
        """Run mpf game."""
        signal.signal(signal.SIGINT, self.exit)

        parser = argparse.ArgumentParser(
            description='Starts the MPF game engine')

        parser.add_argument("-a",
                            action="store_true",
                            dest="no_load_cache",
                            help="Forces the config to be loaded from files "
                            "and not cache")

        parser.add_argument("-A",
                            action="store_false",
                            dest="create_config_cache",
                            help="Does not create the cache config files")

        parser.add_argument("-b",
                            action="store_false",
                            dest="bcp",
                            default=True,
                            help="Runs MPF without making a connection "
                            "attempt to a "
                            "BCP Server")

        parser.add_argument("-c",
                            action="store",
                            dest="configfile",
                            default="config.yaml",
                            metavar='config_file',
                            help="The name of a config file to load. Default "
                            "is "
                            "config.yaml. Multiple files can be used "
                            "via a comma-"
                            "separated list (no spaces between)")

        parser.add_argument("-C",
                            action="store",
                            dest="mpfconfigfile",
                            default=os.path.join(mpf_path, "mpfconfig.yaml"),
                            metavar='config_file',
                            help="The MPF framework default config file. "
                            "Default is "
                            "mpf/mpfconfig.yaml")

        parser.add_argument("-f",
                            action="store_true",
                            dest="force_assets_load",
                            default=False,
                            help="Load all assets upon startup.  Useful for "
                            "ensuring all assets are set up properly "
                            "during development.")

        parser.add_argument(
            "-l",
            action="store",
            dest="logfile",
            metavar='file_name',
            default=os.path.join(
                "logs",
                datetime.now().strftime("%Y-%m-%d-%H-%M-%S-mpf-" +
                                        socket.gethostname() + ".log")),
            help="The name (and path) of the log file")

        parser.add_argument("-p",
                            action="store_true",
                            dest="pause",
                            default=False,
                            help="Pause the terminal window on exit. Useful "
                            "when launching in a separate window so you can "
                            "see any errors before the window closes.")

        parser.add_argument(
            "-P",
            action="store_true",
            dest="production",
            default=False,
            help=
            "Production mode. Will suppress errors, wait for hardware on start and "
            "try to exit when startup fails. Run this inside a loop.")

        parser.add_argument("-t",
                            action="store_false",
                            dest='text_ui',
                            default=True,
                            help="Use the ASCII test-based UI")

        parser.add_argument("-v",
                            action="store_const",
                            dest="loglevel",
                            const=10,
                            default=11,
                            help="Enables verbose logging to the"
                            " log file")

        parser.add_argument("-V",
                            action="store_true",
                            dest="consoleloglevel",
                            default=logging.INFO,
                            help="Enables verbose logging to the console. Do "
                            "NOT on "
                            "Windows platforms. Must also use -v for "
                            "this to work.")

        parser.add_argument("-x",
                            action="store_const",
                            dest="force_platform",
                            const='virtual',
                            help="Forces the virtual platform to be "
                            "used for all devices")

        parser.add_argument("--syslog_address",
                            action="store",
                            dest="syslog_address",
                            help="Log to the specified syslog address. This "
                            "can be a domain socket such as /dev/og on "
                            "Linux or /var/run/syslog on Mac. "
                            "Alternatively, you an specify host:port for "
                            "remote logging over UDP.")

        parser.add_argument("-X",
                            action="store_const",
                            dest="force_platform",
                            const='smart_virtual',
                            help="Forces the smart virtual platform to be "
                            "used for all"
                            " devices")

        # The following are just included for full compatibility with mc
        # which is needed when using "mpf both".

        parser.add_argument("-L",
                            action="store",
                            dest="mc_file_name",
                            metavar='mc_file_name',
                            default=None,
                            help=argparse.SUPPRESS)

        self.args = parser.parse_args(args)
        self.args.configfile = Util.string_to_list(self.args.configfile)

        # Configure logging. Creates a logfile and logs to the console.
        # Formatting options are documented here:
        # https://docs.python.org/2.7/library/logging.html#logrecord-attributes

        try:
            os.makedirs(os.path.join(machine_path, 'logs'))
        except OSError as exception:
            if exception.errno != errno.EEXIST:
                raise

        full_logfile_path = os.path.join(machine_path, self.args.logfile)

        try:
            os.remove(full_logfile_path)
        except OSError:
            pass

        if self.args.text_ui:
            console_log = logging.NullHandler()
            console_log.setLevel(logging.ERROR)
        else:
            console_log = logging.StreamHandler()
            console_log.setLevel(self.args.consoleloglevel)

        # tell the handler to use this format
        console_log.setFormatter(
            logging.Formatter('%(levelname)s : %(name)s : %(message)s'))

        # initialise async handler for console
        console_log_queue = Queue()
        console_queue_handler = QueueHandler(console_log_queue)
        self.console_queue_listener = logging.handlers.QueueListener(
            console_log_queue, console_log)
        self.console_queue_listener.start()

        # initialise file log
        file_log = logging.FileHandler(full_logfile_path)
        file_log.setFormatter(
            logging.Formatter(
                '%(asctime)s : %(levelname)s : %(name)s : %(message)s'))

        # initialise async handler for file log
        file_log_queue = Queue()
        file_queue_handler = QueueHandler(file_log_queue)
        self.file_queue_listener = logging.handlers.QueueListener(
            file_log_queue, file_log)
        self.file_queue_listener.start()

        # add loggers
        logger = logging.getLogger()
        logger.addHandler(console_queue_handler)
        logger.addHandler(file_queue_handler)
        logger.setLevel(self.args.loglevel)

        if self.args.syslog_address:
            try:
                host, port = self.args.syslog_address.split(":")
            except ValueError:
                syslog_logger = SysLogHandler(self.args.syslog_address)
            else:
                syslog_logger = SysLogHandler((host, int(port)))

            logger.addHandler(syslog_logger)

        try:
            MachineController(mpf_path, machine_path, vars(self.args)).run()
            logging.info("MPF run loop ended.")
            self.exit()

        # pylint: disable-msg=broad-except
        except Exception as e:
            self.exit(exception=e)
Ejemplo n.º 13
0
USER_AGENT_VERSION = "v2"

import sys
import os
import argparse

from time import time

if sys.version_info >= (3, 0):
    import configparser
else:
    import ConfigParser as configparser

import logging

logging.getLogger('libcloud.common.google').addHandler(logging.NullHandler())

try:
    import json
except ImportError:
    import simplejson as json

try:
    from libcloud.compute.types import Provider
    from libcloud.compute.providers import get_driver
    _ = Provider.GCE
except:
    sys.exit("GCE inventory script requires libcloud >= 0.13")


class CloudInventoryCache(object):
Ejemplo n.º 14
0
LOG = logging.getLogger('bw_plex')
INI_FILE = os.path.join(DEFAULT_FOLDER, 'config.ini')


makedirs(DEFAULT_FOLDER, exist_ok=True)
makedirs(THEMES, exist_ok=True)
makedirs(TEMP_THEMES, exist_ok=True)

CONFIG = read_or_make(INI_FILE)

if CONFIG.get('level') in ['', 'info']:  # Should we just use a int?
    lvl = logging.INFO
else:
    lvl = logging.DEBUG

handle = logging.NullHandler()

frmt = logging.Formatter(CONFIG.get('logformat', '%(asctime)s :: %(name)s :: %(levelname)s :: %(message)s'))
handle.setFormatter(frmt)
LOG.addHandler(handle)

# CONSOLE
stream_handle = logging.StreamHandler()
stream_handle.setFormatter(frmt)
LOG.addHandler(stream_handle)

handle.setFormatter(frmt)
LOG.addHandler(handle)

# FILE
rfh = RotatingFileHandler(LOG_FILE, 'a', 512000, 3)
Ejemplo n.º 15
0
    a['UnicodeIOType'] = TextIO() # the new StringIO
    a['LoggingAdapterType'] = logging.LoggingAdapter(_logger,_dict) # pickle ok
    if HAS_CTYPES:
        a['CBoolType'] = ctypes.c_bool(1)
        a['CLongDoubleType'] = ctypes.c_longdouble()
except ImportError:
    pass
try: # python 2.7
    import argparse
    # data types (CH 8)
    a['OrderedDictType'] = collections.OrderedDict(_dict)
    a['CounterType'] = collections.Counter(_dict)
    if HAS_CTYPES:
        a['CSSizeTType'] = ctypes.c_ssize_t()
    # generic operating system services (CH 15)
    a['NullHandlerType'] = logging.NullHandler() # pickle ok  # new 2.7
    a['ArgParseFileType'] = argparse.FileType() # pickle ok
#except AttributeError:
except ImportError:
    pass

# -- pickle fails on all below here -----------------------------------------
# types module (part of CH 8)
a['CodeType'] = compile('','','exec')
a['DictProxyType'] = type.__dict__
a['DictProxyType2'] = _newclass.__dict__
a['EllipsisType'] = Ellipsis
a['ClosedFileType'] = open(os.devnull, 'wb', buffering=0).close()
a['GetSetDescriptorType'] = array.array.typecode
a['LambdaType'] = _lambda = lambda x: lambda y: x #XXX: works when not imported!
a['MemberDescriptorType'] = type.__dict__['__weakrefoffset__']
Ejemplo n.º 16
0
 def set_null(self):
     self._logger.addHandler(logging.NullHandler())
Ejemplo n.º 17
0
import logging

from .oauth1_auth import OAuth1
from .oauth1_session import OAuth1Session
from .oauth2_auth import OAuth2
from .oauth2_session import OAuth2Session, TokenUpdated

__version__ = "1.2.0"

import requests

if requests.__version__ < "2.0.0":
    msg = ("You are using requests version %s, which is older than "
           "requests-oauthlib expects, please upgrade to 2.0.0 or later.")
    raise Warning(msg % requests.__version__)

logging.getLogger("requests_oauthlib").addHandler(logging.NullHandler())
Ejemplo n.º 18
0
  def initialize(self, logs=None, verbosity=None, formatter=None, propagate=False):
    """
    This resets and configures the "returnn" logger.

    :param list[str] logs: "stdout", "|<pipe-cmd>", "<filename>"|"<filename>$date<ext>". "stdout" is always added
    :param list[int] verbosity: levels 0-5 for the log handlers
    :param list[str] formatter: 'default', 'timed', 'raw' or 'verbose', for the log handlers
    :param bool propagate:
    """
    if formatter is None:
      formatter = []
    if verbosity is None:
      verbosity = []
    if logs is None:
      logs = []
    self.initialized = True
    fmt = {
      'default': logging.Formatter('%(message)s'),
      'timed': logging.Formatter('%(asctime)s %(message)s', datefmt='%Y-%m-%d,%H:%M:%S.%MS'),
      'raw': logging.Formatter('%(message)s'),
      'verbose': logging.Formatter('%(levelname)s - %(asctime)s %(message)s', datefmt='%Y-%m-%d,%H:%M:%S.%MS')
    }
    logger = logging.getLogger("returnn")
    # Note on propagation:
    # This is not so clear. By default, the root logger anyway does nothing.
    # However, if you mix RETURNN with other code, which might setup the root logger
    # (e.g. via logging.basicConfig(...)), then there is some root logger,
    # and maybe we should also use it.
    # But at this point here, we might not know about this
    # -- maybe the user would call logging.basicConfig(...) at some later point.
    # In any case, if there is a root logger and we would propagate,
    # we should not add "stdout" here,
    # although that might depend on the root logger level and handlers.
    # For now, the default is to just disable propagation, to keep that separated
    # and avoid any such problems.
    logger.propagate = propagate
    # Reset handler list, in case we have initialized some earlier (e.g. multiple log.initialize() calls).
    logger.handlers = []
    self.v = [logger] * 6  # no need for separate loggers, we do all via log levels
    if 'stdout' not in logs and not propagate:
      logs.append('stdout')
    if len(formatter) == 1:
      # if only one format provided, use it for all logs
      formatter = [formatter[0]] * len(logs)
    # Define own level names. In reverse order, such that the name by default still has the default behavior.
    logging.addLevelName(logging.DEBUG + 2, "DEBUG")
    logging.addLevelName(logging.DEBUG + 1, "DEBUG")
    logging.addLevelName(logging.DEBUG + 0, "DEBUG")
    logging.addLevelName(logging.INFO + 1, "INFO")
    logging.addLevelName(logging.INFO + 0, "INFO")
    _VerbosityToLogLevel = {
      0: logging.ERROR,
      1: logging.INFO + 1,
      2: logging.INFO,
      3: logging.DEBUG + 2,
      4: logging.DEBUG + 1,
      5: logging.DEBUG}
    self.verbose = [False] * 6
    for i in range(len(logs)):
      t = logs[i]
      v = 3
      if i < len(verbosity):
        v = verbosity[i]
      elif len(verbosity) == 1:
        v = verbosity[0]
      assert v <= 5, "invalid verbosity: " + str(v)
      for j in range(v + 1):
        self.verbose[j] = True
      f = fmt['default'] if i >= len(formatter) or formatter[i] not in fmt else fmt[formatter[i]]
      if t == 'stdout':
        handler = logging.StreamHandler(sys.stdout)
      elif t.startswith("|"):  # pipe-format
        proc_cmd = t[1:].strip()
        from subprocess import Popen, PIPE
        proc = Popen(proc_cmd, shell=True, stdin=PIPE)
        handler = logging.StreamHandler(proc.stdin)
      elif os.path.isdir(os.path.dirname(t)):
        if "$" in t:
          from returnn.util.basic import get_utc_start_time_filename_part
          t = string.Template(t).substitute(date=get_utc_start_time_filename_part())
        self.filename = t
        handler = logging.FileHandler(t)
      else:
        assert False, "invalid log target %r" % t
      handler.setLevel(_VerbosityToLogLevel[v])
      handler.setFormatter(f)
      logger.addHandler(handler)
    logger.setLevel(logging.DEBUG)
    if not logger.handlers:
      logger.addHandler(logging.NullHandler())
    self.v1 = Stream(self.v[1], _VerbosityToLogLevel[1])
    self.v2 = Stream(self.v[2], _VerbosityToLogLevel[2])
    self.v3 = Stream(self.v[3], _VerbosityToLogLevel[3])
    self.v4 = Stream(self.v[4], _VerbosityToLogLevel[4])
    self.v5 = Stream(self.v[5], _VerbosityToLogLevel[5])
Ejemplo n.º 19
0
import sys
import functools
import logging
import pprint
import os
import warnings

import six
from lxml import objectify

from contextlib import contextmanager
from ..report.base import Report, ReportGroup
from ..utils.comparison import is_regex

null_handler = logging.NullHandler()


def context_wrapper(ctx_manager, *ctx_args, **ctx_kwargs):
    """
    Higher order function that returns a decorator that runs the wrapped
    func within the context of the given `ctx_manager` initialized
    by `ctx_args` and `ctx_kwargs`
    """
    def _wrapper(func):
        @functools.wraps(func)
        def _inner(*args, **kwargs):
            with ctx_manager(*ctx_args, **ctx_kwargs):
                return func(*args, **kwargs)

        return _inner
Ejemplo n.º 20
0
sqla_094 = _vers >= (0, 9, 4)
sqla_099 = _vers >= (0, 9, 9)
sqla_100 = _vers >= (1, 0, 0)
if not sqla_07:
    raise CommandError(
        "SQLAlchemy 0.7.3 or greater is required. ")

from sqlalchemy.util import format_argspec_plus, update_wrapper
from sqlalchemy.util.compat import inspect_getfullargspec

import logging
log = logging.getLogger(__name__)

if py27:
    # disable "no handler found" errors
    logging.getLogger('alembic').addHandler(logging.NullHandler())


try:
    import fcntl
    import termios
    import struct
    ioctl = fcntl.ioctl(0, termios.TIOCGWINSZ,
                        struct.pack('HHHH', 0, 0, 0, 0))
    _h, TERMWIDTH, _hp, _wp = struct.unpack('HHHH', ioctl)
    if TERMWIDTH <= 0:  # can occur if running in emacs pseudo-tty
        TERMWIDTH = None
except (ImportError, IOError):
    TERMWIDTH = None

Ejemplo n.º 21
0
    def __init__(self):
        """

        """
        self._logger = logging.getLogger(__name__)
        self._logger.addHandler(logging.NullHandler())
Ejemplo n.º 22
0
 def __init__(self):
     logging.getLogger(__name__).addHandler(logging.NullHandler())
Ejemplo n.º 23
0
import typer
from rich.console import Console
from rich.logging import RichHandler

from mk import __version__
from mk._typer import CustomTyper
from mk.ctx import ctx

app = CustomTyper()
handlers: List[logging.Handler]
console_err = Console(stderr=True)

if "_MK_COMPLETE" in os.environ:
    level = logging.CRITICAL
    handlers = [logging.NullHandler()]
else:
    level = logging.DEBUG
    handlers = [
        RichHandler(console=console_err,
                    show_time=False,
                    show_path=False,
                    markup=True)
    ]

logging.basicConfig(
    level=logging.DEBUG,
    format="%(message)s",
    handlers=handlers,
)
def null_logger():
    logger = logging.getLogger("cvescan.null")
    if not logger.hasHandlers():
        logger.addHandler(logging.NullHandler())

    return logger
Ejemplo n.º 25
0
import sys
import logging
import random
import unicodedata
import urllib
from collections import namedtuple
from requests import Session, exceptions
from requests.adapters import HTTPAdapter
from operator import itemgetter

# For use during debugging
# import pprint
# from traceback import print_exception

logging.captureWarnings(True)
logging.getLogger().addHandler(logging.NullHandler())
logging.getLogger().setLevel(logging.DEBUG)

class _Colors:
    """A trivial class that defines various ANSI color codes.

    """
    BOLD = '\033[1m'
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'

def pretty_print(color, msg=""):
Ejemplo n.º 26
0
    def serve_forever(self):
        self.readline_support()

        if not self._rooms:
            # artificially join a room if None were specified.
            self.query_room("#testroom").join()

        if self.demo_mode:
            # disable the console logging once it is serving in demo mode.
            root = logging.getLogger()
            root.removeHandler(console_hdlr)
            root.addHandler(logging.NullHandler())
        self.connect_callback()  # notify that the connection occured
        self.callback_presence(Presence(identifier=self.user, status=ONLINE))

        self.send_message(Message(INTRO))

        try:
            while True:

                if self._inroom:
                    frm = TextOccupant(self.user, self.rooms[0])
                    to = self.rooms[0]
                else:
                    frm = self.user
                    to = self.bot_identifier

                print()
                full_msg = ""
                while True:
                    prompt = "[␍] " if full_msg else ">>> "
                    if ANSI or self.demo_mode:
                        color = (
                            fg.red
                            if self.user.person in self.bot_config.BOT_ADMINS[0]
                            else fg.green
                        )
                        prompt = f"{color}[{frm} ➡ {to}] {fg.cyan}{prompt}{fx.reset}"
                        entry = input(prompt)
                    else:
                        entry = input(f"[{frm} ➡ {to}] {prompt}")

                    if not self._multiline:
                        full_msg = entry
                        break

                    if not entry:
                        break

                    full_msg += entry + "\n"

                msg = Message(full_msg)
                msg.frm = frm
                msg.to = to

                self.callback_message(msg)

                mentioned = [
                    self.build_identifier(word)
                    for word in re.findall(r"(?<=\s)@[\w]+", entry)
                ]
                if mentioned:
                    self.callback_mention(msg, mentioned)

                sleep(0.5)
        except EOFError:
            pass
        except KeyboardInterrupt:
            pass
        finally:
            # simulate some real presence
            self.callback_presence(Presence(identifier=self.user, status=OFFLINE))
            log.debug("Trigger disconnect callback")
            self.disconnect_callback()
            log.debug("Trigger shutdown")
            self.shutdown()
Ejemplo n.º 27
0
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""`bert_fun.py` - some useful utility methods for interacting with BERT models"""
from copy import deepcopy
import logging
import math
from itertools import chain
from typing import Tuple, List, Dict

import torch
from nltk import word_tokenize
from transformers import BertForMaskedLM, BertTokenizer

LOG = logging.getLogger(__name__)
LOG.addHandler(logging.NullHandler())

# pylint: disable=too-many-locals


def get_word_probabilities(
    sentence: str, bert_model: BertForMaskedLM, bert_tokenizer: BertTokenizer
) -> Tuple[Tuple[str, Tuple[str, ...], Tuple[float, ...]]]:
    """
    Returns the probability of each word in a sentence.
    Returns a sequence of subtokens and their probabilities.

    :param sentence: A sentence providing context for the word, max tokens 512.
    :param bert_model: an instance of BertForMaskedLM (preferably cased, large)
    :param bert_tokenizer: a BertTokenizer (preferably cased, large)
    :return: a Tuple of values: original token string, word as subtokens, subtoken ids
Ejemplo n.º 28
0
 def setUp(self):
     self.param = pyx12.params.params()
     self.logger = logging.getLogger('pyx12')
     formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
     hdlr = logging.NullHandler()
     self.logger.addHandler(hdlr)
Ejemplo n.º 29
0
# coding=utf-8

import logging

from medusa import app, common
from medusa.logger.adapters.style import BraceAdapter
from pynma import pynma
from six import text_type

log = BraceAdapter(logging.getLogger(__name__))
log.logger.addHandler(logging.NullHandler())


class Notifier(object):
    def test_notify(self, nma_api, nma_priority):
        return self._sendNMA(nma_api,
                             nma_priority,
                             event='Test',
                             message='Testing NMA settings from Medusa',
                             force=True)

    def notify_snatch(self, ep_name, is_proper):
        if app.NMA_NOTIFY_ONSNATCH:
            self._sendNMA(nma_api=None,
                          nma_priority=None,
                          event=common.notifyStrings[(
                              common.NOTIFY_SNATCH,
                              common.NOTIFY_SNATCH_PROPER)[is_proper]],
                          message=ep_name)

    def notify_download(self, ep_name):
Ejemplo n.º 30
0
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#

import logging
import sys

__all__ = ['configure_default_logger', 'LOG_VERBOSE']

LOG_VERBOSE = 5

_root = logging.getLogger()
_root.addHandler(logging.NullHandler())

logger = logging.getLogger('pycomm3')


def _verbose(self: logging.Logger, msg, *args, **kwargs):
    if self.isEnabledFor(LOG_VERBOSE):
        self._log(LOG_VERBOSE, msg, *args, **kwargs)


logging.addLevelName(LOG_VERBOSE, 'VERBOSE')
logging.verbose = _verbose
logging.Logger.verbose = _verbose


def configure_default_logger(level: int = logging.INFO, filename=None):