<commit_before>#!/usr/bin/env python2
# -*- coding: utf-8 -*-

"""

    Scribber, a text editor that focuses on minimalism

    Icons provided by the Tango Desktop Project (http://tango.freedesktop.org/)

"""

# TODO: Spellcheck when exporting (?) gtkspell

import pygtk
pygtk.require('2.0')
import gtk
import pango
import re

class ScribberView(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        gtk.rc_parse(".gtkrc")

        self.is_fullscreen = False

        self.set_title("Scribber")
        self.set_border_width(2)
        #self.set_decorated(False)
        self.resize(300, 100)

        # Callbacks
        self.connect("delete_event", self.delete_event)
        self.connect("destroy", self.destroy)
        self.connect("window-state-event", self.on_window_state_event)

        scrolled_window = gtk.ScrolledWindow()
        scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)

        vbox = gtk.VBox(False, 0)
        self.add(vbox)

        # TextView
        self.view = ScribberTextView()
        vbox.pack_start(scrolled_window, True, True, 2)

        scrolled_window.add_with_viewport(self.view)

        #check = gtkspell.Spell(view)
        #check.set_language("de_DE")

        # Statusbar
        self.sbarbox = gtk.HBox(False, 0)

        # Buttons
        self.button_focus = gtk.ToggleButton("Focus")
        self.button_focus.set_image(
            gtk.image_new_from_file("system-search.png"))
        self.button_focus.set_active(True)
        self.button_focus.connect("clicked", self.on_focus_click)
        self.button_fullscreen = gtk.ToggleButton("Fullscreen")
        self.button_fullscreen.set_image(
            gtk.image_new_from_file("view-fullscreen.png"))
        self.button_fullscreen.connect("clicked", self.on_fullscreen_click)

        sbar_wc = gtk.Statusbar()
        context_id = sbar_wc.get_context_id("main_window")

        sbar_wc.push(context_id, "wc")

        self.sbarbox.pack_start(self.button_focus, False, False, 2)
        self.sbarbox.pack_start(self.button_fullscreen, False, False, 2)
        self.sbarbox.pack_end(sbar_wc, True, True, 2)

        vbox.pack_end(self.sbarbox, False, False, 2)

        # Go!
        self.show_all()
        gtk.main()

    def on_focus_click(self, widget, data=None):
        self.view.get_buffer().focus = self.view.get_buffer().focus

    def on_fullscreen_click(self, widget, data=None):
        if self.is_fullscreen:
            self.unfullscreen()
            # Gtk doesnt provied a way to check a windows state, so we have to
            # keep track ourselves
            self.is_fullscreen = False
        else:
            self.fullscreen()
            self.is_fullscreen = True

    def on_window_state_event(self, event, data=None):
        if data.new_window_state == gtk.gdk.WINDOW_STATE_FULLSCREEN:
            self.button_fullscreen.set_active(True)
            self.is_fullscreen = True
        else:
            self.button_fullscreen.set_active(False)
            self.is_fullscreen = False

    def delete_event(self, widget, event, data=None):
        # Really quit?
        return False

    def destroy(self, widget, data=None):
        gtk.main_quit()

class ScribberTextView(gtk.TextView):
    def __init__(self):
        gtk.TextView.__init__(self)

        self.set_buffer(ScribberTextBuffer())

        self.connect_after('key-press-event', self.on_key_event)
        self.connect('key-release-event', self.on_key_event)
        self.connect_after('button-press-event', self.on_button_event)
        self.connect('button-release-event', self.on_button_event)
        self.connect('move-cursor', self.on_move_cursor)

        # http://www.tortall.net/mu/wiki/PyGTKCairoTutorial
        font = pango.FontDescription("envy code r 12")
        self.modify_font(font)

        # Wrap mode
        self.set_wrap_mode(gtk.WRAP_WORD_CHAR)

        # Paragraph spacing
        self.set_pixels_above_lines(3)
        self.set_pixels_below_lines(3)

        self.set_right_margin(80)
        self.set_left_margin(80)

        # Line spacing
        self.set_pixels_inside_wrap(7)

    def on_move_cursor(self, widget, event, data=None, asd=None):
        # TODO: argument names
        #self.get_buffer()._focus_sentence()
        pass

    def on_key_event(self, widget, event, data=None):
        #self.get_buffer()._focus_sentence()
        pass

    def on_button_event(self, widget, event, data=None):
        #self.get_buffer()._focus_sentence()
        pass

class ScribberTextBuffer(gtk.TextBuffer):

    patterns = [ ["heading", re.compile("\#"), re.compile("$"), 1],
                 ["italic", re.compile("(?<!\*)(\*\w)"),
                    re.compile("(\w\*)(?!\*)"), 1],
                 ["bold", re.compile("\*\*\w"), re.compile("\w\*\*"), 2] ]

    def __init__(self):
        gtk.TextBuffer.__init__(self)

        self.set_text("""Lorem ipsum dolor sit amet, consectetur adipiscing \
elit. Ut sit amet diam mauris. Fusce ac erat, ut ultrices ligula. \
Vestibulum adipiscing mi libero. Suspendisse potenti. Fusce eu dui nunc, at \
tempus leo. Nulla facilisi. Morbi dignissim ultrices velit, posuere accumsan \
leo vehicula eget. Mauris at urna eget arcu vulputate feugiat nec id nunc. \
Nullam in faucibus ipsum. Maecenas rhoncus massa eu libero vestibulum \
sollicitudin. Morbi tempus sapien id magna molestie ut sodales lectus \
fringilla. In a quam nibh. Nullam vulputate nunc at velit ultricies at \
feugiat erat dignissim. Aliquam tempus, quam non suscipit varius, ligula quam \
elementum orci, vitae euismod lectus nulla non mauris. Proin rutrum massa \
feugiat sem scelerisque imperdiet laoreet vulputate. Quisque ullamcorper\
 justo et velit dapibus vulputate pharetra lorem lobortis. Phasellus eget \
tellus sed odio facilisis euismod. Mauris a elit libero, a gravida ligula. Nam\
 vel nisi eget tortor sodales dictum.""")

        self.focus = True

        self.connect_after("insert-text", self._on_insert_text)
        self.connect_after("delete-range", self._on_delete_range)

        self.tag_default = self.create_tag("default", foreground="#999999")

        self.tag_focus = self.create_tag("focus", foreground="#000000")

        self.tag_heading = self.create_tag("heading", weight=pango.WEIGHT_BOLD,
            left_margin=50, pixels_above_lines=15, pixels_below_lines=10)

        self.tag_mytable = self.create_tag("mytable", left_margin=110,
            pixels_above_lines=20, pixels_below_lines=20)

        self.tag_bold = self.create_tag("bold", weight=pango.WEIGHT_BOLD)
        self.tag_italic = self.create_tag("italic", style=pango.STYLE_ITALIC)
        self.tag_bolditalic = self.create_tag("bolditalic",
            weight=pango.WEIGHT_BOLD, style=pango.STYLE_ITALIC)

    def _on_insert_text(self, buf, iter, text, length):
        self._update_markdown(self.get_start_iter())

    def _on_delete_range(self, buf, start, end):
        self._update_markdown(self.get_start_iter())

    def _update_markdown(self, start, end=None):
        # Used to save which iters we already used as start or end of a pattern
        used_iters = []

        if end is None: end = self.get_end_iter()

        finished = False

        # Only remove old markdown tags (no hilight tags)
        for p in self.patterns:
            self.remove_tag_by_name(p[0], start, end)

        while not finished:
            tagn, mstart, mend, length = self._get_first_pattern(start, end)

            if tagn:
                # Found a pattern

                # TODO: +1 universal?
                if mstart.get_offset() + 1 in used_iters or \
                    (mend.get_offset() in used_iters and not mend.equal(end)):
                    start = mstart
                    start.forward_chars(length)
                    continue

                # print "************** Apply tag ", tagn, "to: ",mstart.get_text(mend)
                self.apply_tag_by_name(tagn, mstart, mend)

                used_iters.append(mstart.get_offset())
                used_iters.append(mend.get_offset())

                start = mstart
                start.forward_chars(length)

                if start == end:
                    finished = True
            else:
                # No pattern found
                finished = True

    def _get_first_pattern(self, start, end):
        """ Returns (tagname, start, end) of the first occurence of any
            known pattern in this buffer. """
        matches = []

        for pattern in self.patterns:
            mstart = start.copy()

            pattern_tagn, pattern_start, pattern_end, pattern_length = pattern

            # Match begining
            result_start = pattern_start.search(mstart.get_text(end))
            if result_start:
                # TODO: Maybe: Shortcut here, if we found a match at
                # mstart.get_offset() == 0, because no match will be before
                # that one

                # Forward until start of match
                mstart.forward_chars(result_start.start())

                # Match end
                result_end = pattern_end.search(mstart.get_text(end))
                if result_end:
                    mend = mstart.copy()
                    mend.forward_chars(result_end.end())
                else:
                    # No pattern for end found -> match until end
                    mend = self.get_end_iter()

                matches.append([result_start.start(), [pattern_tagn, mstart,
                    mend, pattern[3]]])

        if len(matches) == 0: return (None, None, None, None)
        return min(matches)[1]

    def _focus_sentence(self):
        """ Applys a highlighting tag to the sentence the cursor is in """
        if self.focus:# and not self.get_has_selection():
            # TODO: get_start_iter().has_tag("table") -> hilight whole table
            start = self.get_start_iter()
            end = self.get_end_iter()

            sentence_start = self.get_iter_at_mark(self.get_insert())
            sentence_start.backward_sentence_start()

            sentence_end = self.get_iter_at_mark(self.get_insert())
            sentence_end.forward_sentence_end()

            # Set normal style for whole text
            self.apply_tag_by_name("default", start, end)

            # Remove from currently hilighted sentence
            self.remove_tag_by_name("default", sentence_start, sentence_end)

if __name__ == '__main__':
    ScribberView()
<commit_msg>Minor cleanup and improved the Hilight Mode<commit_after>#!/usr/bin/env python2
# -*- coding: utf-8 -*-

"""

    Scribber, a text editor that focuses on minimalism

    Icons provided by the Tango Desktop Project (http://tango.freedesktop.org/)

"""

# TODO: Spellcheck when exporting (?) gtkspell

import pygtk
pygtk.require('2.0')
import gtk
import pango
import re

