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

import sys
import random
import logging
import math

from collections import namedtuple
from functools import partial
from itertools import takewhile, repeat, islice, zip_longest

from imread import imread
import numpy as np
from moviepy.editor import VideoClip
import nphusl

logging.basicConfig(level=logging.INFO)

###################
# Filtering pixels

_props = namedtuple("channel", "h s l")

def hsl_filter(h=(), s=(), l=()):
    """Returns a namedtuple of properties that are used to find
    'channels' of a certain hue, saturation, and/or lightness in an image.
    If the user wants to filter for light rows/columns of pixels, they might
    use `hsl_filter(l=[90.5, 99.0])."""
    h, s, l = (tuple(p) for p in [h, s, l])  # let's make them all tuples

    # let's make sure the user has specified (min, max) for HSL
    if any(len(p) not in (0, 2) for p in (h, s, l)):
        raise ValueError("HSL filters can only be tuples like (min, max)")

    # let's make sure min, max values fit within their appropriate range
    if h:
        h = _valid_h(*h)
    if s:
        s = _valid_s(*s)
    if l:
        l = _valid_l(*l)

    return _props(h, s, l)

def _valid_h(hmin, hmax):
    return (max(hmin, 0), max(hmax, 0))

def _valid_s(smin, smax):
    return (min(max(smin, 0), 100),
            min(max(smax, 0), 100))

_valid_l = _valid_s  # saturation has the same range as lightness

def get_channel(img, filter_hsl, avg_husl):
    """Returns row indices for which the HSL filter constraints are met"""
    idx_select = np.ones(img.shape[0], dtype=bool)  # no "rows" selected initially
    for prop_idx, prop in enumerate(filter_hsl):
        if not prop:
            continue
        pmin, pmax = prop
        avg = avg_husl[:, prop_idx]
        # add/subtract 0.1 for convenient comparison of numpy arrays of floats
        # i.e. we want 100 to be not < 100.0000000000023
        idx_select[avg < (pmin - 0.1)] = 0
        idx_select[avg > (pmax + 0.1)] = 0
    return idx_select

################
# Moving pixels

def move(i, travel):
    """Shift `img` a distance of `travel` in the positive direction.
    The pixel array wraps around, so the last pixels will end up being
    the first pixels."""
    # NOTE: This copy is needed. Doing a backwards slice assignment is a good
    # workaround for an in place shift (numpy.roll creates a copy!), but it
    # is a real hack. Backwards slice assignment will work with c array
    # ordering, for example, but will break for Fortran style arrays.
    if travel < 0:
        tail = i[:-travel].copy()
        i[:travel] = i[-travel:]
        i[travel:] = tail
    else:
        tail = i[-travel:].copy()  # pixel array wraps around
        i[travel:] = i[:-travel]  # move bulk of pixels
        i[:travel] = tail  # move the saved `tail` into vacated space
    return i

def flipped(fn):
    def wrapper(img, select, *args, **kwargs):
        _img = np.rot90(img)
        _select = np.rot90(select)
        for _ in fn(_img, _select, *args, **kwargs):
            yield img
    return wrapper

def run_while_changed(fn, img, *args, **kwargs):
    changed = True
    while changed:
        changed = fn(img, *args, **kwargs)
        yield img

def mover(transform, fn, *args, **kwargs):
    return transform(fn(*args, **kwargs))

def move_chunks(moves):
    moved = 0
    for arr, travel in moves:
        moved += arr.shape[0]
        move(arr, travel)
    return moved

def move_chunks_back(moves):
    backward_moves = ((arr, -travel) for arr, travel in moves)
    return move_chunks(backward_moves)

move_forward = partial(mover, move_chunks)
move_backward = partial(mover, move_chunks_back)

def clump_cols(img, select, moves):
    rwhere, cwhere = np.nonzero(select)
    total_avg = np.mean(rwhere)
    cols = np.nonzero(np.any(select, axis=0))[0]
    if len(moves) == 1:
        travels = np.zeros((cols.size,))
        travels[:] = moves[0]
    else:
        travels = np.random.choice(moves, cols.size)
    nz = np.nonzero(travels)
    cols, travels = cols[nz], travels[nz]

    s = select[:, cols]
    row_indices = np.arange(0, img.shape[0])
    row_matrix = np.zeros(select.shape, dtype=np.float)
    rm = row_matrix.swapaxes(0, 1)
    rm[:, None] = row_indices
    row_matrix = rm.swapaxes(0, 1)
    row_matrix[~select] = np.nan
    valid_cols = ~np.all(np.isnan(row_matrix), axis=0)
    row_matrix = row_matrix[:, valid_cols]

    col_avgs = np.nanmean(row_matrix, axis=0)
    diff = col_avgs - total_avg
    abs_diff = np.abs(diff)

    # when columns are as close the median as they can be, we want them to
    # gradually move with a random choice between moving 0 or 1 pixels
    stuck = abs_diff < travels
    travels[stuck] = np.random.choice((0, 1), np.count_nonzero(stuck))
    travels[diff > 0] *= -1  # reverse row travel dir if row avg < total avg
    nz = np.nonzero(travels)
    travels = travels[nz]
    cols = cols[nz]

    all_travels = tuple(-m for m in moves) + moves
    for travel in all_travels:
        cols_to_move = cols[travels == travel]
        if not cols_to_move.size:
            continue
        yield from _chunk_select(cols_to_move, img, select, travel)

def _chunk_select(indices, img, select, travel):
    """Generate contiguous chunks of indices in tuples of
    (start_index, stop_index) where stop_index is not inclusive"""
    contiguous = np.diff(indices) == 1
    row_cont = zip_longest(indices, contiguous)
    for left, do_continue in row_cont:
        if not do_continue:
            yield img[:, left], travel
            yield select[:, left], travel
        else:
            for right, do_continue in row_cont:
                if not do_continue:
                    yield img[:, left: right + 1], travel
                    yield select[:, left: right + 1], travel
                    break

clump = partial(move_forward, clump_cols)
clump_vert = partial(run_while_changed, clump)
clump_horz = flipped(clump_vert)
disperse = partial(move_backward, clump_cols)
disperse_vert = partial(run_while_changed, disperse)
disperse_horz = flipped(disperse_vert)

def fuzz_horz(img, select, moves=(0, 1)):
    moves = tuple(moves)
    fuzz_moves = tuple(-m for m in moves if m) + moves
    while True:
        rows = np.nonzero(np.any(select, axis=1))[0]
        fuzz_rows(img, select, rows, fuzz_moves)
        yield img

fuzz_vert = flipped(fuzz_horz)

def fuzz_rows(img, select, rows, moves):
    travels = np.random.choice(moves, len(rows))
    for row, travel in zip(rows, travels):
        if not travel:
            continue
        move(img[row, :], travel)
        move(select[row, :], travel)

######################
# Creating animations

def read_img(path):
    img = np.squeeze(imread(path))
    if len(img.shape) == 2:
        _img = np.ndarray(img.shape + (3,), dtype=img.dtype)
        _img[:] = img[..., None]
        img = _img
    elif img.shape[-1] == 4:
        # we can't handle an RGBA array
        img = img[..., :3]
    logging.info("Initial image shape: {}".format(img.shape))
    logging.info("Working image shape: {}".format(img.shape))
    return img

def dark_clump_turns(img, select, moves):
    yield img
    while True:
        for _ in clump_vert(img, select, moves):
            yield img
        for _ in clump_horz(img, select, moves):
            yield img

def zip_effects(img, *effects):
    try:
        yield img
        for imgs in zip(*effects):
            yield imgs[-1]
    except KeyboardInterrupt:
        raise StopIteration

def interleave_effects(img, *effects, repeats=1):
    try:
        yield img
        for effect in effects:
            for _ in range(repeats):
                yield next(effect)
    except KeyboardInterrupt:
        raise StopIteration

def frame_maker(effects):
    def make(_):
        return next(effects)
    return make

#################
# Whole programs

def clump_dark(filename, percentile=4.0):
    img = read_img(filename)
    hsl = nphusl.to_husl(img)
    _, _, L = (hsl[..., n] for n in range(3))
    dark = L > np.percentile(L, 6.0)
    logging.info("Selection ratio: {:1.1f}%".format(
                 100 * np.count_nonzero(dark) / dark.size))
    travel = ( 1,)
    vert = clump_vert(img, dark, travel)
    horz = clump_horz(img, dark, travel)
    return zip_effects(img, horz, vert)

if __name__ == "__main__":
    infile, outfile = sys.argv[1: 3]
    frames = clump_dark(infile, 4.0)
    make_frame = frame_maker(frames)
    animation = VideoClip(make_frame, duration=6)
    animation.write_videofile(outfile, fps=24, audio=False, threads=2,
                              preset="ultrafast")

<commit_msg>minor refactoring; new effect generator<commit_after>#!/usr/bin/env python
# coding: utf-8

import sys
import random
import logging
import math

from collections import namedtuple
from functools import partial
from itertools import takewhile, repeat, islice, zip_longest

from imread import imread, imwrite
import numpy as np
from moviepy.editor import VideoClip
import nphusl

logging.basicConfig(level=logging.INFO)

###################
# Filtering pixels

_props = namedtuple("channel", "h s l")

def hsl_filter(h=(), s=(), l=()):
    """Returns a namedtuple of properties that are used to find
    'channels' of a certain hue, saturation, and/or lightness in an image.
    If the user wants to filter for light rows/columns of pixels, they might
    use `hsl_filter(l=[90.5, 99.0])."""
    h, s, l = (tuple(p) for p in [h, s, l])  # let's make them all tuples

    # let's make sure the user has specified (min, max) for HSL
    if any(len(p) not in (0, 2) for p in (h, s, l)):
        raise ValueError("HSL filters can only be tuples like (min, max)")

    # let's make sure min, max values fit within their appropriate range
    if h:
        h = _valid_h(*h)
    if s:
        s = _valid_s(*s)
    if l:
        l = _valid_l(*l)

    return _props(h, s, l)

def _valid_h(hmin, hmax):
    return (max(hmin, 0), max(hmax, 0))

def _valid_s(smin, smax):
    return (min(max(smin, 0), 100),
            min(max(smax, 0), 100))

_valid_l = _valid_s  # saturation has the same range as lightness

