<commit_before># -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))

# -- Project information -----------------------------------------------------

project = 'AFF3CT'
copyright = '2018, AFF3CT\'s team'
author = 'AFF3CT\'s team'

# The short X.Y version
version = ''
# The full version, including alpha/beta/rc tags
release = '2.0.0'

# -- General configuration ---------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.mathjax',
    'sphinxcontrib.bibtex',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The master toctree document.
master_doc = 'index'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = None

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# The default sidebars (for documents that don't match any pattern) are
# defined by theme itself.  Builtin themes are using these templates by
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
# 'searchbox.html']``.
#
# html_sidebars = {}

# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'AFF3CTdoc'

# -- Options for LaTeX output ------------------------------------------------

latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    #
    # 'papersize': 'letterpaper',

    # The font size ('10pt', '11pt' or '12pt').
    #
    # 'pointsize': '10pt',

    # Additional stuff for the LaTeX preamble.
    #
    # 'preamble': '',

    # Latex figure (float) alignment
    #
    # 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (master_doc, 'AFF3CT.tex', 'AFF3CT Documentation',
     'AFF3CT\'s team', 'manual'),
]

# -- Options for manual page output ------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'aff3ct', 'AFF3CT Documentation',
     [author], 1)
]

# -- Options for Texinfo output ----------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (master_doc, 'AFF3CT', 'AFF3CT Documentation',
     author, 'AFF3CT', 'One line description of project.',
     'Miscellaneous'),
]

# -- Options for Epub output -------------------------------------------------

# Bibliographic Dublin Core info.
epub_title = project

# The unique identifier of the text. This can be a ISBN number
# or the project homepage.
#
# epub_identifier = ''

# A unique identification for the text.
#
# epub_uid = ''

# A list of files that should not be packed into the epub file.
epub_exclude_files = ['search.html']

rst_epilog = """

.. |logo_ims| image:: https://www.ims-bordeaux.fr/images/logoimsjoom.png
    :alt: logo-ims-bordeaux
    :width: 60px
    :height: 30px

.. |image_required_argument| image:: /simulation/parameters/images/required.png
                             :alt:  Required
                             :width:  80px
                             :height: 25px

.. |image_advanced_argument| image:: /simulation/parameters/images/advanced.png
                             :alt:  Advanced
                             :width:  80px
                             :height: 25px

"""

# -- Extension configuration -------------------------------------------------<commit_msg>Add html_favicon and html_logo in conf.py but set to None.<commit_after># -*- coding: utf-8 -*-
#
# Configuration file for the Sphinx documentation builder.
#
# This file does only contain a selection of the most common options. For a
# full list see the documentation:
# http://www.sphinx-doc.org/en/master/config

# -- Path setup --------------------------------------------------------------

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#
# import os
# import sys
# sys.path.insert(0, os.path.abspath('.'))

# -- Project information -----------------------------------------------------

project = 'AFF3CT'
copyright = '2018, AFF3CT\'s team'
author = 'AFF3CT\'s team'

# The short X.Y version
version = ''
# The full version, including alpha/beta/rc tags
release = '2.1.0'

# -- General configuration ---------------------------------------------------

# If your documentation needs a minimal Sphinx version, state it here.
#
# needs_sphinx = '1.0'

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.mathjax',
    'sphinxcontrib.bibtex',
]

# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

# The suffix(es) of source filenames.
# You can specify multiple suffix as a list of string:
#
# source_suffix = ['.rst', '.md']
source_suffix = '.rst'

# The master toctree document.
master_doc = 'index'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#
# This is also used if you do content translation via gettext catalogs.
# Usually you set "language" from the command line for these cases.
language = None

# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []

# The name of the Pygments (syntax highlighting) style to use.
pygments_style = None

# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages.  See the documentation for
# a list of builtin themes.
#
html_theme = 'sphinx_rtd_theme'

# Theme options are theme-specific and customize the look and feel of a theme
# further.  For a list of options available for each theme, see the
# documentation.
#
# html_theme_options = {}

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']

# Custom sidebar templates, must be a dictionary that maps document names
# to template names.
#
# The default sidebars (for documents that don't match any pattern) are
# defined by theme itself.  Builtin themes are using these templates by
# default: ``['localtoc.html', 'relations.html', 'sourcelink.html',
# 'searchbox.html']``.
#
# html_sidebars = {}

html_favicon = None
html_logo    = None

# -- Options for HTMLHelp output ---------------------------------------------

# Output file base name for HTML help builder.
htmlhelp_basename = 'AFF3CTdoc'

# -- Options for LaTeX output ------------------------------------------------

latex_elements = {
    # The paper size ('letterpaper' or 'a4paper').
    #
    # 'papersize': 'letterpaper',

    # The font size ('10pt', '11pt' or '12pt').
    #
    # 'pointsize': '10pt',

    # Additional stuff for the LaTeX preamble.
    #
    # 'preamble': '',

    # Latex figure (float) alignment
    #
    # 'figure_align': 'htbp',
}

# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title,
#  author, documentclass [howto, manual, or own class]).
latex_documents = [
    (master_doc, 'AFF3CT.tex', 'AFF3CT Documentation',
     'AFF3CT\'s team', 'manual'),
]

# -- Options for manual page output ------------------------------------------

# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
    (master_doc, 'aff3ct', 'AFF3CT Documentation',
     [author], 1)
]

# -- Options for Texinfo output ----------------------------------------------

# Grouping the document tree into Texinfo files. List of tuples
# (source start file, target name, title, author,
#  dir menu entry, description, category)
texinfo_documents = [
    (master_doc, 'AFF3CT', 'AFF3CT Documentation',
     author, 'AFF3CT', 'One line description of project.',
     'Miscellaneous'),
]

# -- Options for Epub output -------------------------------------------------

# Bibliographic Dublin Core info.
epub_title = project

# The unique identifier of the text. This can be a ISBN number
# or the project homepage.
#
# epub_identifier = ''

# A unique identification for the text.
#
# epub_uid = ''

# A list of files that should not be packed into the epub file.
epub_exclude_files = ['search.html']

rst_epilog = """

.. |logo_ims| image:: https://www.ims-bordeaux.fr/images/logoimsjoom.png
    :alt: logo-ims-bordeaux
    :width: 60px
    :height: 30px

.. |image_required_argument| image:: /simulation/parameters/images/required.png
                             :alt:  Required
                             :width:  80px
                             :height: 25px

.. |image_advanced_argument| image:: /simulation/parameters/images/advanced.png
                             :alt:  Advanced
                             :width:  80px
                             :height: 25px

"""

# -- Extension configuration -------------------------------------------------<|endoftext|>


<commit_before>import json
from network import Listener, Handler, poll
import sqlite3
from random import randint

HOST_NAME = 'localhost'
PORT_NUMBER = 8888
SALT = 'secret salt'  # protects from dictionary attacks

class DB:

    def __init__(self):
        self._conn = sqlite3.connect('whale.sqlite3')
        self._cur = self._conn.cursor()
        # TODO: add expiration timestamp to the players table
        self.execute('''
            CREATE TABLE IF NOT EXISTS players
            (url TEXT, name TEXT, x INT, y INT, size INT,
            PRIMARY KEY (url, name) );
            ''')
        self.execute('''
            CREATE TABLE IF NOT EXISTS pellets
            (id INTEGER PRIMARY KEY, x INT, y INT, size INT);
            ''')
        for pid in range(4):
            try:
                q = 'INSERT OR ABORT INTO pellets VALUES (?, ?, ?, ?);'
                args = pid, randint(10, 380), randint(10, 280), 5
                self.execute(q, args)
            except sqlite3.IntegrityError:
                # primary key conflict: row already exists
                pass

    def execute(self, q, args=()):
        return self._cur.execute(q, args)

    def commit(self):
        self._conn.commit()

    def close(self):
        self._conn.close()

db = DB()

input_map = {'up': (0, -5), 'down': (0, 5), 'left': (-5, 0), 'right': (5, 0)}
borders = [ [0, 0, 2, 300], [0, 0, 400, 2], [398, 0, 2, 300], [0, 298, 400, 2] ]
def collide_boxes(box1, box2):
    x1, y1, w1, h1 = box1
    x2, y2, w2, h2 = box2
    return x1 < x2 + w2 and y1 < y2 + h2 and x2 < x1 + w1 and y2 < y1 + h1

def get_game(args):
    repr = {'borders': {'link': '/borders'},
            'pellets': {'link': '/pellets'},
            'players': {'link': '/players'}
            }
    if 'name' in args:  # TODO: clients know to submit their name from /login
        name = args['name']  # TODO: also add password detection
        q = 'SELECT name, url FROM players'
        players = db.execute(q).fetchall()
        name2url = {p[0]: p[1] for p in players}
        if name in name2url:
            url = name2url[name]
        else:
            url = '/player/' + str(hash(name + SALT))
            q = '''INSERT INTO players(url, name, x, y, size)
                    VALUES (?, ?, ?, ?, ?);'''
            qargs = url, name, randint(10, 380), randint(10, 280), 10
            db.execute(q, qargs)
            db.commit()
        repr['you'] = {'form': {'method': 'POST',
                                'url': url,
                                'args': ['direction']
                                }
                       }
    return 'app/game+json', repr

def get_borders(args):
    # borders are static, can be cached, no need to store them in DB
    return 'app/boxlist+json', borders

def get_pellets(args):
    # pellets may change, fetch them from DB
    q = 'SELECT x, y, size FROM pellets;'
    pellets = db.execute(q).fetchall()
    box_list = [ [p[0], p[1], p[2], p[2]] for p in pellets]
    return 'app/boxlist+json', box_list

def get_players(args):
    # players always change, fetch them from DB
    q = 'SELECT name, x, y, size, url FROM players;'
    players = db.execute(q).fetchall()
    box_dict = { p[0]: [p[1], p[2], p[3], p[3]] for p in players}
    return 'app/boxdict+json', box_dict

def post_player(url, args):
    # TODO: detect collisions with other players
    q = 'SELECT name, x, y, size FROM players WHERE url = ?;'
    qargs = (url,)
    row = db.execute(q, qargs).fetchone()
    if not row:
        return 'text/plain', 'Wrong URL'
    name, x, y, size = row
    if 'direction' not in args:
        return 'text/plain', 'Need direction argument'
    dx, dy = input_map[args['direction']]
    box = [x + dx, y + dy, size, size]
    # collision with pellets
    q = 'SELECT id, x, y, size FROM pellets;'
    pellets = db.execute(q).fetchall()
    for pellet in pellets:
        pid, px, py, psize = pellet
        if collide_boxes(box, [px, py, psize, psize]):
            box = [box[0], box[1], box[2] * 1.2, box[3] * 1.2]  # grow
            q = '''UPDATE pellets SET x = ?, y = ?, size = ?
                    WHERE id = ?;'''
            qargs = randint(10, 380), randint(10, 280), 5, pid
            db.execute(q, qargs)
            db.commit()
    # collision with borders
    col_brdr = any([collide_boxes(box, brdr) for brdr in borders])
    if col_brdr:
        box = [randint(10, 380), randint(10, 280), 10, 10]
    q = 'UPDATE players SET x = ?, y = ?, size = ? WHERE url = ?;'
    qargs = box[0], box[1], box[2], url
    db.execute(q, qargs)
    db.commit()
    return 'app/boxdict+json', {name: box}