class ScribberView(gtk.Window):
    def __init__(self):
        gtk.Window.__init__(self)
        gtk.rc_parse(".gtkrc")

        self.is_fullscreen = False

        self.set_title("Scribber")
        self.set_border_width(2)
        #self.set_decorated(False)
        self.resize(300, 100)

        # Callbacks
        self.connect("delete_event", self.delete_event)
        self.connect("destroy", self.destroy)
        self.connect("window-state-event", self.on_window_state_event)

        scrolled_window = gtk.ScrolledWindow()
        scrolled_window.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)

        vbox = gtk.VBox(False, 0)
        self.add(vbox)

        # TextView
        self.view = ScribberTextView()
        vbox.pack_start(scrolled_window, True, True, 2)

        scrolled_window.add_with_viewport(self.view)

        #check = gtkspell.Spell(view)
        #check.set_language("de_DE")

        # Statusbar
        self.sbarbox = gtk.HBox(False, 0)

        # Buttons
        self.button_focus = gtk.ToggleButton("Focus")
        self.button_focus.set_image(
            gtk.image_new_from_file("system-search.png"))
        self.button_focus.set_active(True)
        self.button_focus.connect("clicked", self.on_focus_click)
        self.button_fullscreen = gtk.ToggleButton("Fullscreen")
        self.button_fullscreen.set_image(
            gtk.image_new_from_file("view-fullscreen.png"))
        self.button_fullscreen.connect("clicked", self.on_fullscreen_click)

        sbar_wc = gtk.Statusbar()
        context_id = sbar_wc.get_context_id("main_window")

        sbar_wc.push(context_id, "wc")

        self.sbarbox.pack_start(self.button_focus, False, False, 2)
        self.sbarbox.pack_start(self.button_fullscreen, False, False, 2)
        self.sbarbox.pack_end(sbar_wc, True, True, 2)

        vbox.pack_end(self.sbarbox, False, False, 2)

        # Go!
        self.show_all()
        gtk.main()

    def on_focus_click(self, widget, data=None):
        self.view.get_buffer().focus = self.view.get_buffer().focus

    def on_fullscreen_click(self, widget, data=None):
        if self.is_fullscreen:
            self.unfullscreen()
            # Gtk doesnt provied a way to check a windows state, so we have to
            # keep track ourselves
            self.is_fullscreen = False
        else:
            self.fullscreen()
            self.is_fullscreen = True

    def on_window_state_event(self, event, data=None):
        if data.new_window_state == gtk.gdk.WINDOW_STATE_FULLSCREEN:
            self.button_fullscreen.set_active(True)
            self.is_fullscreen = True
        else:
            self.button_fullscreen.set_active(False)
            self.is_fullscreen = False

    def delete_event(self, widget, event, data=None):
        # Really quit?
        return False

    def destroy(self, widget, data=None):
        gtk.main_quit()

class ScribberTextView(gtk.TextView):
    def __init__(self):
        gtk.TextView.__init__(self)

        self.set_buffer(ScribberTextBuffer())

        self.connect_after('key-press-event', self._on_key_event)
        self.connect('key-release-event', self._on_key_event)
        self.connect_after('button-press-event', self._on_button_event)
        self.connect('button-release-event', self._on_button_event)
        self.connect('move-cursor', self._on_move_cursor)

        # http://www.tortall.net/mu/wiki/PyGTKCairoTutorial
        font = pango.FontDescription("envy code r 12")
        self.modify_font(font)

        # Wrap mode
        self.set_wrap_mode(gtk.WRAP_WORD_CHAR)

        # Paragraph spacing
        self.set_pixels_above_lines(3)
        self.set_pixels_below_lines(3)

        self.set_right_margin(80)
        self.set_left_margin(80)

        # Line spacing
        self.set_pixels_inside_wrap(7)

    def _on_move_cursor(self, widget, event, data=None, asd=None):
        self._focus_current_sentence()

    def _on_key_event(self, widget, event, data=None):
        self._focus_current_sentence()

    def _on_button_event(self, widget, event, data=None):
        self._focus_current_sentence()

    def _focus_current_sentence(self):
        # TODO: Scroll doesnt work
        sentence_start = self.get_buffer()._focus_current_sentence()
        self.scroll_to_iter(sentence_start, 0, True, 0.0, 0.0)

class ScribberTextBuffer(gtk.TextBuffer):

    patterns = [ ["heading", re.compile("\#"), re.compile("$"), 1],
                 ["italic", re.compile("(?<!\*)(\*\w)"),
                    re.compile("(\w\*)(?!\*)"), 1],
                 ["bold", re.compile("\*\*\w"), re.compile("\w\*\*"), 2] ]

    def __init__(self):
        gtk.TextBuffer.__init__(self)

        self.set_text("""Lorem ipsum dolor sit amet, consectetur adipiscing \
elit. Ut sit amet diam mauris. Fusce ac erat, ut ultrices ligula. \
Vestibulum adipiscing mi libero. Suspendisse potenti. Fusce eu dui nunc, at \
tempus leo. Nulla facilisi. Morbi dignissim ultrices velit, posuere accumsan \
leo vehicula eget. Mauris at urna eget arcu vulputate feugiat nec id nunc. \
Nullam in faucibus ipsum. Maecenas rhoncus massa eu libero vestibulum \
sollicitudin. Morbi tempus sapien id magna molestie ut sodales lectus \
fringilla. In a quam nibh. Nullam vulputate nunc at velit ultricies at \
feugiat erat dignissim. Aliquam tempus, quam non suscipit varius, ligula quam \
elementum orci, vitae euismod lectus nulla non mauris. Proin rutrum massa \
feugiat sem scelerisque imperdiet laoreet vulputate. Quisque ullamcorper\
 justo et velit dapibus vulputate pharetra lorem lobortis. Phasellus eget \
tellus sed odio facilisis euismod. Mauris a elit libero, a gravida ligula. Nam\
elit. Ut sit amet diam mauris. Fusce ac erat, ut ultrices ligula. \
Vestibulum adipiscing mi libero. Suspendisse potenti. Fusce eu dui nunc, at \
tempus leo. Nulla facilisi. Morbi dignissim ultrices velit, posuere accumsan \
leo vehicula eget. Mauris at urna eget arcu vulputate feugiat nec id nunc. \
Nullam in faucibus ipsum. Maecenas rhoncus massa eu libero vestibulum \
sollicitudin. Morbi tempus sapien id magna molestie ut sodales lectus \
fringilla. In a quam nibh. Nullam vulputate nunc at velit ultricies at \
feugiat erat dignissim. Aliquam tempus, quam non suscipit varius, ligula quam \
elementum orci, vitae euismod lectus nulla non mauris. Proin rutrum massa \
feugiat sem scelerisque imperdiet laoreet vulputate. Quisque ullamcorper\
 justo et velit dapibus vulputate pharetra lorem lobortis. Phasellus eget \
tellus sed odio facilisis euismod. Mauris a elit libero, a gravida ligula. Nam\
elit. Ut sit amet diam mauris. Fusce ac erat, ut ultrices ligula. \
Vestibulum adipiscing mi libero. Suspendisse potenti. Fusce eu dui nunc, at \
tempus leo. Nulla facilisi. Morbi dignissim ultrices velit, posuere accumsan \
leo vehicula eget. Mauris at urna eget arcu vulputate feugiat nec id nunc. \
Nullam in faucibus ipsum. Maecenas rhoncus massa eu libero vestibulum \
sollicitudin. Morbi tempus sapien id magna molestie ut sodales lectus \
fringilla. In a quam nibh. Nullam vulputate nunc at velit ultricies at \
feugiat erat dignissim. Aliquam tempus, quam non suscipit varius, ligula quam \
elementum orci, vitae euismod lectus nulla non mauris. Proin rutrum massa \
feugiat sem scelerisque imperdiet laoreet vulputate. Quisque ullamcorper\
 justo et velit dapibus vulputate pharetra lorem lobortis. Phasellus eget \
tellus sed odio facilisis euismod. Mauris a elit libero, a gravida ligula. Nam\
 vel nisi eget tortor sodales dictum.""")

        self.focus = True

        self.connect_after("insert-text", self._on_insert_text)
        self.connect_after("delete-range", self._on_delete_range)

        self.tag_default = self.create_tag("default", foreground="#999999")

        self.tag_focus = self.create_tag("focus", foreground="#000000")

        self.tag_heading = self.create_tag("heading", weight=pango.WEIGHT_BOLD,
            left_margin=50, pixels_above_lines=15, pixels_below_lines=10)

        self.tag_mytable = self.create_tag("mytable", left_margin=110,
            pixels_above_lines=20, pixels_below_lines=20)

        self.tag_bold = self.create_tag("bold", weight=pango.WEIGHT_BOLD)
        self.tag_italic = self.create_tag("italic", style=pango.STYLE_ITALIC)
        self.tag_bolditalic = self.create_tag("bolditalic",
            weight=pango.WEIGHT_BOLD, style=pango.STYLE_ITALIC)

    def _on_insert_text(self, buf, iter, text, length):
        self._update_markdown(self.get_start_iter())

    def _on_delete_range(self, buf, start, end):
        self._update_markdown(self.get_start_iter())

    def _update_markdown(self, start, end=None):
        # Used to save which iters we already used as start or end of a pattern
        used_iters = []

        if end is None: end = self.get_end_iter()

        finished = False

        # Only remove old markdown tags (no hilight tags)
        for p in self.patterns:
            self.remove_tag_by_name(p[0], start, end)

        while not finished:
            tagn, mstart, mend, length = self._get_first_pattern(start, end)

            if tagn:
                # Found a pattern
                # TODO: +1 universal?
                if mstart.get_offset() + 1 in used_iters or \
                    (mend.get_offset() in used_iters and not mend.equal(end)):
                    start = mstart
                    start.forward_chars(length)
                    continue

                print "************** Apply tag ", tagn, "to: ",mstart.get_text(mend)
                self.apply_tag_by_name(tagn, mstart, mend)

                used_iters.append(mstart.get_offset())
                used_iters.append(mend.get_offset())

                start = mstart
                start.forward_chars(length)

                if start == end:
                    finished = True
            else:
                # No pattern found
                finished = True

    def _get_first_pattern(self, start, end):
        """ Returns (tagname, start, end) of the first occurence of any
            known pattern in this buffer. """
        matches = []

        for pattern in self.patterns:
            mstart = start.copy()

            pattern_tagn, pattern_start, pattern_end, pattern_length = pattern

            # Match begining
            result_start = pattern_start.search(mstart.get_text(end))
            if result_start:
                # TODO: Maybe: Shortcut here, if we found a match at
                # mstart.get_offset() == 0, because no match will be before
                # that one

                # Forward until start of match
                mstart.forward_chars(result_start.start())

                # Match end
                result_end = pattern_end.search(mstart.get_text(end))
                if result_end:
                    mend = mstart.copy()
                    mend.forward_chars(result_end.end())
                else:
                    # No pattern for end found -> match until end
                    mend = self.get_end_iter()

                matches.append([result_start.start(), [pattern_tagn, mstart,
                    mend, pattern[3]]])

        if len(matches) == 0: return (None, None, None, None)
        return min(matches)[1]

    def _focus_current_sentence(self):
        """ Applys a highlighting tag to the sentence the cursor is on """

        if self.focus:# and not self.get_has_selection():

            # TODO: get_start_iter().has_tag("table") -> hilight whole table

            start = self.get_start_iter()
            end = self.get_end_iter()

            sentence_start = self.get_iter_at_mark(self.get_insert())
            sentence_start.backward_sentence_start()

            sentence_end = self.get_iter_at_mark(self.get_insert())
            sentence_end.forward_sentence_end()

            self.apply_tag_by_name("default", start, end)
            self.remove_tag_by_name("focus", start, end)
            self.apply_tag_by_name("focus", sentence_start, sentence_end)

        return sentence_start

            # Set normal style for whole text