def get_channel(img, filter_hsl, avg_husl):
    """Returns row indices for which the HSL filter constraints are met"""
    idx_select = np.ones(img.shape[0], dtype=bool)  # no "rows" selected initially
    for prop_idx, prop in enumerate(filter_hsl):
        if not prop:
            continue
        pmin, pmax = prop
        avg = avg_husl[:, prop_idx]
        # add/subtract 0.1 for convenient comparison of numpy arrays of floats
        # i.e. we want 100 to be not < 100.0000000000023
        idx_select[avg < (pmin - 0.1)] = 0
        idx_select[avg > (pmax + 0.1)] = 0
    return idx_select

################
# Moving pixels

@profile
def move(img, travel):
    """Shift `img` a distance of `travel` in the positive direction.
    The array wraps around, so the last pixels will end up being
    the first pixels."""
    # NOTE: This copy is needed. Doing a backwards slice assignment is a good
    # workaround for an in place shift (numpy.roll creates a copy!), but it
    # is a real hack. Backwards slice assignment will work with c array
    # ordering, for example, but will break for Fortran style arrays.
    if travel < 0:
        tail = img[:-travel].copy()
        img[:travel] = img[-travel:]
        img[travel:] = tail
    else:
        tail = img[-travel:].copy()  # pimgxel array wraps around
        img[travel:] = img[:-travel]  # move bulk of pimgxels
        img[:travel] = tail  # move the saved `taimgl` imgnto vacated space
    return img

def flipped(fn):
    def wrapper(img, select, *args, **kwargs):
        _img = np.rot90(img)
        _select = np.rot90(select)
        for _ in fn(_img, _select, *args, **kwargs):
            yield img
    return wrapper

@profile
def run_forever(fn, img, *args, **kwargs):
    while True:
        fn(img, *args, **kwargs)
        yield img

def mover(transform, fn, *args, **kwargs):
    return transform(fn(*args, **kwargs))

@profile
def move_chunks(moves):
    for arr, travel in moves:
        move(arr, travel)

def move_chunks_back(moves):
    backward_moves = ((arr, -travel) for arr, travel in moves)
    return move_chunks(backward_moves)

move_forward = partial(mover, move_chunks)
move_backward = partial(mover, move_chunks_back)

@profile
def clump_cols(img, select, moves):
    rwhere, cwhere = np.nonzero(select)
    if not rwhere.size:
        return
    total_avg = np.mean(rwhere)
    cols = np.nonzero(np.any(select, axis=0))[0]
    if len(moves) == 1:
        travels = np.zeros((cols.size,))
        travels[:] = moves[0]
    else:
        travels = np.random.choice(moves, cols.size)
    nz = np.nonzero(travels)
    cols, travels = cols[nz], travels[nz]

    s = select[:, cols]
    row_indices = np.arange(0, img.shape[0])
    row_matrix = np.zeros(select.shape, dtype=np.float)
    rm = row_matrix.swapaxes(0, 1)
    rm[:, None] = row_indices
    row_matrix = rm.swapaxes(0, 1)
    row_matrix[~select] = np.nan
    valid_cols = ~np.all(np.isnan(row_matrix), axis=0)
    row_matrix = row_matrix[:, valid_cols]

    col_avgs = np.nanmean(row_matrix, axis=0)
    diff = col_avgs - total_avg
    abs_diff = np.abs(diff)

    travels[diff > 0] *= -1  # reverse row travel dir if row avg < total avg
    nz = np.nonzero(travels)
    travels = travels[nz]
    cols = cols[nz]
    all_travels = tuple(-m for m in moves) + moves
    for travel in all_travels:
        cols_to_move = cols[travels == travel]
        if not cols_to_move.size:
            continue
        yield from _chunk_select(cols_to_move, img, select, travel)

@profile
def _chunk_select(indices, img, select, travel):
    """Generate contiguous chunks of indices in tuples of
    (start_index, stop_index) where stop_index is not inclusive"""
    contiguous = np.diff(indices) == 1
    row_cont = zip_longest(indices, contiguous)
    for left, do_continue in row_cont:
        if not do_continue:
            yield img[:, left], travel
            yield select[:, left], travel
        else:
            for right, do_continue in row_cont:
                if not do_continue:
                    yield img[:, left: right + 1], travel
                    yield select[:, left: right + 1], travel
                    break

clump = partial(move_forward, clump_cols)
clump_vert = partial(run_forever, clump)
clump_horz = flipped(clump_vert)
disperse = partial(move_backward, clump_cols)
disperse_vert = partial(run_forever, disperse)
disperse_horz = flipped(disperse_vert)

def fuzz_horz(img, select, moves=(0, 1)):
    moves = tuple(moves)
    fuzz_moves = tuple(-m for m in moves if m) + moves
    while True:
        rows = np.nonzero(np.any(select, axis=1))[0]
        fuzz_rows(img, select, rows, fuzz_moves)
        yield img

fuzz_vert = flipped(fuzz_horz)

def fuzz_rows(img, select, rows, moves):
    travels = np.random.choice(moves, len(rows))
    for row, travel in zip(rows, travels):
        if not travel:
            continue
        move(img[row, :], travel)
        move(select[row, :], travel)

######################
# Creating animations

def read_img(path):
    img = np.squeeze(imread(path))
    if len(img.shape) == 2:
        _img = np.ndarray(img.shape + (3,), dtype=img.dtype)
        _img[:] = img[..., None]
        img = _img
    elif img.shape[-1] == 4:
        # we can't handle an RGBA array
        img = img[..., :3]
    logging.info("Initial image shape: {}".format(img.shape))
    logging.info("Working image shape: {}".format(img.shape))
    return img

def dark_clump_turns(img, select, moves):
    yield img
    while True:
        for _ in clump_vert(img, select, moves):
            yield img
        for _ in clump_horz(img, select, moves):
            yield img

def zip_effects(img, *effects):
    try:
        yield img
        for imgs in zip(*effects):
            yield imgs[-1]
    except KeyboardInterrupt:
        raise StopIteration

def interleave_effects(img, *effects,
                       repeats=1, effects_per_frame=1, rand=False):
    effects = list(effects)
    try:
        yield from _iterleave(img, effects, repeats, effects_per_frame, rand)
    except KeyboardInterrupt:
        raise StopIteration

def _iterleave(img, effects, repeats, effects_per_frame, rand):
    yield img
    while True:
        count = 0
        if rand:
            random.shuffle(effects)
        for effect in effects:
            for _ in range(repeats):
                e = next(effect)
                count += 1
                if not count % effects_per_frame:
                    yield e

def frame_maker(effects):
    def make(_):
        return next(effects)
    return make

#################
# Whole programs

def clump_dark(filename, percentile=4.0):
    img = read_img(filename)
    hsl = nphusl.to_husl(img)
    H, _, L = (hsl[..., n] for n in range(3))
    dark = L < np.percentile(L, 6.0)
    logging.info("Selection ratio: {:1.1f}%".format(
                 100 * np.count_nonzero(dark) / dark.size))
    travel = (1,)
    vert = clump_vert(img, dark, travel)
    horz = clump_horz(img, dark, travel)
    return zip_effects(img, horz, vert)

def clump_hues(filename):
    img = read_img(filename)
    hsl = nphusl.to_husl(img)
    H, _, L = (hsl[..., n] for n in range(3))
    light = L > 5
    light_hues = H[light]
    travel = (1,)

    def gen_selects():
        min_hue = min_pct = 0
        for max_pct in range(20, 120, 20):
            max_hue = np.percentile(light_hues, max_pct)
            select = np.logical_and(H < max_hue, H > min_hue)
            select = np.logical_and(light, select)
            yield clump_horz(img, select, travel)
            yield clump_vert(img, select, travel)
            min_hue = max_hue

    return zip_effects(img, *gen_selects())

def blueb(img):
    hsl = nphusl.to_husl(img)
    H, _, L = (hsl[..., n] for n in range(3))
    dark = L < 5
    bright = L > 80
    travel = (1,)
    blue = np.logical_and(H > 240, H < 290)

    hblue = clump_horz(img, bright, travel)
    vblue = clump_vert(img, bright, travel)
    hdark = disperse_horz(img, dark, travel)
    vdark = disperse_vert(img, dark, travel)
    return zip_effects(img, hblue, vblue, hdark, vdark)

if __name__ == "__main__":
    infile, outfile = sys.argv[1: 3]
    img = imread(infile)
    frames = blueb(img)
    make_frame = frame_maker(frames)
    animation = VideoClip(make_frame, duration=60)
    try:
        animation.write_videofile(outfile, fps=24, audio=False, threads=2,
                                  preset="fast")
    except KeyboardInterrupt:
        pass
    imwrite("last.jpg", img)

<|endoftext|>


<commit_before># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This script is used to synthesize generated parts of this library."""

import os
import synthtool as s
import synthtool.gcp as gcp
import logging

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICGenerator()
common = gcp.CommonTemplates()

library = gapic.php_library(
    service='clouderrorreporting',
    version='v1beta1',
    config_path='/google/devtools/clouderrorreporting/artman_errorreporting.yaml',
    artman_output_name='google-cloud-error-reporting-v1beta1')

# copy all src including partial veneer classes
s.move(library / 'src')

# copy proto files to src also
s.move(library / 'proto/src/Google/Cloud/ErrorReporting', 'src/')
s.move(library / 'tests/')

# copy GPBMetadata file to metadata
s.move(library / 'proto/src/GPBMetadata/Google/Devtools/Clouderrorreporting', 'metadata/')

# document and utilize apiEndpoint instead of serviceAddress
s.replace(
    "**/Gapic/*GapicClient.php",
    r"'serviceAddress' =>",
    r"'apiEndpoint' =>")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"@type string \$serviceAddress\n\s+\*\s+The address",
    r"""@type string $serviceAddress
     *           **Deprecated**. This option will be removed in a future major release. Please
     *           utilize the `$apiEndpoint` option instead.
     *     @type string $apiEndpoint
     *           The address""")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"\$transportConfig, and any \$serviceAddress",
    r"$transportConfig, and any `$apiEndpoint`")

# prevent proto messages from being marked final
s.replace(
    "src/V*/**/*.php",
    r"final class",
    r"class")

# Replace "Unwrapped" with "Value" for method names.
s.replace(
    "src/V*/**/*.php",
    r"public function (\w{0,})Unwrapped",
    r"public function \1Value"
)

# fix year
s.replace(
    '**/Gapic/*GapicClient.php',
    r'Copyright \d{4}',
    'Copyright 2017')