class MyHandler(Handler):

    def on_msg(self, req):
        method = req['method']
        resource = req['resource']
        args = req['args']
        if (method, resource) == ('GET', '/game/1'):
            mtype, repr = get_game(args)
        elif (method, resource) == ('GET', '/borders'):
            mtype, repr = get_borders(args)
        elif (method, resource) == ('GET', '/pellets'):
            mtype, repr = get_pellets(args)
        elif (method, resource) == ('GET', '/players'):
            mtype, repr = get_players(args)
        elif method == 'POST' and resource.startswith('/player/'):
            mtype, repr = post_player(resource, args)
        else:  # invalid resource
            mtype, repr = 'text/plain', 'Wrong resource or method.\nGo to /'
        response = {'resource': resource,
                    'type': mtype,
                    'representation': repr
                    }
        self.do_send(response)

if __name__ == '__main__':
    server = Listener(8888, MyHandler)
    try:
        while 1:
            poll()
    except KeyboardInterrupt:
        db.close()
<commit_msg>apply collisions between players<commit_after>import json
from network import Listener, Handler, poll
from random import randint
import sqlite3

HOST_NAME = 'localhost'
PORT_NUMBER = 8888
SALT = 'secret salt'  # protects from dictionary attacks

BORDERS = [ [0, 0, 2, 300], [0, 0, 400, 2], [398, 0, 2, 300], [0, 298, 400, 2] ]

class DB:

    def __init__(self):
        self._conn = sqlite3.connect('whale.sqlite3')
        self._cur = self._conn.cursor()
        # TODO: add expiration timestamp to the players table
        self.execute('''
            CREATE TABLE IF NOT EXISTS players
            (url TEXT, name TEXT, x INT, y INT, size INT,
            PRIMARY KEY (url, name) );
            ''')
        self.execute('''
            CREATE TABLE IF NOT EXISTS pellets
            (id INTEGER PRIMARY KEY, x INT, y INT, size INT);
            ''')
        for pid in range(4):
            try:
                q = 'INSERT OR ABORT INTO pellets VALUES (?, ?, ?, ?);'
                args = pid, randint(10, 380), randint(10, 280), 5
                self.execute(q, args)
            except sqlite3.IntegrityError:
                # primary key conflict: row already exists
                pass

    def execute(self, q, args=()):
        return self._cur.execute(q, args)

    def commit(self):
        self._conn.commit()

    def close(self):
        self._conn.close()

class MyHandler(Handler):

    def on_msg(self, req):
        method = req['method']
        resource = req['resource']
        args = req['args']
        if (method, resource) == ('GET', '/game/1'):
            mtype, repr = get_game(args)
        elif (method, resource) == ('GET', '/borders'):
            mtype, repr = get_borders(args)
        elif (method, resource) == ('GET', '/pellets'):
            mtype, repr = get_pellets(args)
        elif (method, resource) == ('GET', '/players'):
            mtype, repr = get_players(args)
        elif method == 'POST' and resource.startswith('/player/'):
            mtype, repr = post_player(resource, args)
        else:  # invalid resource
            mtype, repr = 'text/plain', 'Wrong resource or method.\nGo to /'
        response = {'resource': resource,
                    'type': mtype,
                    'representation': repr
                    }
        self.do_send(response)

def collide_boxes(box1, box2):
    x1, y1, w1, h1 = box1
    x2, y2, w2, h2 = box2
    return x1 < x2 + w2 and y1 < y2 + h2 and x2 < x1 + w1 and y2 < y1 + h1

def get_game(args):
    repr = {'borders': {'link': '/borders'},
            'pellets': {'link': '/pellets'},
            'players': {'link': '/players'}
            }
    if 'name' in args:  # TODO: clients know to submit their name from /login
        name = args['name']  # TODO: also add password detection
        q = 'SELECT name, url FROM players'
        players = db.execute(q).fetchall()
        name2url = {p[0]: p[1] for p in players}
        if name in name2url:
            url = name2url[name]
        else:
            url = '/player/' + str(hash(name + SALT))
            q = '''INSERT INTO players(url, name, x, y, size)
                    VALUES (?, ?, ?, ?, ?);'''
            qargs = url, name, randint(10, 380), randint(10, 280), 10
            db.execute(q, qargs)
            db.commit()
        repr['you'] = {'form': {'method': 'POST',
                                'url': url,
                                'args': ['direction']
                                }
                       }
    return 'app/game+json', repr

def get_borders(args):
    # borders are static, can be cached, no need to store them in DB
    return 'app/boxlist+json', BORDERS

def get_pellets(args):
    # pellets may change, fetch them from DB
    q = 'SELECT x, y, size FROM pellets;'
    pellets = db.execute(q).fetchall()
    box_list = [ [p[0], p[1], p[2], p[2]] for p in pellets]
    return 'app/boxlist+json', box_list

def get_players(args):
    # players always change, fetch them from DB
    q = 'SELECT name, x, y, size, url FROM players;'
    players = db.execute(q).fetchall()
    box_dict = { p[0]: [p[1], p[2], p[3], p[3]] for p in players}
    return 'app/boxdict+json', box_dict

input_map = {'up': (0, -5), 'down': (0, 5), 'left': (-5, 0), 'right': (5, 0)}

def post_player(url, args):
    # TODO: detect collisions with other players
    q = 'SELECT name, x, y, size FROM players WHERE url = ?;'
    qargs = (url,)
    row = db.execute(q, qargs).fetchone()
    if not row:
        return 'text/plain', 'Wrong URL'
    name, x, y, size = row
    if 'direction' not in args:
        return 'text/plain', 'Need direction argument'
    dx, dy = input_map[args['direction']]
    box = [x + dx, y + dy, size, size]
    # collision with other players
    q = 'SELECT name, x, y, size FROM players WHERE name != ?;'
    qargs = (name,)
    players = db.execute(q, qargs).fetchall()
    for player in players:
        pname, px, py, psize = player
        if collide_boxes(box, [px, py, psize, psize]):
            if size > psize:
                box = [box[0], box[1], box[2] + psize, box[3] + psize]
                q = 'UPDATE players SET x = ?, y = ?, size = ? WHERE name = ?;'
                qargs = randint(10, 380), randint(10, 280), 10, pname
                db.execute(q, qargs)
            elif size < psize:
                box = [randint(10, 380), randint(10, 280), 10, 10]
                q = 'UPDATE players SET size = ? WHERE name = ?;'
                qargs = (psize + box[2]), pname
                db.execute(q, qargs)
            else: # same size, kill both
                box = [randint(10, 380), randint(10, 280), 10, 10]
                q = 'UPDATE players SET x = ?, y = ?, size = ? WHERE name = ?;'
                qargs = randint(10, 380), randint(10, 280), 10, pname
                db.execute(q, qargs)
    # collision with pellets
    q = 'SELECT id, x, y, size FROM pellets;'
    pellets = db.execute(q).fetchall()
    for pellet in pellets:
        pid, px, py, psize = pellet
        if collide_boxes(box, [px, py, psize, psize]):
            box = [box[0], box[1], box[2] * 1.2, box[3] * 1.2]  # grow
            q = '''UPDATE pellets SET x = ?, y = ?, size = ?
                    WHERE id = ?;'''
            qargs = randint(10, 380), randint(10, 280), 5, pid
            db.execute(q, qargs)
    # collision with borders
    col_brdr = any([collide_boxes(box, brdr) for brdr in BORDERS])
    if col_brdr:
        box = [randint(10, 380), randint(10, 280), 10, 10]
    q = 'UPDATE players SET x = ?, y = ?, size = ? WHERE url = ?;'
    qargs = box[0], box[1], box[2], url
    db.execute(q, qargs)
    db.commit()
    return 'app/boxdict+json', {name: box}

if __name__ == '__main__':
    db = DB()
    server = Listener(8888, MyHandler)
    try:
        while 1:
            poll()
    except KeyboardInterrupt:
        db.close()
<|endoftext|>


<commit_before>
# This file has been generated using `bazel run scripts:pinned_browsers`

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//common/private:dmg_archive.bzl", "dmg_archive")
load("//common/private:drivers.bzl", "local_drivers")
load("//common/private:pkg_archive.bzl", "pkg_archive")

def pin_browsers():
    local_drivers()

    http_archive(
        name = "linux_firefox",
        url = "https://ftp.mozilla.org/pub/firefox/releases/91.0.2/linux-x86_64/en-US/firefox-91.0.2.tar.bz2",
        sha256 = "9eaac9c88ff4696228292590b65ab2fd1b0d98b7a1edf5a21abc11b7803a046d",
        build_file_content = "exports_files([\"firefox\"])",
    )

    dmg_archive(
        name = "mac_firefox",
        url = "https://ftp.mozilla.org/pub/firefox/releases/91.0.2/mac/en-US/Firefox%2091.0.2.dmg",
        sha256 = "4147fff1b176a659d6d4d928efe6ec98af313533508809264d78cc83c93cab30",
        build_file_content = "exports_files([\"Firefox.app\"])",
    )

    http_archive(
        name = "linux_geckodriver",
        url = "https://github.com/mozilla/geckodriver/releases/download/v0.29.1/geckodriver-v0.29.1-linux64.tar.gz",
        sha256 = "ec164910a3de7eec71e596bd2a1814ae27ba4c9d112b611680a6470dbe2ce27b",
        build_file_content = "exports_files([\"geckodriver\"])",
    )

    http_archive(
        name = "mac_geckodriver",
        url = "https://github.com/mozilla/geckodriver/releases/download/v0.29.1/geckodriver-v0.29.1-macos.tar.gz",
        sha256 = "9929c804ad0157ca13fdafca808866c88815b658e7059280a9f08f7e70364963",
        build_file_content = "exports_files([\"geckodriver\"])",
    )

    pkg_archive(
        name = "mac_edge",
        url = "https://officecdn-microsoft-com.akamaized.net/pr/C1297A47-86C4-4C1F-97FA-950631F94777/MacAutoupdate/MicrosoftEdge-92.0.902.84.pkg?platform=Mac&Consent=0&channel=Stable",
        sha256 = "1cb38e5d07392a3e4598e8644842e97e5583324c1c53df77f4c3c60fde726c2b",
        move = {
            "MicrosoftEdge-92.0.902.84.pkg/Payload/Microsoft Edge.app": "Edge.app",
        },
        build_file_content = "exports_files([\"Edge.app\"])",
    )

    http_archive(
        name = "mac_edgedriver",
        url = "https://msedgedriver.azureedge.net/92.0.902.84/edgedriver_mac64.zip",
        sha256 = "c4bd74dc6e1026f6cb6beaf8fa0ecc2e6208ac1e604460432d14e6de90b0dabe",
        build_file_content = "exports_files([\"msedgedriver\"])",
    )

    http_archive(
        name = "linux_chrome",
        url = "https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/885174/chrome-linux.zip",
        sha256 = "7a7781b13a9296f67aefda073b056446ef850c01c5b56e90dde1f3cfb1bde806",
        build_file_content = "exports_files([\"chrome-linux\"])",
    )

    http_archive(
        name = "mac_chrome",
        url = "https://storage.googleapis.com/chromium-browser-snapshots/Mac/885174/chrome-mac.zip",
        sha256 = "8aab4d6052d42c660afd18b100fe7c0b4df6d1d523be953dce6601b873b6f205",
        strip_prefix = "chrome-mac",
        build_file_content = "exports_files([\"Chromium.app\"])",
    )

    http_archive(
        name = "linux_chromedriver",
        url = "https://chromedriver.storage.googleapis.com/92.0.4515.107/chromedriver_linux64.zip",
        sha256 = "c3cfba55df31a0a0d62d901049e91ee1ec4e38dd165e752409b7430ae59fcc8b",
        build_file_content = "exports_files([\"chromedriver\"])",
    )

    http_archive(
        name = "mac_chromedriver",
        url = "https://chromedriver.storage.googleapis.com/92.0.4515.107/chromedriver_mac64.zip",
        sha256 = "97f9a5733ef83a001922fd14ff2f5ac5b03c8a16aa02986ecca300cfbffaa03e",
        build_file_content = "exports_files([\"chromedriver\"])",
    )