#            self.apply_tag_by_name("default", start, end)
#
#            # Remove from currently hilighted sentence
#            self.remove_tag_by_name("default", sentence_start, sentence_end)
#            self.apply_tag_by_name("focus", sentence_start, sentence_end)

if __name__ == '__main__':
    ScribberView()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

"""
Querying
========

Entities can be queried from a service with a Query object:

.. code-block:: python

    query = Service.query(Order)

Adding filters and other options always creates a new Query object with the
given directives:

.. code-block:: python

    >>> query.filter(Order.Name == 'Foo')
    <Query for <Order>>

This makes object chaining possible:

.. code-block:: python

    >>> first_order = query.filter(...).filter(...).order_by(...).first()

The resulting objects can be fetched with :py:func:`~Query.first`,
:py:func:`~Query.one`, :py:func:`~Query.all`, :py:func:`~Query.get` or
just iterating the Query object itself. Network is not accessed until one of
these ways is triggered.

Navigation properties can be loaded in the same request with
:py:func:`~Query.expand`:

.. code-block:: python

    >>> query.expand(Order.Shipper, Order.Customer)
    >>> order = query.first()

----

API
---
"""

try:
    # noinspection PyUnresolvedReferences
    from urllib.parse import urljoin
except ImportError:
    # noinspection PyUnresolvedReferences
    from urlparse import urljoin

import odata.exceptions as exc

class Query(object):
    """
    This class should not be instantiated directly, but from a
    :py:class:`~odata.service.ODataService` object.
    """
    def __init__(self, entitycls, options=None):
        self.entity = entitycls
        self.options = options or dict()

    def __iter__(self):
        connection = self._get_connection()
        url = self._get_url()
        options = self._get_options()
        while True:
            data = connection.execute_get(url, options)
            if 'value' in data:
                value = data.get('value', [])
                for row in value:
                    yield self._create_model(row)

                if '@odata.nextLink' in data:
                    url = urljoin(self.entity.__odata_url_base__, data['@odata.nextLink'])
                    options = {}  # we get all options in the nextLink url
                else:
                    break

    def __repr__(self):
        return '<Query for {0}>'.format(self.entity)

    def __str__(self):
        return self.as_string()

    def _get_connection(self):
        return self.entity.__odata_connection__

    def _get_url(self):
        return self.entity.__odata_url__()

    def _get_options(self):
        """
        Format current query options to a dict that can be passed to requests
        :return: Dictionary
        """
        options = dict()

        _top = self.options.get('$top')
        if _top is not None:
            options['$top'] = _top

        _offset = self.options.get('$skip')
        if _offset is not None:
            options['$skip'] = _offset

        _select = self.options.get('$select')
        if _select:
            options['$select'] = ','.join(_select)

        _filters = self.options.get('$filter')
        if _filters:
            options['$filter'] = ' and '.join(_filters)

        _expand = self.options.get('$expand')
        if _expand:
            options['$expand'] = ','.join(_expand)

        _order_by = self.options.get('$orderby')
        if _order_by:
            options['$orderby'] = ','.join(_order_by)
        return options

    def _create_model(self, row):
        if len(self.options.get('$select', [])):
            return row
        else:
            return self.entity.__new__(self.entity, from_data=row)

    def _get_or_create_option(self, name):
        if name not in self.options:
            self.options[name] = []
        return self.options[name]

    def _format_params(self, options):
        return '&'.join(['='.join((key, str(value))) for key, value in options.items() if value is not None])

    def _new_query(self):
        """
        Create copy of this query without mutable values. All query builders
        should use this first.

        :return: Query instance
        """
        o = dict()
        o['$top'] = self.options.get('$top', None)
        o['$skip'] = self.options.get('$skip', None)
        o['$select'] = self.options.get('$select', [])[:]
        o['$filter'] = self.options.get('$filter', [])[:]
        o['$expand'] = self.options.get('$expand', [])[:]
        o['$orderby'] = self.options.get('$orderby', [])[:]
        return Query(self.entity, options=o)

    def as_string(self):
        query = self._format_params(self._get_options())
        return urljoin(self._get_url(), '?{0}'.format(query))

    # Query builders ###########################################################

    def select(self, *values):
        """
        Set properties to fetch instead of full Entity objects

        :return: Raw JSON values for given properties
        """
        q = self._new_query()
        option = q._get_or_create_option('$select')
        for prop in values:
            option.append(prop.name)
        return q

    def filter(self, value):
        """
        Set ``$filter`` query parameter. Can be called multiple times. Multiple
        :py:func:`filter` calls are concatenated with 'and'

        :param value: Property comparison. For example, ``Entity.Property == 2``
        :return: Query instance
        """
        q = self._new_query()
        option = q._get_or_create_option('$filter')
        option.append(value)
        return q

    def expand(self, *values):
        """
        Set ``$expand`` query parameter

        :param values: ``Entity.Property`` instance
        :return: Query instance
        """
        q = self._new_query()
        option = q._get_or_create_option('$expand')
        for prop in values:
            option.append(prop.name)
        return q

    def order_by(self, *values):
        """
        Set ``$orderby`` query parameter

        :param values: One of more of Property.asc() or Property.desc()
        :return: Query instance
        """
        q = self._new_query()
        option = q._get_or_create_option('$orderby')
        option.extend(values)
        return q

    def limit(self, value):
        """
        Set ``$top`` query parameter

        :param value: Number of records to return
        :return: Query instance
        """
        q = self._new_query()
        q.options['$top'] = value
        return q

    def offset(self, value):
        """
        Set ``$skip`` query parameter

        :param value: Number of records to skip
        :return: Query instance
        """
        q = self._new_query()
        q.options['$skip'] = value
        return q

    @staticmethod
    def and_(value1, value2):
        return '{0} and {1}'.format(value1, value2)

    @staticmethod
    def or_(value1, value2):
        return '{0} or {1}'.format(value1, value2)

    @staticmethod
    def grouped(value):
        return '({0})'.format(value)

    # Actions ##################################################################

    def all(self):
        """
        Returns a list of all Entity instances that match the current query
        options. Iterates through all results with multiple requests fired if
        necessary, exhausting the query

        :return: A list of Entity instances
        """
        return list(iter(self))

    def first(self):
        """
        Return the first Entity instance that matches current query

        :return: Entity instance or None
        """
        oldvalue = self.options.get('$top', None)
        self.options['$top'] = 1
        data = list(iter(self))
        self.options['$top'] = oldvalue
        if data:
            return data[0]

    def one(self):
        """
        Return only one resulting Entity

        :return: Entity instance
        :raises NoResultsFound: Zero results returned
        :raises MultipleResultsFound: Multiple results returned
        """
        oldlimit = self.options.get('$top', None)

        self.options['$top'] = 2
        data = self.all()

        self.options['$top'] = oldlimit
        if len(data) == 0:
            raise exc.NoResultsFound()
        if len(data) > 1:
            raise exc.MultipleResultsFound()
        return data[0]

    def get(self, pk):
        """
        Return a Entity with the given primary key

        :param pk: Primary key value
        :return: Entity instance or None
        """
        i = self.entity.__new__(self.entity)
        es = i.__odata__
        _, prop = es.primary_key_property
        oldfilters = self._get_or_create_option('$filter')

        self.options['$filter'] = [prop == pk]
        data = list(iter(self))

        self.options['$filter'] = oldfilters
        if len(data) > 0:
            return data[0]
        raise exc.NoResultsFound()

    def call(self, fname, **kwargs):
        """
        Call a function bound on the entity. Keyword arguments must be tuples
        of value and property type

        :param fname: Function name to call
        :param kwargs: Function arguments. Tuples of value and type
        :return: Entities returned by the call
        """
        connection = self._get_connection()

        url = urljoin(self.entity.__odata_url__() + '/', fname)

        kwargs_escaped = []
        for key, value in kwargs.items():
            q_value = value[0]
            q_type = value[1]
            escaped_value = q_type('temp').escape_value(q_value)
            kwargs_escaped.append((key, escaped_value))

        params = ['='.join([key, value]) for key, value in kwargs_escaped]
        params = ','.join(params)
        url += '({0})'.format(params)
        data = connection.execute_get(url)

        r = []
        if data:
            for row in data.get('value', []):
                r.append(self._create_model(row))
        return r
<commit_msg>Add raw queries<commit_after># -*- coding: utf-8 -*-

"""
Querying
========

Entities can be queried from a service with a Query object:

.. code-block:: python

    query = Service.query(Order)

Adding filters and other options always creates a new Query object with the
given directives:

.. code-block:: python

    >>> query.filter(Order.Name == 'Foo')
    <Query for <Order>>

This makes object chaining possible:

.. code-block:: python

    >>> first_order = query.filter(...).filter(...).order_by(...).first()

The resulting objects can be fetched with :py:func:`~Query.first`,
:py:func:`~Query.one`, :py:func:`~Query.all`, :py:func:`~Query.get` or
just iterating the Query object itself. Network is not accessed until one of
these ways is triggered.

Navigation properties can be loaded in the same request with
:py:func:`~Query.expand`:

.. code-block:: python

    >>> query.expand(Order.Shipper, Order.Customer)
    >>> order = query.first()

----

API
---
"""

try:
    # noinspection PyUnresolvedReferences
    from urllib.parse import urljoin
except ImportError:
    # noinspection PyUnresolvedReferences
    from urlparse import urljoin

import odata.exceptions as exc