for client in ['ErrorGroupService', 'ErrorStatsService', 'ReportErrorsService']:
    s.replace(
        f'**/V1beta1/{client}Client.php',
        r'Copyright \d{4}',
        'Copyright 2017')
s.replace(
    'tests/**/V1beta1/*Test.php',
    r'Copyright \d{4}',
    'Copyright 2018')
<commit_msg>chore: Fix Unwrapped regex to be more specific (#2289)<commit_after># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This script is used to synthesize generated parts of this library."""

import os
import synthtool as s
import synthtool.gcp as gcp
import logging

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICGenerator()
common = gcp.CommonTemplates()

library = gapic.php_library(
    service='clouderrorreporting',
    version='v1beta1',
    config_path='/google/devtools/clouderrorreporting/artman_errorreporting.yaml',
    artman_output_name='google-cloud-error-reporting-v1beta1')

# copy all src including partial veneer classes
s.move(library / 'src')

# copy proto files to src also
s.move(library / 'proto/src/Google/Cloud/ErrorReporting', 'src/')
s.move(library / 'tests/')

# copy GPBMetadata file to metadata
s.move(library / 'proto/src/GPBMetadata/Google/Devtools/Clouderrorreporting', 'metadata/')

# document and utilize apiEndpoint instead of serviceAddress
s.replace(
    "**/Gapic/*GapicClient.php",
    r"'serviceAddress' =>",
    r"'apiEndpoint' =>")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"@type string \$serviceAddress\n\s+\*\s+The address",
    r"""@type string $serviceAddress
     *           **Deprecated**. This option will be removed in a future major release. Please
     *           utilize the `$apiEndpoint` option instead.
     *     @type string $apiEndpoint
     *           The address""")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"\$transportConfig, and any \$serviceAddress",
    r"$transportConfig, and any `$apiEndpoint`")

# prevent proto messages from being marked final
s.replace(
    "src/V*/**/*.php",
    r"final class",
    r"class")

# Replace "Unwrapped" with "Value" for method names.
s.replace(
    "src/V*/**/*.php",
    r"public function ([s|g]\w{3,})Unwrapped",
    r"public function \1Value"
)

# fix year
s.replace(
    '**/Gapic/*GapicClient.php',
    r'Copyright \d{4}',
    'Copyright 2017')
for client in ['ErrorGroupService', 'ErrorStatsService', 'ReportErrorsService']:
    s.replace(
        f'**/V1beta1/{client}Client.php',
        r'Copyright \d{4}',
        'Copyright 2017')
s.replace(
    'tests/**/V1beta1/*Test.php',
    r'Copyright \d{4}',
    'Copyright 2018')
<|endoftext|>


<commit_before># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This script is used to synthesize generated parts of this library."""

import os
import synthtool as s
import synthtool.gcp as gcp
import logging

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICGenerator()
common = gcp.CommonTemplates()

library = gapic.php_library(
    service='logging',
    version='v2',
    config_path='/google/logging/artman_logging.yaml',
    artman_output_name='google-cloud-logging-v2')

# copy all src including partial veneer classes
s.move(library / 'src')

# copy proto files to src also
s.move(
    library / 'proto/src/Google/Cloud/Logging',
    'src/',
    [library / 'proto/src/Google/Cloud/Logging/Type'])

s.move(library / 'tests/')

# copy GPBMetadata file to metadata
s.move(
    library / 'proto/src/GPBMetadata/Google/Logging',
    'metadata/',
    [library / 'proto/src/GPBMetadata/Google/Logging/Type'])

# document and utilize apiEndpoint instead of serviceAddress
s.replace(
    "**/Gapic/*GapicClient.php",
    r"'serviceAddress' =>",
    r"'apiEndpoint' =>")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"@type string \$serviceAddress\n\s+\*\s+The address",
    r"""@type string $serviceAddress
     *           **Deprecated**. This option will be removed in a future major release. Please
     *           utilize the `$apiEndpoint` option instead.
     *     @type string $apiEndpoint
     *           The address""")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"\$transportConfig, and any \$serviceAddress",
    r"$transportConfig, and any `$apiEndpoint`")

# fix year
s.replace(
    '**/Gapic/*GapicClient.php',
    r'Copyright \d{4}',
    r'Copyright 2016')
for client in ['ConfigServiceV2', 'LoggingServiceV2', 'MetricsServiceV2']:
    s.replace(
        f'**/V2/{client}Client.php',
        r'Copyright \d{4}',
        'Copyright 2016')
s.replace(
    'tests/**/V2/*Test.php',
    r'Copyright \d{4}',
    r'Copyright 2018')

### [START] protoc backwards compatibility fixes

# roll back to private properties.
s.replace(
    "src/**/V*/**/*.php",
    r"Generated from protobuf field ([^\n]{0,})\n\s{5}\*/\n\s{4}protected \$",
    r"""Generated from protobuf field \1
     */
    private $""")

# prevent proto messages from being marked final
s.replace(
    "src/**/V*/**/*.php",
    r"final class",
    r"class")

# Replace "Unwrapped" with "Value" for method names.
s.replace(
    "src/**/V*/**/*.php",
    r"public function ([s|g]\w{3,})Unwrapped",
    r"public function \1Value"
)

### [END] protoc backwards compatibility fixes

# fix relative cloud.google.com links
s.replace(
    "src/**/V*/**/*.php",
    r"(.{0,})\]\((/.{0,})\)",
    r"\1](https://cloud.google.com\2)"
)
<commit_msg>chore: Migrate all synth.py scripts from artman to bazel (#2845)<commit_after># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This script is used to synthesize generated parts of this library."""

import synthtool as s
import synthtool.gcp as gcp
import logging

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICBazel()
common = gcp.CommonTemplates()

library = gapic.php_library(
    service='logging',
    version='v2',
    bazel_target='//google/logging/v2:google-cloud-logging-v2-php'
)

# copy all src including partial veneer classes
s.move(library / 'src')

# copy proto files to src also
s.move(
    library / 'proto/src/Google/Cloud/Logging',
    'src/',
    [library / 'proto/src/Google/Cloud/Logging/Type'])

s.move(library / 'tests/')

# copy GPBMetadata file to metadata
s.move(
    library / 'proto/src/GPBMetadata/Google/Logging',
    'metadata/',
    [library / 'proto/src/GPBMetadata/Google/Logging/Type'])

# document and utilize apiEndpoint instead of serviceAddress
s.replace(
    "**/Gapic/*GapicClient.php",
    r"'serviceAddress' =>",
    r"'apiEndpoint' =>")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"@type string \$serviceAddress\n\s+\*\s+The address",
    r"""@type string $serviceAddress
     *           **Deprecated**. This option will be removed in a future major release. Please
     *           utilize the `$apiEndpoint` option instead.
     *     @type string $apiEndpoint
     *           The address""")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"\$transportConfig, and any \$serviceAddress",
    r"$transportConfig, and any `$apiEndpoint`")

# fix year
s.replace(
    '**/Gapic/*GapicClient.php',
    r'Copyright \d{4}',
    r'Copyright 2016')
for client in ['ConfigServiceV2', 'LoggingServiceV2', 'MetricsServiceV2']:
    s.replace(
        f'**/V2/{client}Client.php',
        r'Copyright \d{4}',
        'Copyright 2016')
s.replace(
    'tests/**/V2/*Test.php',
    r'Copyright \d{4}',
    r'Copyright 2018')

### [START] protoc backwards compatibility fixes

# roll back to private properties.
s.replace(
    "src/**/V*/**/*.php",
    r"Generated from protobuf field ([^\n]{0,})\n\s{5}\*/\n\s{4}protected \$",
    r"""Generated from protobuf field \1
     */
    private $""")

# prevent proto messages from being marked final
s.replace(
    "src/**/V*/**/*.php",
    r"final class",
    r"class")

# Replace "Unwrapped" with "Value" for method names.
s.replace(
    "src/**/V*/**/*.php",
    r"public function ([s|g]\w{3,})Unwrapped",
    r"public function \1Value"
)

### [END] protoc backwards compatibility fixes

# fix relative cloud.google.com links
s.replace(
    "src/**/V*/**/*.php",
    r"(.{0,})\]\((/.{0,})\)",
    r"\1](https://cloud.google.com\2)"
)
<|endoftext|>


<commit_before># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This script is used to synthesize generated parts of this library."""

import os
import synthtool as s
import synthtool.gcp as gcp
import logging

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICGenerator()
common = gcp.CommonTemplates()

library = gapic.php_library(
    service='iot',
    version='v1',
    config_path='artman_cloudiot.yaml',
    artman_output_name='google-cloud-iot-v1')

# copy all src including partial veneer classes
s.move(library / f'src')

# copy proto files to src also
s.move(library / f'proto/src/Google/Cloud/Iot', f'src/')
s.move(library / f'tests/')

# copy GPBMetadata file to metadata
s.move(library / f'proto/src/GPBMetadata/Google/Cloud/Iot', f'metadata/')

# document and utilize apiEndpoint instead of serviceAddress
s.replace(
    "**/Gapic/*GapicClient.php",
    r"'serviceAddress' =>",
    r"'apiEndpoint' =>")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"@type string \$serviceAddress\n\s+\*\s+The address",
    r"""@type string $serviceAddress
     *           **Deprecated**. This option will be removed in a future major release. Please
     *           utilize the `$apiEndpoint` option instead.
     *     @type string $apiEndpoint
     *           The address""")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"\$transportConfig, and any \$serviceAddress",
    r"$transportConfig, and any `$apiEndpoint`")

# V1 is GA, so remove @experimental tags
s.replace(
    'src/V1/**/*Client.php',
    r'^(\s+\*\n)?\s+\*\s@experimental\n',
    '')

# fix year
s.replace(
    '**/*Client.php',
    r'Copyright \d{4}',
    r'Copyright 2018')
s.replace(
    'tests/**/V1/*Test.php',
    r'Copyright \d{4}',
    r'Copyright 2018')

### [START] protoc backwards compatibility fixes

# roll back to private properties.
s.replace(
    "src/**/V*/**/*.php",
    r"Generated from protobuf field ([^\n]{0,})\n\s{5}\*/\n\s{4}protected \$",
    r"""Generated from protobuf field \1
     */
    private $""")

# prevent proto messages from being marked final
s.replace(
    "src/**/V*/**/*.php",
    r"final class",
    r"class")