<commit_msg>Bump chromium version to v93<commit_after>
# This file has been generated using `bazel run scripts:pinned_browsers`

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("//common/private:dmg_archive.bzl", "dmg_archive")
load("//common/private:drivers.bzl", "local_drivers")
load("//common/private:pkg_archive.bzl", "pkg_archive")

def pin_browsers():
    local_drivers()

    http_archive(
        name = "linux_firefox",
        url = "https://ftp.mozilla.org/pub/firefox/releases/91.0.2/linux-x86_64/en-US/firefox-91.0.2.tar.bz2",
        sha256 = "9eaac9c88ff4696228292590b65ab2fd1b0d98b7a1edf5a21abc11b7803a046d",
        build_file_content = "exports_files([\"firefox\"])",
    )

    dmg_archive(
        name = "mac_firefox",
        url = "https://ftp.mozilla.org/pub/firefox/releases/91.0.2/mac/en-US/Firefox%2091.0.2.dmg",
        sha256 = "4147fff1b176a659d6d4d928efe6ec98af313533508809264d78cc83c93cab30",
        build_file_content = "exports_files([\"Firefox.app\"])",
    )

    http_archive(
        name = "linux_geckodriver",
        url = "https://github.com/mozilla/geckodriver/releases/download/v0.29.1/geckodriver-v0.29.1-linux64.tar.gz",
        sha256 = "ec164910a3de7eec71e596bd2a1814ae27ba4c9d112b611680a6470dbe2ce27b",
        build_file_content = "exports_files([\"geckodriver\"])",
    )

    http_archive(
        name = "mac_geckodriver",
        url = "https://github.com/mozilla/geckodriver/releases/download/v0.29.1/geckodriver-v0.29.1-macos.tar.gz",
        sha256 = "9929c804ad0157ca13fdafca808866c88815b658e7059280a9f08f7e70364963",
        build_file_content = "exports_files([\"geckodriver\"])",
    )

    pkg_archive(
        name = "mac_edge",
        url = "https://officecdn-microsoft-com.akamaized.net/pr/C1297A47-86C4-4C1F-97FA-950631F94777/MacAutoupdate/MicrosoftEdge-92.0.902.84.pkg?platform=Mac&Consent=0&channel=Stable",
        sha256 = "1cb38e5d07392a3e4598e8644842e97e5583324c1c53df77f4c3c60fde726c2b",
        move = {
            "MicrosoftEdge-92.0.902.84.pkg/Payload/Microsoft Edge.app": "Edge.app",
        },
        build_file_content = "exports_files([\"Edge.app\"])",
    )

    http_archive(
        name = "mac_edgedriver",
        url = "https://msedgedriver.azureedge.net/92.0.902.84/edgedriver_mac64.zip",
        sha256 = "c4bd74dc6e1026f6cb6beaf8fa0ecc2e6208ac1e604460432d14e6de90b0dabe",
        build_file_content = "exports_files([\"msedgedriver\"])",
    )

    http_archive(
        name = "linux_chrome",
        url = "https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/902192/chrome-linux.zip",
        sha256 = "2fd5218274f01d2ae5db666903189a379ebe683826b579aba0e582d11e7a2722",
        build_file_content = "exports_files([\"chrome-linux\"])",
    )

    http_archive(
        name = "mac_chrome",
        url = "https://storage.googleapis.com/chromium-browser-snapshots/Mac/902192/chrome-mac.zip",
        sha256 = "b944959f7e75bd4457f55662541d921b3b8d0be4e488450006ae615079bc0814",
        strip_prefix = "chrome-mac",
        build_file_content = "exports_files([\"Chromium.app\"])",
    )

    http_archive(
        name = "linux_chromedriver",
        url = "https://chromedriver.storage.googleapis.com/92.0.4515.107/chromedriver_linux64.zip",
        sha256 = "c3cfba55df31a0a0d62d901049e91ee1ec4e38dd165e752409b7430ae59fcc8b",
        build_file_content = "exports_files([\"chromedriver\"])",
    )

    http_archive(
        name = "mac_chromedriver",
        url = "https://chromedriver.storage.googleapis.com/92.0.4515.107/chromedriver_mac64.zip",
        sha256 = "97f9a5733ef83a001922fd14ff2f5ac5b03c8a16aa02986ecca300cfbffaa03e",
        build_file_content = "exports_files([\"chromedriver\"])",
    )

<|endoftext|>


<commit_before>"""
This module contains user interface related code, such as the
main screen and diagram windows.
"""

import os, os.path
import gtk

def _get_accel_map_filename():
    """
    The Gaphor accelMap file ($HOME/.gaphor/accelmap).
    """
    from gaphor import resource
    datadir = resource('UserDataDir')
    if not os.path.exists(datadir):
        os.mkdir(datadir)
    return os.path.join(datadir, 'accelmap')

def load_accel_map():
    """
    Load the user accelerator map from the gaphor user home directory
    """
    filename = _get_accel_map_filename()
    if os.path.exists(filename) and os.path.isfile(filename):
        gtk.accel_map_load(filename)

def save_accel_map():
    """
    Save the contents of the GtkAccelMap to a file.
    """
    filename = _get_accel_map_filename()
    gtk.accel_map_save(filename)

# vim:sw=4:et:
<commit_msg><commit_after>"""
This module contains user interface related code, such as the
main screen and diagram windows.
"""

import os, os.path
import gtk

if os.name == 'nt':
    home = 'USERPROFILE'
else:
    home = 'HOME'

user_data_dir = os.path.join(os.getenv(home), '.gaphor')

def _get_accel_map_filename():
    """
    The Gaphor accelMap file ($HOME/.gaphor/accelmap).
    """
    if not os.path.exists(user_data_dir):
        os.mkdir(user_data_dir)
    return os.path.join(user_data_dir, 'accelmap')

def load_accel_map():
    """
    Load the user accelerator map from the gaphor user home directory
    """
    filename = _get_accel_map_filename()
    if os.path.exists(filename) and os.path.isfile(filename):
        gtk.accel_map_load(filename)

def save_accel_map():
    """
    Save the contents of the GtkAccelMap to a file.
    """
    filename = _get_accel_map_filename()
    gtk.accel_map_save(filename)

# vim:sw=4:et:
<|endoftext|>


<commit_before>from __future__ import absolute_import
from gbdxtools.images.idaho_image import IdahoImage
from gbdxtools.images.catalog_image import CatalogImage
from gbdxtools.images.landsat_image import LandsatImage
from .interface import Interface
<commit_msg>refactoring the FuturesSession so that other pieces of gbdxtools  can more easily use it<commit_after>from __future__ import absolute_import
from requests_futures.sessions import FuturesSession
_session = FuturesSession(max_workers=64)
from gbdxtools.images.idaho_image import IdahoImage
from gbdxtools.images.catalog_image import CatalogImage
from gbdxtools.images.landsat_image import LandsatImage
from .interface import Interface
<|endoftext|>


<commit_before>from StringIO import StringIO
from unittest import TestCase
import csv

from gdt.vumigomessage import (
    VumiGoMessageParser, DirectionalFilter, MSISDNFilter, TimestampFilter,
    FilterPipeline, FilterException)

class GdtTestCase(TestCase):

    HEADER = ("timestamp,from_addr,to_addr,content,message_id,in_reply_to,"
              "session_event,transport_type,direction,"
              "network_handover_status,network_handover_reason,"
              "delivery_status,endpoint\r\n")
    INBOUND = ("2013-09-10 19:24:03.289543,+27817030792,*120*8864*1203#,"
               ",af266289e40949388b5a8cacb4a2d13a,,new,ussd,inbound"
               ",,,,default\r\n")
    OUTBOUND = ("2013-09-11 19:24:03.289543,*120*8864*1203#,+27817030792,"
                ",af266289e40949388b5a8cacb4a2d13b,,resume,ussd,outbound"
                ",,,,default\r\n")
    INBOUND_2 = ("2013-09-09 19:24:03.289543,+27817030710,*120*8864*1203#,"
               ",af266289e40949388b5a8cacb4a2d13a,,new,ussd,inbound"
               ",,,,default\r\n")
    OUTBOUND_2 = ("2013-09-09 19:24:03.289543,*120*8864*1203#,+27817030710,"
                ",af266289e40949388b5a8cacb4a2d13b,,resume,ussd,outbound"
                ",,,,default\r\n")

    def get_parser(self, options):
        defaults = {
            # some parser argparse defaults here
        }
        defaults.update(options)
        return VumiGoMessageParser(defaults)

    def parse(self, parser, csv):
        parser.stdin = StringIO(csv)
        parser.stdout = StringIO()
        parser.run()
        return parser.stdout.getvalue()

    def test_date_match(self):
        parser = self.get_parser({
            'start': '2013-09-10 19:20',
            'end': '2013-09-12 19:40'
        })
        SAMPLE = self.HEADER + self.INBOUND + self.OUTBOUND
        output = self.parse(parser, SAMPLE)
        self.assertEqual(output, SAMPLE)

    def test_date_not_match(self):
        parser = self.get_parser({
            'start': '2013-09-10 19:20',
            'end': '2013-09-10 19:40'
        })
        SAMPLE = self.HEADER + self.INBOUND_2 + self.OUTBOUND_2
        output = self.parse(parser, SAMPLE)
        self.assertEqual(output, self.HEADER)

    def test_date_msisdn_match_inbound(self):
        parser = self.get_parser({
            'msisdn': '+27817030710',
            'direction': 'inbound'
        })
        SAMPLE = self.HEADER + self.INBOUND_2 + self.OUTBOUND_2
        output = self.parse(parser, SAMPLE)
        self.assertEqual(output, self.HEADER + self.INBOUND_2)

    def test_date_msisdn_match_outbound(self):
        parser = self.get_parser({
            'msisdn': '+27817030710',
            'direction': 'outbound'
        })
        SAMPLE = self.HEADER + self.INBOUND_2 + self.OUTBOUND_2
        output = self.parse(parser, SAMPLE)
        self.assertEqual(output, self.HEADER + self.OUTBOUND_2)

    def test_date_msisdn_match_all(self):
        parser = self.get_parser({
            'msisdn': '+27817030710',
            'direction': 'all'
        })
        SAMPLE = self.HEADER + self.INBOUND_2 + self.OUTBOUND_2
        output = self.parse(parser, SAMPLE)
        self.assertEqual(output, SAMPLE)

