<commit_before># -*- coding: utf-8 -*-
"""
Allow, Block, and Reject USB devices.

USBGuard is a software framework for implementing USB device authorization
policies. For more information, see https://usbguard.github.io/

Configuration parameters:
    allow_urgent: display urgency on unread messages (default False)
    button_next: mouse button to switch next device (default 4)
    button_previous: mouse button to switch previous device (default 5)
    filters: specify a list of filters to use:
        (default ['None', 'any', 'allow', 'block'])
    format: display format for this module
        *(default '[{format_device_list} ]{format_button_device} '
        '{format_button_filter}[\?color=darkgray&show \|]'
        '{format_button_permanent}')*
    format_button_allow: display format for allow button
        (default '[\?if=!policy=allow&color=good Allow]')
    format_button_block: display format for block button
        (default '[\?if=!policy=block&color=degraded Block]')
    format_button_device: display format for device button
        (default '[\?color=deepskyblue&show Device] {device}/{devices}')
    format_button_filter: display format for filter button
        *(default '[\?color=deepskyblue&show Filter ]'
        '[\?if=filter=allow Allow|[\?if=filter=block '
        'Block|[\?if=filter=None None|Any]]]')*
    format_button_permanent: display format for permanent button
        *(default '[\?if=permanent&color=white Permanently'
        '|\?color=darkgray Permanently]')*
    format_button_reject: display format for reject button
        (default '\?color=bad Reject')
    format_device: display format for USB devices
        *(default '[{name}|Unknown {id} [\?color=darkgray {usb_id}]]'
        '[ {format_button_allow}][ {format_button_block}]')*
    format_device_separator: show separator if more than one (default ' ')
    format_notification_message: specify notification message to use
        (default 'USB ID: {usb_id}\\nName: {name}\\nPort: {port}')
    format_notification_title: specify notification title to use
        *(default 'USB Device [\?if=policy=allow Allowed]'
        '[\?if=policy=block Blocked][\?if=policy=reject Rejected]')*
    permanent: specify behavior to use, otherwise auto False (default None)

Format placeholders:
    {device}                  number of USB devices
    {format_button_filter}    button to toggle USB device display filters
    {format_button_permanent} button to toggle permanent states
    {format_device}           format for USB devices

format_button_filter:
    {filter}      USB device filter, eg None, any, allow, block

format_button_permanent:
    {permanent}   permanent boolean, eg False, True

format_device:
    {id}                   eg 1, 2, 5, 6, 7, 22, 23, 33
    {policy}               eg allow, block
    {usb_id}               eg 054c:0268
    {name}                 eg Poker II, PLAYSTATION(R)3 Controller
    {serial}               eg 0000:00:00.0
    {port}                 eg usb1, usb2, usb3, 1-1, 4-1.2.1
    {interface}            eg 00:00:00:00 00:00:00 00:00:00
    {hash}                 eg ihYz60+8pxZBi/cm+Q/4Ibrsyyzq/iZ9xtMDAh53sng
    {parent_hash}          eg npSDT1xuEIOSLNt2RT2EbFrE8XRZoV29t1n7kg6GxXg
    {format_button_allow}  format for allow button
    {format_button_block}  format for block button
    {format_button_reject} format for reject button

format_notification_*:
    See `format_device`

Requires:
    python-gobject: Python Bindings for GLib/GObject/GIO/GTK+
    usbguard: USB device authorization policy framework

Examples:
```
# specify a list of filters: None, any, allow, block
usbguard {
    * None: show any devices with any options
    * any: show any devices
    * allow: show authorized devices
    * block: show unauthorized devices
}

# show block device only, hide button
usbguard {
    format_button_filter = ''
    filters = ['block']
}
```

@author Cyril Levis (@cyrinux)
@license BSD

SAMPLE OUTPUT
[
    {'full_text': 'Poker II '},
    {'full_text': 'Allow ', 'color': '#00ff00'},
    {'full_text': 'Block ', 'color': '#ffff00'},
    {'full_text': 'Reject', 'color': '#ff0000'},
]

filters
[
    {'full_text': 'PLAYSTATION(R)3 Controller '},
    {'full_text': 'Allow ', 'color': '#00ff00'},
    {'full_text': 'Block', 'color': '#ffff00'},
]

unknown
[
    {'full_text': 'Unknown 12 '},
    {'full_text': '8087:0024 ', 'color': '#a9a9a9'},
    {'full_text': 'Allow ', 'color': '#00ff00'},
    {'full_text': 'Block', 'color': '#ffff00'},
]
"""

from copy import deepcopy
from threading import Thread
from gi.repository import Gio
import re

STRING_USBGUARD_DBUS = "start usbguard-dbus.service"
STRING_INVALID_FILTER = "invalid filters `{}`"

class Py3status:
    """
    """

    # available configuration parameters
    allow_urgent = False
    button_next = 4
    button_previous = 5
    filters = ["None", "any", "allow", "block"]
    format = (
        "[{format_device_list} ]{format_button_device} {format_button_filter}"
        "[\?color=darkgray&show \|]{format_button_permanent}"
    )
    format_button_allow = "[\?if=!policy=allow&color=good Allow]"
    format_button_block = "[\?if=!policy=block&color=degraded Block]"
    format_button_device = "[\?color=deepskyblue&show Device] {device}/{devices}"
    format_button_filter = (
        "[\?color=deepskyblue&show Filter ]"
        "[\?if=filter=allow Allow|[\?if=filter=block "
        "Block|[\?if=filter=None None|Any]]]"
    )
    format_button_permanent = (
        "[\?if=permanent&color=white Permanently" "|\?color=darkgray Permanently]"
    )
    format_button_reject = "\?color=bad Reject"
    format_device = (
        "[{name}|Unknown {id} [\?color=darkgray {usb_id}]]"
        "[ {format_button_allow}][ {format_button_block}]"
    )
    format_device_separator = " "
    format_notification_message = "USB ID: {usb_id}\nName: {name}\nPort: {port}"
    format_notification_title = (
        "USB Device [\?if=policy=allow Allowed]"
        "[\?if=policy=block Blocked]"
        "[\?if=policy=reject Rejected]"
    )
    permanent = None

    # playground

    def post_config_hook(self):
        for _filter in self.filters:
            if _filter not in ["None", "any", "allow", "block"]:
                raise Exception(STRING_INVALID_FILTER.format(_filter))

        self.init = {
            "format_button": self.py3.get_placeholders_list(
                self.format_device, "format_button_*"
            ),
            "notifications": (
                self.format_notification_title or self.format_notification_message
            ),
            "permanent": self.permanent is None,
            "target": {"allow": 0, "block": 1, "reject": 2},
        }

        self.filter_index = 0
        self.filter_length = len(self.filters)
        self.device_index = 1
        self.device_length = 0
        self.permanent = self.permanent or False
        self.cache_devices = {}
        self.device_keys = [
            ("serial", re.compile(r"\S*serial \"(\S+)\"\S*")),
            ("policy", re.compile(r"^(\S+)")),
            ("usb_id", re.compile(r"id (\S+)")),
            ("name", re.compile(r"name \"(.*)\" hash")),
            ("hash", re.compile(r"hash \"(.*)\" parent-hash")),
            ("parent_hash", re.compile(r"parent-hash \"(.*)\" via-port")),
            ("port", re.compile(r"via-port \"(.*)\" with-interface")),
            ("interface", re.compile(r"with-interface \{ (.*) \}$")),
        ]

        try:
            self.bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
            self.proxy = Gio.DBusProxy.new_sync(
                self.bus,
                Gio.DBusProxyFlags.NONE,
                None,
                "org.usbguard",
                "/org/usbguard/Devices",
                "org.usbguard.Devices",
                None,
            )
            for signal in ["DevicePolicyChanged", "DevicePresenceChanged"]:
                self.bus.signal_subscribe(
                    None, "org.usbguard.Devices", signal, None, None, 0, self.py3.update
                )
        except Exception:
            raise Exception(STRING_USBGUARD_DBUS)

        self.thread = Thread(target=self._start_loop)
        self.thread.daemon = True
        # self.thread.start()

    def _start_loop(self):
        pass

    def _get_devices(self, device_filter):
        if device_filter in ["any", "None"]:
            device_filter = "match"

        devices = self.proxy.listDevices("(s)", device_filter)
        new_devices = []

        for device_id, device_string in devices:
            try:
                device = self.cache_devices[device_string]
            except KeyError:
                device = {"id": device_id}
                for name, regex in self.device_keys:
                    value = regex.findall(device_string) or None
                    if value:
                        value = (
                            value[0]
                            .encode("latin-1")
                            .decode("unicode_escape")
                            .encode("latin-1")
                            .decode()
                        )
                    device[name] = value
                self.cache_devices[device_string] = device
            new_devices.append(device)

        return deepcopy(new_devices)

    def _manipulate_devices(self, devices, device_filter):
        new_device = []
        format_device_list = None
        self.device_length = len(devices)
        for index, device in enumerate(devices, 1):
            if device_filter == "any":
                pass
            elif device_filter == "None":
                device["policy"] = None
            elif device["policy"] != device_filter:
                continue
            for x in self.init["format_button"]:
                composite = self.py3.safe_format(getattr(self, x), device)
                device[x] = self.py3.composite_update(
                    composite, {"index": "{}/{}".format(device["id"], x.split("_")[-1])}
                )

            device_composite = self.py3.safe_format(self.format_device, device)
            if index == self.device_index:
                format_device_list = device_composite

            new_device.append(device_composite)

        format_device_separator = self.py3.safe_format(self.format_device_separator)
        format_device = self.py3.composite_join(format_device_separator, new_device)

        return format_device, format_device_list

    def _notify_user(self, device):
        format_notification_message = self.py3.safe_format(
            self.format_notification_message, device
        )
        format_notification_title = self.py3.safe_format(
            self.format_notification_title, device
        )
        self.py3.notify_user(
            msg=format_notification_message,
            title=format_notification_title,
            icon="/usr/share/icons/hicolor/scalable/apps/usbguard-icon.svg",
        )

    def usbguard(self):
        filter_name = self.filters[self.filter_index]
        device_data = self._get_devices(filter_name)
        format_device, format_device_list = self._manipulate_devices(
            device_data, filter_name
        )

        if self.device_index > self.device_length:
            self.device_index = self.device_length

        format_button_device = self.py3.safe_format(
            self.format_button_device,
            {"device": self.device_index, "devices": self.device_length},
        )
        self.py3.composite_update(format_button_device, {"index": "button_device"})

        format_button_filter = self.py3.safe_format(
            self.format_button_filter, {"filter": filter_name}
        )
        self.py3.composite_update(format_button_filter, {"index": "button_filter"})
        format_button_permanent = self.py3.safe_format(self.format_button_permanent)
        self.py3.composite_update(
            format_button_permanent, {"index": "button_permanent"}
        )

        usbguard_data = {
            "device": len(device_data),
            "format_button_filter": format_button_filter,
            "format_button_permanent": format_button_permanent,
            "format_device": format_device,
            "format_button_device": format_button_device,
            "format_device_list": format_device_list,
        }

        response = {
            "cached_until": self.py3.time_in(10),
            "full_text": self.py3.safe_format(self.format, usbguard_data),
        }

        if self.allow_urgent:
            response["urgent"] = True

        self.device_data = device_data
        return response

    def on_click(self, event):
        index = event["index"]
        button = event["button"]
        if isinstance(index, int):
            return
        elif index == "button_device":
            if button == self.button_next:
                self.device_index += 1
                self.device_index %= self.device_length + 1
            elif button == self.button_previous:
                self.device_index -= 1
                self.device_index %= self.device_length + 1
        elif index == "button_filter":
            if button == self.button_next:
                self.filter_index += 1
                self.filter_index %= self.filter_length
            elif button == self.button_previous:
                self.filter_index -= 1
                self.filter_index %= self.filter_length
        elif index == "button_permanent":
            self.permanent = not self.permanent
        else:
            device_id, policy_name = index.split("/")
            device_id = int(device_id)

            if self.init["notifications"]:
                for device in self.device_data:
                    if device["id"] == device_id:
                        device["policy"] = policy_name
                        self._notify_user(device)
                        break

            policy = self.init["target"][policy_name]
            self.proxy.applyDevicePolicy("(uub)", device_id, policy, self.permanent)
            if self.init["permanent"]:
                self.permanent = False
            self.device_index = 0