class Query(object):
    """
    This class should not be instantiated directly, but from a
    :py:class:`~odata.service.ODataService` object.
    """
    def __init__(self, entitycls, options=None):
        self.entity = entitycls
        self.options = options or dict()

    def __iter__(self):
        connection = self._get_connection()
        url = self._get_url()
        options = self._get_options()
        while True:
            data = connection.execute_get(url, options)
            if 'value' in data:
                value = data.get('value', [])
                for row in value:
                    yield self._create_model(row)

                if '@odata.nextLink' in data:
                    url = urljoin(self.entity.__odata_url_base__, data['@odata.nextLink'])
                    options = {}  # we get all options in the nextLink url
                else:
                    break

    def __repr__(self):
        return '<Query for {0}>'.format(self.entity)

    def __str__(self):
        return self.as_string()

    def _get_connection(self):
        return self.entity.__odata_connection__

    def _get_url(self):
        return self.entity.__odata_url__()

    def _get_options(self):
        """
        Format current query options to a dict that can be passed to requests
        :return: Dictionary
        """
        options = dict()

        _top = self.options.get('$top')
        if _top is not None:
            options['$top'] = _top

        _offset = self.options.get('$skip')
        if _offset is not None:
            options['$skip'] = _offset

        _select = self.options.get('$select')
        if _select:
            options['$select'] = ','.join(_select)

        _filters = self.options.get('$filter')
        if _filters:
            options['$filter'] = ' and '.join(_filters)

        _expand = self.options.get('$expand')
        if _expand:
            options['$expand'] = ','.join(_expand)

        _order_by = self.options.get('$orderby')
        if _order_by:
            options['$orderby'] = ','.join(_order_by)
        return options

    def _create_model(self, row):
        if len(self.options.get('$select', [])):
            return row
        else:
            return self.entity.__new__(self.entity, from_data=row)

    def _get_or_create_option(self, name):
        if name not in self.options:
            self.options[name] = []
        return self.options[name]

    def _format_params(self, options):
        return '&'.join(['='.join((key, str(value))) for key, value in options.items() if value is not None])

    def _new_query(self):
        """
        Create copy of this query without mutable values. All query builders
        should use this first.

        :return: Query instance
        """
        o = dict()
        o['$top'] = self.options.get('$top', None)
        o['$skip'] = self.options.get('$skip', None)
        o['$select'] = self.options.get('$select', [])[:]
        o['$filter'] = self.options.get('$filter', [])[:]
        o['$expand'] = self.options.get('$expand', [])[:]
        o['$orderby'] = self.options.get('$orderby', [])[:]
        return Query(self.entity, options=o)

    def as_string(self):
        query = self._format_params(self._get_options())
        return urljoin(self._get_url(), '?{0}'.format(query))

    # Query builders ###########################################################

    def select(self, *values):
        """
        Set properties to fetch instead of full Entity objects

        :return: Raw JSON values for given properties
        """
        q = self._new_query()
        option = q._get_or_create_option('$select')
        for prop in values:
            option.append(prop.name)
        return q

    def filter(self, value):
        """
        Set ``$filter`` query parameter. Can be called multiple times. Multiple
        :py:func:`filter` calls are concatenated with 'and'

        :param value: Property comparison. For example, ``Entity.Property == 2``
        :return: Query instance
        """
        q = self._new_query()
        option = q._get_or_create_option('$filter')
        option.append(value)
        return q

    def expand(self, *values):
        """
        Set ``$expand`` query parameter

        :param values: ``Entity.Property`` instance
        :return: Query instance
        """
        q = self._new_query()
        option = q._get_or_create_option('$expand')
        for prop in values:
            option.append(prop.name)
        return q

    def order_by(self, *values):
        """
        Set ``$orderby`` query parameter

        :param values: One of more of Property.asc() or Property.desc()
        :return: Query instance
        """
        q = self._new_query()
        option = q._get_or_create_option('$orderby')
        option.extend(values)
        return q

    def limit(self, value):
        """
        Set ``$top`` query parameter

        :param value: Number of records to return
        :return: Query instance
        """
        q = self._new_query()
        q.options['$top'] = value
        return q

    def offset(self, value):
        """
        Set ``$skip`` query parameter

        :param value: Number of records to skip
        :return: Query instance
        """
        q = self._new_query()
        q.options['$skip'] = value
        return q

    @staticmethod
    def and_(value1, value2):
        return '{0} and {1}'.format(value1, value2)

    @staticmethod
    def or_(value1, value2):
        return '{0} or {1}'.format(value1, value2)

    @staticmethod
    def grouped(value):
        return '({0})'.format(value)

    # Actions ##################################################################

    def all(self):
        """
        Returns a list of all Entity instances that match the current query
        options. Iterates through all results with multiple requests fired if
        necessary, exhausting the query

        :return: A list of Entity instances
        """
        return list(iter(self))

    def first(self):
        """
        Return the first Entity instance that matches current query

        :return: Entity instance or None
        """
        oldvalue = self.options.get('$top', None)
        self.options['$top'] = 1
        data = list(iter(self))
        self.options['$top'] = oldvalue
        if data:
            return data[0]

    def one(self):
        """
        Return only one resulting Entity

        :return: Entity instance
        :raises NoResultsFound: Zero results returned
        :raises MultipleResultsFound: Multiple results returned
        """
        oldlimit = self.options.get('$top', None)

        self.options['$top'] = 2
        data = self.all()

        self.options['$top'] = oldlimit
        if len(data) == 0:
            raise exc.NoResultsFound()
        if len(data) > 1:
            raise exc.MultipleResultsFound()
        return data[0]

    def get(self, pk):
        """
        Return a Entity with the given primary key

        :param pk: Primary key value
        :return: Entity instance or None
        """
        i = self.entity.__new__(self.entity)
        es = i.__odata__
        _, prop = es.primary_key_property
        oldfilters = self._get_or_create_option('$filter')

        self.options['$filter'] = [prop == pk]
        data = list(iter(self))

        self.options['$filter'] = oldfilters
        if len(data) > 0:
            return data[0]
        raise exc.NoResultsFound()

    def raw(self, query_params):
        """
        Execute a query with custom parameters. Allows queries that
        :py:class:`Query` does not support otherwise. Results are not converted
        to Entity objects

        .. code-block:: python

            >>> query = Service.query(MyEntity)
            >>> query.raw({'$filter': 'EntityId eq 123456'})
            [{'EntityId': 123456, 'Name': 'Example entity'}]

        :param query_params: A dictionary of query params containing $filter, $orderby, etc.
        :type query_params: dict
        :return: Query result
        """
        connection = self._get_connection()
        url = self.entity.__odata_url__()
        response_data = connection.execute_get(url, params=query_params)
        return (response_data or {}).get('value')

    def call(self, fname, **kwargs):
        """
        Call a function bound on the entity. Keyword arguments must be tuples
        of value and property type

        :param fname: Function name to call
        :param kwargs: Function arguments. Tuples of value and type
        :return: Entities returned by the call
        """
        connection = self._get_connection()

        url = urljoin(self.entity.__odata_url__() + '/', fname)

        kwargs_escaped = []
        for key, value in kwargs.items():
            q_value = value[0]
            q_type = value[1]
            escaped_value = q_type('temp').escape_value(q_value)
            kwargs_escaped.append((key, escaped_value))

        params = ['='.join([key, value]) for key, value in kwargs_escaped]
        params = ','.join(params)
        url += '({0})'.format(params)
        data = connection.execute_get(url)

        r = []
        if data:
            for row in data.get('value', []):
                r.append(self._create_model(row))
        return r
<|endoftext|>


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

import pygame
from classes.race.race import Race
from classes.race.arm.player_arm import PlayerARM

class RaceARM(Race):

    def __init__(self, screen, players, race_length, diameter, actions):
        Race.__init__(self, screen, race_length, diameter, actions)

        self.players.append(
            PlayerARM(
                self.screen,
                players[0],
                (255, 0, 0),
                0,
                0,
                self.race_length,
                self.diameter,
                23
            )
        )
        self.players.append(
            PlayerARM(
                self.screen,
                players[1],
                (0, 0, 255),
                0,
                self.screen_height / 2,
                self.race_length,
                self.diameter,
                24
            )
        )

    def handle_input_data(self, event):
        if self.race_status != 'COUNTDOWN' and self.race_status != 'FINISHED' and self.race_status == 'READY':
            if event.key == pygame.K_ESCAPE:
                for player in self.players:
                    player.bike_observer.stop_listening()
        Race.handle_input_data(self, event)<commit_msg>* Progress-Bar-Logos für ARM<commit_after># -*- coding: utf-8 -*-
# !/usr/bin/python

import pygame
from classes.race.race import Race
from classes.race.arm.player_arm import PlayerARM

class RaceARM(Race):

    def __init__(self, screen, players, race_length, diameter, actions):
        Race.__init__(self, screen, race_length, diameter, actions)

        self.players.append(
            PlayerARM(
                self.screen,
                players[0],
                (255, 0, 0),
                0,
                0,
                self.race_length,
                self.diameter,
                23,
                'bg_1'
            )
        )
        self.players.append(
            PlayerARM(
                self.screen,
                players[1],
                (0, 0, 255),
                0,
                self.screen_height / 2,
                self.race_length,
                self.diameter,
                24,
                'bg_2'
            )
        )

    def handle_input_data(self, event):
        if self.race_status != 'COUNTDOWN' and self.race_status != 'FINISHED' and self.race_status == 'READY':
            if event.key == pygame.K_ESCAPE:
                for player in self.players:
                    player.bike_observer.stop_listening()
        Race.handle_input_data(self, event)<|endoftext|>


<commit_before># Copyright (C) 2010,2011  Chris Lalancette <clalance@redhat.com>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

import re
import shutil
import os

import Guest
import ozutil
import RedHat
import OzException