# Replace "Unwrapped" with "Value" for method names.
s.replace(
    "src/**/V*/**/*.php",
    r"public function ([s|g]\w{3,})Unwrapped",
    r"public function \1Value"
)

### [END] protoc backwards compatibility fixes

# fix relative cloud.google.com links
s.replace(
    "src/**/V*/**/*.php",
    r"(.{0,})\]\((/.{0,})\)",
    r"\1](https://cloud.google.com\2)"
)
<commit_msg>chore: Migrate all synth.py scripts from artman to bazel (#2845)<commit_after># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This script is used to synthesize generated parts of this library."""

import synthtool as s
import synthtool.gcp as gcp
import logging

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICBazel()
common = gcp.CommonTemplates()

library = gapic.php_library(
    service='iot',
    version='v1',
    bazel_target='//google/cloud/iot/v1:google-cloud-iot-v1-php',
)

# copy all src including partial veneer classes
s.move(library / f'src')

# copy proto files to src also
s.move(library / f'proto/src/Google/Cloud/Iot', f'src/')
s.move(library / f'tests/')

# copy GPBMetadata file to metadata
s.move(library / f'proto/src/GPBMetadata/Google/Cloud/Iot', f'metadata/')

# document and utilize apiEndpoint instead of serviceAddress
s.replace(
    "**/Gapic/*GapicClient.php",
    r"'serviceAddress' =>",
    r"'apiEndpoint' =>")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"@type string \$serviceAddress\n\s+\*\s+The address",
    r"""@type string $serviceAddress
     *           **Deprecated**. This option will be removed in a future major release. Please
     *           utilize the `$apiEndpoint` option instead.
     *     @type string $apiEndpoint
     *           The address""")
s.replace(
    "**/Gapic/*GapicClient.php",
    r"\$transportConfig, and any \$serviceAddress",
    r"$transportConfig, and any `$apiEndpoint`")

# V1 is GA, so remove @experimental tags
s.replace(
    'src/V1/**/*Client.php',
    r'^(\s+\*\n)?\s+\*\s@experimental\n',
    '')

# fix year
s.replace(
    '**/*Client.php',
    r'Copyright \d{4}',
    r'Copyright 2018')
s.replace(
    'tests/**/V1/*Test.php',
    r'Copyright \d{4}',
    r'Copyright 2018')

### [START] protoc backwards compatibility fixes

# roll back to private properties.
s.replace(
    "src/**/V*/**/*.php",
    r"Generated from protobuf field ([^\n]{0,})\n\s{5}\*/\n\s{4}protected \$",
    r"""Generated from protobuf field \1
     */
    private $""")

# prevent proto messages from being marked final
s.replace(
    "src/**/V*/**/*.php",
    r"final class",
    r"class")

# Replace "Unwrapped" with "Value" for method names.
s.replace(
    "src/**/V*/**/*.php",
    r"public function ([s|g]\w{3,})Unwrapped",
    r"public function \1Value"
)

### [END] protoc backwards compatibility fixes

# fix relative cloud.google.com links
s.replace(
    "src/**/V*/**/*.php",
    r"(.{0,})\]\((/.{0,})\)",
    r"\1](https://cloud.google.com\2)"
)
<|endoftext|>


<commit_before>import synthtool as s
import synthtool.gcp as gcp
import logging
import subprocess

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICMicrogenerator()

# tasks has two product names, and a poorly named artman yaml
version = 'v1'

library = gapic.typescript_library(
    'bigquerydatatransfer', version,
    generator_args={
            "grpc-service-config": f"google/cloud/bigquery/datatransfer/{version}/bigquerydatatransfer_grpc_service_config.json",
            "package-name": f"@google-cloud/bigquery-data-transfer"
            },
            proto_path=f'/google/cloud/bigquery/datatransfer/{version}',
            extra_proto_files=['google/cloud/common_resources.proto'],
            )

# skip index, protos, package.json, and README.md
s.copy(
    library,
    excludes=['package.json', 'README.md', 'src/index.ts',
              f'src/{version}/index.ts'])

# Copy over templated files
common_templates = gcp.CommonTemplates()
templates = common_templates.node_library(source_location='build/src')
s.copy(templates)

subprocess.run(['npm', 'install'])
subprocess.run(['npm', 'run', 'fix'])
subprocess.run(['npm', 'compileProtos', 'run'])
<commit_msg>chore: fix compileProtos call (#332)<commit_after>import synthtool as s
import synthtool.gcp as gcp
import logging
import subprocess

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICMicrogenerator()

# tasks has two product names, and a poorly named artman yaml
version = 'v1'

library = gapic.typescript_library(
    'bigquerydatatransfer', version,
    generator_args={
            "grpc-service-config": f"google/cloud/bigquery/datatransfer/{version}/bigquerydatatransfer_grpc_service_config.json",
            "package-name": f"@google-cloud/bigquery-data-transfer"
            },
            proto_path=f'/google/cloud/bigquery/datatransfer/{version}',
            extra_proto_files=['google/cloud/common_resources.proto'],
            )

# skip index, protos, package.json, and README.md
s.copy(
    library,
    excludes=['package.json', 'README.md', 'src/index.ts',
              f'src/{version}/index.ts'])

# Copy over templated files
common_templates = gcp.CommonTemplates()
templates = common_templates.node_library(source_location='build/src')
s.copy(templates)

subprocess.run(['npm', 'install'])
subprocess.run(['npm', 'run', 'fix'])
subprocess.run(['npx', 'compileProtos', 'src'])
<|endoftext|>


<commit_before># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This script is used to synthesize generated parts of this library."""
import os
from pathlib import Path

import synthtool as s
import synthtool.gcp as gcp
import logging

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICGenerator()
common = gcp.CommonTemplates()

# ----------------------------------------------------------------------------
# Generate phishingprotection GAPIC layer
# ----------------------------------------------------------------------------
versions = ["v1beta1"]
for version in versions:
    library = gapic.py_library(
        "phishingprotection",
        version,
        config_path=f"/google/cloud/phishingprotection/artman_phishingprotection_{version}.yaml",
        include_protos=True,
        generator_args=["--dev_samples"],
    )

    excludes = ["README.rst", "nox.py", "setup.py", "docs/index.rst"]

    s.move(library, excludes=excludes)

    # Files to be renamed (remove v1_beta1 from name)
    gapic_dir = Path(f"google/cloud/phishingprotection_{version}/gapic")
    client = gapic_dir / "phishing_protection_service_v1_beta1_client.py"
    client_config = gapic_dir / "phishing_protection_service_v1_beta1_client_config.py"
    transport = (
        gapic_dir / "transports/phishing_protection_service_v1_beta1_grpc_transport.py"
    )
    unit_test = Path(
        "tests/unit/gapic/v1beta1/test_phishing_protection_service_v1_beta1_client_v1beta1.py"
    )

    s.replace(
        client,
        "google-cloud-phishingprotection",
        "google-cloud-phishing-protection",
    )

    files = [client, client_config, transport, unit_test]
    for file_ in files:
        new_name = str(file_).replace("v1_beta1_", "")
        os.rename(file_, new_name)

# Rename classes in google/cloud and in tests/
class_names = [
    "PhishingProtectionServiceV1Beta1Client",
    "PhishingProtectionServiceV1Beta1GrpcTransport",
]

for name in class_names:
    new_name = name.replace("V1Beta1", "")
    s.replace("google/cloud/**/*.py", name, new_name)
    s.replace("tests/**/*.py", name, new_name)

# Rename references to modules

module_names = [
    "phishing_protection_v1_beta1_service_client",
    "phishing_protection_service_v1_beta1_grpc_transport",
    "phishing_protection_service_v1_beta1_client",
]

for name in module_names:
    new_name = name.replace("v1_beta1_", "")
    s.replace("google/cloud/**/*.py", name, new_name)

# ----------------------------------------------------------------------------
# Add templated files
# ----------------------------------------------------------------------------
templated_files = common.py_library(unit_cov_level=97, cov_level=75)
s.move(templated_files)

s.shell.run(["nox", "-s", "blacken"], hide_output=False)
<commit_msg>chore: Migrate python-phishingprotection synth.py from artman to bazel (#14)<commit_after># Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""This script is used to synthesize generated parts of this library."""
import os
from pathlib import Path

import synthtool as s
import synthtool.gcp as gcp
import logging

logging.basicConfig(level=logging.DEBUG)

gapic = gcp.GAPICBazel()
common = gcp.CommonTemplates()

# ----------------------------------------------------------------------------
# Generate phishingprotection GAPIC layer
# ----------------------------------------------------------------------------
versions = ["v1beta1"]
for version in versions:
    library = gapic.py_library(
        service="phishingprotection",
        version=version,
        bazel_target=f"//google/cloud/phishingprotection/{version}:phishingprotection-{version}-py",
        include_protos=True,
    )

    excludes = ["README.rst", "nox.py", "setup.py", "docs/index.rst"]

    s.move(library, excludes=excludes)

    # Files to be renamed (remove v1_beta1 from name)
    gapic_dir = Path(f"google/cloud/phishingprotection_{version}/gapic")
    client = gapic_dir / "phishing_protection_service_v1_beta1_client.py"
    client_config = gapic_dir / "phishing_protection_service_v1_beta1_client_config.py"
    transport = (
        gapic_dir / "transports/phishing_protection_service_v1_beta1_grpc_transport.py"
    )
    unit_test = Path(
        "tests/unit/gapic/v1beta1/test_phishing_protection_service_v1_beta1_client_v1beta1.py"
    )

    s.replace(
        client,
        "google-cloud-phishingprotection",
        "google-cloud-phishing-protection",
    )

    files = [client, client_config, transport, unit_test]
    for file_ in files:
        new_name = str(file_).replace("v1_beta1_", "")
        os.rename(file_, new_name)

# Rename classes in google/cloud and in tests/
class_names = [
    "PhishingProtectionServiceV1Beta1Client",
    "PhishingProtectionServiceV1Beta1GrpcTransport",
]

for name in class_names:
    new_name = name.replace("V1Beta1", "")
    s.replace("google/cloud/**/*.py", name, new_name)
    s.replace("tests/**/*.py", name, new_name)

# Rename references to modules

module_names = [
    "phishing_protection_v1_beta1_service_client",
    "phishing_protection_service_v1_beta1_grpc_transport",
    "phishing_protection_service_v1_beta1_client",
]

for name in module_names:
    new_name = name.replace("v1_beta1_", "")
    s.replace("google/cloud/**/*.py", name, new_name)

# ----------------------------------------------------------------------------
# Add templated files
# ----------------------------------------------------------------------------
templated_files = common.py_library(unit_cov_level=97, cov_level=75)
s.move(templated_files)

s.shell.run(["nox", "-s", "blacken"], hide_output=False)
<|endoftext|>


<commit_before>import argparse
import os
import re
import sys
from pkg_resources import Requirement

try:
    unicode
except NameError:
    # Python 3
    basestring = unicode = str

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)

# todo: add ssh, sftp protocols
uri_regex = re.compile(r'^(svn|git|bzr|hg|http|https|file|ftp):(\.+)')
file_uri_regex = re.compile(
    r'^(?P<path>[^#]+)#egg=(?P<name>[^&]+)$', re.MULTILINE)
editable_uri_regex = re.compile(r'^((?P<vcs>svn|git|bzr|hg)\+)?'
                                '(?P<uri>[^#&]+)#egg=(?P<name>[^&]+)$',
                                re.MULTILINE)
vcs_uri_regex = re.compile(r'^(?P<vcs>svn|git|bzr|hg)\+'
                           '(?P<uri>[^#&]+)#egg=(?P<name>[^&]+)$',
                           re.MULTILINE)

parser = argparse.ArgumentParser(version="Pipin {0}".format('0.1'))
parser.add_argument("path", action="store",
    help="a directory to search in (use `.` for current directory).")
parser.add_argument("apps", nargs="+",
    help="the apps you wish to look up for.")
parser.add_argument("-f", "--file", action="store",
    help="name of requirements file (default to `requirements.txt`).")
args = parser.parse_args()

def is_uri(uri):
    match = re.match(uri_regex, uri.lower())
    return match is not None

def is_vcs_uri(uri):
    match = re.match(vcs_uri_regex, uri.lower())
    return match is not None

def exclude_line(line):
    lines = (
        '#', '-r', '--requirement',
        '-f', '--find-links',
        '-i', '--index-url', '--extra-index-url', '--no-index',
        '-Z', '--always-unzip')

    for x in lines:
        if line.startswith(x):
            return True
    return False

def parse(s):
    if not isinstance(s, basestring):
        s = s.read()

    for line in s.splitlines():
        line = line.strip()
        if not line:
            continue

        if exclude_line(line):
            continue

        if line.startswith('file:'):
            match = re.match(file_uri_regex, line)
        elif line.startswith('-e') or line.startswith('--editable') or \
                is_uri(line) or is_vcs_uri(line):
            if line.startswith('-e'):
                tmpstr = line[len('-e'):].strip()
            elif line.startswith('--editable'):
                tmpstr = line[len('--editable'):].strip()
            else:
                tmpstr = line
            match = re.match(editable_uri_regex, tmpstr)
        else:
            try:
                yield Requirement.parse(line)
                continue
            except ValueError:
                match = None

        if match:
            yield match.groupdict()
        else:
            raise ValueError('Invalid requirement line "%s"' % line)

def _locate(root, filename):
    for path, dirs, files in os.walk(os.path.abspath(root)):
        if filename in files:
            yield path, os.path.join(path, filename)

def pr(text, path, color):
    text = text + " in {0}".format(path)
    sys.stdout.write("\x1b[1;%dm" % (30+color) + text + "\x1b[0m\n")

def main():
    filename = args.file or 'requirements.txt'
    for path, fpath in _locate(root=args.path, filename=filename):
        with open(fpath, 'r') as fopen:
            items = parse(fopen)
            keys = map(lambda x: x.key, items)
            for app in args.apps:
                p = filename if path == os.getcwd() else fpath
                if app in keys:
                    pr("{0} found".format(app), p, BLUE)
                else:
                    pr("{0} not found".format(app), p, RED)

if __name__ == '__main__':
    main()
<commit_msg>Add todo comments<commit_after>import argparse
import os
import re
import sys
from pkg_resources import Requirement

try:
    unicode
except NameError:
    # Python 3
    basestring = unicode = str

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)