if __name__ == "__main__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test

    module_test(Py3status)
<commit_msg>usbguard module: simpler implementation (#1707)<commit_after># -*- coding: utf-8 -*-
"""
Allow or Reject newly plugged USB devices using USBGuard.

Configuration parameters:
    format: display format for this module
        (default '{format_device}')
    format_button_allow: display format for allow button filter
        (default '\[Allow\]')
    format_button_reject: display format for reject button filter
        (default '\[Reject\]')
    format_device: display format for USB devices
        (default '{format_button_reject} [{name}|{usb_id}] {format_button_allow}')
    format_device_separator: show separator if more than one (default ' ')

Format placeholders:
    {device}                  number of USB devices
    {format_device}           format for USB devices

format_device:
    {format_button_allow}     button to allow the device
    {format_button_reject}    button to reject the device
    {id}                      eg 1, 2, 5, 6, 7, 22, 23, 33
    {policy}                  eg allow, block, reject
    {usb_id}                  eg 054c:0268
    {name}                    eg Poker II, PLAYSTATION(R)3 Controller
    {serial}                  eg 0000:00:00.0
    {port}                    eg usb1, usb2, usb3, 1-1, 4-1.2.1
    {interface}               eg 00:00:00:00 00:00:00 00:00:00
    {hash}                    eg ihYz60+8pxZBi/cm+Q/4Ibrsyyzq/iZ9xtMDAh53sng
    {parent_hash}             eg npSDT1xuEIOSLNt2RT2EbFrE8XRZoV29t1n7kg6GxXg

Requires:
    python-gobject: Python Bindings for GLib/GObject/GIO/GTK+
    usbguard: USB device authorization policy framework

@author @cyrinux, @maximbaz
@license BSD

SAMPLE OUTPUT
[
    {'full_text': '[Reject] ', 'urgent': True},
    {'full_text': 'USB Flash Drive ', 'urgent': True},
    {'full_text': '[Allow]', 'urgent': True}
]
"""

from threading import Thread
from gi.repository import GLib, Gio
import re

STRING_USBGUARD_DBUS = "start usbguard-dbus.service"

class Py3status:
    """
    """

    # available configuration parameters
    format = "{format_device}"
    format_button_allow = "\[Allow\]"
    format_button_reject = "\[Reject\]"
    format_device = "{format_button_reject} [{name}|{usb_id}] {format_button_allow}"
    format_device_separator = " "

    def post_config_hook(self):
        self.init = {
            "format_button": self.py3.get_placeholders_list(
                self.format_device, "format_button_*"
            ),
            "target": {"allow": 0, "reject": 2},
        }

        self.keys = [
            ("serial", re.compile(r"\S*serial \"(\S+)\"\S*")),
            ("policy", re.compile(r"^(\S+)")),
            ("usb_id", re.compile(r"id (\S+)")),
            ("name", re.compile(r"name \"(.*)\" hash")),
            ("hash", re.compile(r"hash \"(.*)\" parent-hash")),
            ("parent_hash", re.compile(r"parent-hash \"(.*)\" via-port")),
            ("port", re.compile(r"via-port \"(.*)\" with-interface")),
            ("interface", re.compile(r"with-interface \{ (.*) \}$")),
        ]

        self._init_dbus()

    def _init_dbus(self):
        self.bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
        self.proxy = Gio.DBusProxy.new_sync(
            self.bus,
            Gio.DBusProxyFlags.NONE,
            None,
            "org.usbguard",
            "/org/usbguard/Devices",
            "org.usbguard.Devices",
            None,
        )
        for signal in ["DevicePolicyChanged", "DevicePresenceChanged"]:
            self.bus.signal_subscribe(
                None,
                "org.usbguard.Devices",
                signal,
                None,
                None,
                0,
                lambda *args: self.py3.update(),
            )

        thread = Thread(target=lambda: GLib.MainLoop().run())
        thread.daemon = True
        thread.start()

    def _get_devices(self):
        try:
            raw_devices = self.proxy.listDevices("(s)", "block")
        except Exception:
            raise Exception(STRING_USBGUARD_DBUS)

        devices = []
        for device_id, string in raw_devices:
            device = {"id": device_id}
            string = string.encode("latin-1").decode("unicode_escape")
            string = string.encode("latin-1").decode("utf-8")
            for name, regex in self.keys:
                value = regex.findall(string) or None
                if value:
                    value = value[0]
                device[name] = value
            devices.append(device)

        return devices

    def _format_device(self, devices):
        device_info = []
        for device in devices:
            for btn in self.init["format_button"]:
                composite = self.py3.safe_format(getattr(self, btn), device)
                device[btn] = self.py3.composite_update(
                    composite,
                    {"index": "{}/{}".format(device["id"], btn.split("_")[-1])},
                )

            device_info.append(self.py3.safe_format(self.format_device, device))

        format_device_separator = self.py3.safe_format(self.format_device_separator)
        format_device = self.py3.composite_join(format_device_separator, device_info)

        return format_device

    def usbguard(self):
        devices = self._get_devices()

        usbguard_data = {
            "device": len(devices),
            "format_device": self._format_device(devices),
        }

        return {
            "cached_until": self.py3.CACHE_FOREVER,
            "full_text": self.py3.safe_format(self.format, usbguard_data),
            "urgent": True,
        }

    def on_click(self, event):
        if isinstance(event["index"], int):
            return

        device_id, policy_name = event["index"].split("/")
        policy = self.init["target"][policy_name]
        self.proxy.applyDevicePolicy("(uub)", int(device_id), policy, False)

if __name__ == "__main__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test

    module_test(Py3status)
<|endoftext|>


<commit_before>from subprocess import call
from docx import Document

call(["git", "add", "."])
call(["git", "commit", "-m", "\"Update\""])
call(["git", "push"])

doc = Document("frontpage.docx")
p = doc.paragraphs
for i in range(len(p)):
	text = p[i].text
	if text == "BANNER":
		i += 1
		print("Banner Text:")
		print(p[i].text)
	elif text == "EVENT":
		i += 1
		print("Event Details:")
		event_name = p[i].text
		i += 1
		event_name += "\n"
		event_details = p[i].text
		i += 1
		event_details += " - "
		event_details += p[i].text
		i += 1
		event_details += " - "
		event_details += p[i].text

input()<commit_msg>"Update"<commit_after>from subprocess import call
from docx import Document

call(["git", "add", "."])
call(["git", "commit", "-m", "\"Update\""])
call(["git", "push"])

doc = Document("frontpage.docx")
p = doc.paragraphs
for i in range(len(p)):
	text = p[i].text
	if text == "BANNER":
		i += 1
		print("Banner Text:")
		print(p[i].text)
	elif text == "EVENT":
		i += 1
		print("Event Details:")
		event_name = p[i].text
		i += 1
		event_name += "\n"
		event_details = p[i].text
		i += 1
		event_details += " - "
		event_details += p[i].text
		i += 1
		event_details += " - "
		event_details += p[i].text
		print(event_name + event_name)

input()<|endoftext|>


<commit_before>#!/usr/bin/env python

from __future__ import print_function
import os
import json
import logging
from setuptools import setup

logger = logging.getLogger(__name__)
METADATA_FILENAME = 'django_easy_app/package_metadata.json'
BASEPATH = os.path.dirname(os.path.abspath(__file__))

def readme():
    with open('README.rst') as f:
        return f.read()

# Create a dictionary of our arguments, this way this script can be imported
#  without running setup() to allow external scripts to see the setup settings.
args = {
    'name': 'django-easy-app',
    'version': '0.0.1',
    'author': 'Dwight Hubbard',
    'author_email': 'd@d-h.us',
    'url': 'https://github.com/dwighthubbard/django-easy-app',
    'license': 'BSD',
    'packages': [
        'django_easy_app', 'django_easy_app.management',
        'django_easy_app.management.commands'
    ],
    'description': 'Extend django-admin to create apps that are easier to set',
    'install_requires': ['django'],
    'requires': ['django'],
    'long_description': readme(),
    'classifiers': [
            'Development Status :: 2 - Pre-Alpha',
            'Environment :: Console',
            'Intended Audience :: Developers',
            'License :: OSI Approved :: BSD License',
            'Operating System :: MacOS :: MacOS X',
            'Operating System :: POSIX :: BSD :: FreeBSD',
            'Operating System :: POSIX :: Linux',
            'Operating System :: POSIX :: SunOS/Solaris',
            'Operating System :: POSIX',
            'Programming Language :: C',
            'Programming Language :: Python :: 2',
            'Programming Language :: Python :: 2.7',
            'Programming Language :: Python :: 3',
            'Programming Language :: Python :: 3.4',
            'Programming Language :: Python :: Implementation :: CPython',
            'Programming Language :: Python :: Implementation :: PyPy',
            'Programming Language :: Python',
            'Topic :: Software Development :: Libraries :: Python Modules',
            'Topic :: Software Development :: Libraries',
            'Topic :: System :: Systems Administration',
            'Topic :: Utilities',
    ],
    'package_data': {
        'django_easy_app': ['package_metadata.json'],
    },
    'include_package_data': True,
}
setup_arguments = args

class Git(object):
    version_list = ['0', '7', '0']

    def __init__(self, version=None):
        if version:
            self.version_list = version.split('.')

    @property
    def version(self):
        """
        Generate a Unique version value from the git information
        :return:
        """
        git_rev = len(os.popen('git rev-list HEAD').readlines())
        if git_rev != 0:
            self.version_list[-1] = '%d' % git_rev
        version = '.'.join(self.version_list)
        return version

    @property
    def branch(self):
        """
        Get the current git branch
        :return:
        """
        return os.popen('git rev-parse --abbrev-ref HEAD').read().strip()

    @property
    def hash(self):
        """
        Return the git hash for the current build
        :return:
        """
        return os.popen('git rev-parse HEAD').read().strip()

    @property
    def origin(self):
        """
        Return the fetch url for the git origin
        :return:
        """
        for item in os.popen('git remote -v'):
            split_item = item.strip().split()
            if split_item[0] == 'origin' and split_item[-1] == '(push)':
                return split_item[1]

def get_and_update_metadata():
    """
    Get the package metadata or generate it if missing
    :return:
    """
    global METADATA_FILENAME
    global REDIS_SERVER_METADATA

    if not os.path.exists('.git') and os.path.exists(METADATA_FILENAME):
        with open(METADATA_FILENAME) as fh:
            metadata = json.load(fh)
    else:
        git = Git(version=setup_arguments['version'])
        metadata = {
            'git_version': git.version,
            'git_origin': git.origin,
            'git_branch': git.branch,
            'git_hash': git.hash,
            'version': git.version,
        }
        with open(METADATA_FILENAME, 'w') as fh:
            json.dump(metadata, fh, indent=4)
    return metadata

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)

    metadata = get_and_update_metadata()
    setup_arguments['version'] = metadata['version']

    setup(**setup_arguments)
<commit_msg>Add scripts to the package<commit_after>#!/usr/bin/env python

from __future__ import print_function
import os
import json
import logging
from setuptools import setup

logger = logging.getLogger(__name__)
METADATA_FILENAME = 'django_easy_app/package_metadata.json'
BASEPATH = os.path.dirname(os.path.abspath(__file__))

def readme():
    """
    Return the data from the README file in the current directory
    :return:
    """
    with open('README.rst') as f:
        return f.read()

def scripts():
    """
    Return a list of script files in the scripts directory
    :return:
    """
    script_list = []
    if os.path.isdir('scripts'):
        script_list = [
            os.path.join('scripts', f) for f in os.listdir('scripts')
        ]
    return script_list