class RHL9Guest(RedHat.RedHatCDGuest):
    def __init__(self, tdl, config, auto):
        self.tdl = tdl

        self.ks_file = auto
        if self.ks_file is None:
            self.ks_file = ozutil.generate_full_auto_path("rhl-" + self.tdl.update + "-jeos.ks")

        if self.tdl.installtype != 'url':
            raise OzException.OzException("RHL installs must be done via url")

        self.url = self.tdl.url

        ozutil.deny_localhost(self.url)

        if self.tdl.arch != "i386":
            raise OzException.OzException("Invalid arch " + self.tdl.arch + "for RHL guest")

        Guest.CDGuest.__init__(self, self.tdl.name, "RHL", "9", "i386", "url",
                               "rtl8139", None, None, None, config)

    def modify_iso(self):
        self.log.debug("Putting the kickstart in place")

        output_ks = os.path.join(self.iso_contents, "ks.cfg")
        shutil.copyfile(self.ks_file, output_ks)
        f = open(output_ks, "r")
        lines = f.readlines()
        f.close()

        for line in lines:
            if re.match("^url", line):
                lines[lines.index(line)] = "url --url " + self.url + "\n"

        f = open(output_ks, "w")
        f.writelines(lines)
        f.close()

        self.log.debug("Modifying the boot options")
        isolinuxcfg = os.path.join(self.iso_contents, "isolinux", "isolinux.cfg")
        f = open(isolinuxcfg, "r")
        lines = f.readlines()
        f.close()
        for line in lines:
            if re.match("timeout", line):
                lines[lines.index(line)] = "timeout 1\n"
            elif re.match("default", line):
                lines[lines.index(line)] = "default customiso\n"
        lines.append("label customiso\n")
        lines.append("  kernel vmlinuz\n")
        lines.append("  append initrd=initrd.img ks=cdrom:/ks.cfg method=" + self.url + "\n")

        f = open(isolinuxcfg, "w")
        f.writelines(lines)
        f.close()

class RHL70and71and72and73and8Guest(Guest.FDGuest):
    def __init__(self, tdl, config, auto, nicmodel):
        self.tdl = tdl

        self.ks_file = auto
        if self.ks_file is None:
            self.ks_file = ozutil.generate_full_auto_path("rhl-" + self.tdl.update + "-jeos.ks")

        if self.tdl.installtype != 'url':
            raise OzException.OzException("RHL installs must be done via url")

        ozutil.deny_localhost(self.tdl.url)

        if self.tdl.arch != "i386":
            raise OzException.OzException("Invalid arch " + self.tdl.arch + "for RHL guest")

        Guest.FDGuest.__init__(self, self.tdl.name, "RHL", self.tdl.update,
                               "i386", nicmodel, None, None, None, config)

    def modify_floppy(self):
        self.mkdir_p(self.floppy_contents)

        self.log.debug("Putting the kickstart in place")

        output_ks = os.path.join(self.floppy_contents, "ks.cfg")
        shutil.copyfile(self.ks_file, output_ks)
        f = open(output_ks, "r")
        lines = f.readlines()
        f.close()

        for line in lines:
            if re.match("^url", line):
                lines[lines.index(line)] = "url --url " + self.tdl.url + "\n"

        f = open(output_ks, "w")
        f.writelines(lines)
        f.close()
        Guest.subprocess_check_output(["mcopy", "-i", self.output_floppy,
                                       output_ks, "::KS.CFG"])

        self.log.debug("Modifying the syslinux.cfg")

        Guest.subprocess_check_output(["mcopy", "-n", "-o", "-i",
                                       self.output_floppy, "::SYSLINUX.CFG",
                                       self.floppy_contents])
        syslinux = os.path.join(self.floppy_contents, "SYSLINUX.CFG")
        f = open(syslinux, "r")
        lines = f.readlines()
        f.close()

        for line in lines:
            if re.match("timeout", line):
                lines[lines.index(line)] = "timeout 1\n"
            if re.match("default", line):
                lines[lines.index(line)] = "default customboot\n"
        lines.append("label customboot\n")
        lines.append("  kernel vmlinuz\n")
        lines.append("  append initrd=initrd.img lang= devfs=nomount ramdisk_size=9216 ks=floppy method=" + self.tdl.url + "\n")

        f = open(syslinux, "w")
        f.writelines(lines)
        f.close()

        # sometimes, syslinux.cfg on the floppy gets marked read-only.  Avoid
        # problems with the subsequent mcopy by marking it read/write.
        Guest.subprocess_check_output(["mattrib", "-r", "-i",
                                       self.output_floppy, "::SYSLINUX.CFG"])

        Guest.subprocess_check_output(["mcopy", "-n", "-o", "-i",
                                       self.output_floppy, syslinux,
                                       "::SYSLINUX.CFG"])

    def generate_install_media(self, force_download=False):
        self.log.info("Generating install media")

        if not force_download and os.access(self.modified_floppy_cache,
                                            os.F_OK):
            self.log.info("Using cached modified media")
            shutil.copyfile(self.modified_floppy_cache, self.output_floppy)
            return

        self.get_original_floppy(self.url + "/images/bootnet.img",
                                 force_download)
        self.copy_floppy()
        try:
            self.modify_floppy()
            if self.cache_modified_media:
                self.log.info("Caching modified media for future use")
                shutil.copyfile(self.output_floppy, self.modified_floppy_cache)
        finally:
            self.cleanup_floppy()

def get_class(tdl, config, auto):
    if tdl.update in ["9"]:
        return RHL9Guest(tdl, config, auto)
    if tdl.update in ["7.2", "7.3", "8"]:
        return RHL70and71and72and73and8Guest(tdl, config, auto, "rtl8139")
    # FIXME: RHL 6.2 does not work via HTTP because of a bug in the installer;
    # when parsing a URL passed in via "method", it fails to put a / at the
    # beginning of the URL.  What this means is that when the installer goes
    # to fetch the install images via "GET path/to/netstg2.img HTTP/0.9", the
    # web server then returns an error.  To do a fully automated install, we
    # need to use an ISO, NFS or FTP install method; I could not get FTP
    # to work, but I did not try that hard
    # FIXME: RHL 6.1 fails for a different reason, namely that there is no
    # netstg2.img available in the distribution I have.  Unfortunately, I have
    # not been able to find the netstg2.img, nor an ISO of 6.1 to do an
    # alternate install.  NFS may still work here.
    # FIXME: RHL 6.0 fails for yet a different reason, a kernel panic on boot
    # The panic is:
    # VFS: Cannot open root device 08:21
    # Kernel panic: VFS: Unable to mount root fs on 08:21
    if tdl.update in ["7.0", "7.1"]:
        return RHL70and71and72and73and8Guest(tdl, config, auto, "ne2k_pci")
    raise OzException.OzException("Unsupported RHL update " + update)
<commit_msg>Make sure to define RHL self.url.<commit_after># Copyright (C) 2010,2011  Chris Lalancette <clalance@redhat.com>

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.

# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

import re
import shutil
import os

import Guest
import ozutil
import RedHat
import OzException

class RHL9Guest(RedHat.RedHatCDGuest):
    def __init__(self, tdl, config, auto):
        self.tdl = tdl

        self.ks_file = auto
        if self.ks_file is None:
            self.ks_file = ozutil.generate_full_auto_path("rhl-" + self.tdl.update + "-jeos.ks")

        if self.tdl.installtype != 'url':
            raise OzException.OzException("RHL installs must be done via url")

        self.url = self.tdl.url

        ozutil.deny_localhost(self.url)

        if self.tdl.arch != "i386":
            raise OzException.OzException("Invalid arch " + self.tdl.arch + "for RHL guest")

        Guest.CDGuest.__init__(self, self.tdl.name, "RHL", "9", "i386", "url",
                               "rtl8139", None, None, None, config)

    def modify_iso(self):
        self.log.debug("Putting the kickstart in place")

        output_ks = os.path.join(self.iso_contents, "ks.cfg")
        shutil.copyfile(self.ks_file, output_ks)
        f = open(output_ks, "r")
        lines = f.readlines()
        f.close()

        for line in lines:
            if re.match("^url", line):
                lines[lines.index(line)] = "url --url " + self.url + "\n"

        f = open(output_ks, "w")
        f.writelines(lines)
        f.close()

        self.log.debug("Modifying the boot options")
        isolinuxcfg = os.path.join(self.iso_contents, "isolinux", "isolinux.cfg")
        f = open(isolinuxcfg, "r")
        lines = f.readlines()
        f.close()
        for line in lines:
            if re.match("timeout", line):
                lines[lines.index(line)] = "timeout 1\n"
            elif re.match("default", line):
                lines[lines.index(line)] = "default customiso\n"
        lines.append("label customiso\n")
        lines.append("  kernel vmlinuz\n")
        lines.append("  append initrd=initrd.img ks=cdrom:/ks.cfg method=" + self.url + "\n")

        f = open(isolinuxcfg, "w")
        f.writelines(lines)
        f.close()

class RHL70and71and72and73and8Guest(Guest.FDGuest):
    def __init__(self, tdl, config, auto, nicmodel):
        self.tdl = tdl

        self.ks_file = auto
        if self.ks_file is None:
            self.ks_file = ozutil.generate_full_auto_path("rhl-" + self.tdl.update + "-jeos.ks")

        if self.tdl.installtype != 'url':
            raise OzException.OzException("RHL installs must be done via url")

        ozutil.deny_localhost(self.tdl.url)

        self.url = self.tdl.url

        if self.tdl.arch != "i386":
            raise OzException.OzException("Invalid arch " + self.tdl.arch + "for RHL guest")

        Guest.FDGuest.__init__(self, self.tdl.name, "RHL", self.tdl.update,
                               "i386", nicmodel, None, None, None, config)

    def modify_floppy(self):
        self.mkdir_p(self.floppy_contents)

        self.log.debug("Putting the kickstart in place")

        output_ks = os.path.join(self.floppy_contents, "ks.cfg")
        shutil.copyfile(self.ks_file, output_ks)
        f = open(output_ks, "r")
        lines = f.readlines()
        f.close()

        for line in lines:
            if re.match("^url", line):
                lines[lines.index(line)] = "url --url " + self.tdl.url + "\n"

        f = open(output_ks, "w")
        f.writelines(lines)
        f.close()
        Guest.subprocess_check_output(["mcopy", "-i", self.output_floppy,
                                       output_ks, "::KS.CFG"])

        self.log.debug("Modifying the syslinux.cfg")

        Guest.subprocess_check_output(["mcopy", "-n", "-o", "-i",
                                       self.output_floppy, "::SYSLINUX.CFG",
                                       self.floppy_contents])
        syslinux = os.path.join(self.floppy_contents, "SYSLINUX.CFG")
        f = open(syslinux, "r")
        lines = f.readlines()
        f.close()

        for line in lines:
            if re.match("timeout", line):
                lines[lines.index(line)] = "timeout 1\n"
            if re.match("default", line):
                lines[lines.index(line)] = "default customboot\n"
        lines.append("label customboot\n")
        lines.append("  kernel vmlinuz\n")
        lines.append("  append initrd=initrd.img lang= devfs=nomount ramdisk_size=9216 ks=floppy method=" + self.tdl.url + "\n")

        f = open(syslinux, "w")
        f.writelines(lines)
        f.close()

        # sometimes, syslinux.cfg on the floppy gets marked read-only.  Avoid
        # problems with the subsequent mcopy by marking it read/write.
        Guest.subprocess_check_output(["mattrib", "-r", "-i",
                                       self.output_floppy, "::SYSLINUX.CFG"])

        Guest.subprocess_check_output(["mcopy", "-n", "-o", "-i",
                                       self.output_floppy, syslinux,
                                       "::SYSLINUX.CFG"])

    def generate_install_media(self, force_download=False):
        self.log.info("Generating install media")

        if not force_download and os.access(self.modified_floppy_cache,
                                            os.F_OK):
            self.log.info("Using cached modified media")
            shutil.copyfile(self.modified_floppy_cache, self.output_floppy)
            return

        self.get_original_floppy(self.url + "/images/bootnet.img",
                                 force_download)
        self.copy_floppy()
        try:
            self.modify_floppy()
            if self.cache_modified_media:
                self.log.info("Caching modified media for future use")
                shutil.copyfile(self.output_floppy, self.modified_floppy_cache)
        finally:
            self.cleanup_floppy()