# todo: add ssh, sftp protocols
uri_regex = re.compile(r'^(svn|git|bzr|hg|http|https|file|ftp):(\.+)')
file_uri_regex = re.compile(
    r'^(?P<path>[^#]+)#egg=(?P<name>[^&]+)$', re.MULTILINE)
editable_uri_regex = re.compile(r'^((?P<vcs>svn|git|bzr|hg)\+)?'
                                '(?P<uri>[^#&]+)#egg=(?P<name>[^&]+)$',
                                re.MULTILINE)
vcs_uri_regex = re.compile(r'^(?P<vcs>svn|git|bzr|hg)\+'
                           '(?P<uri>[^#&]+)#egg=(?P<name>[^&]+)$',
                           re.MULTILINE)

parser = argparse.ArgumentParser(version="Pipin {0}".format('0.1'))
parser.add_argument("path", action="store",
    help="a directory to search in (use `.` for current directory).")
parser.add_argument("apps", nargs="+",
    help="the apps you wish to look up for.")
parser.add_argument("-f", "--file", action="store",
    help="name of requirements file (default to `requirements.txt`).")
args = parser.parse_args()

def is_uri(uri):
    match = re.match(uri_regex, uri.lower())
    return match is not None

def is_vcs_uri(uri):
    match = re.match(vcs_uri_regex, uri.lower())
    return match is not None

def exclude_line(line):
    lines = (
        '#', '-r', '--requirement',
        '-f', '--find-links',
        '-i', '--index-url', '--extra-index-url', '--no-index',
        '-Z', '--always-unzip')

    for x in lines:
        if line.startswith(x):
            return True
    return False

def parse(s):
    if not isinstance(s, basestring):
        s = s.read()

    for line in s.splitlines():
        line = line.strip()
        if not line:
            continue

        if exclude_line(line):
            continue

        if line.startswith('file:'):
            match = re.match(file_uri_regex, line)
        elif line.startswith('-e') or line.startswith('--editable') or \
                is_uri(line) or is_vcs_uri(line):
            if line.startswith('-e'):
                tmpstr = line[len('-e'):].strip()
            elif line.startswith('--editable'):
                tmpstr = line[len('--editable'):].strip()
            else:
                tmpstr = line
            match = re.match(editable_uri_regex, tmpstr)
        else:
            try:
                yield Requirement.parse(line)
                continue
            except ValueError:
                match = None

        if match:
            yield match.groupdict()
        else:
            raise ValueError('Invalid requirement line "%s"' % line)

def _locate(root, filename):
    for path, dirs, files in os.walk(os.path.abspath(root)):
        if filename in files:
            yield path, os.path.join(path, filename)

def pr(text, path, color):
    text = text + " in {0}".format(path)
    sys.stdout.write("\x1b[1;%dm" % (30+color) + text + "\x1b[0m\n")

def main():
    filename = args.file or 'requirements.txt'
    for path, fpath in _locate(root=args.path, filename=filename):
        with open(fpath, 'r') as fopen:
            items = parse(fopen)
            # TODO: reduce iterations
            keys = map(lambda x: x.key, items)
            for app in args.apps:
                # TODO: quicker way of obtaining current dir
                p = filename if path == os.getcwd() else fpath
                if app in keys:
                    # TODO: if version(s) provided then compare it
                    pr("{0} found".format(app), p, BLUE)
                else:
                    pr("{0} not found".format(app), p, RED)

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


<commit_before>from ..random import random
from ..color import colors, colorsLength
from ..operations import operation

# Configuration
maxOperationsLength = 7

# Pixel
def generatePixel():
    currentOperationsLength = (random() % maxOperationsLength) + 1
    return [operation(currentOperationsLength), operation(currentOperationsLength), operation(currentOperationsLength), colors[random() % colorsLength]]
<commit_msg>add pixel comment<commit_after>from ..random import random
from ..color import colors, colorsLength
from ..operations import operation

# Configuration
maxOperationsLength = 7

# Pixel Generator
def generatePixel():
    currentOperationsLength = (random() % maxOperationsLength) + 1
    return [operation(currentOperationsLength), operation(currentOperationsLength), operation(currentOperationsLength), colors[random() % colorsLength]]
<|endoftext|>


<commit_before>import yaml
import numpy
import pandas as pd
from pprint import pprint
import sys
from PyOrgMode import PyOrgMode

elements = PyOrgMode.OrgDataStructure()
elements.load_from_file('elements.org')

rows = []

def categories(element):
    # recursive; return ['Cognitive-behavioral flow', 'Presentation']

    def extract(root):
        if all([]):

            return root.heading

        for node in root.content:
            extract(node)

    extract(elements.root)

categories('Patient')

exit

for study_path in sys.argv[1:]:
    elements = yaml.load(open(study_path))['elements']

    study_name = study_path[9:-4]

    for element, data in elements.items():
        data.update(dict(study=study_name,
                         element=element))
        rows.append(data)

df = pd.DataFrame(rows).set_index(['study', 'element'])

def classify(flags):
    classifications = {
        ('Reported', 'Not reported'): ['Partially reported']
    }

    return classifications.get(tuple(flags), flags)[0]

classifications = df['classification']
df['flags'] = classifications.map(lambda cs: ', '.join(cs))
df['classification'] = classifications.map(classify)

def quantify(classification):
    quantifications = {
        'Not reported': 0,
        'Inferred and uncertain': 1,
        'Inferred but nearly certain': 2,
        'Partially reported': 3,
        'Reported': 4
    }

    return quantifications[classification]

df['score'] = df['classification'].map(quantify)

df = df[['flags', 'classification', 'score', 'summary']]
df.to_excel('tmp/studies.xlsx')

df.reset_index(1)\
  .pivot(columns='element', values='score')\
  .to_excel('tmp/elements.xlsx')
<commit_msg>Hierarchical columns<commit_after>import yaml
import numpy
import pandas as pd
from pprint import pprint
import sys
from PyOrgMode import PyOrgMode