# Create a dictionary of our arguments, this way this script can be imported
#  without running setup() to allow external scripts to see the setup settings.
setup_arguments = {
    'name': 'django-easy-app',
    'version': '0.0.1',
    'author': 'Dwight Hubbard',
    'author_email': 'd@d-h.us',
    'url': 'https://github.com/dwighthubbard/django-easy-app',
    'license': 'BSD',
    'packages': [
        'django_easy_app', 'django_easy_app.management',
        'django_easy_app.management.commands'
    ],
    'description': 'Extend django-admin to create apps that are easier to set',
    'install_requires': ['django'],
    'requires': ['django'],
    'long_description': readme(),
    'classifiers': [
            'Development Status :: 2 - Pre-Alpha',
            'Environment :: Console',
            'Intended Audience :: Developers',
            'License :: OSI Approved :: BSD License',
            'Operating System :: MacOS :: MacOS X',
            'Operating System :: POSIX :: BSD :: FreeBSD',
            'Operating System :: POSIX :: Linux',
            'Operating System :: POSIX :: SunOS/Solaris',
            'Operating System :: POSIX',
            'Programming Language :: C',
            'Programming Language :: Python :: 2',
            'Programming Language :: Python :: 2.7',
            'Programming Language :: Python :: 3',
            'Programming Language :: Python :: 3.4',
            'Programming Language :: Python :: Implementation :: CPython',
            'Programming Language :: Python :: Implementation :: PyPy',
            'Programming Language :: Python',
            'Topic :: Software Development :: Libraries :: Python Modules',
            'Topic :: Software Development :: Libraries',
            'Topic :: System :: Systems Administration',
            'Topic :: Utilities',
    ],
    'package_data': {
        'django_easy_app': ['package_metadata.json'],
    },
    'include_package_data': True,
}

class Git(object):
    version_list = ['0', '7', '0']

    def __init__(self, version=None):
        if version:
            self.version_list = version.split('.')

    @property
    def version(self):
        """
        Generate a Unique version value from the git information
        :return:
        """
        git_rev = len(os.popen('git rev-list HEAD').readlines())
        if git_rev != 0:
            self.version_list[-1] = '%d' % git_rev
        version = '.'.join(self.version_list)
        return version

    @property
    def branch(self):
        """
        Get the current git branch
        :return:
        """
        return os.popen('git rev-parse --abbrev-ref HEAD').read().strip()

    @property
    def hash(self):
        """
        Return the git hash for the current build
        :return:
        """
        return os.popen('git rev-parse HEAD').read().strip()

    @property
    def origin(self):
        """
        Return the fetch url for the git origin
        :return:
        """
        for item in os.popen('git remote -v'):
            split_item = item.strip().split()
            if split_item[0] == 'origin' and split_item[-1] == '(push)':
                return split_item[1]

def get_and_update_metadata():
    """
    Get the package metadata or generate it if missing
    :return:
    """
    global METADATA_FILENAME
    global REDIS_SERVER_METADATA

    if not os.path.exists('.git') and os.path.exists(METADATA_FILENAME):
        with open(METADATA_FILENAME) as fh:
            metadata = json.load(fh)
    else:
        git = Git(version=setup_arguments['version'])
        metadata = {
            'git_version': git.version,
            'git_origin': git.origin,
            'git_branch': git.branch,
            'git_hash': git.hash,
            'version': git.version,
        }
        with open(METADATA_FILENAME, 'w') as fh:
            json.dump(metadata, fh, indent=4)
    return metadata

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)

    # Update the setup arguments with our version number
    metadata = get_and_update_metadata()
    setup_arguments['version'] = metadata['version']

    # Add any scripts that should be part of the package
    if scripts():
        setup_arguments['scripts'] = scripts()

    setup(**setup_arguments)
<|endoftext|>


<commit_before>#!/usr/bin/python
import re, json, copy
from main import *

### Hex to bin converter and vice versa for objects

def json_is_base(obj,base):
    alpha = get_code_string(base)
    if isinstance(obj,(str,unicode)):
        for i in range(len(obj)):
            if alpha.find(obj[i]) == -1: return False
        return True
    elif isinstance(obj,(int,float,long)) or obj is None: return True
    elif isinstance(obj,list):
        for i in range(len(obj)):
            if not json_is_base(obj[i],base): return False
        return True
    else:
        for x in obj:
            if not json_is_base(obj[x],base): return False
        return True

def json_changebase(obj,changer):
    if isinstance(obj,(str,unicode)): return changer(obj)
    elif isinstance(obj,(int,float,long)) or obj is None: return obj
    elif isinstance(obj,list): return [json_changebase(x,changer) for x in obj]
    return dict((x, json_changebase(obj[x], changer)) for x in obj)

### Transaction serialization and deserialization

def deserialize(tx):
    if re.match('^[0-9a-fA-F]*$',tx):
        return json_changebase(deserialize(tx.decode('hex')),lambda x:x.encode('hex'))
    # http://stackoverflow.com/questions/4851463/python-closure-write-to-variable-in-parent-scope
    # Python's scoping rules are demented, requiring me to make pos an object so that it is call-by-reference
    pos = [0]

    def read_as_int(bytez):
        pos[0] += bytez
        return decode(tx[pos[0]-bytez:pos[0]][::-1],256)

    def read_var_int():
        pos[0] += 1
        if ord(tx[pos[0]-1]) < 253: return ord(tx[pos[0]-1])
        return read_as_int(pow(2,ord(tx[pos[0]-1]) - 252))

    def read_bytes(bytez):
        pos[0] += bytez
        return tx[pos[0]-bytez:pos[0]]

    def read_var_string():
        size = read_var_int()
        return read_bytes(size)

    obj = { "ins" : [] , "outs" : [] }
    obj["version"] = read_as_int(4)
    ins = read_var_int()
    for i in range(ins):
        obj["ins"].append({
            "outpoint" : {
                "hash" : read_bytes(32)[::-1],
                "index": read_as_int(4)
            },
            "script" : read_var_string(),
            "sequence" : read_as_int(4)
        })
    outs = read_var_int()
    for i in range(outs):
        obj["outs"].append({
            "value" : read_as_int(8),
            "script": read_var_string()
        })
    obj["locktime"] = read_as_int(4)
    return obj

def serialize(txobj):
    o = []
    if json_is_base(txobj,16):
        return serialize(json_changebase(txobj,lambda x: x.decode('hex'))).encode('hex')
    o.append(encode(txobj["version"],256,4)[::-1])
    o.append(num_to_var_int(len(txobj["ins"])))
    for inp in txobj["ins"]:
        o.append(inp["outpoint"]["hash"][::-1])
        o.append(encode(inp["outpoint"]["index"],256,4)[::-1])
        o.append(num_to_var_int(len(inp["script"]))+inp["script"])
        o.append(encode(inp["sequence"],256,4)[::-1])
    o.append(num_to_var_int(len(txobj["outs"])))
    for out in txobj["outs"]:
        o.append(encode(out["value"],256,8)[::-1])
        o.append(num_to_var_int(len(out["script"]))+out["script"])
    o.append(encode(txobj["locktime"],256,4)[::-1])
    return ''.join(o)

### Hashing transactions for signing

SIGHASH_ALL = 1
SIGHASH_NONE = 2
SIGHASH_SINGLE = 3
SIGHASH_ANYONECANPAY = 80

def signature_form(tx, i, script, hashcode = SIGHASH_ALL):
    i, hashcode = int(i), int(hashcode)
    if isinstance(tx,str):
        return serialize(signature_form(deserialize(tx),i,script))
    newtx = copy.deepcopy(tx)
    for inp in newtx["ins"]: inp["script"] = ""
    newtx["ins"][i]["script"] = script
    if hashcode == SIGHASH_NONE:
        newtx["outs"] = []
    elif hashcode == SIGHASH_SINGLE:
        newtx["outs"] = newtx["outs"][:len(newtx["ins"])]
        for out in range(len(newtx["ins"]) - 1):
            out.value = 2**64 - 1
            out.script = ""
    elif hashcode == SIGHASH_ANYONECANPAY:
        newtx["ins"] = [newtx["ins"][i]]
    else:
        pass
    return newtx

### Making the actual signatures

def der_encode_sig(v,r,s):
    b1, b2 = encode(r,16,64), encode(s,16,64)
    if r >= 2**255: b1 = '00' + b1
    if s >= 2**255: b2 = '00' + b2
    left = '02'+encode(len(b1)/2,16,2)+b1
    right = '02'+encode(len(b2)/2,16,2)+b2
    return '30'+encode(len(left+right)/2,16,2)+left+right

def der_decode_sig(sig):
    leftlen = decode(sig[6:8],16)*2
    left = sig[8:8+leftlen]
    rightlen = decode(sig[10+leftlen:12+leftlen],16)*2
    right = sig[12+leftlen:12+leftlen+rightlen]
    return (None,decode(left,16),decode(right,16))

def txhash(tx,hashcode=None):
    if re.match('^[0-9a-fA-F]*$',tx):
        tx = changebase(tx,16,256)
    if hashcode: return dbl_sha256(tx + encode(int(hashcode),256,4)[::-1])
    else: return dbl_sha256(tx)[::-1]

def bin_txhash(tx,hashcode=None):
    return txhash(tx,hashcode).decode('hex')

def ecdsa_tx_sign(tx,priv,hashcode=SIGHASH_ALL):
    rawsig = ecdsa_raw_sign(bin_txhash(tx,hashcode),priv)
    return der_encode_sig(*rawsig)+encode(hashcode,16,2)

def ecdsa_tx_verify(tx,sig,pub,hashcode=SIGHASH_ALL):
    return ecdsa_raw_verify(bin_txhash(tx,hashcode),der_decode_sig(sig),pub)

def ecdsa_tx_recover(tx,sig,hashcode=SIGHASH_ALL):
    z = bin_txhash(tx,hashcode)
    _,r,s = der_decode_sig(sig)
    left = ecdsa_raw_recover(z,(0,r,s))
    right = ecdsa_raw_recover(z,(1,r,s))
    return (encode_pubkey(left,'hex'), encode_pubkey(right,'hex'))

### Scripts

def mk_pubkey_script(addr): # Keep the auxiliary functions around for altcoins' sake
    return '76a914' + b58check_to_hex(addr) + '88ac'

def mk_scripthash_script(addr):
    return 'a914' + b58check_to_hex(addr) + '87'

# Address representation to output script
def address_to_script(addr):
    if addr[0] == '3': return mk_scripthash_script(addr)
    else: return mk_pubkey_script(addr)

# Output script to address representation
def script_to_address(script,vbyte=0):
    if re.match('^[0-9a-fA-F]*$',script):
        script = script.decode('hex')
    if script[:3] == '\x76\xa9\x14' and script[-2:] == '\x88\xac' and len(script) == 25:
        return bin_to_b58check(script[3:-2],vbyte) # pubkey hash addresses
    else:
        return bin_to_b58check(script[2:-1],5) # BIP0016 scripthash addresses

def p2sh_scriptaddr(script):
    if re.match('^[0-9a-fA-F]*$',script): script = script.decode('hex')
    return hex_to_b58check(hash160(script),5)
scriptaddr = p2sh_scriptaddr

def deserialize_script(script):
    if re.match('^[0-9a-fA-F]*$',script):
        return json_changebase(deserialize_script(script.decode('hex')),lambda x:x.encode('hex'))
    out, pos = [], 0
    while pos < len(script):
        code = ord(script[pos])
        if code == 0:
            out.append(None)
            pos += 1
        elif code <= 75:
            out.append(script[pos+1:pos+1+code])
            pos += 1 + code
        elif code <= 78:
            szsz = pow(2,code - 76)
            sz = decode(script[pos + szsz : pos : -1],256)
            out.append(script[pos + 1 + szsz:pos + 1 + szsz + sz])
            pos += 1 + szsz + sz
        elif code <= 96:
            out.append(code - 80)
            pos += 1
        else:
            out.append(code)
            pos += 1
    return out