class FilterTestCase(TestCase):

    def test_directional_filter(self):
        f = DirectionalFilter('inbound')
        self.assertTrue(f.apply({'direction': 'inbound'}))
        self.assertFalse(f.apply({'direction': 'outbound'}))
        self.assertFalse(f.apply({}))

    def test_msisdn_filter(self):
        f = MSISDNFilter('to_addr', '123')
        self.assertTrue(f.apply({'to_addr': '123'}))
        self.assertFalse(f.apply({'from_addr': '123'}))

        f = MSISDNFilter('from_addr', '123')
        self.assertTrue(f.apply({'from_addr': '123'}))
        self.assertFalse(f.apply({'to_addr': '123'}))

    def test_timestamp_filter(self):
        f = TimestampFilter('2013-01-01')
        self.assertTrue(f.apply({'timestamp': '2013-01-01'}))
        self.assertFalse(f.apply({'timestamp': '2012-01-01'}))

        f = TimestampFilter('2013-01-01', '2013-02-01')
        self.assertTrue(f.apply({'timestamp': '2013-01-01'}))
        self.assertFalse(f.apply({'timestamp': '2013-03-01'}))

        self.assertRaises(
            FilterException, TimestampFilter,
            '2013-01-01', '2000-01-01')

    def test_filter_chaining(self):
        f = TimestampFilter('2013-01-01').chain(
            DirectionalFilter('inbound'))
        self.assertTrue(f.process({
            'direction': 'inbound', 'timestamp': '2013-01-01'}))
        self.assertFalse(f.process({
            'direction': 'outbound', 'timestamp': '2013-01-01'}))
        self.assertFalse(f.process({
            'direction': 'inbound', 'timestamp': '2000-01-01'}))

        f = TimestampFilter('2013-01-01', '2013-12-31').chain(
            DirectionalFilter('inbound'))

        self.assertTrue(f.process({
            'direction': 'inbound', 'timestamp': '2013-06-01'}))
        self.assertFalse(f.process({
            'direction': 'inbound', 'timestamp': '2014-06-01'}))

class FilterPipelineTestCase(TestCase):

    HEADER = ("timestamp,from_addr,to_addr,content,message_id,in_reply_to,"
              "session_event,transport_type,direction,"
              "network_handover_status,network_handover_reason,"
              "delivery_status,endpoint\r\n")
    INBOUND = ("2013-09-10 19:24:03.289543,+27817030792,*120*8864*1203#,"
               ",af266289e40949388b5a8cacb4a2d13a,,new,ussd,inbound"
               ",,,,default\r\n")
    OUTBOUND = ("2013-09-11 19:24:03.289543,+27817030792,27123456789,"
                ",af266289e40949388b5a8cacb4a2d13b,,resume,ussd,outbound"
                ",,,,default\r\n")
    SAMPLE = HEADER + INBOUND + OUTBOUND

    def test_filter_pipeline(self):
        fp = FilterPipeline([DirectionalFilter('inbound')])
        stdin = StringIO(self.SAMPLE)
        stdout = StringIO()
        fp.process(stdin=stdin, stdout=stdout)
        self.assertEqual(stdout.getvalue(), self.HEADER + self.INBOUND)

    def test_filter_pipeline_chaining(self):
        fp = FilterPipeline([
            DirectionalFilter('inbound').chain(
                MSISDNFilter('from_addr', '+27817030792')),
            TimestampFilter('2013-09-10 00:00:00', '2013-09-10 23:59:59')
        ])
        stdin = StringIO(self.SAMPLE)
        stdout = StringIO()
        fp.process(stdin=stdin, stdout=stdout)
        self.assertEqual(stdout.getvalue(), self.HEADER + self.INBOUND)
<commit_msg>Add no direction test for msisdn filter<commit_after>from StringIO import StringIO
from unittest import TestCase
import csv

from gdt.vumigomessage import (
    VumiGoMessageParser, DirectionalFilter, MSISDNFilter, TimestampFilter,
    FilterPipeline, FilterException)

class GdtTestCase(TestCase):

    HEADER = ("timestamp,from_addr,to_addr,content,message_id,in_reply_to,"
              "session_event,transport_type,direction,"
              "network_handover_status,network_handover_reason,"
              "delivery_status,endpoint\r\n")
    INBOUND = ("2013-09-10 19:24:03.289543,+27817030792,*120*8864*1203#,"
               ",af266289e40949388b5a8cacb4a2d13a,,new,ussd,inbound"
               ",,,,default\r\n")
    OUTBOUND = ("2013-09-11 19:24:03.289543,*120*8864*1203#,+27817030792,"
                ",af266289e40949388b5a8cacb4a2d13b,,resume,ussd,outbound"
                ",,,,default\r\n")
    INBOUND_2 = ("2013-09-09 19:24:03.289543,+27817030710,*120*8864*1203#,"
               ",af266289e40949388b5a8cacb4a2d13a,,new,ussd,inbound"
               ",,,,default\r\n")
    OUTBOUND_2 = ("2013-09-09 19:24:03.289543,*120*8864*1203#,+27817030710,"
                ",af266289e40949388b5a8cacb4a2d13b,,resume,ussd,outbound"
                ",,,,default\r\n")

    def get_parser(self, options):
        defaults = {
            # some parser argparse defaults here
        }
        defaults.update(options)
        return VumiGoMessageParser(defaults)

    def parse(self, parser, csv):
        parser.stdin = StringIO(csv)
        parser.stdout = StringIO()
        parser.run()
        return parser.stdout.getvalue()

    def test_date_match(self):
        parser = self.get_parser({
            'start': '2013-09-10 19:20',
            'end': '2013-09-12 19:40'
        })
        SAMPLE = self.HEADER + self.INBOUND + self.OUTBOUND
        output = self.parse(parser, SAMPLE)
        self.assertEqual(output, SAMPLE)

    def test_date_not_match(self):
        parser = self.get_parser({
            'start': '2013-09-10 19:20',
            'end': '2013-09-10 19:40'
        })
        SAMPLE = self.HEADER + self.INBOUND_2 + self.OUTBOUND_2
        output = self.parse(parser, SAMPLE)
        self.assertEqual(output, self.HEADER)

    def test_date_msisdn_match_inbound(self):
        parser = self.get_parser({
            'msisdn': '+27817030710',
            'direction': 'inbound'
        })
        SAMPLE = self.HEADER + self.INBOUND_2 + self.OUTBOUND_2
        output = self.parse(parser, SAMPLE)
        self.assertEqual(output, self.HEADER + self.INBOUND_2)

    def test_date_msisdn_match_outbound(self):
        parser = self.get_parser({
            'msisdn': '+27817030710',
            'direction': 'outbound'
        })
        SAMPLE = self.HEADER + self.INBOUND_2 + self.OUTBOUND_2
        output = self.parse(parser, SAMPLE)
        self.assertEqual(output, self.HEADER + self.OUTBOUND_2)

    def test_date_msisdn_match_all(self):
        parser = self.get_parser({
            'msisdn': '+27817030710',
            'direction': 'all'
        })
        SAMPLE = self.HEADER + self.INBOUND_2 + self.OUTBOUND_2
        output = self.parse(parser, SAMPLE)
        self.assertEqual(output, SAMPLE)

    def test_date_msisdn_no_direction(self):
        parser = self.get_parser({
            'msisdn': '+27817030710'
        })
        SAMPLE = self.HEADER + self.INBOUND + self.OUTBOUND + self.INBOUND_2 + self.OUTBOUND_2
        output = self.parse(parser, SAMPLE)
        self.assertEqual(output, self.HEADER + self.INBOUND_2 + self.OUTBOUND_2)

class FilterTestCase(TestCase):

    def test_directional_filter(self):
        f = DirectionalFilter('inbound')
        self.assertTrue(f.apply({'direction': 'inbound'}))
        self.assertFalse(f.apply({'direction': 'outbound'}))
        self.assertFalse(f.apply({}))

    def test_msisdn_filter(self):
        f = MSISDNFilter('to_addr', '123')
        self.assertTrue(f.apply({'to_addr': '123'}))
        self.assertFalse(f.apply({'from_addr': '123'}))

        f = MSISDNFilter('from_addr', '123')
        self.assertTrue(f.apply({'from_addr': '123'}))
        self.assertFalse(f.apply({'to_addr': '123'}))

    def test_timestamp_filter(self):
        f = TimestampFilter('2013-01-01')
        self.assertTrue(f.apply({'timestamp': '2013-01-01'}))
        self.assertFalse(f.apply({'timestamp': '2012-01-01'}))

        f = TimestampFilter('2013-01-01', '2013-02-01')
        self.assertTrue(f.apply({'timestamp': '2013-01-01'}))
        self.assertFalse(f.apply({'timestamp': '2013-03-01'}))

        self.assertRaises(
            FilterException, TimestampFilter,
            '2013-01-01', '2000-01-01')

    def test_filter_chaining(self):
        f = TimestampFilter('2013-01-01').chain(
            DirectionalFilter('inbound'))
        self.assertTrue(f.process({
            'direction': 'inbound', 'timestamp': '2013-01-01'}))
        self.assertFalse(f.process({
            'direction': 'outbound', 'timestamp': '2013-01-01'}))
        self.assertFalse(f.process({
            'direction': 'inbound', 'timestamp': '2000-01-01'}))

        f = TimestampFilter('2013-01-01', '2013-12-31').chain(
            DirectionalFilter('inbound'))

        self.assertTrue(f.process({
            'direction': 'inbound', 'timestamp': '2013-06-01'}))
        self.assertFalse(f.process({
            'direction': 'inbound', 'timestamp': '2014-06-01'}))