def categories(path):
    elements = PyOrgMode.OrgDataStructure()
    elements.load_from_file(path)

    # recursive; return ['Cognitive-behavioral flow', 'Presentation']

    def extract(root):
        base = [root.heading] if root.heading.strip() else []

        if all([type(node) is str for node in root.content]):
            return base
        else:
            branches = [extract(node) for node in root.content if type(node) != str]
            results = []

            for branch in branches:
                for subbranch in branch:
                    if type(subbranch) is str:
                        results.append(base + [subbranch])
                    else:
                        results.append(base + subbranch)
            return results

    def tableize(items):
        result = dict(element=items.pop(len(items) - 1))
        stadia = ['category', 'subcategory']
        for stadium in stadia:
            try:
                result[stadium] = items.pop(0)
            except IndexError:
                result[stadium] = ' '
        return result

    return [tableize(element) for element in extract(elements.root)]

categories = categories('elements.org')
orders = []
for item in categories:
    orders.append(item['element'])
categories = pd.DataFrame(categories).set_index('element')
rows = []

for study_path in sys.argv[1:]:
    elements = yaml.load(open(study_path))['elements']

    study_name = study_path[9:-4]

    for element, data in elements.items():
        data.update(dict(study=study_name,
                         element=element))
        rows.append(data)

df = pd.DataFrame(rows) \
       .join(categories, on='element') \
       .set_index(['study', 'category', 'subcategory', 'element'])

def classify(flags):
    classifications = {
        ('Reported', 'Not reported'): ['Partially reported']
    }

    return classifications.get(tuple(flags), flags)[0]

classifications = df['classification']
df['flags'] = classifications.map(lambda cs: ', '.join(cs))
df['classification'] = classifications.map(classify)

def quantify(classification):
    quantifications = {
        'Not reported': 0,
        'Inferred and uncertain': 1,
        'Inferred but nearly certain': 2,
        'Partially reported': 3,
        'Reported': 4
    }

    return quantifications[classification]

df['score'] = df['classification'].map(quantify)

df = df[['flags', 'classification', 'score', 'summary']].sort_index(level=['study', 'category', 'subcategory']) \
                                                        .reindex(['Common', 'Clinical stream', 'Cognitive-behavioral stream'], level=1) \
                                                        .reindex(orders, level=3)

df.to_excel('tmp/studies.xlsx')

df['score'].unstack(['category', 'subcategory', 'element']).to_excel('tmp/elements.xlsx')
<|endoftext|>


<commit_before># vim: set et ts=4 sw=4 fdm=marker
"""
MIT License

Copyright (c) 2016 Jesse Hogan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from entities import *

class table(entity):
    def __init__(self, x=None, y=None, initval=None):
        self.rows = rows(self)
        self._fields = fields()

        self.rows.onfieldadd += self._fields_onadd
        self.rows.onfieldremove += self._fields_onremove

        # If we have y, we can initialize the table using y, x and initval
        if y != None:
            x = y if x == None else x
            for _ in range(y):
                r = self.newrow()
                for _ in range(x):
                    r.newfield(initval)

    def _fields_onadd(self, src, eargs):
        self._fields += eargs.entity

    def _fields_onremove(self, src, eargs):
        self._fields -= eargs.entity

    def __iter__(self):
        for r in self.rows:
            yield r

    def __getitem__(self, ix):
        return self.rows[ix]

    def __call__(self, y, x):
        return self[y][x]

    def newrow(self):
        r = row()
        self.rows += r
        r.rows = self.rows
        return r

    @property
    def columns(self):
        cs = columns()

        for r in self:
            for i, f in enumerate(r):
                c = cs(i)
                if not c:
                    c = column()
                    cs += c
                c.fields += f

        return cs

    @property
    def fields(self):
        return self._fields

    def count(self, o):
        return self.where(o).count

    def where(self, v, limit=None):
        """
        Return a fields collection where each field in the table matches v.
        """
        if type(v) == type:
            return self.fields.indexes['type'](v, limit=limit)

        elif callable(v) and not isinstance(v, entity) \
                         and not isinstance(v, entities):

            # If v is a callable, a scan is necessary
            ls = []
            for r in self:
                for f in r:
                    if v(f.value):
                        if limit != None and len(ls) >= limit:
                            break
                        ls.append(f)
                else:
                    continue
                break
        else:
            # If v is an arbitrary value, use the value index.
            return self.fields.indexes['value'](v)

        return fields(initial=ls)

    def remove(self, values):
        # TODO Use fieldvalueindex
        for r in self:
            for f in r:
                if any(v is f.value for v in values):
                    f.value = None

    def slice(self, center=None, radius=None):
        if radius == None:
            raise NotImplementedError("'radius' must be provided")
        else:
            if type(radius) != int:
                raise TypeError("'radius' must be an integer")
            if center == None or type(center) != field:
                raise TypeError("'center' must be a field")

        # Return a <table> object. If <table> has been subclassed, return the
        # subclassed version of the table.
        tbl = type(self)()

        # Get top-most, left-most field of slice
        top = center.getabove(radius, closest=True)
        leftmost = top.getleft(radius, closest=True)

        f = leftmost
        r = tbl.newrow()
        while True:
            if f and f.index - center.index <= radius:
                r.newfield(f.value)
                f = f.right
            else:
                f = leftmost = leftmost.below
                if not f or f.row.index - center.row.index  > radius:
                    return tbl
                else:
                    r = tbl.newrow()

        return tbl

    def __str__(self, f=None):
        Reverse='\033[07m'
        Endc = '\033[0m'

        R = ''

        if self.rows.isempty:
            return R

        widths = self.columns.widths

        b = '-' * (sum(widths) + len(widths) + (len(widths) * 2) - 1)

        R += '+' + b + '+'

        for i, r in enumerate(self):
            R += '\n'
            for j, f1 in enumerate(r):
                R += '| ' if j == 0 else ''

                if f != None and f1 is f:
                    R += Reverse

                R += str(f1).ljust(widths[j])

                if f != None and f1 is f:
                    R += Endc

                R += ' |'
                if j < r.fields.ubound:
                    R += ' '

            if i < self.rows.ubound:
                R += '\n|' + b + '|'

        R += '\n+' + b + '+\n'
        return R

class columns(entities):
    @property
    def widths(self):
        return [c.width for c in self]

class column(entity):
    def __init__(self):
        super().__init__()
        self.fields = fields()

    def __iter__(self):
        for f in self.fields:
            yield f

    @property
    def width(self):
        return max([len(str(f.value)) for f in self])

class rows(entities):
    def __init__(self, tbl, initial=None):
        super().__init__(initial=initial)
        self.table = tbl

        # Events
        self.onfieldadd = event()
        self.onfieldremove = event()

    def _self_onremove(self, src, eargs):
        """ Whenever a row is removed from a rows collection, ensure that all
        field objects are removed first. This ensure the fields objects'
        onremove events are raised which is important for indexing, cache
        invalidation and so forth. """

        eargs.entity.fields.clear()

class row(entity):
    def __init__(self):
        super().__init__()
        self.fields = fields(self)

        self.fields.onadd += self._fields_onadd
        self.fields.onremove += self._fields_onremove

    def _fields_onadd(self, src, eargs):
        self.rows.onfieldadd(src, eargs)

    def _fields_onremove(self, src, eargs):
        self.rows.onfieldremove(src, eargs)

    def __getitem__(self, ix):
        return self.fields[ix]

    def __setitem__(self, ix, item):
        self.fields[ix] = item

    @property
    def index(self):
        return self.rows.getindex(self)

    @property
    def table(self):
        return self.rows.table

    def __iter__(self):
        for f in self.fields:
            yield f

    @property
    def above(self):
        ix = self.index
        if ix == 0:
            return None
        return self.rows(ix - 1)

    @property
    def below(self):
        ix = self.index
        if ix == self.rows.ubound:
            return None
        return self.rows(ix + 1)

    def newfield(self, v):
        f, fs = field(v), self.fields
        fs += f
        f.fields = fs
        return f

    def newfields(self, *vs):
        for v in vs:
            self.newfield(v)

class fields(entities):
    def __init__(self, row=None, initial=None):

        # Create index on the type of value
        self.indexes += index(name='type', keyfn=lambda f: type(f.value))

        # Create index on the value
        self.indexes += index(name='value', keyfn=lambda f: f.value)

        if row:
            self.row = row

        # Ensure that each element in initial is a field object. If not,
        # append a new field object using the element as the field's value.
        if initial != None:
            fs = fields()
            for v in initial:
                if isinstance(v, field):
                    fs += v
                else:
                    fs += field(v)

            initial = fs

        super().__init__(initial=initial)

    @property
    def table(self):
        return self.row.table if self.row else None

    @property
    def values(self):
        return [x.value for x in self]

class field(entity):
    def __init__(self, v, initial=None):
        super().__init__()
        self._v = v

    @property
    def value(self):
        return self._v

    @value.setter
    def value(self, v):
        if v != self.value:
            self._v = v

    def clone(self):
        return field(self.value)

    @property
    def column(self):
        return column(self.index)

    @property
    def index(self):
        return self.fields.getindex(self)

    @property
    def table(self):
        return self.fields.row.table if self.fields.row else None

    @property
    def row(self):
        return self.fields.row

    @property
    def above(self):
        r = self.row.above

        if not r:
            return None

        return r.fields(self.index)

    @property
    def below(self):
        r = self.row.below

        if not r:
            return None

        return r.fields(self.index)

    @property
    def left(self):
        ix = self.index
        if ix == 0:
            return None
        return self.row.fields(ix - 1)

    @property
    def right(self):
        ix = self.index
        if ix == self.row.fields.ubound:
            return None
        return self.row.fields(ix + 1)

    @property
    def aboveleft(self):
        f = self.above
        return f.left if f else None

    @property
    def belowleft(self):
        f = self.below
        return f.left if f else None

    @property
    def aboveright(self):
        f = self.above
        return f.right if f else None

    @property
    def belowright(self):
        f = self.below
        return f.right if f else None

    def getabove(self, number, closest=False):
        return self.getneighbor('above', number, closest)

    def getbelow(self, number, closest=False):
        return self.getneighbor('below', number, closest)

    def getleft(self, number, closest=False):
        return self.getneighbor('left', number, closest)

    def getright(self, number, closest=False):
        return self.getneighbor('right', number, closest)

    def getaboveleft(self, number, closest=False):
        return self.getneighbor('aboveleft', number, closest)

    def getbelowleft(self, number, closest=False):
        return self.getneighbor('belowleft', number, closest)

    def getaboveright(self, number, closest=False):
        return self.getneighbor('aboveright', number, closest)

    def getbelowright(self, number, closest=False):
        return self.getneighbor('belowright', number, closest)

    def getneighbor(self, direction, number, closest):
        """
        Get a neigboring field.

        :param str direction: The direction of the neigbor

        :param int number: The number of cells to skip in the given
        direction to get to the desired neigbor

        :param bool closest: If True, return the closest field that can be
        found given the direction and number. If False, return None if a
        neigbor can't be found with those conditions.
        """

        if number == 0:
            return self

        f = self
        for i in range(number):
            neighbor = getattr(f, direction)
            if not neighbor:
                return f if closest else None
            f = neighbor
        return neighbor

    def __str__(self, table=False):
        if table:
            return self.table.__str__(self)
        return str(self.value)

    @property
    def pt(self):
        """
        Print table and highlight this field.

        This property is merely intended for debugging purposes.
        """
        print(self.__str__(table=True))
<commit_msg>Add limit parameter to value index call<commit_after># vim: set et ts=4 sw=4 fdm=marker
"""
MIT License