def get_class(tdl, config, auto):
    if tdl.update in ["9"]:
        return RHL9Guest(tdl, config, auto)
    if tdl.update in ["7.2", "7.3", "8"]:
        return RHL70and71and72and73and8Guest(tdl, config, auto, "rtl8139")
    # FIXME: RHL 6.2 does not work via HTTP because of a bug in the installer;
    # when parsing a URL passed in via "method", it fails to put a / at the
    # beginning of the URL.  What this means is that when the installer goes
    # to fetch the install images via "GET path/to/netstg2.img HTTP/0.9", the
    # web server then returns an error.  To do a fully automated install, we
    # need to use an ISO, NFS or FTP install method; I could not get FTP
    # to work, but I did not try that hard
    # FIXME: RHL 6.1 fails for a different reason, namely that there is no
    # netstg2.img available in the distribution I have.  Unfortunately, I have
    # not been able to find the netstg2.img, nor an ISO of 6.1 to do an
    # alternate install.  NFS may still work here.
    # FIXME: RHL 6.0 fails for yet a different reason, a kernel panic on boot
    # The panic is:
    # VFS: Cannot open root device 08:21
    # Kernel panic: VFS: Unable to mount root fs on 08:21
    if tdl.update in ["7.0", "7.1"]:
        return RHL70and71and72and73and8Guest(tdl, config, auto, "ne2k_pci")
    raise OzException.OzException("Unsupported RHL update " + update)
<|endoftext|>


<commit_before>import random

from django.http import HttpResponseNotFound
from django.contrib.contenttypes.models import ContentType
from django import template

from secretballot import views
from secretballot.models import Vote

from likes.utils import can_vote
from likes import signals

def can_vote_test(request, content_type, object_id, vote):
    return can_vote(
        content_type.get_object_for_this_type(id=object_id),
        request.user,
        request
    )

def like(request, content_type, id, vote):
    # Crawlers will follow the like link if anonymous liking is enabled. They
    # typically do not have referrer set.
    if 'HTTP_REFERER' not in request.META:
        return HttpResponseNotFound()

    url_friendly_content_type = content_type
    app, modelname = content_type.split('-')
    likes_template = 'likes/inclusion_tags/likes_%s.html' % modelname

    try:
        template.loader.get_template(likes_template)
    except template.TemplateDoesNotExist:
        likes_template = 'likes/inclusion_tags/likes.html'

    content_type = ContentType.objects.get(app_label=app, model__iexact=modelname)
    if request.is_ajax():
        response = views.vote(
            request,
            content_type=content_type,
            object_id=id,
            vote=vote,
            template_name=likes_template,
            can_vote_test=can_vote_test,
            extra_context={
                'likes_enabled': True,
                'can_vote': False,
                "content_type": url_friendly_content_type,
                "request": request,
            }
        )
    else:
        # Redirect to referer but append unique number(determined
        # from global vote count) to end of URL to bypass local cache.
        redirect_url = '%s?v=%s' % (request.META['HTTP_REFERER'],
                                    random.randint(0, 10))
        response = views.vote(
            request,
            content_type=content_type,
            object_id=id,
            vote=vote,
            redirect_url=redirect_url,
            can_vote_test=can_vote_test
        )

    signals.object_liked.send(sender=content_type.model_class(),
        instance=content_type.get_object_for_this_type(id=id), request=request)
    return response
<commit_msg>Removed debug field<commit_after>import random

from django.http import HttpResponseNotFound
from django.contrib.contenttypes.models import ContentType
from django import template

from secretballot import views
from secretballot.models import Vote

from likes.utils import can_vote
from likes import signals

def can_vote_test(request, content_type, object_id, vote):
    return can_vote(
        content_type.get_object_for_this_type(id=object_id),
        request.user,
        request
    )

def like(request, content_type, id, vote):
    # Crawlers will follow the like link if anonymous liking is enabled. They
    # typically do not have referrer set.
    if 'HTTP_REFERER' not in request.META:
        return HttpResponseNotFound()

    url_friendly_content_type = content_type
    app, modelname = content_type.split('-')
    likes_template = 'likes/inclusion_tags/likes_%s.html' % modelname

    try:
        template.loader.get_template(likes_template)
    except template.TemplateDoesNotExist:
        likes_template = 'likes/inclusion_tags/likes.html'

    content_type = ContentType.objects.get(app_label=app, model__iexact=modelname)
    if request.is_ajax():
        response = views.vote(
            request,
            content_type=content_type,
            object_id=id,
            vote=vote,
            template_name=likes_template,
            can_vote_test=can_vote_test,
            extra_context={
                'likes_enabled': True,
                'can_vote': False,
                "content_type": url_friendly_content_type,
            }
        )
    else:
        # Redirect to referer but append unique number(determined
        # from global vote count) to end of URL to bypass local cache.
        redirect_url = '%s?v=%s' % (request.META['HTTP_REFERER'],
                                    random.randint(0, 10))
        response = views.vote(
            request,
            content_type=content_type,
            object_id=id,
            vote=vote,
            redirect_url=redirect_url,
            can_vote_test=can_vote_test
        )

    signals.object_liked.send(sender=content_type.model_class(),
        instance=content_type.get_object_for_this_type(id=id), request=request)
    return response
<|endoftext|>


<commit_before>import numpy as np

"""
Arguments:
  vec: A numpy 1-D vector.
  size: A 2D tuple

Returns:
  A 2-D vector of dimension 'size', only if 'vec' has compatible dimensions.
  Otherwise it throws an error.
"""
def vectorToImage(vec, size):
  return vec.reshape(size)

""" Transforms the 2D images into 1D vectors
Arguments:
  images: is a python list of numpy arrays
Returns:
  A python list of 1-D numpy arrays, transformed from the input 2D ones
  No data is lost in the transformation.
"""
def imagesToVectors(images):
  return np.array(map(lambda x: x.reshape(-1), images))

def sigmoid(x):
  return 1 / (1 + np.exp(-x))

def softmax(activation):
  out = np.exp(activation)
  return out / out.sum()

# Takes the value of the sigmoid function and returns the derivative
# Works for numpy arrays as well
def softmaxDerivativeFromVal(valueFunction):
  return valueFunction * (1.0 - valueFunction)

def labelsToVectors(labels, size):
  result = np.zeros((len(labels), size), dtype=float)
  for index, label in enumerate(labels):
    result[index, label] = 1.0

  return result

def indexOfMax(l):
  return max(xrange(len(l)),key=l.__getitem__)

# can make the thing class methods
# if I feel like I am hardcore, make this callable and replace value with
# the call
class ActivationFunction(object):

  def value(self, input):
    pass

  # why is this not used anywhere? should be  unless it is used explicitely in the
  # functions below
  def derivativeFromValue(self, val):
    pass

  def derivativeForLinearSum(self, topLayerDerivatives, topLayerActivations):
    pass

class Softmax(ActivationFunction):

  def value(self, inputVector):
    out = np.exp(inputVector)
    return out / out.sum()

  def derivativeFromValue(self, value):
    return value * (1.0 - value)

  def derivativeForLinearSum(self, topLayerDerivatives, topLayerActivations):
    # write it as matrix multiplication
    d = - np.outer(topLayerActivations, topLayerActivations)
    d[np.diag_indices_from(d)] = topLayerActivations * (1 - topLayerActivations)
    return np.dot(topLayerDerivatives, d)

class Sigmoid(ActivationFunction):

  def value(self, inputVector):
    return 1 / (1 + np.exp(-inputVector))

  def derivativeFromValue(self, value):
    return value * (1.0 - value)

  def derivativeForLinearSum(self, topLayerDerivatives, topLayerActivations):
    return topLayerActivations * (1 - topLayerActivations) * topLayerDerivatives
<commit_msg>Added nice notImplementedError for subclasses<commit_after>import numpy as np

"""
Arguments:
  vec: A numpy 1-D vector.
  size: A 2D tuple

Returns:
  A 2-D vector of dimension 'size', only if 'vec' has compatible dimensions.
  Otherwise it throws an error.
"""
def vectorToImage(vec, size):
  return vec.reshape(size)

""" Transforms the 2D images into 1D vectors
Arguments:
  images: is a python list of numpy arrays
Returns:
  A python list of 1-D numpy arrays, transformed from the input 2D ones
  No data is lost in the transformation.
"""
def imagesToVectors(images):
  return np.array(map(lambda x: x.reshape(-1), images))

def sigmoid(x):
  return 1 / (1 + np.exp(-x))

def softmax(activation):
  out = np.exp(activation)
  return out / out.sum()

# Takes the value of the sigmoid function and returns the derivative
# Works for numpy arrays as well
def softmaxDerivativeFromVal(valueFunction):
  return valueFunction * (1.0 - valueFunction)

def labelsToVectors(labels, size):
  result = np.zeros((len(labels), size), dtype=float)
  for index, label in enumerate(labels):
    result[index, label] = 1.0

  return result

def indexOfMax(l):
  return max(xrange(len(l)),key=l.__getitem__)

# can make the thing class methods
# if I feel like I am hardcore, make this callable and replace value with
# the call
class ActivationFunction(object):

  def value(self, input):
    raise NotImplementedError("subclasses must implement method \"value\"")

  # why is this not used anywhere? should be  unless it is used explicitely in the
  # functions below
  def derivativeFromValue(self, val):
    raise NotImplementedError("subclasses must implement method \"derivativeFromValue\"")

  def derivativeForLinearSum(self, topLayerDerivatives, topLayerActivations):
    raise NotImplementedError("subclasses must implement method \"derivativeForLinearSum\"")