def serialize_script_unit(unit):
    if isinstance(unit,int):
        if unit < 16: return chr(unit + 80)
        else: return chr(unit)
    elif unit is None:
        return '\x00'
    else:
        if len(unit) <= 75: return chr(len(unit))+unit
        elif len(unit) < 256: return chr(76)+chr(len(unit))+unit
        elif len(unit) < 65536: return chr(77)+encode(len(unit),256,2)[::-1]+unit
        else: return chr(78)+encode(len(unit),256,4)[::-1]+unit

def serialize_script(script):
    if json_is_base(script,16):
        return serialize_script(json_changebase(script,lambda x:x.decode('hex'))).encode('hex')
    return ''.join(map(serialize_script_unit,script))

def mk_multisig_script(*args): # [pubs],k,n or pub1,pub2...pub[n],k,n
    if len(args) == 3: pubs, k, n = args[0], int(args[1]), int(args[2])
    else: pubs, k, n = list(args[:-2]), int(args[-2]), int(args[-1])
    return serialize_script([k]+pubs+[n,174])

### Signing and verifying

def verify_tx_input(tx,i,script,sig,pub):
    if re.match('^[0-9a-fA-F]*$',tx): tx = tx.decode('hex')
    if re.match('^[0-9a-fA-F]*$',script): script = script.decode('hex')
    if not re.match('^[0-9a-fA-F]*$',sig): sig = sig.encode('hex')
    hashcode = ord(sig[-1])
    modtx = signature_form(tx,int(i),script)
    return ecdsa_tx_verify(modtx,sig,pub)

def sign(tx,i,priv):
    i = int(i)
    if not re.match('^[0-9a-fA-F]*$',tx):
        return sign(tx.encode('hex'),i,priv).decode('hex')
    if len(priv) <= 33: priv = priv.encode('hex')
    pub = privkey_to_pubkey(priv)
    address = pubkey_to_address(pub)
    signing_tx = signature_form(tx,i,mk_pubkey_script(address))
    sig = ecdsa_tx_sign(signing_tx,priv)
    txobj = deserialize(tx)
    txobj["ins"][i]["script"] = serialize_script([sig,pub])
    return serialize(txobj)

def multisign(tx,i,script,pk):
    if re.match('^[0-9a-fA-F]*$',tx): tx = tx.decode('hex')
    if re.match('^[0-9a-fA-F]*$',script): script = script.decode('hex')
    modtx = signature_form(tx,i,script)
    return ecdsa_tx_sign(modtx,pk)

def apply_multisignatures(*args): # tx,i,script,sigs OR tx,i,script,sig1,sig2...,sig[n]
    tx, i, script = args[0], int(args[1]), args[2]
    sigs = args[3] if isinstance(args[3],list) else list(args[3:])

    if re.match('^[0-9a-fA-F]*$',script): script = script.decode('hex')
    sigs = [x.decode('hex') if x[:2] == '30' else x for x in sigs]
    if re.match('^[0-9a-fA-F]*$',tx):
        return apply_multisignatures(tx.decode('hex'),i,script,sigs).encode('hex')

    txobj = deserialize(tx)
    txobj["ins"][i]["script"] = serialize_script([None]+sigs+[script])
    return serialize(txobj)

def mktx(*args): # [in0, in1...],[out0, out1...] or in0, in1 ... out0 out1 ...
    if isinstance(args[0],list): ins, outs = args[0], args[1]
    else:
        def is_inp(arg): return len(arg) > 64 or "output" in arg or "outpoint" in arg
        ins, outs = filter(is_inp, args), filter(lambda x: not is_inp(x), args)

    txobj = { "locktime" : 0, "version" : 1,"ins" : [], "outs" : [] }
    for i in ins:
        if isinstance(i,dict) and "outpoint" in i:
            txobj["ins"].append(i)
        else:
            if isinstance(i,dict) and "output" in i: i = i["output"]
            txobj["ins"].append({
                "outpoint" : { "hash": i[:64], "index": int(i[65:]) },
                "script": "",
                "sequence": 4294967295
            })
    for o in outs:
        if isinstance(o,str): o = {
            "address": o[:o.find(':')],
            "value": int(o[o.find(':')+1:])
        }
        txobj["outs"].append({
            "script": address_to_script(o["address"]),
            "value": o["value"]
        })
    return serialize(txobj)
<commit_msg>Small tx hashing fix<commit_after>#!/usr/bin/python
import re, json, copy
from main import *

### Hex to bin converter and vice versa for objects

def json_is_base(obj,base):
    alpha = get_code_string(base)
    if isinstance(obj,(str,unicode)):
        for i in range(len(obj)):
            if alpha.find(obj[i]) == -1: return False
        return True
    elif isinstance(obj,(int,float,long)) or obj is None: return True
    elif isinstance(obj,list):
        for i in range(len(obj)):
            if not json_is_base(obj[i],base): return False
        return True
    else:
        for x in obj:
            if not json_is_base(obj[x],base): return False
        return True

def json_changebase(obj,changer):
    if isinstance(obj,(str,unicode)): return changer(obj)
    elif isinstance(obj,(int,float,long)) or obj is None: return obj
    elif isinstance(obj,list): return [json_changebase(x,changer) for x in obj]
    return dict((x, json_changebase(obj[x], changer)) for x in obj)

### Transaction serialization and deserialization

def deserialize(tx):
    if re.match('^[0-9a-fA-F]*$',tx):
        return json_changebase(deserialize(tx.decode('hex')),lambda x:x.encode('hex'))
    # http://stackoverflow.com/questions/4851463/python-closure-write-to-variable-in-parent-scope
    # Python's scoping rules are demented, requiring me to make pos an object so that it is call-by-reference
    pos = [0]

    def read_as_int(bytez):
        pos[0] += bytez
        return decode(tx[pos[0]-bytez:pos[0]][::-1],256)

    def read_var_int():
        pos[0] += 1
        if ord(tx[pos[0]-1]) < 253: return ord(tx[pos[0]-1])
        return read_as_int(pow(2,ord(tx[pos[0]-1]) - 252))

    def read_bytes(bytez):
        pos[0] += bytez
        return tx[pos[0]-bytez:pos[0]]

    def read_var_string():
        size = read_var_int()
        return read_bytes(size)

    obj = { "ins" : [] , "outs" : [] }
    obj["version"] = read_as_int(4)
    ins = read_var_int()
    for i in range(ins):
        obj["ins"].append({
            "outpoint" : {
                "hash" : read_bytes(32)[::-1],
                "index": read_as_int(4)
            },
            "script" : read_var_string(),
            "sequence" : read_as_int(4)
        })
    outs = read_var_int()
    for i in range(outs):
        obj["outs"].append({
            "value" : read_as_int(8),
            "script": read_var_string()
        })
    obj["locktime"] = read_as_int(4)
    return obj

def serialize(txobj):
    o = []
    if json_is_base(txobj,16):
        return serialize(json_changebase(txobj,lambda x: x.decode('hex'))).encode('hex')
    o.append(encode(txobj["version"],256,4)[::-1])
    o.append(num_to_var_int(len(txobj["ins"])))
    for inp in txobj["ins"]:
        o.append(inp["outpoint"]["hash"][::-1])
        o.append(encode(inp["outpoint"]["index"],256,4)[::-1])
        o.append(num_to_var_int(len(inp["script"]))+inp["script"])
        o.append(encode(inp["sequence"],256,4)[::-1])
    o.append(num_to_var_int(len(txobj["outs"])))
    for out in txobj["outs"]:
        o.append(encode(out["value"],256,8)[::-1])
        o.append(num_to_var_int(len(out["script"]))+out["script"])
    o.append(encode(txobj["locktime"],256,4)[::-1])
    return ''.join(o)

### Hashing transactions for signing

SIGHASH_ALL = 1
SIGHASH_NONE = 2
SIGHASH_SINGLE = 3
SIGHASH_ANYONECANPAY = 80

def signature_form(tx, i, script, hashcode = SIGHASH_ALL):
    i, hashcode = int(i), int(hashcode)
    if isinstance(tx,str):
        return serialize(signature_form(deserialize(tx),i,script))
    newtx = copy.deepcopy(tx)
    for inp in newtx["ins"]: inp["script"] = ""
    newtx["ins"][i]["script"] = script
    if hashcode == SIGHASH_NONE:
        newtx["outs"] = []
    elif hashcode == SIGHASH_SINGLE:
        newtx["outs"] = newtx["outs"][:len(newtx["ins"])]
        for out in range(len(newtx["ins"]) - 1):
            out.value = 2**64 - 1
            out.script = ""
    elif hashcode == SIGHASH_ANYONECANPAY:
        newtx["ins"] = [newtx["ins"][i]]
    else:
        pass
    return newtx

### Making the actual signatures

def der_encode_sig(v,r,s):
    b1, b2 = encode(r,16,64), encode(s,16,64)
    if r >= 2**255: b1 = '00' + b1
    if s >= 2**255: b2 = '00' + b2
    left = '02'+encode(len(b1)/2,16,2)+b1
    right = '02'+encode(len(b2)/2,16,2)+b2
    return '30'+encode(len(left+right)/2,16,2)+left+right

def der_decode_sig(sig):
    leftlen = decode(sig[6:8],16)*2
    left = sig[8:8+leftlen]
    rightlen = decode(sig[10+leftlen:12+leftlen],16)*2
    right = sig[12+leftlen:12+leftlen+rightlen]
    return (None,decode(left,16),decode(right,16))

def txhash(tx,hashcode=None):
    if re.match('^[0-9a-fA-F]*$',tx):
        tx = changebase(tx,16,256)
    if hashcode: return dbl_sha256(tx + encode(int(hashcode),256,4)[::-1])
    else: return bin_dbl_sha256(tx)[::-1].encode('hex')

def bin_txhash(tx,hashcode=None):
    return txhash(tx,hashcode).decode('hex')

def ecdsa_tx_sign(tx,priv,hashcode=SIGHASH_ALL):
    rawsig = ecdsa_raw_sign(bin_txhash(tx,hashcode),priv)
    return der_encode_sig(*rawsig)+encode(hashcode,16,2)

def ecdsa_tx_verify(tx,sig,pub,hashcode=SIGHASH_ALL):
    return ecdsa_raw_verify(bin_txhash(tx,hashcode),der_decode_sig(sig),pub)

def ecdsa_tx_recover(tx,sig,hashcode=SIGHASH_ALL):
    z = bin_txhash(tx,hashcode)
    _,r,s = der_decode_sig(sig)
    left = ecdsa_raw_recover(z,(0,r,s))
    right = ecdsa_raw_recover(z,(1,r,s))
    return (encode_pubkey(left,'hex'), encode_pubkey(right,'hex'))

### Scripts

def mk_pubkey_script(addr): # Keep the auxiliary functions around for altcoins' sake
    return '76a914' + b58check_to_hex(addr) + '88ac'

def mk_scripthash_script(addr):
    return 'a914' + b58check_to_hex(addr) + '87'

# Address representation to output script
def address_to_script(addr):
    if addr[0] == '3': return mk_scripthash_script(addr)
    else: return mk_pubkey_script(addr)

# Output script to address representation
def script_to_address(script,vbyte=0):
    if re.match('^[0-9a-fA-F]*$',script):
        script = script.decode('hex')
    if script[:3] == '\x76\xa9\x14' and script[-2:] == '\x88\xac' and len(script) == 25:
        return bin_to_b58check(script[3:-2],vbyte) # pubkey hash addresses
    else:
        return bin_to_b58check(script[2:-1],5) # BIP0016 scripthash addresses

def p2sh_scriptaddr(script):
    if re.match('^[0-9a-fA-F]*$',script): script = script.decode('hex')
    return hex_to_b58check(hash160(script),5)
scriptaddr = p2sh_scriptaddr