Copyright (c) 2016 Jesse Hogan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from entities import *

class table(entity):
    def __init__(self, x=None, y=None, initval=None):
        self.rows = rows(self)
        self._fields = fields()

        self.rows.onfieldadd += self._fields_onadd
        self.rows.onfieldremove += self._fields_onremove

        # If we have y, we can initialize the table using y, x and initval
        if y != None:
            x = y if x == None else x
            for _ in range(y):
                r = self.newrow()
                for _ in range(x):
                    r.newfield(initval)

    def _fields_onadd(self, src, eargs):
        self._fields += eargs.entity

    def _fields_onremove(self, src, eargs):
        self._fields -= eargs.entity

    def __iter__(self):
        for r in self.rows:
            yield r

    def __getitem__(self, ix):
        return self.rows[ix]

    def __call__(self, y, x):
        return self[y][x]

    def newrow(self):
        r = row()
        self.rows += r
        r.rows = self.rows
        return r

    @property
    def columns(self):
        cs = columns()

        for r in self:
            for i, f in enumerate(r):
                c = cs(i)
                if not c:
                    c = column()
                    cs += c
                c.fields += f

        return cs

    @property
    def fields(self):
        return self._fields

    def count(self, o):
        return self.where(o).count

    def where(self, v, limit=None):
        """
        Return a fields collection where each field in the table matches v.
        """
        if type(v) == type:
            return self.fields.indexes['type'](v, limit=limit)

        elif callable(v) and not isinstance(v, entity) \
                         and not isinstance(v, entities):

            # If v is a callable, a scan is necessary
            ls = []
            for r in self:
                for f in r:
                    if v(f.value):
                        if limit != None and len(ls) >= limit:
                            break
                        ls.append(f)
                else:
                    continue
                break
        else:
            # If v is an arbitrary value, use the value index.
            return self.fields.indexes['value'](v, limit=limit)

        return fields(initial=ls)

    def remove(self, values):
        # TODO Use fieldvalueindex
        for r in self:
            for f in r:
                if any(v is f.value for v in values):
                    f.value = None

    def slice(self, center=None, radius=None):
        if radius == None:
            raise NotImplementedError("'radius' must be provided")
        else:
            if type(radius) != int:
                raise TypeError("'radius' must be an integer")
            if center == None or type(center) != field:
                raise TypeError("'center' must be a field")

        # Return a <table> object. If <table> has been subclassed, return the
        # subclassed version of the table.
        tbl = type(self)()

        # Get top-most, left-most field of slice
        top = center.getabove(radius, closest=True)
        leftmost = top.getleft(radius, closest=True)

        f = leftmost
        r = tbl.newrow()
        while True:
            if f and f.index - center.index <= radius:
                r.newfield(f.value)
                f = f.right
            else:
                f = leftmost = leftmost.below
                if not f or f.row.index - center.row.index  > radius:
                    return tbl
                else:
                    r = tbl.newrow()

        return tbl

    def __str__(self, f=None):
        Reverse='\033[07m'
        Endc = '\033[0m'

        R = ''

        if self.rows.isempty:
            return R

        widths = self.columns.widths

        b = '-' * (sum(widths) + len(widths) + (len(widths) * 2) - 1)

        R += '+' + b + '+'

        for i, r in enumerate(self):
            R += '\n'
            for j, f1 in enumerate(r):
                R += '| ' if j == 0 else ''

                if f != None and f1 is f:
                    R += Reverse

                R += str(f1).ljust(widths[j])

                if f != None and f1 is f:
                    R += Endc

                R += ' |'
                if j < r.fields.ubound:
                    R += ' '

            if i < self.rows.ubound:
                R += '\n|' + b + '|'

        R += '\n+' + b + '+\n'
        return R

class columns(entities):
    @property
    def widths(self):
        return [c.width for c in self]

class column(entity):
    def __init__(self):
        super().__init__()
        self.fields = fields()

    def __iter__(self):
        for f in self.fields:
            yield f

    @property
    def width(self):
        return max([len(str(f.value)) for f in self])

class rows(entities):
    def __init__(self, tbl, initial=None):
        super().__init__(initial=initial)
        self.table = tbl

        # Events
        self.onfieldadd = event()
        self.onfieldremove = event()

    def _self_onremove(self, src, eargs):
        """ Whenever a row is removed from a rows collection, ensure that all
        field objects are removed first. This ensure the fields objects'
        onremove events are raised which is important for indexing, cache
        invalidation and so forth. """

        eargs.entity.fields.clear()

class row(entity):
    def __init__(self):
        super().__init__()
        self.fields = fields(self)

        self.fields.onadd += self._fields_onadd
        self.fields.onremove += self._fields_onremove

    def _fields_onadd(self, src, eargs):
        self.rows.onfieldadd(src, eargs)

    def _fields_onremove(self, src, eargs):
        self.rows.onfieldremove(src, eargs)

    def __getitem__(self, ix):
        return self.fields[ix]

    def __setitem__(self, ix, item):
        self.fields[ix] = item

    @property
    def index(self):
        return self.rows.getindex(self)

    @property
    def table(self):
        return self.rows.table

    def __iter__(self):
        for f in self.fields:
            yield f

    @property
    def above(self):
        ix = self.index
        if ix == 0:
            return None
        return self.rows(ix - 1)

    @property
    def below(self):
        ix = self.index
        if ix == self.rows.ubound:
            return None
        return self.rows(ix + 1)

    def newfield(self, v):
        f, fs = field(v), self.fields
        fs += f
        f.fields = fs
        return f

    def newfields(self, *vs):
        for v in vs:
            self.newfield(v)

class fields(entities):
    def __init__(self, row=None, initial=None):

        # Create index on the type of value
        self.indexes += index(name='type', keyfn=lambda f: type(f.value))

        # Create index on the value
        self.indexes += index(name='value', keyfn=lambda f: f.value)

        if row:
            self.row = row

        # Ensure that each element in initial is a field object. If not,
        # append a new field object using the element as the field's value.
        if initial != None:
            fs = fields()
            for v in initial:
                if isinstance(v, field):
                    fs += v
                else:
                    fs += field(v)

            initial = fs

        super().__init__(initial=initial)

    @property
    def table(self):
        return self.row.table if self.row else None

    @property
    def values(self):
        return [x.value for x in self]

class field(entity):
    def __init__(self, v, initial=None):
        super().__init__()
        self._v = v

    @property
    def value(self):
        return self._v

    @value.setter
    def value(self, v):
        if v != self.value:
            self._v = v

    def clone(self):
        return field(self.value)

    @property
    def column(self):
        return column(self.index)

    @property
    def index(self):
        return self.fields.getindex(self)

    @property
    def table(self):
        return self.fields.row.table if self.fields.row else None

    @property
    def row(self):
        return self.fields.row

    @property
    def above(self):
        r = self.row.above

        if not r:
            return None

        return r.fields(self.index)

    @property
    def below(self):
        r = self.row.below

        if not r:
            return None

        return r.fields(self.index)

    @property
    def left(self):
        ix = self.index
        if ix == 0:
            return None
        return self.row.fields(ix - 1)

    @property
    def right(self):
        ix = self.index
        if ix == self.row.fields.ubound:
            return None
        return self.row.fields(ix + 1)

    @property
    def aboveleft(self):
        f = self.above
        return f.left if f else None

    @property
    def belowleft(self):
        f = self.below
        return f.left if f else None

    @property
    def aboveright(self):
        f = self.above
        return f.right if f else None

    @property
    def belowright(self):
        f = self.below
        return f.right if f else None

    def getabove(self, number, closest=False):
        return self.getneighbor('above', number, closest)

    def getbelow(self, number, closest=False):
        return self.getneighbor('below', number, closest)

    def getleft(self, number, closest=False):
        return self.getneighbor('left', number, closest)

    def getright(self, number, closest=False):
        return self.getneighbor('right', number, closest)

    def getaboveleft(self, number, closest=False):
        return self.getneighbor('aboveleft', number, closest)

    def getbelowleft(self, number, closest=False):
        return self.getneighbor('belowleft', number, closest)

    def getaboveright(self, number, closest=False):
        return self.getneighbor('aboveright', number, closest)

    def getbelowright(self, number, closest=False):
        return self.getneighbor('belowright', number, closest)

    def getneighbor(self, direction, number, closest):
        """
        Get a neigboring field.

        :param str direction: The direction of the neigbor

        :param int number: The number of cells to skip in the given
        direction to get to the desired neigbor

        :param bool closest: If True, return the closest field that can be
        found given the direction and number. If False, return None if a
        neigbor can't be found with those conditions.
        """

        if number == 0:
            return self

        f = self
        for i in range(number):
            neighbor = getattr(f, direction)
            if not neighbor:
                return f if closest else None
            f = neighbor
        return neighbor

    def __str__(self, table=False):
        if table:
            return self.table.__str__(self)
        return str(self.value)

    @property
    def pt(self):
        """
        Print table and highlight this field.

        This property is merely intended for debugging purposes.
        """
        print(self.__str__(table=True))
<|endoftext|>