class FilterPipelineTestCase(TestCase):

    HEADER = ("timestamp,from_addr,to_addr,content,message_id,in_reply_to,"
              "session_event,transport_type,direction,"
              "network_handover_status,network_handover_reason,"
              "delivery_status,endpoint\r\n")
    INBOUND = ("2013-09-10 19:24:03.289543,+27817030792,*120*8864*1203#,"
               ",af266289e40949388b5a8cacb4a2d13a,,new,ussd,inbound"
               ",,,,default\r\n")
    OUTBOUND = ("2013-09-11 19:24:03.289543,+27817030792,27123456789,"
                ",af266289e40949388b5a8cacb4a2d13b,,resume,ussd,outbound"
                ",,,,default\r\n")
    SAMPLE = HEADER + INBOUND + OUTBOUND

    def test_filter_pipeline(self):
        fp = FilterPipeline([DirectionalFilter('inbound')])
        stdin = StringIO(self.SAMPLE)
        stdout = StringIO()
        fp.process(stdin=stdin, stdout=stdout)
        self.assertEqual(stdout.getvalue(), self.HEADER + self.INBOUND)

    def test_filter_pipeline_chaining(self):
        fp = FilterPipeline([
            DirectionalFilter('inbound').chain(
                MSISDNFilter('from_addr', '+27817030792')),
            TimestampFilter('2013-09-10 00:00:00', '2013-09-10 23:59:59')
        ])
        stdin = StringIO(self.SAMPLE)
        stdout = StringIO()
        fp.process(stdin=stdin, stdout=stdout)
        self.assertEqual(stdout.getvalue(), self.HEADER + self.INBOUND)
<|endoftext|>


<commit_before>"""
Client for Genderize.io web service.
"""

import requests

__all__ = ['Genderize', 'GenderizeException']
__version__ = '0.1.3'

class GenderizeException(Exception):
    """
    Exception from Genderize.io web service.
    """

class Genderize(object):
    """
    Client for Genderize.io web service.
    Uses a Requests session for persistent HTTP connections.
    """

    def __init__(self, user_agent=None):
        """
        @param user_agent: Optional user agent string.
        """
        if user_agent is None:
            user_agent = 'Genderize/{0}'.format(__version__)

        self.session = requests.Session()
        self.session.headers = {'User-Agent': user_agent}

    @staticmethod
    def _fixtypes(data):
        """
        'probability' key is clearly supposed to be a float but is sent as a string.
        """
        if 'probability' in data:
            data['probability'] = float(data['probability'])
        return data

    def get(self, names, country_id=None, language_id=None):
        """
        Look up gender for a list of names.
        Can optionally refine search with locale info.

        @param names: List of names.
        @param country_id: Optional ISO 3166-1 alpha-2 country code.
        @param language_id: Optional ISO 639-1 language code.
        @return: List of dicts containing 'name', 'gender', 'probability', 'count' keys.
                 If 'gender' is None, 'probability' and 'count' will be omitted.
        """
        params = [('name[]', name) for name in names]
        if country_id:
            params.append(('country_id', country_id))
        if language_id:
            params.append(('language_id', language_id))

        response = self.session.get(
            'http://api.genderize.io/',
            params=params)

        if 'application/json' not in response.headers.get('content-type', ''):
            status = "server responded with {http_code}: {reason}".format(
                http_code=response.status_code, reason=response.reason)
            raise GenderizeException(
                'response not in JSON format ({status})'.format(status=status))

        decoded = response.json()
        if response.ok:
            return [self._fixtypes(data) for data in decoded]
        else:
            raise GenderizeException(decoded['error'], response.status_code)

    def get1(self, name, **kwargs):
        """
        Look up gender for a single name.

        @see: get
        """
        return self.get([name], **kwargs)[0]
<commit_msg>bug fix: _fixtypes not working as designed<commit_after>"""
Client for Genderize.io web service.
"""

import requests

__all__ = ['Genderize', 'GenderizeException']
__version__ = '0.1.3'

class GenderizeException(Exception):
    """
    Exception from Genderize.io web service.
    """

class Genderize(object):
    """
    Client for Genderize.io web service.
    Uses a Requests session for persistent HTTP connections.
    """

    def __init__(self, user_agent=None):
        """
        @param user_agent: Optional user agent string.
        """
        if user_agent is None:
            user_agent = 'Genderize/{0}'.format(__version__)

        self.session = requests.Session()
        self.session.headers = {'User-Agent': user_agent}

    @staticmethod
    def _fixtypes(data):
        """
        'probability' key is clearly supposed to be a float but is sent as a string.
        """
        if 'probability' in data:
            data['probability'] = float(data['probability'])
        return data

    def get(self, names, country_id=None, language_id=None):
        """
        Look up gender for a list of names.
        Can optionally refine search with locale info.

        @param names: List of names.
        @param country_id: Optional ISO 3166-1 alpha-2 country code.
        @param language_id: Optional ISO 639-1 language code.
        @return: List of dicts containing 'name', 'gender', 'probability', 'count' keys.
                 If 'gender' is None, 'probability' and 'count' will be omitted.
        """
        params = [('name[]', name) for name in names]
        if country_id:
            params.append(('country_id', country_id))
        if language_id:
            params.append(('language_id', language_id))

        response = self.session.get(
            'http://api.genderize.io/',
            params=params)

        if 'application/json' not in response.headers.get('content-type', ''):
            status = "server responded with {http_code}: {reason}".format(
                http_code=response.status_code, reason=response.reason)
            raise GenderizeException(
                'response not in JSON format ({status})'.format(status=status))

        decoded = response.json()
        if response.ok:
            return self._fixtypes(decoded)
        else:
            raise GenderizeException(decoded['error'], response.status_code)

    def get1(self, name, **kwargs):
        """
        Look up gender for a single name.

        @see: get
        """
        return self.get([name], **kwargs)[0]
<|endoftext|>


<commit_before># coding=utf-8
"""
"""
from __future__ import absolute_import

from urllib import quote

from flask import (redirect, request, make_response, flash, g,
                   current_app, render_template)
from flask_mail import Message
from flask_babel import gettext as _
from werkzeug.exceptions import NotFound
from abilian.i18n import render_template_i18n
from abilian.core.extensions import db, mail
from abilian.core.signals import activity
from abilian.services import audit_service
from abilian.services.image import resize
from abilian.services.conversion import converter
from abilian.web.action import actions
from abilian.web.frontend import add_to_recent_items
from abilian.web.views import default_view
from abilian.web import csrf, url_for

from abilian.sbe.apps.communities.views import default_view_kw
from ..tasks import preview_document, convert_document_content
from ..repository import repository
from ..models import Document
from .util import get_document, check_read_access, breadcrumbs_for, \
  check_write_access, edit_object, match, check_manage_access
from .views import documents

route = documents.route

MAX_PREVIEW_SIZE = 1000

@default_view(documents, Document, id_attr='doc_id', kw_func=default_view_kw)
@route("/doc/<int:doc_id>")
def document_view(doc_id):
  doc = get_document(doc_id)
  check_read_access(doc)
  doc.ensure_antivirus_scheduled()
  db.session.commit()

  bc = breadcrumbs_for(doc)
  actions.context['object'] = doc

  if doc.content_type.startswith("image/"):
    add_to_recent_items(doc, "image")
  else:
    add_to_recent_items(doc, "document")

  has_preview = doc.has_preview()
  audit_entries = audit_service.entries_for(doc)

  ctx = dict(doc=doc, audit_entries=audit_entries, breadcrumbs=bc,
             folder=doc.parent,
             has_preview=has_preview,
             csrf_token=csrf.field())
  return render_template("documents/document.html", **ctx)

#
# Actions on documents
#
@route("/doc/<int:doc_id>", methods=['POST'])
@csrf.protect
def document_edit(doc_id):
  doc = get_document(doc_id)
  check_write_access(doc)

  changed = edit_object(doc)

  if changed:
    db.session.commit()
    flash(_(u"Document properties successfully edited."), "success")
  else:
    flash(_(u"You didn't change any property."), "success")

  return redirect(url_for(doc))

@route("/doc/<int:doc_id>/delete", methods=['POST'])
@csrf.protect
def document_delete(doc_id):
  doc = get_document(doc_id)
  check_write_access(doc)

  parent_folder = doc.parent
  repository.delete_object(doc)
  db.session.commit()

  flash(_(u"File successfully deleted."), "success")
  return redirect(url_for(parent_folder))

@route("/doc/<int:doc_id>/upload", methods=['POST'])
@csrf.protect
def document_upload(doc_id):
  doc = get_document(doc_id)
  check_write_access(doc)

  fd = request.files['file']
  doc.set_content(fd.read(), fd.content_type)

  self = current_app._get_current_object()
  activity.send(self, actor=g.user, verb="update", object=doc)

  db.session.commit()

  flash(_(u"New version successfully uploaded"), "success")
  return redirect(url_for(doc))

@route("/doc/<int:doc_id>/download")
def document_download(doc_id):
  """Download the file content."""

  doc = get_document(doc_id)

  response = make_response(doc.content)
  response.headers['content-length'] = doc.content_length
  response.headers['content-type'] = doc.content_type

  attach = request.args.get('attach')
  if attach or \
    not match(doc.content_type, ("text/plain", "application/pdf", "image/*")):
    # Note: we omit text/html for security reasons.
    quoted_filename = quote(doc.title.encode('utf8'))
    response.headers['content-disposition'] \
      = 'attachment;filename="{}"'.format(quoted_filename)

  return response

def preview_missing_image():
  response = redirect(
    url_for('static', filename='images/preview_missing.png'))
  response.headers['Cache-Control'] = 'no-cache'
  return response

@route("/doc/<int:doc_id>/preview")
def document_preview(doc_id):
  """Returns a preview (image) for the file given by its id."""

  doc = get_document(doc_id)

  if not doc.antivirus_ok:
    return preview_missing_image()

  size = int(request.args.get("size", 0))

  # Just in case
  if size > MAX_PREVIEW_SIZE:
    size = MAX_PREVIEW_SIZE

  # compute image if size != standard document size
  get_image = (converter.get_image
               if size == doc.preview_size
               else converter.to_image)

  content_type = "image/jpeg"

  if doc.content_type.startswith('image/svg'):
    image = doc.content
    content_type = doc.content_type
  elif doc.content_type.startswith("image/"):
    image = doc.content
    if size:
      image = resize(image, size)
  else:
    page = int(request.args.get("page", 0))
    try:
      image = get_image(doc.digest, doc.content, doc.content_type, page, size)
    except:
      # TODO: use generic "conversion failed" image
      image = ""

  if not image:
    return preview_missing_image()

  response = make_response(image)
  response.headers['content-type'] = content_type
  return response