def deserialize_script(script):
    if re.match('^[0-9a-fA-F]*$',script):
        return json_changebase(deserialize_script(script.decode('hex')),lambda x:x.encode('hex'))
    out, pos = [], 0
    while pos < len(script):
        code = ord(script[pos])
        if code == 0:
            out.append(None)
            pos += 1
        elif code <= 75:
            out.append(script[pos+1:pos+1+code])
            pos += 1 + code
        elif code <= 78:
            szsz = pow(2,code - 76)
            sz = decode(script[pos + szsz : pos : -1],256)
            out.append(script[pos + 1 + szsz:pos + 1 + szsz + sz])
            pos += 1 + szsz + sz
        elif code <= 96:
            out.append(code - 80)
            pos += 1
        else:
            out.append(code)
            pos += 1
    return out

def serialize_script_unit(unit):
    if isinstance(unit,int):
        if unit < 16: return chr(unit + 80)
        else: return chr(unit)
    elif unit is None:
        return '\x00'
    else:
        if len(unit) <= 75: return chr(len(unit))+unit
        elif len(unit) < 256: return chr(76)+chr(len(unit))+unit
        elif len(unit) < 65536: return chr(77)+encode(len(unit),256,2)[::-1]+unit
        else: return chr(78)+encode(len(unit),256,4)[::-1]+unit

def serialize_script(script):
    if json_is_base(script,16):
        return serialize_script(json_changebase(script,lambda x:x.decode('hex'))).encode('hex')
    return ''.join(map(serialize_script_unit,script))

def mk_multisig_script(*args): # [pubs],k,n or pub1,pub2...pub[n],k,n
    if len(args) == 3: pubs, k, n = args[0], int(args[1]), int(args[2])
    else: pubs, k, n = list(args[:-2]), int(args[-2]), int(args[-1])
    return serialize_script([k]+pubs+[n,174])

### Signing and verifying

def verify_tx_input(tx,i,script,sig,pub):
    if re.match('^[0-9a-fA-F]*$',tx): tx = tx.decode('hex')
    if re.match('^[0-9a-fA-F]*$',script): script = script.decode('hex')
    if not re.match('^[0-9a-fA-F]*$',sig): sig = sig.encode('hex')
    hashcode = ord(sig[-1])
    modtx = signature_form(tx,int(i),script)
    return ecdsa_tx_verify(modtx,sig,pub)

def sign(tx,i,priv):
    i = int(i)
    if not re.match('^[0-9a-fA-F]*$',tx):
        return sign(tx.encode('hex'),i,priv).decode('hex')
    if len(priv) <= 33: priv = priv.encode('hex')
    pub = privkey_to_pubkey(priv)
    address = pubkey_to_address(pub)
    signing_tx = signature_form(tx,i,mk_pubkey_script(address))
    sig = ecdsa_tx_sign(signing_tx,priv)
    txobj = deserialize(tx)
    txobj["ins"][i]["script"] = serialize_script([sig,pub])
    return serialize(txobj)

def multisign(tx,i,script,pk):
    if re.match('^[0-9a-fA-F]*$',tx): tx = tx.decode('hex')
    if re.match('^[0-9a-fA-F]*$',script): script = script.decode('hex')
    modtx = signature_form(tx,i,script)
    return ecdsa_tx_sign(modtx,pk)

def apply_multisignatures(*args): # tx,i,script,sigs OR tx,i,script,sig1,sig2...,sig[n]
    tx, i, script = args[0], int(args[1]), args[2]
    sigs = args[3] if isinstance(args[3],list) else list(args[3:])

    if re.match('^[0-9a-fA-F]*$',script): script = script.decode('hex')
    sigs = [x.decode('hex') if x[:2] == '30' else x for x in sigs]
    if re.match('^[0-9a-fA-F]*$',tx):
        return apply_multisignatures(tx.decode('hex'),i,script,sigs).encode('hex')

    txobj = deserialize(tx)
    txobj["ins"][i]["script"] = serialize_script([None]+sigs+[script])
    return serialize(txobj)

def mktx(*args): # [in0, in1...],[out0, out1...] or in0, in1 ... out0 out1 ...
    if isinstance(args[0],list): ins, outs = args[0], args[1]
    else:
        def is_inp(arg): return len(arg) > 64 or "output" in arg or "outpoint" in arg
        ins, outs = filter(is_inp, args), filter(lambda x: not is_inp(x), args)

    txobj = { "locktime" : 0, "version" : 1,"ins" : [], "outs" : [] }
    for i in ins:
        if isinstance(i,dict) and "outpoint" in i:
            txobj["ins"].append(i)
        else:
            if isinstance(i,dict) and "output" in i: i = i["output"]
            txobj["ins"].append({
                "outpoint" : { "hash": i[:64], "index": int(i[65:]) },
                "script": "",
                "sequence": 4294967295
            })
    for o in outs:
        if isinstance(o,str): o = {
            "address": o[:o.find(':')],
            "value": int(o[o.find(':')+1:])
        }
        txobj["outs"].append({
            "script": address_to_script(o["address"]),
            "value": o["value"]
        })
    return serialize(txobj)
<|endoftext|>


<commit_before>"""
  _______     _____ _        _
 |__   __|   / ____| |      (_)
    | |_   _| (___ | |_ _ __ _ _ __   __ _ ___
    | | | | |\___ \| __| '__| | '_ \ / _` / __|
    | | |_| |____) | |_| |  | | | | | (_| \__ \
    |_|\__, |_____/ \__|_|  |_|_| |_|\__, |___/
        __/ |                         __/ |
       |___/                         |___/

See:
https://packaging.python.org/en/latest/distributing.html
"""

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

setup(
    name='tystrings',

    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/single_source_version.html
    version=__import__('tystrings').__version__,

    description='strings file generation tool for iOS',
    # long_description=long_description,

    # The project's main homepage.
    url='https://github.com/luckytianyiyan/TyStrings',

    # Author details
    author='luckytianyiyan',
    author_email='luckytianyiyan@gmail.com',

    # Choose your license
    license='MIT',

    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        'Development Status :: 3 - Alpha',

        # Indicate who your project is intended for
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Internationalization',
        'Topic :: Software Development :: Interpreters',

        # Pick your license as you wish (should match "license" above)
        'License :: OSI Approved :: MIT License',

        # Specify the Python versions you support here. In particular, ensure
        # that you indicate whether you support Python 2, Python 3 or both.
        'Programming Language :: Python :: 2.7',

        'Operating System :: MacOS :: MacOS X',
    ],

    # What does your project relate to?
    keywords='i18n iOS localization internationalization strings .strings',

    # You can just specify the packages manually here if your project is
    # simple. Or you can use find_packages().
    # packages=find_packages(exclude=['contrib', 'docs', 'tests']),
    packages=find_packages(),

    # Alternatively, if you want to distribute just a my_module.py, uncomment
    # this:
    #   py_modules=["my_module"],

    # List run-time dependencies here.  These will be installed by pip when
    # your project is installed. For an analysis of "install_requires" vs pip's
    # requirements files see:
    # https://packaging.python.org/en/latest/requirements.html
    # install_requires=['peppercorn'],

    # List additional groups of dependencies here (e.g. development
    # dependencies). You can install these using the following syntax,
    # for example:
    # $ pip install -e .[dev,test]
    # extras_require={
    #     'dev': ['check-manifest'],
    #     'test': ['coverage'],
    # },

    # If there are data files included in your packages that need to be
    # installed, specify them here.  If using Python 2.6 or less, then these
    # have to be included in MANIFEST.in as well.
    # package_data={
    #     'sample': ['package_data.dat'],
    # },

    # Although 'package_data' is the preferred approach, in some case you may
    # need to place data files outside of your packages. See:
    # http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files # noqa
    # In this case, 'data_file' will be installed into '<sys.prefix>/my_data'
    # data_files=[('my_data', ['data/data_file'])],

    # To provide executable scripts, use entry points in preference to the
    # "scripts" keyword. Entry points provide cross-platform support and allow
    # pip to create the appropriate form of executable for the target platform.
    entry_points={
        'console_scripts': [
            'tystrings=tystrings:main',
        ],
    },
)
<commit_msg>M setup.py add long description<commit_after>"""
  _______     _____ _        _
 |__   __|   / ____| |      (_)
    | |_   _| (___ | |_ _ __ _ _ __   __ _ ___
    | | | | |\___ \| __| '__| | '_ \ / _` / __|
    | | |_| |____) | |_| |  | | | | | (_| \__ \
    |_|\__, |_____/ \__|_|  |_|_| |_|\__, |___/
        __/ |                         __/ |
       |___/                         |___/

See:
https://packaging.python.org/en/latest/distributing.html
"""

# Always prefer setuptools over distutils
from setuptools import setup, find_packages
# To use a consistent encoding
from codecs import open
from os import path

here = path.abspath(path.dirname(__file__))

with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
    long_description = f.read()

setup(
    name='tystrings',

    # Versions should comply with PEP440.  For a discussion on single-sourcing
    # the version across setup.py and the project code, see
    # https://packaging.python.org/en/latest/single_source_version.html
    version=__import__('tystrings').__version__,

    description='strings file generation tool for iOS',
    long_description=long_description,

    # The project's main homepage.
    url='https://github.com/luckytianyiyan/TyStrings',

    # Author details
    author='luckytianyiyan',
    author_email='luckytianyiyan@gmail.com',

    # Choose your license
    license='MIT',

    # See https://pypi.python.org/pypi?%3Aaction=list_classifiers
    classifiers=[
        # How mature is this project? Common values are
        #   3 - Alpha
        #   4 - Beta
        #   5 - Production/Stable
        'Development Status :: 3 - Alpha',

        # Indicate who your project is intended for
        'Intended Audience :: Developers',
        'Topic :: Software Development :: Internationalization',
        'Topic :: Software Development :: Interpreters',

        # Pick your license as you wish (should match "license" above)
        'License :: OSI Approved :: MIT License',

        # Specify the Python versions you support here. In particular, ensure
        # that you indicate whether you support Python 2, Python 3 or both.
        'Programming Language :: Python :: 2.7',

        'Operating System :: MacOS :: MacOS X',
    ],

    # What does your project relate to?
    keywords='i18n iOS localization internationalization strings .strings',

    # You can just specify the packages manually here if your project is
    # simple. Or you can use find_packages().
    # packages=find_packages(exclude=['contrib', 'docs', 'tests']),
    packages=find_packages(exclude=['tests']),

    # Alternatively, if you want to distribute just a my_module.py, uncomment
    # this:
    #   py_modules=["my_module"],

    # List run-time dependencies here.  These will be installed by pip when
    # your project is installed. For an analysis of "install_requires" vs pip's
    # requirements files see:
    # https://packaging.python.org/en/latest/requirements.html
    # install_requires=['peppercorn'],

    # List additional groups of dependencies here (e.g. development
    # dependencies). You can install these using the following syntax,
    # for example:
    # $ pip install -e .[dev,test]
    # extras_require={
    #     'dev': ['check-manifest'],
    #     'test': ['coverage'],
    # },

    # If there are data files included in your packages that need to be
    # installed, specify them here.  If using Python 2.6 or less, then these
    # have to be included in MANIFEST.in as well.
    # package_data={
    #     'sample': ['package_data.dat'],
    # },

    # Although 'package_data' is the preferred approach, in some case you may
    # need to place data files outside of your packages. See:
    # http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files # noqa
    # In this case, 'data_file' will be installed into '<sys.prefix>/my_data'
    # data_files=[('my_data', ['data/data_file'])],

    # To provide executable scripts, use entry points in preference to the
    # "scripts" keyword. Entry points provide cross-platform support and allow
    # pip to create the appropriate form of executable for the target platform.
    entry_points={
        'console_scripts': [
            'tystrings=tystrings:main',
        ],
    },
)
<|endoftext|>


<commit_before># -*- coding: utf8 -*-
"""
pybossa-discourse
-----------------

A PyBossa plugin for Discourse integration.
"""