<commit_before># vim: set et ts=4 sw=4 fdm=marker
"""
MIT License

Copyright (c) 2016 Jesse Hogan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from entities import *

class table(entity):
    def __init__(self):
        self.rows = rows(self)

class rows(entities):
    def __init__(self, tbl):
        self.table = tbl

    def append(self, r):
        r.table = self.table
        super().append(r)
        return r

class row(entity):
    def __init__(self):
        self.fields = fields(self)

class fields(entities):
    def __init__(self, row):
        self.row = row

    def append(self, f):
        f.table = self.row
        super().append(f)
        return f

class field(entity):
    def __init__(self, v):
        self.value = v

    def __str__(self):
        return str(self.value)
<commit_msg>Add table.__iter__, count and where<commit_after># vim: set et ts=4 sw=4 fdm=marker
"""
MIT License

Copyright (c) 2016 Jesse Hogan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
from entities import *

class table(entity):
    def __init__(self):
        self.rows = rows(self)

    def __iter__(self):
        for r in self.rows:
            yield r

    def count(self, fn):
        return self.where(fn).count

    def where(self, qry):
        if type(qry) == type:
            def fn(x): return type(x) == qry
        elif type(qry) == callable:
            fn = qry

        fs = fields()
        for r in self:
            for f in r:
                if fn(f.value):
                    fs += f
        return fs

class rows(entities):
    def __init__(self, tbl):
        self.table = tbl

    def append(self, r):
        r.table = self.table
        super().append(r)
        return r

class row(entity):
    def __init__(self):
        self.fields = fields(self)

class fields(entities):
    def __init__(self, row):
        self.row = row

    def append(self, f):
        f.table = self.row
        super().append(f)
        return f

class field(entity):
    def __init__(self, v):
        self.value = v

    def __str__(self):
        return str(self.value)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Created By: Virgil Dupras
# Created On: 2009-11-01
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
#
# This software is licensed under the "HS" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.hardcoded.net/licenses/hs_license

from PyQt4.QtCore import SIGNAL, Qt, QAbstractTableModel, QModelIndex
from PyQt4.QtGui import QItemSelectionModel, QItemSelection

from .column import ColumnBearer

class Table(QAbstractTableModel, ColumnBearer):
    # Flags you want when index.isValid() is False. In those cases, _getFlags() is never called.
    INVALID_INDEX_FLAGS = Qt.ItemIsEnabled

    def __init__(self, model, view):
        QAbstractTableModel.__init__(self)
        ColumnBearer.__init__(self, view.horizontalHeader())
        self.model = model
        self.view = view
        self.view.setModel(self)

        self.connect(self.view.selectionModel(), SIGNAL('selectionChanged(QItemSelection,QItemSelection)'), self.selectionChanged)

    def _updateModelSelection(self):
        # Takes the selection on the view's side and update the model with it.
        # an _updateViewSelection() call will normally result in an _updateModelSelection() call.
        # to avoid infinite loops, we check that the selection will actually change before calling
        # model.select()
        newIndexes = [modelIndex.row() for modelIndex in self.view.selectionModel().selectedRows()]
        if newIndexes != self.model.selected_indexes:
            self.model.select(newIndexes)

    def _updateViewSelection(self):
        # Takes the selection on the model's side and update the view with it.
        newSelection = QItemSelection()
        columnCount = self.columnCount(QModelIndex())
        for index in self.model.selected_indexes:
            newSelection.select(self.createIndex(index, 0), self.createIndex(index, columnCount-1))
        self.view.selectionModel().select(newSelection, QItemSelectionModel.ClearAndSelect)
        if len(newSelection.indexes()):
            self.view.selectionModel().setCurrentIndex(newSelection.indexes()[0], QItemSelectionModel.Current)

    #--- Data Model methods
    # Virtual
    def _getData(self, row, column, role):
        if role in (Qt.DisplayRole, Qt.EditRole):
            attrname = column.attrname
            if attrname == 'from':
                attrname = 'from_'
            return getattr(row, attrname)
        elif role == Qt.TextAlignmentRole:
            return column.alignment
        return None

    # Virtual
    def _getFlags(self, row, column):
        flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable
        if row.can_edit_cell(column.attrname):
            flags |= Qt.ItemIsEditable
        return flags

    # Virtual
    def _setData(self, row, column, value, role):
        if role == Qt.EditRole:
            value = unicode(value.toString())
            attrname = column.attrname
            if attrname == 'from':
                attrname = 'from_'
            setattr(row, attrname, value)
            return True
        return False

    def columnCount(self, index):
        return len(self.COLUMNS)

    def data(self, index, role):
        if not index.isValid():
            return None
        row = self.model[index.row()]
        column = self.COLUMNS[index.column()]
        return self._getData(row, column, role)

    def flags(self, index):
        if not index.isValid():
            return self.INVALID_INDEX_FLAGS
        row = self.model[index.row()]
        column = self.COLUMNS[index.column()]
        return self._getFlags(row, column)

    def headerData(self, section, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole and section < len(self.COLUMNS):
            return self.COLUMNS[section].title
        return None

    def revert(self):
        self.model.cancel_edits()

    def rowCount(self, index):
        if index.isValid():
            return 0
        return len(self.model)

    def setData(self, index, value, role):
        if not index.isValid():
            return False
        row = self.model[index.row()]
        column = self.COLUMNS[index.column()]
        return self._setData(row, column, value, role)

    def sort(self, section, order):
        column = self.COLUMNS[section]
        attrname = column.attrname
        self.model.sort_by(attrname, desc=order==Qt.DescendingOrder)

    def submit(self):
        self.model.save_edits()
        return True

    #--- Events
    def selectionChanged(self, selected, deselected):
        self._updateModelSelection()

    #--- model --> view
    def refresh(self):
        self.reset()
        self._updateViewSelection()

    def show_selected_row(self):
        if self.model.selected_index is not None:
            self.view.showRow(self.model.selected_index)

    def start_editing(self):
        self.view.editSelected()

    def stop_editing(self):
        self.view.setFocus() # enough to stop editing

<commit_msg>Right aligned columns now have their header right-aligned as well.<commit_after># -*- coding: utf-8 -*-
# Created By: Virgil Dupras
# Created On: 2009-11-01
# Copyright 2010 Hardcoded Software (http://www.hardcoded.net)
#
# This software is licensed under the "HS" License as described in the "LICENSE" file,
# which should be included with this package. The terms are also available at
# http://www.hardcoded.net/licenses/hs_license

from PyQt4.QtCore import SIGNAL, Qt, QAbstractTableModel, QModelIndex
from PyQt4.QtGui import QItemSelectionModel, QItemSelection

from .column import ColumnBearer

class Table(QAbstractTableModel, ColumnBearer):
    # Flags you want when index.isValid() is False. In those cases, _getFlags() is never called.
    INVALID_INDEX_FLAGS = Qt.ItemIsEnabled

    def __init__(self, model, view):
        QAbstractTableModel.__init__(self)
        ColumnBearer.__init__(self, view.horizontalHeader())
        self.model = model
        self.view = view
        self.view.setModel(self)

        self.connect(self.view.selectionModel(), SIGNAL('selectionChanged(QItemSelection,QItemSelection)'), self.selectionChanged)

    def _updateModelSelection(self):
        # Takes the selection on the view's side and update the model with it.
        # an _updateViewSelection() call will normally result in an _updateModelSelection() call.
        # to avoid infinite loops, we check that the selection will actually change before calling
        # model.select()
        newIndexes = [modelIndex.row() for modelIndex in self.view.selectionModel().selectedRows()]
        if newIndexes != self.model.selected_indexes:
            self.model.select(newIndexes)

    def _updateViewSelection(self):
        # Takes the selection on the model's side and update the view with it.
        newSelection = QItemSelection()
        columnCount = self.columnCount(QModelIndex())
        for index in self.model.selected_indexes:
            newSelection.select(self.createIndex(index, 0), self.createIndex(index, columnCount-1))
        self.view.selectionModel().select(newSelection, QItemSelectionModel.ClearAndSelect)
        if len(newSelection.indexes()):
            self.view.selectionModel().setCurrentIndex(newSelection.indexes()[0], QItemSelectionModel.Current)

    #--- Data Model methods
    # Virtual
    def _getData(self, row, column, role):
        if role in (Qt.DisplayRole, Qt.EditRole):
            attrname = column.attrname
            if attrname == 'from':
                attrname = 'from_'
            return getattr(row, attrname)
        elif role == Qt.TextAlignmentRole:
            return column.alignment
        return None

    # Virtual
    def _getFlags(self, row, column):
        flags = Qt.ItemIsEnabled | Qt.ItemIsSelectable
        if row.can_edit_cell(column.attrname):
            flags |= Qt.ItemIsEditable
        return flags

    # Virtual
    def _setData(self, row, column, value, role):
        if role == Qt.EditRole:
            value = unicode(value.toString())
            attrname = column.attrname
            if attrname == 'from':
                attrname = 'from_'
            setattr(row, attrname, value)
            return True
        return False

    def columnCount(self, index):
        return len(self.COLUMNS)

    def data(self, index, role):
        if not index.isValid():
            return None
        row = self.model[index.row()]
        column = self.COLUMNS[index.column()]
        return self._getData(row, column, role)

    def flags(self, index):
        if not index.isValid():
            return self.INVALID_INDEX_FLAGS
        row = self.model[index.row()]
        column = self.COLUMNS[index.column()]
        return self._getFlags(row, column)

    def headerData(self, section, orientation, role):
        if orientation != Qt.Horizontal:
            return None
        if section >= len(self.COLUMNS):
            return None
        column = self.COLUMNS[section]
        if role == Qt.DisplayRole:
            return column.title
        elif role == Qt.TextAlignmentRole:
            return column.alignment
        else:
            return None

    def revert(self):
        self.model.cancel_edits()

    def rowCount(self, index):
        if index.isValid():
            return 0
        return len(self.model)

    def setData(self, index, value, role):
        if not index.isValid():
            return False
        row = self.model[index.row()]
        column = self.COLUMNS[index.column()]
        return self._setData(row, column, value, role)

    def sort(self, section, order):
        column = self.COLUMNS[section]
        attrname = column.attrname
        self.model.sort_by(attrname, desc=order==Qt.DescendingOrder)

    def submit(self):
        self.model.save_edits()
        return True

    #--- Events
    def selectionChanged(self, selected, deselected):
        self._updateModelSelection()

    #--- model --> view
    def refresh(self):
        self.reset()
        self._updateViewSelection()

    def show_selected_row(self):
        if self.model.selected_index is not None:
            self.view.showRow(self.model.selected_index)

    def start_editing(self):
        self.view.editSelected()

    def stop_editing(self):
        self.view.setFocus() # enough to stop editing

<|endoftext|>