@route("/doc/<int:doc_id>/refresh_preview")
def refresh_preview(doc_id):
  """ Force to compute a new preview
  """
  doc = get_document(doc_id)
  if not doc:
    raise NotFound()

  ct = doc.find_content_type(doc.content_type)
  if ct != doc.content_type:
    doc.content_type = ct
    db.session.commit()

  check_manage_access(doc)
  convert_document_content(doc_id)
  preview_document(doc_id)
  return redirect(url_for(doc))

@route("/doc/<int:doc_id>/send", methods=['POST'])
@csrf.protect
def document_send(doc_id):
  doc = get_document(doc_id)

  recipient = request.form.get("recipient")
  user_msg = request.form.get('message')

  site_name = u'[{}] '.format(current_app.config['SITE_NAME'])
  sender_name = g.user.name
  subject = site_name + _(u'{sender} sent you a file').format(sender=sender_name)
  msg = Message(subject)
  msg.sender = g.user.email
  msg.recipients = [recipient]
  msg.body = render_template_i18n('documents/mail_file_sent.txt',
                                  sender_name=sender_name,
                                  message=user_msg,
                                  document_url=url_for(doc),
                                  filename=doc.title)

  filename = doc.title
  msg.attach(filename, doc.content_type, doc.content)

  mail.send(msg)
  flash(_(u"Email successfully sent"), "success")

  return redirect(url_for(doc))

#
# Tagging (currently not used!)
#
#@route("/tag")
#def tag():
#  tag = request.args.get("tag")
#  if not tag:
#    return redirect("/dm/")
#
#  bc = [dict(path="/", label="Home"), dict(path="/dm/", label="DM")]
#  bc += [dict(path=request.path, label="Filter by tag")]
#  # TODO ...
#  docs = Document.query.filter(Document.tags.like("%" + tag + "%"))
#  docs = list(docs.all())
#  docs = [f for f in docs if tag in f.tags.split(",")]
#  title = "Files filtered by tag: %s" % tag
#  return render_template("dm/home.html", title=title, breadcrumbs=bc,
# files=docs)
#
#
#@route("/<int:file_id>/tag", methods=['POST'])
#def tag_post(file_id):
#  doc = get_document(file_id)
#  tags = request.form.get("tags")
#
#  doc.tags = tags
#  self = current_app._get_current_object()
#  activity.send(self, actor=g.user, verb="tag", object=doc)
#
#  db.session.commit()
#
#  flash("Tags successfully successfully updated", "success")
#  return redirect(url_for(".document_view", doc_id=doc.id))
<commit_msg>fix refresh preview<commit_after># coding=utf-8
"""
"""
from __future__ import absolute_import

from urllib import quote

from flask import (redirect, request, make_response, flash, g,
                   current_app, render_template)
from flask_mail import Message
from flask_babel import gettext as _
from werkzeug.exceptions import NotFound
from abilian.i18n import render_template_i18n
from abilian.core.extensions import db, mail
from abilian.core.signals import activity
from abilian.services import audit_service
from abilian.services.image import resize
from abilian.services.conversion import converter
from abilian.web.action import actions
from abilian.web.frontend import add_to_recent_items
from abilian.web.views import default_view
from abilian.web import csrf, url_for

from abilian.sbe.apps.communities.views import default_view_kw
from ..tasks import preview_document, convert_document_content
from ..repository import repository
from ..models import Document
from .util import get_document, check_read_access, breadcrumbs_for, \
  check_write_access, edit_object, match, check_manage_access
from .views import documents

route = documents.route

MAX_PREVIEW_SIZE = 1000

@default_view(documents, Document, id_attr='doc_id', kw_func=default_view_kw)
@route("/doc/<int:doc_id>")
def document_view(doc_id):
  doc = get_document(doc_id)
  check_read_access(doc)
  doc.ensure_antivirus_scheduled()
  db.session.commit()

  bc = breadcrumbs_for(doc)
  actions.context['object'] = doc

  if doc.content_type.startswith("image/"):
    add_to_recent_items(doc, "image")
  else:
    add_to_recent_items(doc, "document")

  has_preview = doc.has_preview()
  audit_entries = audit_service.entries_for(doc)

  ctx = dict(doc=doc, audit_entries=audit_entries, breadcrumbs=bc,
             folder=doc.parent,
             has_preview=has_preview,
             csrf_token=csrf.field())
  return render_template("documents/document.html", **ctx)

#
# Actions on documents
#
@route("/doc/<int:doc_id>", methods=['POST'])
@csrf.protect
def document_edit(doc_id):
  doc = get_document(doc_id)
  check_write_access(doc)

  changed = edit_object(doc)

  if changed:
    db.session.commit()
    flash(_(u"Document properties successfully edited."), "success")
  else:
    flash(_(u"You didn't change any property."), "success")

  return redirect(url_for(doc))

@route("/doc/<int:doc_id>/delete", methods=['POST'])
@csrf.protect
def document_delete(doc_id):
  doc = get_document(doc_id)
  check_write_access(doc)

  parent_folder = doc.parent
  repository.delete_object(doc)
  db.session.commit()

  flash(_(u"File successfully deleted."), "success")
  return redirect(url_for(parent_folder))

@route("/doc/<int:doc_id>/upload", methods=['POST'])
@csrf.protect
def document_upload(doc_id):
  doc = get_document(doc_id)
  check_write_access(doc)

  fd = request.files['file']
  doc.set_content(fd.read(), fd.content_type)

  self = current_app._get_current_object()
  activity.send(self, actor=g.user, verb="update", object=doc)

  db.session.commit()

  flash(_(u"New version successfully uploaded"), "success")
  return redirect(url_for(doc))

@route("/doc/<int:doc_id>/download")
def document_download(doc_id):
  """Download the file content."""
  doc = get_document(doc_id)

  response = make_response(doc.content)
  response.headers['content-length'] = doc.content_length
  response.headers['content-type'] = doc.content_type

  attach = request.args.get('attach')
  if attach or \
    not match(doc.content_type, ("text/plain", "application/pdf", "image/*")):
    # Note: we omit text/html for security reasons.
    quoted_filename = quote(doc.title.encode('utf8'))
    response.headers['content-disposition'] \
      = 'attachment;filename="{}"'.format(quoted_filename)

  return response

def preview_missing_image():
  response = redirect(
    url_for('static', filename='images/preview_missing.png'))
  response.headers['Cache-Control'] = 'no-cache'
  return response

@route("/doc/<int:doc_id>/preview")
def document_preview(doc_id):
  """Returns a preview (image) for the file given by its id."""

  doc = get_document(doc_id)

  if not doc.antivirus_ok:
    return preview_missing_image()

  size = int(request.args.get("size", 0))

  # Just in case
  if size > MAX_PREVIEW_SIZE:
    size = MAX_PREVIEW_SIZE

  # compute image if size != standard document size
  get_image = (converter.get_image
               if size == doc.preview_size
               else converter.to_image)

  content_type = "image/jpeg"

  if doc.content_type.startswith('image/svg'):
    image = doc.content
    content_type = doc.content_type
  elif doc.content_type.startswith("image/"):
    image = doc.content
    if size:
      image = resize(image, size)
  else:
    page = int(request.args.get("page", 0))
    try:
      image = get_image(doc.digest, doc.content, doc.content_type, page, size)
    except:
      # TODO: use generic "conversion failed" image
      image = ""

  if not image:
    return preview_missing_image()

  response = make_response(image)
  response.headers['content-type'] = content_type
  return response

@route("/doc/<int:doc_id>/refresh_preview")
def refresh_preview(doc_id):
  """ Force to compute a new preview
  """
  doc = get_document(doc_id)
  if not doc:
    raise NotFound()

  ct = doc.find_content_type(doc.content_type)
  if ct != doc.content_type:
    doc.content_type = ct
    db.session.commit()

  check_manage_access(doc)
  convert_document_content.apply([doc_id])
  preview_document.apply([doc_id])
  return redirect(url_for(doc))

@route("/doc/<int:doc_id>/send", methods=['POST'])
@csrf.protect
def document_send(doc_id):
  doc = get_document(doc_id)

  recipient = request.form.get("recipient")
  user_msg = request.form.get('message')

  site_name = u'[{}] '.format(current_app.config['SITE_NAME'])
  sender_name = g.user.name
  subject = site_name + _(u'{sender} sent you a file').format(sender=sender_name)
  msg = Message(subject)
  msg.sender = g.user.email
  msg.recipients = [recipient]
  msg.body = render_template_i18n('documents/mail_file_sent.txt',
                                  sender_name=sender_name,
                                  message=user_msg,
                                  document_url=url_for(doc),
                                  filename=doc.title)

  filename = doc.title
  msg.attach(filename, doc.content_type, doc.content)

  mail.send(msg)
  flash(_(u"Email successfully sent"), "success")

  return redirect(url_for(doc))

#
# Tagging (currently not used!)
#
#@route("/tag")
#def tag():
#  tag = request.args.get("tag")
#  if not tag:
#    return redirect("/dm/")
#
#  bc = [dict(path="/", label="Home"), dict(path="/dm/", label="DM")]
#  bc += [dict(path=request.path, label="Filter by tag")]
#  # TODO ...
#  docs = Document.query.filter(Document.tags.like("%" + tag + "%"))
#  docs = list(docs.all())
#  docs = [f for f in docs if tag in f.tags.split(",")]
#  title = "Files filtered by tag: %s" % tag
#  return render_template("dm/home.html", title=title, breadcrumbs=bc,
# files=docs)
#
#
#@route("/<int:file_id>/tag", methods=['POST'])
#def tag_post(file_id):
#  doc = get_document(file_id)
#  tags = request.form.get("tags")
#
#  doc.tags = tags
#  self = current_app._get_current_object()
#  activity.send(self, actor=g.user, verb="tag", object=doc)
#
#  db.session.commit()
#
#  flash("Tags successfully successfully updated", "success")
#  return redirect(url_for(".document_view", doc_id=doc.id))
<|endoftext|>


<commit_before># coding: utf8
"""
Provides user registration and login using PEAM (Pôle Emploi Access Management).
"""
import requests

from social_core.exceptions import AuthUnreachableProvider
from social_core.backends.open_id_connect import OpenIdConnectAuth

from labonneboite.conf import settings
from labonneboite.common import activity
from labonneboite.common import user_util
from .exceptions import AuthFailedMissingReturnValues