import os
import json
from flask import current_app as app
from flask.ext.plugins import Plugin
from .client import DiscourseClient
from .sso import DiscourseSSO

__plugin__ = "PyBossaDiscourse"
__version__ = json.load(open(os.path.join(os.path.dirname(__file__),
                                          'info.json')))['version']

DISCOURSE_SETTINGS = ('DISCOURSE_API_KEY',
                      'DISCOURSE_API_USERNAME',
                      'DISCOURSE_SECRET',
                      'DISCOURSE_URL')

class PyBossaDiscourse(Plugin):
    """PyBossa Discourse plugin class."""

    def setup(self):
        """Setup the plugin."""

        # Check all settings are provided
        for setting in DISCOURSE_SETTINGS:
            try:
                app.config[setting]
            except KeyError:
                raise ValueError('{} setting not found.'.format(setting))

        # Application specific state
        app.extensions['discourse'] = {'client': None, 'sso': None}

        self.setup_client()
        self.setup_sso()
        self.setup_global_envar()
        self.setup_blueprint()

    def setup_client(self):
        """Setup the Discourse client."""
        app.extensions['discourse']['client'] = DiscourseClient(app)

    def setup_sso(self):
        """Setup Discourse SSO."""
        app.extensions['discourse']['sso'] = DiscourseSSO(app)

    def setup_global_envar(self):
        """Setup global environment variable."""
        client = app.extensions['discourse']['client']
        app.jinja_env.globals.update(discourse=client)

    def setup_blueprint(self):
        """Setup blueprint."""
        from .blueprint import DiscourseBlueprint
        blueprint = DiscourseBlueprint()
        app.register_blueprint(blueprint, url_prefix="/discourse")
<commit_msg>Disable plugin if settings not found<commit_after># -*- coding: utf8 -*-
"""
pybossa-discourse
-----------------

A PyBossa plugin for Discourse integration.
"""

import os
import json
from flask import current_app as app
from flask.ext.plugins import Plugin
from .client import DiscourseClient
from .sso import DiscourseSSO

__plugin__ = "PyBossaDiscourse"
__version__ = json.load(open(os.path.join(os.path.dirname(__file__),
                                          'info.json')))['version']

DISCOURSE_SETTINGS = ('DISCOURSE_API_KEY',
                      'DISCOURSE_API_USERNAME',
                      'DISCOURSE_SECRET',
                      'DISCOURSE_URL')

class PyBossaDiscourse(Plugin):
    """PyBossa Discourse plugin class."""

    def setup(self):
        """Setup the plugin."""

        # Check all settings are provided
        for setting in DISCOURSE_SETTINGS:
            try:
                app.config[setting]
            except KeyError as inst:
                msg = "PyBossa Discourse disabled"
                print type(inst)
                print inst.args
                print inst
                print msg
                log_message = '{0}: {1}'.format(msg, str(inst))
                app.logger.info(log_message)
                self.disable()
                return

        # Application specific state
        app.extensions['discourse'] = {'client': None, 'sso': None}

        self.setup_client()
        self.setup_sso()
        self.setup_global_envar()
        self.setup_blueprint()

    def setup_client(self):
        """Setup the Discourse client."""
        app.extensions['discourse']['client'] = DiscourseClient(app)

    def setup_sso(self):
        """Setup Discourse SSO."""
        app.extensions['discourse']['sso'] = DiscourseSSO(app)

    def setup_global_envar(self):
        """Setup global environment variable."""
        client = app.extensions['discourse']['client']
        app.jinja_env.globals.update(discourse=client)

    def setup_blueprint(self):
        """Setup blueprint."""
        from .blueprint import DiscourseBlueprint
        blueprint = DiscourseBlueprint()
        app.register_blueprint(blueprint, url_prefix="/discourse")
<|endoftext|>


<commit_before>#!/usr/bin/python
# -* encoding: utf-8 *-

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import re
import sys

from distutils.core import setup
from setuptools import find_packages

_package_root = "src/py"
_root_package = 'gopythongo'
_HERE = os.path.abspath(os.path.dirname(__file__))

with open("src/py/gopythongo/__init__.py", "rt", encoding="utf-8") as vf:
    lines = vf.readlines()

_version = "0.0.0+local"
for l in lines:
    m = re.match("version = \"(.*?)\"", l)
    if m:
        _version = m.group(1)

_packages = find_packages(_package_root, exclude=["*.tests", "*.tests.*", "tests.*", "tests"])

_requirements = [
    'Jinja2==2.10.1',
    'ConfigArgParse==0.14.0',
    'Sphinx==2.1.2',
    'sphinx-rtd-theme==0.4.3',
    'colorama==0.4.1',
    'semantic-version==2.6.0',
    'packaging==19.0',
    'typing==3.7.4',
    'hvac==0.9.3',
    'docker-py==1.10.6',
    'dockerpty==0.4.1',
    'pyopenssl==19.0.0',
    'bumpversion==0.5.3',
    'aptly-api-client==0.2.0',
]

if sys.version_info.major < 3 or (sys.version_info.major == 3 and sys.version_info.minor < 3):
    _requirements.append('backports.shutil_get_terminal_size==1.0.0')

try:
    long_description = open(os.path.join(_HERE, 'README.md')).read()
except IOError:
    long_description = None

setup(
    name='gopythongo',
    version=_version,
    packages=_packages,
    package_dir={
        '': _package_root,
    },
    entry_points={
        "console_scripts": [
            "gopythongo = gopythongo.main:main",
            "vaultwrapper = gopythongo.vaultwrapper:main",
            "vaultgetcert = gopythongo.vaultgetcert:main",
        ]
    },
    install_requires=_requirements,
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "Intended Audience :: System Administrators",
        "Environment :: Console",
        "Programming Language :: Python :: 3 :: Only",
        "License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)",
        "Operating System :: POSIX",
    ],
    author="Jonas Maurus (@jdelic)",
    author_email="jonas@gopythongo.com",
    maintainer="GoPythonGo.com",
    maintainer_email="info@gopythongo.com",
    description="Build shippable virtualenvs",
    long_description=long_description,
)
<commit_msg>[requires.io] dependency update<commit_after>#!/usr/bin/python
# -* encoding: utf-8 *-

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import re
import sys

from distutils.core import setup
from setuptools import find_packages

_package_root = "src/py"
_root_package = 'gopythongo'
_HERE = os.path.abspath(os.path.dirname(__file__))

with open("src/py/gopythongo/__init__.py", "rt", encoding="utf-8") as vf:
    lines = vf.readlines()

_version = "0.0.0+local"
for l in lines:
    m = re.match("version = \"(.*?)\"", l)
    if m:
        _version = m.group(1)

_packages = find_packages(_package_root, exclude=["*.tests", "*.tests.*", "tests.*", "tests"])

_requirements = [
    'Jinja2==2.10.1',
    'ConfigArgParse==0.14.0',
    'Sphinx==2.1.2',
    'sphinx-rtd-theme==0.4.3',
    'colorama==0.4.1',
    'semantic-version==2.6.0',
    'packaging==19.0',
    'typing==3.7.4',
    'hvac==0.9.4',
    'docker-py==1.10.6',
    'dockerpty==0.4.1',
    'pyopenssl==19.0.0',
    'bumpversion==0.5.3',
    'aptly-api-client==0.2.0',
]

if sys.version_info.major < 3 or (sys.version_info.major == 3 and sys.version_info.minor < 3):
    _requirements.append('backports.shutil_get_terminal_size==1.0.0')

try:
    long_description = open(os.path.join(_HERE, 'README.md')).read()
except IOError:
    long_description = None

setup(
    name='gopythongo',
    version=_version,
    packages=_packages,
    package_dir={
        '': _package_root,
    },
    entry_points={
        "console_scripts": [
            "gopythongo = gopythongo.main:main",
            "vaultwrapper = gopythongo.vaultwrapper:main",
            "vaultgetcert = gopythongo.vaultgetcert:main",
        ]
    },
    install_requires=_requirements,
    classifiers=[
        "Development Status :: 4 - Beta",
        "Intended Audience :: Developers",
        "Intended Audience :: System Administrators",
        "Environment :: Console",
        "Programming Language :: Python :: 3 :: Only",
        "License :: OSI Approved :: Mozilla Public License 2.0 (MPL 2.0)",
        "Operating System :: POSIX",
    ],
    author="Jonas Maurus (@jdelic)",
    author_email="jonas@gopythongo.com",
    maintainer="GoPythonGo.com",
    maintainer_email="info@gopythongo.com",
    description="Build shippable virtualenvs",
    long_description=long_description,
)
<|endoftext|>


<commit_before>#!/usr/bin/env python

# Copyright (c) 2001, MetaSlash Inc.  All rights reserved.

"""
Configuration information for checker.
"""

import sys
import os
import getopt
import string
import re

_RC_FILE = ".pycheckrc"
CHECKER_VAR = '__pychecker__'

_DEFAULT_BLACK_LIST = [ "Tkinter", "wxPython", "gtk", "GTK", "GDK", ]
_DEFAULT_VARIABLE_IGNORE_LIST = [ '__version__', '__warningregistry__',
                                  '__all__', ]
_DEFAULT_UNUSED_LIST = [ '_', 'empty', 'unused', 'dummy', ]

_OPTIONS = [
 ('e', 0, 'errors', None, 'turn off all warnings which are not likely errors'),
 ('s', 0, 'doc', None, 'turn off all warnings for no doc strings'),
 ('m', 0, 'moduledoc', 'noDocModule', 'no module doc strings'),
 ('c', 0, 'classdoc', 'noDocClass', 'no class doc strings'),
 ('f', 0, 'funcdoc', 'noDocFunc', 'no function/method doc strings'),
 None,
 ('i', 0, 'import', 'importUsed', 'unused imports'),
 ('k', 0, 'pkgimport', 'packageImportUsed', 'unused imports from __init__.py'),
 ('M', 0, 'reimportself', 'reimportSelf', 'module imports itself'),
 ('x', 0, 'miximport', 'mixImport', 'module does import and from ... import'),
 ('l', 0, 'local', 'localVariablesUsed', 'unused local variables, except tuples'),
 ('t', 0, 'tuple', 'unusedLocalTuple', 'all unused local variables, including tuples'),
 ('v', 0, 'var', 'allVariablesUsed', 'all unused module variables'),
 ('p', 0, 'privatevar', 'privateVariableUsed', 'unused private module variables'),
 ('g', 0, 'allglobals', 'reportAllGlobals', 'report each occurrence of global warnings'),
 ('n', 0, 'namedargs', 'namedArgs', 'functions called with named arguments (like keywords)'),
 ('a', 0, 'initattr', 'onlyCheckInitForMembers', 'Attributes (members) must be defined in __init__()'),
 ('I', 0, 'initsubclass', 'initDefinedInSubclass', 'Subclass.__init__() not defined'),
 ('u', 0, 'callinit', 'baseClassInitted', 'Subclass.__init__() not called'),
 ('N', 0, 'initreturn', 'returnNoneFromInit', 'Return None from __init__()'),
 ('A', 0, 'callattr', 'callingAttribute', 'Calling data members as functions'),
 ('y', 0, 'classattr', 'classAttrExists', 'class attribute does not exist'),
 ('S', 1, 'self', 'methodArgName', 'First argument to methods'),
 ('T', 0, 'argsused', 'argumentsUsed', 'unused method/function arguments'),
 ('G', 0, 'selfused', 'ignoreSelfUnused', 'ignore if self is unused in methods'),
 ('o', 0, 'override', 'checkOverridenMethods', 'check if overridden methods have the same signature'),
 ('U', 0, 'reuseattr', 'redefiningFunction', 'check if function/class/method names are reused'),
 None,
 ('r', 0, 'returnvalues', 'checkReturnValues', 'check consistent return values'),
 ('C', 0, 'implicitreturns', 'checkImplicitReturns', 'check if using implict and explicit return values'),
 ('O', 0, 'objattrs', 'checkObjectAttrs', 'check that attributes of objects exist'),
 ('D', 0, 'intdivide', 'intDivide', 'check if using integer division'),
 None,
 ('q', 0, 'stdlib', 'ignoreStandardLibrary', 'ignore warnings from files under standard library'),
 ('b', 1, 'blacklist', 'blacklist', 'ignore warnings from the list of modules\n\t\t\t'),
 ('V', 1, 'varlist', 'variablesToIgnore', 'ignore global variables not used if name is one of\n\t\t\t'),
 ('E', 1, 'unusednames', 'unusedNames', 'ignore unused locals/arguments if name is one of\n\t\t\t'),
 ('L', 1, 'maxlines', 'maxLines', 'maximum lines in a function'),
 ('B', 1, 'maxbranches', 'maxBranches', 'maximum branches in a function'),
 ('R', 1, 'maxreturns', 'maxReturns', 'maximum returns in a function'),
 ('J', 1, 'maxargs', 'maxArgs', 'maximum # of arguments to a function'),
 ('K', 1, 'maxlocals', 'maxLocals', 'maximum # of locals in a function'),
 None,
 ('F', 0, 'rcfile', None, 'print a .pycheckrc file generated from command line args'),
 ('P', 0, 'printparse', 'printParse', 'print internal checker parse structures'),
 ('d', 0, 'debug', 'debug', 'turn on debugging for checker'),
 ('Q', 0, 'quiet', None, 'turn off all output except warnings'),
 None,
]