class Softmax(ActivationFunction):

  def value(self, inputVector):
    out = np.exp(inputVector)
    return out / out.sum()

  def derivativeFromValue(self, value):
    return value * (1.0 - value)

  def derivativeForLinearSum(self, topLayerDerivatives, topLayerActivations):
    # write it as matrix multiplication
    d = - np.outer(topLayerActivations, topLayerActivations)
    d[np.diag_indices_from(d)] = topLayerActivations * (1 - topLayerActivations)
    return np.dot(topLayerDerivatives, d)

class Sigmoid(ActivationFunction):

  def value(self, inputVector):
    return 1 / (1 + np.exp(-inputVector))

  def derivativeFromValue(self, value):
    return value * (1.0 - value)

  def derivativeForLinearSum(self, topLayerDerivatives, topLayerActivations):
    return topLayerActivations * (1 - topLayerActivations) * topLayerDerivatives
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# (c) 2015 Alex Comba - Agile Business Group
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from openerp import models, api

class StockMove(models.Model):
    _inherit = 'stock.move'

    @api.multi
    def action_confirm(self):
        """
            Pass the incoterm to the picking from the sales order
        """
        procs_to_check = []
        for move in self:
            if (
                move.procurement_id and move.procurement_id.sale_line_id
                and move.procurement_id.sale_line_id.order_id.incoterm
            ):
                procs_to_check += [move.procurement_id]
        res = super(StockMove, self).action_confirm()
        for proc in procs_to_check:
            pickings = proc.mapped('move_ids.picking_id')
            pickings.write(
                {'incoterm': proc.sale_line_id.order_id.incoterm.id})
        return res
<commit_msg>[FIX] stock_picking_invoicing_incoterm_sale: flake8<commit_after># -*- coding: utf-8 -*-
# (c) 2015 Alex Comba - Agile Business Group
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from openerp import models, api

class StockMove(models.Model):
    _inherit = 'stock.move'

    @api.multi
    def action_confirm(self):
        """
            Pass the incoterm to the picking from the sales order
        """
        procs_to_check = []
        for move in self:
            if move.procurement_id.sale_line_id.order_id.incoterm:
                procs_to_check += [move.procurement_id]
        res = super(StockMove, self).action_confirm()
        for proc in procs_to_check:
            pickings = proc.mapped('move_ids.picking_id')
            pickings.write(
                {'incoterm': proc.sale_line_id.order_id.incoterm.id})
        return res
<|endoftext|>


<commit_before>#!/usr/bin/env python
import os
import sys
import shutil
import glob
import odi_config as odi

def main():
    try:
        object_str, filters, instrument, images, illcor_flag, skyflat_src, scale_flag, stack_flag, gaia_flag, cluster_flag, ra_center, dec_center, min_radius = odi.cfgparse('config.yaml')
    except IOError:
        print 'config.yaml does not exist, quitting...'
        exit()

    # os.mkdir(object_str)
#     shutil.copy('derived_props.txt', object_str)
#shutil.copy('config.yaml', object_str)
    fits_files = glob.glob(object_str+'*.fits')
    pl_files = glob.glob(object_str+'*_bpm.pl')
    scale_files = glob.glob('*scales.txt')
    print fits_files, pl_files, scale_files
    pass

if __name__ == '__main__':
    main()
<commit_msg>cleanup executable<commit_after>#!/usr/bin/env python
import os
import sys
import shutil
import glob
import odi_config as odi

def main():
    try:
        object_str, filters, instrument, images, illcor_flag, skyflat_src, wcs_flag, reproject_flag, scale_flag, stack_flag, gaia_flag, cluster_flag, ra_center, dec_center, min_radius = odi.cfgparse('config.yaml')
    except IOError:
        print 'config.yaml does not exist, quitting...'
        exit()

    # os.mkdir(object_str)
#     shutil.copy('derived_props.txt', object_str)
#shutil.copy('config.yaml', object_str)
    fits_files = glob.glob(object_str+'*.fits')
    pl_files = glob.glob(object_str+'*_bpm.pl')
    scale_files = glob.glob('*scales.txt')
    print fits_files, pl_files, scale_files
    pass

if __name__ == '__main__':
    main()
<|endoftext|>


<commit_before>#Copyright (c) 2012, Pablo De La Garza
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
#
#-Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
#-Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#POSSIBILITY OF SUCH DAMAGE.

import os
import json
import subprocess
import re
import zipfile
import zlib
import base64
import hashlib
from datetime import datetime

def quickFileRead(fileFullPath,mode='rt'):

    modeTmp = mode

    if (mode=='json' or
        mode=='csv' or
        mode=='txt'):

        modeTmp = 'rt'

    with open(fileFullPath,modeTmp) as inFile:
        data = inFile.read()

    if mode=='json':
        data = json.loads(data)
    elif mode=='csv':
        data = [row.split(',') for row in data.split('\n') if row!='']
    elif mode=='txt':
        data = data.split('\n')

    return data

def quickFileWrite(fileFullPath,data,mode='wt'):
    dirFullPath = os.path.dirname(fileFullPath)
    createDirectory(dirFullPath)

    if mode=='json':
        data = json.dumps(data,indent=4)
        mode = 'wt'
    elif mode=='csv':
        data = '\n'.join([','.join([str(item) for item in row]) for row in data])
        mode = 'wt'
    elif mode=='txt':
        data = '\n'.join([str(item) for item in data])
        mode = 'wt'

    with open(fileFullPath,mode) as outFile:
        outFile.write(data)

def dateTimeToString(dateTimeObj,formatTypeID=0):
    if formatTypeID==0:
        t = dateTimeObj
        s = t.date().isoformat().replace('-','')+'-'+t.time().isoformat().replace(':','')
        s = s[:15]
        return s
    if formatTypeID==1:
        return str(dateTimeObj).split('.')[0]

def stringToDateTime(string):
    patternList = [
        '%Y%m%d-%H%M%S',
        '%Y-%m-%d %H:%M:%S'
    ]
    for pattern in patternList:
        try:
            obj = datetime.strptime(string,pattern)
            return obj
        except:
            pass
    raise Exception('Unrecognized dateTime format: '+string)

def getTimeStamp(formatTypeID=0,utc=True):
    if utc:
        t = datetime.utcnow()
    else:
        t = datetime.now()
    return dateTimeToString(t,formatTypeID)

def calculateDuration(start,end):
    if isinstance(start,str):
        start = stringToDateTime(start)
    if isinstance(end,str):
        end = stringToDateTime(end)
    delta = end - start
    return delta

def runProgram(programFullPath,shell=False):
    s = subprocess.Popen(programFullPath, stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=shell)
    output, errorCode = s.communicate()
    returnCode = s.returncode
    return {'output':output,'errorCode':errorCode,'returnCode':returnCode}

def getFileSha1(fileNameFullPath):
    sha1Obj = hashlib.sha1()
    sha1Obj.update(quickFileRead(fileNameFullPath,'rb'))
    hexStringSha1 = sha1Obj.hexdigest()
    return hexStringSha1

def pSlice(inFileFullPath,outDirectoryFullPath,sliceSize):

    referenceData = {}
    checksumDict = {}
    referenceData['checksumDict'] = checksumDict

    fileName = os.path.basename(inFileFullPath)

    checksumDict[fileName] = getFileSha1(inFileFullPath)

    counter = 0
    with open(inFileFullPath,'rb') as inFile:
        while True:
            data = inFile.read(sliceSize)
            if not data: break
            counter += 1
            fileFullPath = os.path.join(outDirectoryFullPath,fileName+'.'+str(counter))
            quickFileWrite(fileFullPath,data,'wb')
            checksumDict[os.path.basename(fileFullPath)] = getFileSha1(fileFullPath)

    referenceData['sliceAmount'] = counter
    fileFullPath = os.path.join(outDirectoryFullPath,fileName+'.'+'0')
    quickFileWrite(fileFullPath,json.dumps(referenceData))

    return {'retCode':0,'errMsg':None}

def pUnSlice(inFileFullPath,outFileFullPath):
    base = '.'.join(inFileFullPath.split('.')[:-1])
    referenceData = json.loads(quickFileRead(base+'.0'))
    sliceAmount = referenceData['sliceAmount']

    createDirectory(os.path.dirname(outFileFullPath))
    with open (outFileFullPath,'wb') as outFile:
        for i in range(1,sliceAmount+1):
            data = quickFileRead(base+'.'+str(i),'rb')
            outFile.write(data)

    if getFileSha1(outFileFullPath)!=referenceData['checksumDict'][os.path.basename(inFileFullPath[:-2])]:
        return {'retCode':1,'errMsg':'Checksum for whole file failed'}

    return {'retCode':0,'errMsg':None}

def createZipFile(rootPath,inFileRelativePathList,outFileNameFullPath):
    theZipFile = zipfile.ZipFile(outFileNameFullPath,'w',zipfile.ZIP_DEFLATED)

    for inFileRelativePath in inFileRelativePathList:
        inFileFullPath = os.path.join(rootPath,inFileRelativePath)
        theZipFile.write(inFileFullPath,inFileRelativePath)

    theZipFile.close()
    return outFileNameFullPath

def unzipFile(inFileFullPath,outDirFullPath):
    theZip = zipfile.ZipFile(inFileFullPath,'r')

    fileNameZipPathList=theZip.namelist()
    for fileNameZipPath in fileNameZipPathList:
        path = os.path.join(outDirFullPath,fileNameZipPath)
        if fileNameZipPath[-1]=='/' :
            createDirectory(path)
        else:
            quickFileWrite(path,theZip.read(fileNameZipPath),'wb')
    theZip.close()
    return 0

def pPack(data):
    return base64.b64encode(zlib.compress(data))

def pUnpack(package):
    return zlib.decompress(base64.b64decode(package))

def createDirectory(path):
    if path=='': return 2
    if os.path.exists(path)!=True:
        os.makedirs(path)
        return 0
    else:
        return 1

def removeDirectory(path):
    itemList = os.listdir(path)
    itemList = map(lambda s:os.path.join(path,s),itemList)
    for item in itemList:
        if os.path.isfile(item):
            os.remove(item)
        else:
            removeDirectory(item)
    os.rmdir(path)

def emptyDirectory(path):
    itemList = os.listdir(path)
    itemList = map(lambda s:os.path.join(path,s),itemList)
    for item in itemList:
        if os.path.isfile(item):
            os.remove(item)
        else:
            removeDirectory(item)