# pylint:disable=abstract-method
class PEAMOpenIdConnect(OpenIdConnectAuth):
    """
    Pôle Emploi Access Management OpenId Connect backend.
    """
    name = 'peam-openidconnect'

    AUTHORIZATION_URL = '%s/connexion/oauth2/authorize' % settings.PEAM_AUTH_BASE_URL
    ACCESS_TOKEN_URL = '%s/connexion/oauth2/access_token' % settings.PEAM_AUTH_BASE_URL
    REFRESH_TOKEN_URL = '%s/connexion/oauth2/access_token' % settings.PEAM_AUTH_BASE_URL
    USERINFO_URL = '%s/partenaire/peconnect-individu/v1/userinfo' % settings.PEAM_API_BASE_URL

    def request_access_token(self, *args, **kwargs):
        kwargs['params'] = {'realm': '/individu'}
        result = self.get_json(*args, **kwargs)
        return result

    def user_data(self, access_token, *args, **kwargs):
        url = self.userinfo_url()
        try:
            return self.get_json(
                url, params={'realm': '/individu'},
                headers={'Authorization': 'Bearer {0}'.format(access_token)}
            )
        except requests.HTTPError as e:
            if e.response.status_code == 502: # Bad Gateway
                # 502 errors are not properly handled by social_core (see
                # handle_http_errors decorator in social_core.utils)
                # If we don't raise an exception, the user sees a spinning wheel.
                # This exception must be caught by an error handler of the app.
                raise AuthUnreachableProvider(url)
            raise

    def get_user_details(self, response):
        user_details = {
            'gender': response.get('gender', user_util.GENDER_OTHER),
            'email': response.get('email'),
            'external_id': response.get('sub'),
            'first_name': response.get('given_name'),
            'last_name': response.get('family_name'),
        }
        # Sometimes PEAM responds without the user details. For instance, the
        # email address is empty when a user has not validated its account.
        required_fields = ('email', 'external_id', 'first_name', 'last_name')
        for field in required_fields:
            if not user_details[field]:
                raise AuthFailedMissingReturnValues(self, field)
        return user_details

    def complete(self, *args, **kwargs):
        user = super().complete(*args, **kwargs)
        activity.log('connexion', user=user)
        return user

# pylint:disable=abstract-method
class PEAMOpenIdConnectNoPrompt(PEAMOpenIdConnect):
    name = 'peam-openidconnect-no-prompt'
    # For some reason, the 'state' session parameter is not set by PE.fr, so we
    # need to ignore it.
    STATE_PARAMETER = False
<commit_msg>Increase PEAM quota of req/s to avoid 429 HTTP errors This is done by enabling a custom scope qos_silver_peconnect-individuv1 after the ESD team has manually allowed it.<commit_after># coding: utf8
"""
Provides user registration and login using PEAM (Pôle Emploi Access Management).

Useful documentation of social-auth-core:
https://python-social-auth.readthedocs.io/en/latest/
"""
import requests

from social_core.exceptions import AuthUnreachableProvider
from social_core.backends.open_id_connect import OpenIdConnectAuth

from labonneboite.conf import settings
from labonneboite.common import activity
from labonneboite.common import user_util
from .exceptions import AuthFailedMissingReturnValues

# pylint:disable=abstract-method
class PEAMOpenIdConnect(OpenIdConnectAuth):
    """
    Pôle Emploi Access Management OpenId Connect backend.
    """
    name = 'peam-openidconnect'

    AUTHORIZATION_URL = '%s/connexion/oauth2/authorize' % settings.PEAM_AUTH_BASE_URL
    ACCESS_TOKEN_URL = '%s/connexion/oauth2/access_token' % settings.PEAM_AUTH_BASE_URL
    REFRESH_TOKEN_URL = '%s/connexion/oauth2/access_token' % settings.PEAM_AUTH_BASE_URL
    USERINFO_URL = '%s/partenaire/peconnect-individu/v1/userinfo' % settings.PEAM_API_BASE_URL

    # as documented in https://python-social-auth.readthedocs.io/en/latest/use_cases.html#multiple-scopes-per-provider
    def get_scope(self):
        scope = super(PEAMOpenIdConnect, self).get_scope()
        # enable higher quota of req/s to avoid 429 HTTP errors
        scope = scope + ['qos_silver_peconnect-individuv1']
        return scope

    def request_access_token(self, *args, **kwargs):
        kwargs['params'] = {'realm': '/individu'}
        result = self.get_json(*args, **kwargs)
        return result

    def user_data(self, access_token, *args, **kwargs):
        url = self.userinfo_url()
        try:
            return self.get_json(
                url, params={'realm': '/individu'},
                headers={'Authorization': 'Bearer {0}'.format(access_token)}
            )
        except requests.HTTPError as e:
            if e.response.status_code == 502: # Bad Gateway
                # 502 errors are not properly handled by social_core (see
                # handle_http_errors decorator in social_core.utils)
                # If we don't raise an exception, the user sees a spinning wheel.
                # This exception must be caught by an error handler of the app.
                raise AuthUnreachableProvider(url)
            raise

    def get_user_details(self, response):
        user_details = {
            'gender': response.get('gender', user_util.GENDER_OTHER),
            'email': response.get('email'),
            'external_id': response.get('sub'),
            'first_name': response.get('given_name'),
            'last_name': response.get('family_name'),
        }
        # Sometimes PEAM responds without the user details. For instance, the
        # email address is empty when a user has not validated its account.
        required_fields = ('email', 'external_id', 'first_name', 'last_name')
        for field in required_fields:
            if not user_details[field]:
                raise AuthFailedMissingReturnValues(self, field)
        return user_details

    def complete(self, *args, **kwargs):
        user = super().complete(*args, **kwargs)
        activity.log('connexion', user=user)
        return user

# pylint:disable=abstract-method
class PEAMOpenIdConnectNoPrompt(PEAMOpenIdConnect):
    name = 'peam-openidconnect-no-prompt'
    # For some reason, the 'state' session parameter is not set by PE.fr, so we
    # need to ignore it.
    STATE_PARAMETER = False
<|endoftext|>


<commit_before>#!/usr/bin/python
###############################################################################
# Copyright (C) 2001-2015 Micronaet S.r.l. (<https://micronaet.com>)
# Developer: Nicola Riolini @thebrush
#            https://it.linkedin.com/in/thebrush
#            https://linktr.ee/nicolariolini
###############################################################################

import os
import sys
import hmac
import hashlib
import base64
import json
import uuid
import pytz
import pdb
from datetime import datetime
from zeep import Client
from flask import Flask, request
from flask import Response

try:
    import ConfigParser
except:
    try:
        import configparser as ConfigParser
    except:
        print('Errore importanto i parametri di avvio!')
        sys.exit()

token = False

# -----------------------------------------------------------------------------
#                                  Utility:
# -----------------------------------------------------------------------------
# LOG:
# -----------------------------------------------------------------------------
def write_log(log_f, message, mode='INFO', verbose=True):
    """ Write log file
    """
    complete_message = '%s [%s]: %s' % (
        str(datetime.now())[:19],
        mode,
        message,
    )
    if verbose:
        print(' * {}'.format(complete_message))
    log_f.write('{}\n'.format(complete_message))
    log_f.flush()

# -----------------------------------------------------------------------------
# SOAP:
# -----------------------------------------------------------------------------
message_mask = 'GET+/users/%s/account+%s+%s'
username = bytes('GENERALFOOD')
secret = bytes('5BC478479AB65798A4420F3FB19EF68E96ECFEA8')
namespace = bytes('{it.niuma.mscsoapws.ws}MscWsPortSoap11')
wsdl_root = bytes('https://layer7prod.msccruises.com/pep/wsdl')

# datetime_field = []
decimal_field = [
    # Header:
    'documentValue',

    # Line:
    'itemPrice',
    'quantityOrdered',
    'quantityConfirmed',
    'quantityLogistic',
    'nrPzConf',
    'flDogana',
]

def soap2dict(reply):
    """ Convert soap to dict
    """
    res = {}

    # -------------------------------------------------------------------------
    # Outcome part:
    # -------------------------------------------------------------------------
    res['operationOutcome'] = eval(str(reply['operationOutcome']))

    # -------------------------------------------------------------------------
    # Orders part:
    # -------------------------------------------------------------------------
    for order in reply['orders']:
        new_order = {}  # New structure:

        # ---------------------------------------------------------------------
        # All order fields
        # ---------------------------------------------------------------------
        for field in dir(order):
            print('Header field %s' % field)
            # A. Order line updated after:
            if field == 'orderLines':
                new_order['orderLines'] = []
                continue  # after

            # B. Datetime field:
            if type(order[field]) == datetime:
                new_order[field] = str(order[field])

            # C. Decimal field:
            elif field in decimal_field:
                try:
                    new_order[field] = float(order[field])
                except:
                    print('Field empty: %s' % field)
                    new_order[field] = 0.0

            # D. Normal fields (string, integer, boolean)
            else:
                new_order[field] = order[field]

        # ---------------------------------------------------------------------
        # Line part:
        # ---------------------------------------------------------------------
        for line in order['orderLines']:
            new_line = {}

            # Loop on every field:
            for line_field in dir(line):
                print('Line field %s' % line_field)

                # a. Float
                if line_field in decimal_field:
                    try:
                        new_line[line_field] = float(line[line_field])
                    except:
                        print('Field empty: %s' % line_field)
                        new_line[line_field] = 0.0

                # b. Normal fields (string, integer, boolean)
                else:
                    new_line[line_field] = line[line_field]

            new_order['orderLines'].append(new_line)
    return res

def get_soap_service():
    """ Get WSDL Service link
        if passed namespace and wsdl root use that, instead of
        read from
        parameters
    """
    client = Client(wsdl_root)
    return client.create_service(namespace, wsdl_root)

def get_datetime_tz():
    """ Change datetime removing gap from now and GMT 0
    """
    return pytz.utc.localize(datetime.now()).astimezone(
        pytz.timezone('Europe/Rome'))

def get_token():
    """ Call server and get token
    """
    timestamp = get_datetime_tz().strftime('%d%m%Y%H%M%S')
    number = str(uuid.uuid4())[-6:]
    message = message_mask % (username, timestamp, number)

    signature = hmac.new(
        secret, msg=message, digestmod=hashlib.sha256).digest()
    hash_text = base64.b64encode(signature)

    # Call SOAP portal:
    service = get_soap_service()
    res = service.login(
        username=username, time=timestamp, number=number,
        hash=hash_text)
    res = eval(str(res))
    token = res['accessToken']
    return res

# -----------------------------------------------------------------------------
# Read configuration parameter from external file:
# -----------------------------------------------------------------------------
current_path = os.path.dirname(__file__)
log_file = os.path.join(current_path, 'flask.log')
log_f = open(log_file, 'a')
write_log(log_f, 'Start ODOO-MSC Flask agent')
write_log(log_f, 'Flask log file: {}'.format(log_file))

config_files = [
    os.path.join(current_path, 'flask.cfg'),
]
for config_file in config_files:
    if not os.path.isfile(config_file):
        continue
    cfg_file = os.path.expanduser(config_file)

    config = ConfigParser.ConfigParser()
    config.read([cfg_file])
    host = config.get('flask', 'host')
    port = config.get('flask', 'port')
    write_log(log_f, 'Read config file: {}'.format(config_file))
    break