def init() :
    GET_OPT_VALUE = [ ('', ''), (':', '='), ]
    shortArgs, longArgs = "", []
    for opt in _OPTIONS :
        if opt != None :
            optStr = GET_OPT_VALUE[opt[1]]
            shortArgs = shortArgs + opt[0] + optStr[0]
            longArgs.append(opt[2] + optStr[1])
            longArgs.append('no-' + opt[2] + optStr[1])

    options = {}
    for opt in _OPTIONS :
        if opt != None :
            shortArg, useValue, longArg, member, description = opt
            options['-' + shortArg] = opt
            options['--no-' + longArg] = options['--' + longArg] = opt

    return shortArgs, longArgs, options

_SHORT_ARGS, _LONG_ARGS, _OPTIONS_DICT = init()

def _getRCfile(filename) :
    """Return the .rc filename, on Windows use the current directory
                                on UNIX use the user's home directory"""

    # FIXME: this is really cheating, but should work for now
    home = os.environ.get('HOME')
    if home :
        filename = home + os.sep + filename
    return filename

_RC_FILE_HEADER = '''#
# .pycheckrc file created by PyChecker %s
#
# It should be placed in your home directory (value of $HOME).
# If $HOME is not set, it will look in the current directory.
#

'''

def outputRc(cfg) :
    import time
    output = _RC_FILE_HEADER % time.ctime(time.time())
    for opt in _OPTIONS :
        if opt != None :
            shortArg, useValue, longArg, member, description = opt
            if member is None :
                continue
            description = string.strip(description)
            value = getattr(cfg, member)
            optStr = '# %s\n%s = %s\n\n' % (description, member, `value`)
            output = output + optStr

    return output

class UsageError(Exception) :
    """Exception to indicate that the application should exit due to
       command line usage error."""

_SUPPRESSIONS_ERR = \
'''\nWarning, error processing defaults file: %s
\%s must be a dictionary ({}) -- ignoring suppressions\n'''

def _getSuppressions(name, dict, filename) :
    suppressions = dict.get(name, {})
    if type(suppressions) != type({}) :
        print _SUPPRESSIONS_ERR % (filename, name)
        suppressions = {}
    return suppressions

class Config :
    "Hold configuration information"

    def __init__(self) :
        "Initialize configuration with default values."

        self.debug = 0
        self.quiet = 0
        self.onlyCheckInitForMembers = 0
        self.printParse = 0

        self.noDocModule = 0
        self.noDocClass = 0
        self.noDocFunc = 0

        self.reportAllGlobals = 0
        self.allVariablesUsed = 0
        self.privateVariableUsed = 1
        self.importUsed = 1
        self.reimportSelf = 1
        self.mixImport = 1
        self.packageImportUsed = 1
        self.localVariablesUsed = 1
        self.unusedLocalTuple = 0
        self.initDefinedInSubclass = 0
        self.baseClassInitted = 1
        self.callingAttribute = 0
        self.classAttrExists = 1
        self.namedArgs = 1
        self.returnNoneFromInit = 1

        self.unusedNames = _DEFAULT_UNUSED_LIST
        self.variablesToIgnore = _DEFAULT_VARIABLE_IGNORE_LIST
        self.blacklist = _DEFAULT_BLACK_LIST
        self.ignoreStandardLibrary = 0
        self.methodArgName = 'self'
        self.checkOverridenMethods = 1

        self.argumentsUsed = 1
        self.ignoreSelfUnused = 0
        self.redefiningFunction = 1

        self.maxLines = 200
        self.maxBranches = 50
        self.maxReturns = 10
        self.maxArgs = 10
        self.maxLocals = 40

        self.checkObjectAttrs = 1
        self.checkReturnValues = 1
        self.checkImplicitReturns = 0
        self.intDivide = 1

    def loadFile(self, filename) :
        suppressions = {}
        suppressionRegexs = {}
        try :
            tmpGlobal, dict = {}, {}
            execfile(filename, tmpGlobal, dict)
            for key, value in dict.items() :
                if self.__dict__.has_key(key) :
                    self.__dict__[key] = value
                elif key not in [ 'suppressions', 'suppressionRegexs' ] :
                    print "Warning, option (%s) doesn't exist, ignoring" % key

            suppressions = _getSuppressions('suppressions', dict, filename)
            regexs = _getSuppressions('suppressionRegexs', dict, filename)
            suppressionRegexs = {}
            for regex_str in regexs.keys() :
                regex = re.compile(regex_str)
                suppressionRegexs[regex] = regexs[regex_str]
        except IOError :
            pass       # ignore if no file
        except Exception, detail:
            print "Warning, error loading defaults file:", filename, detail
        return suppressions, suppressionRegexs

    def processArgs(self, argList) :
        try :
            args, files = getopt.getopt(argList, _SHORT_ARGS, _LONG_ARGS)
        except getopt.error, detail :
            raise UsageError, detail

        quiet = self.quiet
        for arg, value in args :
            shortArg, useValue, longArg, member, description = _OPTIONS_DICT[arg]
            if member == None :
                # FIXME: this whole block is a hack
                if longArg == 'printrc' :
                    sys.stdout.write(outputRc(self))
                    continue
                elif longArg == 'quiet' :
                    quiet = 1
                    continue

                self.noDocModule = 0
                self.noDocClass = 0
                self.noDocFunc = 0
                if longArg == 'errors' :
                    self.__dict__.update(errors_only())
            elif value  :
                newValue = value
                memberType = type(getattr(self, member))
                if memberType == type(0) :
                    newValue = int(newValue)
                elif memberType == type([]) :
                    newValue = string.split(newValue, ',')
                setattr(self, member, newValue)
            elif arg[0:5] == '--no-' :
                setattr(self, member, 0)
            elif arg[0:2] == '--' :
                setattr(self, member, 1)
            else :
                # for shortArgs we only toggle
                setattr(self, member, not getattr(self, member))

        self.quiet = quiet
        if self.variablesToIgnore.count(CHECKER_VAR) <= 0 :
            self.variablesToIgnore.append(CHECKER_VAR)

        return files

def errors_only() :
    "Return {} of Config with all warnings turned off"
    dict = Config().__dict__
    for k, v in dict.items() :
        if type(v) == type(0) :
            dict[k] = 0
    return dict

def printArg(shortArg, longArg, description, defaultValue, useValue) :
    defStr = ''
    if defaultValue != None :
        if not useValue :
            if defaultValue :
                defaultValue = 'on'
            else :
                defaultValue = 'off'
        defStr = ' [%s]' % defaultValue
    args = "-%s, --%s" % (shortArg, longArg)
    print "  %-18s %s%s" % (args, description, defStr)

def usage(cfg = None) :
    print "Usage for: checker.py [options] PACKAGE ...\n"
    print "    PACKAGEs can be a python package, module or filename\n"
    print "Long options can be preceded with no- to turn off (e.g., no-namedargs)\n"
    print "Options:           Change warning for ... [default value]"

    if cfg is None :
        cfg = Config()
    for opt in _OPTIONS :
        if opt == None :
            print ""
            continue

        shortArg, useValue, longArg, member, description = opt
        defValue = None
        if member != None :
            defValue = cfg.__dict__[member]

        printArg(shortArg, longArg, description, defValue, useValue)

def setupFromArgs(argList) :
    "Returns (Config, [ file1, file2, ... ]) from argList"

    cfg = Config()
    try :
        suppressions = cfg.loadFile(_getRCfile(_RC_FILE))
        return cfg, cfg.processArgs(argList), suppressions
    except UsageError :
        usage(cfg)
        raise
<commit_msg>don't need to re-init suppressionRegexs<commit_after>#!/usr/bin/env python

# Copyright (c) 2001, MetaSlash Inc.  All rights reserved.

"""
Configuration information for checker.
"""

import sys
import os
import getopt
import string
import re

_RC_FILE = ".pycheckrc"
CHECKER_VAR = '__pychecker__'

_DEFAULT_BLACK_LIST = [ "Tkinter", "wxPython", "gtk", "GTK", "GDK", ]
_DEFAULT_VARIABLE_IGNORE_LIST = [ '__version__', '__warningregistry__',
                                  '__all__', ]
_DEFAULT_UNUSED_LIST = [ '_', 'empty', 'unused', 'dummy', ]

_OPTIONS = [
 ('e', 0, 'errors', None, 'turn off all warnings which are not likely errors'),
 ('s', 0, 'doc', None, 'turn off all warnings for no doc strings'),
 ('m', 0, 'moduledoc', 'noDocModule', 'no module doc strings'),
 ('c', 0, 'classdoc', 'noDocClass', 'no class doc strings'),
 ('f', 0, 'funcdoc', 'noDocFunc', 'no function/method doc strings'),
 None,
 ('i', 0, 'import', 'importUsed', 'unused imports'),
 ('k', 0, 'pkgimport', 'packageImportUsed', 'unused imports from __init__.py'),
 ('M', 0, 'reimportself', 'reimportSelf', 'module imports itself'),
 ('x', 0, 'miximport', 'mixImport', 'module does import and from ... import'),
 ('l', 0, 'local', 'localVariablesUsed', 'unused local variables, except tuples'),
 ('t', 0, 'tuple', 'unusedLocalTuple', 'all unused local variables, including tuples'),
 ('v', 0, 'var', 'allVariablesUsed', 'all unused module variables'),
 ('p', 0, 'privatevar', 'privateVariableUsed', 'unused private module variables'),
 ('g', 0, 'allglobals', 'reportAllGlobals', 'report each occurrence of global warnings'),
 ('n', 0, 'namedargs', 'namedArgs', 'functions called with named arguments (like keywords)'),
 ('a', 0, 'initattr', 'onlyCheckInitForMembers', 'Attributes (members) must be defined in __init__()'),
 ('I', 0, 'initsubclass', 'initDefinedInSubclass', 'Subclass.__init__() not defined'),
 ('u', 0, 'callinit', 'baseClassInitted', 'Subclass.__init__() not called'),
 ('N', 0, 'initreturn', 'returnNoneFromInit', 'Return None from __init__()'),
 ('A', 0, 'callattr', 'callingAttribute', 'Calling data members as functions'),
 ('y', 0, 'classattr', 'classAttrExists', 'class attribute does not exist'),
 ('S', 1, 'self', 'methodArgName', 'First argument to methods'),
 ('T', 0, 'argsused', 'argumentsUsed', 'unused method/function arguments'),
 ('G', 0, 'selfused', 'ignoreSelfUnused', 'ignore if self is unused in methods'),
 ('o', 0, 'override', 'checkOverridenMethods', 'check if overridden methods have the same signature'),
 ('U', 0, 'reuseattr', 'redefiningFunction', 'check if function/class/method names are reused'),
 None,
 ('r', 0, 'returnvalues', 'checkReturnValues', 'check consistent return values'),
 ('C', 0, 'implicitreturns', 'checkImplicitReturns', 'check if using implict and explicit return values'),
 ('O', 0, 'objattrs', 'checkObjectAttrs', 'check that attributes of objects exist'),
 ('D', 0, 'intdivide', 'intDivide', 'check if using integer division'),
 None,
 ('q', 0, 'stdlib', 'ignoreStandardLibrary', 'ignore warnings from files under standard library'),
 ('b', 1, 'blacklist', 'blacklist', 'ignore warnings from the list of modules\n\t\t\t'),
 ('V', 1, 'varlist', 'variablesToIgnore', 'ignore global variables not used if name is one of\n\t\t\t'),
 ('E', 1, 'unusednames', 'unusedNames', 'ignore unused locals/arguments if name is one of\n\t\t\t'),
 ('L', 1, 'maxlines', 'maxLines', 'maximum lines in a function'),
 ('B', 1, 'maxbranches', 'maxBranches', 'maximum branches in a function'),
 ('R', 1, 'maxreturns', 'maxReturns', 'maximum returns in a function'),
 ('J', 1, 'maxargs', 'maxArgs', 'maximum # of arguments to a function'),
 ('K', 1, 'maxlocals', 'maxLocals', 'maximum # of locals in a function'),
 None,
 ('F', 0, 'rcfile', None, 'print a .pycheckrc file generated from command line args'),
 ('P', 0, 'printparse', 'printParse', 'print internal checker parse structures'),
 ('d', 0, 'debug', 'debug', 'turn on debugging for checker'),
 ('Q', 0, 'quiet', None, 'turn off all output except warnings'),
 None,
]