def removeDuplicates(itemList):
    itemList.sort()
    itemList2 = []
    old=''
    for l in itemList:
        if old!=l:
            itemList2 = itemList2 +[l]
            old=l
    return itemList2

def removeEntries(mainList,removeList):
    l=[]
    for mainItem in mainList:
            if mainItem not in removeList:
                l.append(mainItem)
    return l

def filterListByRegex(itemList, regex):
    itemList2 = []
    r = re.compile(regex)
    for item in itemList:
        t = r.search(item)
        if t!=None:
            itemList2.append(item)
    return itemList2

def replaceStrings(string,**kwargs):
    for i,j in kwargs.iteritems():
        string = string.replace(i,j)
    return string

def formatHex(data):
    data = bytearray(data)
    return ' '.join(['%02X' % byte for byte in data])

<commit_msg>quickFileRead in 'txt' mode now filters the empty string that is often generated after the line split. If there were empty lines in the files this would no longer make any difference on this output list.<commit_after>#Copyright (c) 2012, Pablo De La Garza
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
#
#-Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
#-Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
#LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
#INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
#ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#POSSIBILITY OF SUCH DAMAGE.

import os
import json
import subprocess
import re
import zipfile
import zlib
import base64
import hashlib
from datetime import datetime

def quickFileRead(fileFullPath,mode='rt'):

    modeTmp = mode

    if (mode=='json' or
        mode=='csv' or
        mode=='txt'):

        modeTmp = 'rt'

    with open(fileFullPath,modeTmp) as inFile:
        data = inFile.read()

    if mode=='json':
        data = json.loads(data)
    elif mode=='csv':
        data = [row.split(',') for row in data.split('\n') if row!='']
    elif mode=='txt':
        data = [item for item in data.split('\n') if item!='']

    return data

def quickFileWrite(fileFullPath,data,mode='wt'):
    dirFullPath = os.path.dirname(fileFullPath)
    createDirectory(dirFullPath)

    if mode=='json':
        data = json.dumps(data,indent=4)
        mode = 'wt'
    elif mode=='csv':
        data = '\n'.join([','.join([str(item) for item in row]) for row in data])
        mode = 'wt'
    elif mode=='txt':
        data = '\n'.join([str(item) for item in data])
        mode = 'wt'

    with open(fileFullPath,mode) as outFile:
        outFile.write(data)

def dateTimeToString(dateTimeObj,formatTypeID=0):
    if formatTypeID==0:
        t = dateTimeObj
        s = t.date().isoformat().replace('-','')+'-'+t.time().isoformat().replace(':','')
        s = s[:15]
        return s
    if formatTypeID==1:
        return str(dateTimeObj).split('.')[0]

def stringToDateTime(string):
    patternList = [
        '%Y%m%d-%H%M%S',
        '%Y-%m-%d %H:%M:%S'
    ]
    for pattern in patternList:
        try:
            obj = datetime.strptime(string,pattern)
            return obj
        except:
            pass
    raise Exception('Unrecognized dateTime format: '+string)

def getTimeStamp(formatTypeID=0,utc=True):
    if utc:
        t = datetime.utcnow()
    else:
        t = datetime.now()
    return dateTimeToString(t,formatTypeID)

def calculateDuration(start,end):
    if isinstance(start,str):
        start = stringToDateTime(start)
    if isinstance(end,str):
        end = stringToDateTime(end)
    delta = end - start
    return delta

def runProgram(programFullPath,shell=False):
    s = subprocess.Popen(programFullPath, stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=shell)
    output, errorCode = s.communicate()
    returnCode = s.returncode
    return {'output':output,'errorCode':errorCode,'returnCode':returnCode}

def getFileSha1(fileNameFullPath):
    sha1Obj = hashlib.sha1()
    sha1Obj.update(quickFileRead(fileNameFullPath,'rb'))
    hexStringSha1 = sha1Obj.hexdigest()
    return hexStringSha1

def pSlice(inFileFullPath,outDirectoryFullPath,sliceSize):

    referenceData = {}
    checksumDict = {}
    referenceData['checksumDict'] = checksumDict

    fileName = os.path.basename(inFileFullPath)

    checksumDict[fileName] = getFileSha1(inFileFullPath)

    counter = 0
    with open(inFileFullPath,'rb') as inFile:
        while True:
            data = inFile.read(sliceSize)
            if not data: break
            counter += 1
            fileFullPath = os.path.join(outDirectoryFullPath,fileName+'.'+str(counter))
            quickFileWrite(fileFullPath,data,'wb')
            checksumDict[os.path.basename(fileFullPath)] = getFileSha1(fileFullPath)

    referenceData['sliceAmount'] = counter
    fileFullPath = os.path.join(outDirectoryFullPath,fileName+'.'+'0')
    quickFileWrite(fileFullPath,json.dumps(referenceData))

    return {'retCode':0,'errMsg':None}

def pUnSlice(inFileFullPath,outFileFullPath):
    base = '.'.join(inFileFullPath.split('.')[:-1])
    referenceData = json.loads(quickFileRead(base+'.0'))
    sliceAmount = referenceData['sliceAmount']

    createDirectory(os.path.dirname(outFileFullPath))
    with open (outFileFullPath,'wb') as outFile:
        for i in range(1,sliceAmount+1):
            data = quickFileRead(base+'.'+str(i),'rb')
            outFile.write(data)

    if getFileSha1(outFileFullPath)!=referenceData['checksumDict'][os.path.basename(inFileFullPath[:-2])]:
        return {'retCode':1,'errMsg':'Checksum for whole file failed'}

    return {'retCode':0,'errMsg':None}

def createZipFile(rootPath,inFileRelativePathList,outFileNameFullPath):
    theZipFile = zipfile.ZipFile(outFileNameFullPath,'w',zipfile.ZIP_DEFLATED)

    for inFileRelativePath in inFileRelativePathList:
        inFileFullPath = os.path.join(rootPath,inFileRelativePath)
        theZipFile.write(inFileFullPath,inFileRelativePath)

    theZipFile.close()
    return outFileNameFullPath

def unzipFile(inFileFullPath,outDirFullPath):
    theZip = zipfile.ZipFile(inFileFullPath,'r')

    fileNameZipPathList=theZip.namelist()
    for fileNameZipPath in fileNameZipPathList:
        path = os.path.join(outDirFullPath,fileNameZipPath)
        if fileNameZipPath[-1]=='/' :
            createDirectory(path)
        else:
            quickFileWrite(path,theZip.read(fileNameZipPath),'wb')
    theZip.close()
    return 0

def pPack(data):
    return base64.b64encode(zlib.compress(data))

def pUnpack(package):
    return zlib.decompress(base64.b64decode(package))

def createDirectory(path):
    if path=='': return 2
    if os.path.exists(path)!=True:
        os.makedirs(path)
        return 0
    else:
        return 1

def removeDirectory(path):
    itemList = os.listdir(path)
    itemList = map(lambda s:os.path.join(path,s),itemList)
    for item in itemList:
        if os.path.isfile(item):
            os.remove(item)
        else:
            removeDirectory(item)
    os.rmdir(path)

def emptyDirectory(path):
    itemList = os.listdir(path)
    itemList = map(lambda s:os.path.join(path,s),itemList)
    for item in itemList:
        if os.path.isfile(item):
            os.remove(item)
        else:
            removeDirectory(item)

def removeDuplicates(itemList):
    itemList.sort()
    itemList2 = []
    old=''
    for l in itemList:
        if old!=l:
            itemList2 = itemList2 +[l]
            old=l
    return itemList2

def removeEntries(mainList,removeList):
    l=[]
    for mainItem in mainList:
            if mainItem not in removeList:
                l.append(mainItem)
    return l

def filterListByRegex(itemList, regex):
    itemList2 = []
    r = re.compile(regex)
    for item in itemList:
        t = r.search(item)
        if t!=None:
            itemList2.append(item)
    return itemList2

def replaceStrings(string,**kwargs):
    for i,j in kwargs.iteritems():
        string = string.replace(i,j)
    return string

def formatHex(data):
    data = bytearray(data)
    return ' '.join(['%02X' % byte for byte in data])

<|endoftext|>


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

class Node(object):
    def __init__(self, data, nextNode=None):
        self.data = data
        self.nextNode = nextNode

class LinkedList(object):
    def __init__(self, firstNode=None):
        self.firstNode = firstNode

    def insert(self, newNode):
        # insert newNode at beginning of list
        if not self.firstNode:
            self.firstNode = newNode
        else:
            newNode.nextNode = self.firstNode
            self.firstNode = newNode

    def pop(self):
        # pops first value from list and returns it
        obsoleteNode = self.firstNode
        self.firstNode = self.firstNode.nextNode
        return obsoleteNode

    def size(self):
        # returns length of list
        currentNode = self.firstNode
        size = 0
        while currentNode is not None:
            size += 1
            currentNode = self.nextNode
        return size

    def search(self, val):
        # return node containing 'val' in list, if present (else None)
        pass

    def remove(self, node):
        # remove node from list, wherever it might be
        if size == 0:
            return "List is empty"
        else:
            pass

    def display(self):
        # print list as python tuple literal
        # (bonus points if you make it appear like "(12, 'sam', 32, 'fred')")
        for node in self:
            print self.firstNode
<commit_msg>added search func<commit_after>#!/usr/bin/env python

class Node(object):
    def __init__(self, data, nextNode=None):
        self.data = data
        self.nextNode = nextNode

class LinkedList(object):
    def __init__(self, firstNode=None):
        self.firstNode = firstNode

    def insert(self, newNode):
        # insert newNode at beginning of list
        if not self.firstNode:
            self.firstNode = newNode
        else:
            newNode.nextNode = self.firstNode
            self.firstNode = newNode

    def pop(self):
        # pops first value from list and returns it
        obsoleteNode = self.firstNode
        self.firstNode = self.firstNode.nextNode
        return obsoleteNode

    def size(self):
        # returns length of list
        currentNode = self.firstNode
        size = 0
        while currentNode is not None:
            size += 1
            currentNode = self.nextNode
        return size

    def search(self, val):
        # return node containing 'val' in list, if present (else None)
        currentNode = self.firstNode
        while currentNode != val:
            if self.nextNode is None:
                return None
            else:
                currentNode = self.nextNode
        return val

    def remove(self, node):
        # remove node from list, wherever it might be
        if self.size == 0:
            return "List is empty"
        else:
            pass

    def display(self):
        # print list as python tuple literal
        # (bonus points if you make it appear like "(12, 'sam', 32, 'fred')")
        for node in self:
            print self.firstNode
<|endoftext|>