else:
    write_log(log_f, 'Read default parameter [0.0.0.0:5000]')
    host = '0.0.0.0'
    port = '5000'

# -----------------------------------------------------------------------------
# End point definition:
# -----------------------------------------------------------------------------
app = Flask(__name__)

@app.route('/API/v1.0/micronaet/launcher', methods=['POST'])
def ODOOCall():
    """ Master function for Micronaet Call

    """
    # -------------------------------------------------------------------------
    # Get parameters from call:
    # -------------------------------------------------------------------------
    params = request.get_json()
    rpc_call = params['params']
    command = rpc_call['command']
    parameter = rpc_call['parameters']

    # -------------------------------------------------------------------------
    #                             Execute call:
    # -------------------------------------------------------------------------
    # Start reply payload:
    payload = {
        'success': False,
        'reply': {},
    }
    # -------------------------------------------------------------------------
    #                       Import invoice procedure:
    # -------------------------------------------------------------------------
    if command == 'token':
        account_command = parameter.get('command')  # account command
        #try:

        # ---------------------------------------------------------------------
        # Call for token
        # ---------------------------------------------------------------------
        try:
            # -----------------------------------------------------------------
            # Read parameters:
            # -----------------------------------------------------------------
            # wsdl_root = bytes(parameter.get('wsdl_root'))
            # namespace = bytes(parameter.get('namespace'))
            # username = bytes(parameter.get('username'))
            # timestamp = parameter.get('timestamp')
            # number = parameter.get('number')
            # hash_text = parameter.get('hash_text')

            res = get_token()
            payload['reply']['res'] = res
            payload['success'] = True
            return payload
        except:
            print('Errore: %s' % (sys.exc_info(), ))
            payload['reply'].update({
                'error': 'Errore nella chiamata ad MSC portale: '
                         '{}'.format(account_command),
            })
            return payload
        # finally:
        #    pass

    elif command == 'order':
        token = get_token()
        token = token['accessToken']  # Extract order from reply
        service = get_soap_service()
        reply = service.getOngoingPOrders(accessToken=token)
        pdb.set_trace()

        payload['reply']['res'] = soap2dict(reply)
        payload['success'] = True
        return payload

    elif command == 'invoice':
         pass
    else:
        # ---------------------------------------------------------------------
        # Bad call:
        # ---------------------------------------------------------------------
        message = '[ERROR] ODOO is calling wrong command {}\n'.format(
            command)
        payload['reply'].update({
            'error': message.strip(),
        })

    # -------------------------------------------------------------------------
    # Prepare response
    # -------------------------------------------------------------------------
    return payload

app.run(debug=True, host=host, port=port)
write_log(log_f, 'End ODOO-MSC Flask agent')
<commit_msg>generic bugfix (fast message)<commit_after>#!/usr/bin/python
###############################################################################
# Copyright (C) 2001-2015 Micronaet S.r.l. (<https://micronaet.com>)
# Developer: Nicola Riolini @thebrush
#            https://it.linkedin.com/in/thebrush
#            https://linktr.ee/nicolariolini
###############################################################################

import os
import sys
import hmac
import hashlib
import base64
import json
import uuid
import pytz
import pdb
from datetime import datetime
from zeep import Client
from flask import Flask, request
from flask import Response

try:
    import ConfigParser
except:
    try:
        import configparser as ConfigParser
    except:
        print('Errore importanto i parametri di avvio!')
        sys.exit()

token = False

# -----------------------------------------------------------------------------
#                                  Utility:
# -----------------------------------------------------------------------------
# LOG:
# -----------------------------------------------------------------------------
def write_log(log_f, message, mode='INFO', verbose=True):
    """ Write log file
    """
    complete_message = '%s [%s]: %s' % (
        str(datetime.now())[:19],
        mode,
        message,
    )
    if verbose:
        print(' * {}'.format(complete_message))
    log_f.write('{}\n'.format(complete_message))
    log_f.flush()

# -----------------------------------------------------------------------------
# SOAP:
# -----------------------------------------------------------------------------
message_mask = 'GET+/users/%s/account+%s+%s'
username = bytes('GENERALFOOD')
secret = bytes('5BC478479AB65798A4420F3FB19EF68E96ECFEA8')
namespace = bytes('{it.niuma.mscsoapws.ws}MscWsPortSoap11')
wsdl_root = bytes('https://layer7prod.msccruises.com/pep/wsdl')

# datetime_field = []
decimal_field = [
    # Header:
    'documentValue',

    # Line:
    'itemPrice',
    'quantityOrdered',
    'quantityConfirmed',
    'quantityLogistic',
    'nrPzConf',
    'flDogana',
]

def soap2dict(reply):
    """ Convert soap to dict
    """
    res = {}

    # -------------------------------------------------------------------------
    # Outcome part:
    # -------------------------------------------------------------------------
    res['operationOutcome'] = eval(str(reply['operationOutcome']))

    # -------------------------------------------------------------------------
    # Orders part:
    # -------------------------------------------------------------------------
    res['orders'] = []
    for order in reply['orders']:
        new_order = {}  # New structure:

        # ---------------------------------------------------------------------
        # All order fields
        # ---------------------------------------------------------------------
        for field in dir(order):
            print('Header field %s' % field)
            # A. Order line updated after:
            if field == 'orderLines':
                new_order['orderLines'] = []
                continue  # after

            # B. Datetime field:
            if type(order[field]) == datetime:
                new_order[field] = str(order[field])

            # C. Decimal field:
            elif field in decimal_field:
                try:
                    new_order[field] = float(order[field])
                except:
                    print('Field empty: %s' % field)
                    new_order[field] = 0.0

            # D. Normal fields (string, integer, boolean)
            else:
                new_order[field] = order[field]

        # ---------------------------------------------------------------------
        # Line part:
        # ---------------------------------------------------------------------
        for line in order['orderLines']:
            new_line = {}

            # Loop on every field:
            for line_field in dir(line):
                print('Line field %s' % line_field)

                # a. Float
                if line_field in decimal_field:
                    try:
                        new_line[line_field] = float(line[line_field])
                    except:
                        print('Field empty: %s' % line_field)
                        new_line[line_field] = 0.0

                # b. Normal fields (string, integer, boolean)
                else:
                    new_line[line_field] = line[line_field]

            new_order['orderLines'].append(new_line)
        res['orders'].append(new_order)
    return res

def get_soap_service():
    """ Get WSDL Service link
        if passed namespace and wsdl root use that, instead of
        read from
        parameters
    """
    client = Client(wsdl_root)
    return client.create_service(namespace, wsdl_root)

def get_datetime_tz():
    """ Change datetime removing gap from now and GMT 0
    """
    return pytz.utc.localize(datetime.now()).astimezone(
        pytz.timezone('Europe/Rome'))

def get_token():
    """ Call server and get token
    """
    timestamp = get_datetime_tz().strftime('%d%m%Y%H%M%S')
    number = str(uuid.uuid4())[-6:]
    message = message_mask % (username, timestamp, number)

    signature = hmac.new(
        secret, msg=message, digestmod=hashlib.sha256).digest()
    hash_text = base64.b64encode(signature)

    # Call SOAP portal:
    service = get_soap_service()
    res = service.login(
        username=username, time=timestamp, number=number,
        hash=hash_text)
    res = eval(str(res))
    token = res['accessToken']
    return res

# -----------------------------------------------------------------------------
# Read configuration parameter from external file:
# -----------------------------------------------------------------------------
current_path = os.path.dirname(__file__)
log_file = os.path.join(current_path, 'flask.log')
log_f = open(log_file, 'a')
write_log(log_f, 'Start ODOO-MSC Flask agent')
write_log(log_f, 'Flask log file: {}'.format(log_file))

config_files = [
    os.path.join(current_path, 'flask.cfg'),
]
for config_file in config_files:
    if not os.path.isfile(config_file):
        continue
    cfg_file = os.path.expanduser(config_file)

    config = ConfigParser.ConfigParser()
    config.read([cfg_file])
    host = config.get('flask', 'host')
    port = config.get('flask', 'port')
    write_log(log_f, 'Read config file: {}'.format(config_file))
    break
else:
    write_log(log_f, 'Read default parameter [0.0.0.0:5000]')
    host = '0.0.0.0'
    port = '5000'

# -----------------------------------------------------------------------------
# End point definition:
# -----------------------------------------------------------------------------
app = Flask(__name__)

@app.route('/API/v1.0/micronaet/launcher', methods=['POST'])
def ODOOCall():
    """ Master function for Micronaet Call

    """
    # -------------------------------------------------------------------------
    # Get parameters from call:
    # -------------------------------------------------------------------------
    params = request.get_json()
    rpc_call = params['params']
    command = rpc_call['command']
    parameter = rpc_call['parameters']

    # -------------------------------------------------------------------------
    #                             Execute call:
    # -------------------------------------------------------------------------
    # Start reply payload:
    payload = {
        'success': False,
        'reply': {},
    }
    # -------------------------------------------------------------------------
    #                       Import invoice procedure:
    # -------------------------------------------------------------------------
    if command == 'token':
        account_command = parameter.get('command')  # account command
        #try:

        # ---------------------------------------------------------------------
        # Call for token
        # ---------------------------------------------------------------------
        try:
            # -----------------------------------------------------------------
            # Read parameters:
            # -----------------------------------------------------------------
            # wsdl_root = bytes(parameter.get('wsdl_root'))
            # namespace = bytes(parameter.get('namespace'))
            # username = bytes(parameter.get('username'))
            # timestamp = parameter.get('timestamp')
            # number = parameter.get('number')
            # hash_text = parameter.get('hash_text')

            res = get_token()
            payload['reply']['res'] = res
            payload['success'] = True
            return payload
        except:
            print('Errore: %s' % (sys.exc_info(), ))
            payload['reply'].update({
                'error': 'Errore nella chiamata ad MSC portale: '
                         '{}'.format(account_command),
            })
            return payload
        # finally:
        #    pass

    elif command == 'order':
        token = get_token()
        token = token['accessToken']  # Extract order from reply
        service = get_soap_service()
        reply = service.getOngoingPOrders(accessToken=token)
        pdb.set_trace()

        payload['reply']['res'] = soap2dict(reply)
        payload['success'] = True
        return payload

    elif command == 'invoice':
         pass
    else:
        # ---------------------------------------------------------------------
        # Bad call:
        # ---------------------------------------------------------------------
        message = '[ERROR] ODOO is calling wrong command {}\n'.format(
            command)
        payload['reply'].update({
            'error': message.strip(),
        })

    # -------------------------------------------------------------------------
    # Prepare response
    # -------------------------------------------------------------------------
    return payload

app.run(debug=True, host=host, port=port)
write_log(log_f, 'End ODOO-MSC Flask agent')
<|endoftext|>