def init() :
    GET_OPT_VALUE = [ ('', ''), (':', '='), ]
    shortArgs, longArgs = "", []
    for opt in _OPTIONS :
        if opt != None :
            optStr = GET_OPT_VALUE[opt[1]]
            shortArgs = shortArgs + opt[0] + optStr[0]
            longArgs.append(opt[2] + optStr[1])
            longArgs.append('no-' + opt[2] + optStr[1])

    options = {}
    for opt in _OPTIONS :
        if opt != None :
            shortArg, useValue, longArg, member, description = opt
            options['-' + shortArg] = opt
            options['--no-' + longArg] = options['--' + longArg] = opt

    return shortArgs, longArgs, options

_SHORT_ARGS, _LONG_ARGS, _OPTIONS_DICT = init()

def _getRCfile(filename) :
    """Return the .rc filename, on Windows use the current directory
                                on UNIX use the user's home directory"""

    # FIXME: this is really cheating, but should work for now
    home = os.environ.get('HOME')
    if home :
        filename = home + os.sep + filename
    return filename

_RC_FILE_HEADER = '''#
# .pycheckrc file created by PyChecker %s
#
# It should be placed in your home directory (value of $HOME).
# If $HOME is not set, it will look in the current directory.
#

'''

def outputRc(cfg) :
    import time
    output = _RC_FILE_HEADER % time.ctime(time.time())
    for opt in _OPTIONS :
        if opt != None :
            shortArg, useValue, longArg, member, description = opt
            if member is None :
                continue
            description = string.strip(description)
            value = getattr(cfg, member)
            optStr = '# %s\n%s = %s\n\n' % (description, member, `value`)
            output = output + optStr

    return output

class UsageError(Exception) :
    """Exception to indicate that the application should exit due to
       command line usage error."""

_SUPPRESSIONS_ERR = \
'''\nWarning, error processing defaults file: %s
\%s must be a dictionary ({}) -- ignoring suppressions\n'''

def _getSuppressions(name, dict, filename) :
    suppressions = dict.get(name, {})
    if type(suppressions) != type({}) :
        print _SUPPRESSIONS_ERR % (filename, name)
        suppressions = {}
    return suppressions

class Config :
    "Hold configuration information"

    def __init__(self) :
        "Initialize configuration with default values."

        self.debug = 0
        self.quiet = 0
        self.onlyCheckInitForMembers = 0
        self.printParse = 0

        self.noDocModule = 0
        self.noDocClass = 0
        self.noDocFunc = 0

        self.reportAllGlobals = 0
        self.allVariablesUsed = 0
        self.privateVariableUsed = 1
        self.importUsed = 1
        self.reimportSelf = 1
        self.mixImport = 1
        self.packageImportUsed = 1
        self.localVariablesUsed = 1
        self.unusedLocalTuple = 0
        self.initDefinedInSubclass = 0
        self.baseClassInitted = 1
        self.callingAttribute = 0
        self.classAttrExists = 1
        self.namedArgs = 1
        self.returnNoneFromInit = 1

        self.unusedNames = _DEFAULT_UNUSED_LIST
        self.variablesToIgnore = _DEFAULT_VARIABLE_IGNORE_LIST
        self.blacklist = _DEFAULT_BLACK_LIST
        self.ignoreStandardLibrary = 0
        self.methodArgName = 'self'
        self.checkOverridenMethods = 1

        self.argumentsUsed = 1
        self.ignoreSelfUnused = 0
        self.redefiningFunction = 1

        self.maxLines = 200
        self.maxBranches = 50
        self.maxReturns = 10
        self.maxArgs = 10
        self.maxLocals = 40

        self.checkObjectAttrs = 1
        self.checkReturnValues = 1
        self.checkImplicitReturns = 0
        self.intDivide = 1

    def loadFile(self, filename) :
        suppressions = {}
        suppressionRegexs = {}
        try :
            tmpGlobal, dict = {}, {}
            execfile(filename, tmpGlobal, dict)
            for key, value in dict.items() :
                if self.__dict__.has_key(key) :
                    self.__dict__[key] = value
                elif key not in [ 'suppressions', 'suppressionRegexs' ] :
                    print "Warning, option (%s) doesn't exist, ignoring" % key

            suppressions = _getSuppressions('suppressions', dict, filename)
            regexs = _getSuppressions('suppressionRegexs', dict, filename)
            for regex_str in regexs.keys() :
                regex = re.compile(regex_str)
                suppressionRegexs[regex] = regexs[regex_str]
        except IOError :
            pass       # ignore if no file
        except Exception, detail:
            print "Warning, error loading defaults file:", filename, detail
        return suppressions, suppressionRegexs

    def processArgs(self, argList) :
        try :
            args, files = getopt.getopt(argList, _SHORT_ARGS, _LONG_ARGS)
        except getopt.error, detail :
            raise UsageError, detail

        quiet = self.quiet
        for arg, value in args :
            shortArg, useValue, longArg, member, description = _OPTIONS_DICT[arg]
            if member == None :
                # FIXME: this whole block is a hack
                if longArg == 'printrc' :
                    sys.stdout.write(outputRc(self))
                    continue
                elif longArg == 'quiet' :
                    quiet = 1
                    continue

                self.noDocModule = 0
                self.noDocClass = 0
                self.noDocFunc = 0
                if longArg == 'errors' :
                    self.__dict__.update(errors_only())
            elif value  :
                newValue = value
                memberType = type(getattr(self, member))
                if memberType == type(0) :
                    newValue = int(newValue)
                elif memberType == type([]) :
                    newValue = string.split(newValue, ',')
                setattr(self, member, newValue)
            elif arg[0:5] == '--no-' :
                setattr(self, member, 0)
            elif arg[0:2] == '--' :
                setattr(self, member, 1)
            else :
                # for shortArgs we only toggle
                setattr(self, member, not getattr(self, member))

        self.quiet = quiet
        if self.variablesToIgnore.count(CHECKER_VAR) <= 0 :
            self.variablesToIgnore.append(CHECKER_VAR)

        return files

def errors_only() :
    "Return {} of Config with all warnings turned off"
    dict = Config().__dict__
    for k, v in dict.items() :
        if type(v) == type(0) :
            dict[k] = 0
    return dict

def printArg(shortArg, longArg, description, defaultValue, useValue) :
    defStr = ''
    if defaultValue != None :
        if not useValue :
            if defaultValue :
                defaultValue = 'on'
            else :
                defaultValue = 'off'
        defStr = ' [%s]' % defaultValue
    args = "-%s, --%s" % (shortArg, longArg)
    print "  %-18s %s%s" % (args, description, defStr)

def usage(cfg = None) :
    print "Usage for: checker.py [options] PACKAGE ...\n"
    print "    PACKAGEs can be a python package, module or filename\n"
    print "Long options can be preceded with no- to turn off (e.g., no-namedargs)\n"
    print "Options:           Change warning for ... [default value]"

    if cfg is None :
        cfg = Config()
    for opt in _OPTIONS :
        if opt == None :
            print ""
            continue

        shortArg, useValue, longArg, member, description = opt
        defValue = None
        if member != None :
            defValue = cfg.__dict__[member]

        printArg(shortArg, longArg, description, defValue, useValue)

def setupFromArgs(argList) :
    "Returns (Config, [ file1, file2, ... ]) from argList"

    cfg = Config()
    try :
        suppressions = cfg.loadFile(_getRCfile(_RC_FILE))
        return cfg, cfg.processArgs(argList), suppressions
    except UsageError :
        usage(cfg)
        raise
<|endoftext|>


<commit_before>from setuptools import setup

setup(
  name = 'spotpy',
  version = '1.2.37',
  description = 'A Statistical Parameter Optimization Tool',

  author = 'Tobias Houska, Philipp Kraft, Alejandro Chamorro-Chavez and Lutz Breuer',
  author_email = 'tobias.houska@umwelt.uni-giessen.de',
  url = 'http://www.uni-giessen.de/cms/faculties/f09/institutes/ilr/hydro/download/spotpy',
  #download_url = 'svn://fb09-pasig.umwelt.uni-giessen.de/spotpy/trunk/',
  license = 'MIT',
  packages = ["spotpy", "spotpy.examples", "spotpy.algorithms", "spotpy.parallel","spotpy.tools"],
  include_package_data = True,
  use_2to3 = True,
  keywords = ['Monte Carlo', 'MCMC','MLE', 'SCE-UA', 'Simulated Annealing', 'DE-MCz', 'ROPE', 'Artifical Bee Colony', 'Uncertainty', 'Calibration', 'Model', 'HydroIndicies'],
  classifiers = [
        'Development Status :: 4 - Beta',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3.2',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Topic :: Software Development :: Libraries :: Python Modules'],
        )<commit_msg>Update setup.py<commit_after>from setuptools import setup

setup(
  name = 'spotpy',
  version = '1.2.37',
  description = 'A Statistical Parameter Optimization Tool',

  author = 'Tobias Houska, Philipp Kraft, Alejandro Chamorro-Chavez and Lutz Breuer',
  author_email = 'tobias.houska@umwelt.uni-giessen.de',
  url = 'http://www.uni-giessen.de/cms/faculties/f09/institutes/ilr/hydro/download/spotpy',
  #download_url = 'svn://fb09-pasig.umwelt.uni-giessen.de/spotpy/trunk/',
  license = 'MIT',
  packages = ["spotpy", "spotpy.examples", "spotpy.algorithms", "spotpy.parallel"],
  include_package_data = True,
  use_2to3 = True,
  keywords = ['Monte Carlo', 'MCMC','MLE', 'SCE-UA', 'Simulated Annealing', 'DE-MCz', 'ROPE', 'Artifical Bee Colony', 'Uncertainty', 'Calibration', 'Model', 'Signatures'],
  classifiers = [
        'Development Status :: 4 - Beta',
        'Intended Audience :: Developers',
        'Natural Language :: English',
        'License :: OSI Approved :: MIT License',
        'Operating System :: OS Independent',
        'Programming Language :: Python',
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7',
        'Programming Language :: Python :: 3.2',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Topic :: Software Development :: Libraries :: Python Modules'],
        )
<|endoftext|>