<commit_before># coding=utf-8
# Filename: hdf5.py
# pylint: disable=C0103,R0903
# vim:set ts=4 sts=4 sw=4 et:
"""
Pumps for the EVT simulation dataformat.

"""
from __future__ import division, absolute_import, print_function

import os.path

import numpy as np
import tables

import km3pipe
from km3pipe import Pump, Module
from km3pipe.dataclasses import HitSeries, TrackSeries, EventInfo
from km3pipe.logger import logging

log = logging.getLogger(__name__)  # pylint: disable=C0103

__author__ = "Tamas Gal and Moritz Lotze"
__copyright__ = "Copyright 2016, Tamas Gal and the KM3NeT collaboration."
__credits__ = []
__license__ = "MIT"
__maintainer__ = "Tamas Gal and Moritz Lotze"
__email__ = "tgal@km3net.de"
__status__ = "Development"

class EventInfoDesc(tables.IsDescription):
    det_id = tables.IntCol()
    event_id = tables.UIntCol()
    frame_index = tables.UIntCol()
    mc_id = tables.IntCol()
    mc_t = tables.Float64Col()
    overlays = tables.UInt8Col()
    run_id = tables.UIntCol()
    trigger_counter = tables.UInt64Col()
    trigger_mask = tables.UInt64Col()
    utc_nanoseconds = tables.UInt64Col()
    utc_seconds = tables.UInt64Col()
    weight_w1 = tables.Float64Col()
    weight_w2 = tables.Float64Col()
    weight_w3 = tables.Float64Col()

class Hit(tables.IsDescription):
    channel_id = tables.UInt8Col()
    dom_id = tables.UIntCol()
    event_id = tables.UIntCol()
    id = tables.UIntCol()
    pmt_id = tables.UIntCol()
    run_id = tables.UIntCol()
    time = tables.IntCol()
    tot = tables.UInt8Col()
    triggered = tables.BoolCol()

class Track(tables.IsDescription):
    dir_x = tables.FloatCol()
    dir_y = tables.FloatCol()
    dir_z = tables.FloatCol()
    energy = tables.FloatCol()
    event_id = tables.UIntCol()
    id = tables.UIntCol()
    pos_x = tables.FloatCol()
    pos_y = tables.FloatCol()
    pos_z = tables.FloatCol()
    run_id = tables.UIntCol()
    time = tables.IntCol()
    type = tables.IntCol()

class HDF5Sink(Module):
    def __init__(self, **context):
        """A Module to convert (KM3NeT) ROOT files to HDF5."""
        super(self.__class__, self).__init__(**context)
        self.filename = self.get('filename') or 'dump.h5'
        self.index = 1
        self.h5file = tables.open_file(self.filename, mode="w", title="KM3NeT")
        self.filters = tables.Filters(complevel=5, shuffle=True,
                                      fletcher32=True)
        self.hits = self.h5file.create_table('/', 'hits',
                                             Hit, "Hits",
                                             filters=self.filters)
        self.mc_hits = self.h5file.create_table('/', 'mc_hits',
                                                Hit, "MC Hits",
                                                filters=self.filters)
        self.mc_tracks = self.h5file.create_table('/', 'mc_tracks',
                                                  Track, "MC Tracks",
                                                  filters=self.filters)
        self.event_info = self.h5file.create_table('/', 'event_info',
                                                   EventInfoDesc, "Event Info",
                                                   filters=self.filters)
        self.h5file.create_group(
            '/', 'reco', createparents=True, filters=self.filters)
        self._reco_tables = {}

    def _write_hits(self, hits, hit_row):
        """Iterate through the hits and write them to the HDF5 table.

        Parameters
        ----------
        hits : HitSeries
        hit_row : HDF5 TableRow

        """
        event_id = hits.event_id
        if event_id is None:
            log.error("Event ID is `None`")
        for hit in hits:
            hit_row['channel_id'] = hit.channel_id
            hit_row['dom_id'] = hit.dom_id
            hit_row['event_id'] = event_id
            hit_row['id'] = hit.id
            hit_row['pmt_id'] = hit.pmt_id
            # hit_row['run_id'] = hit.run_id
            hit_row['time'] = hit.time
            hit_row['tot'] = hit.tot
            hit_row['triggered'] = hit.triggered
            hit_row.append()

    def _write_tracks(self, tracks, track_row):
        for track in tracks:
            track_row['dir_x'] = track.dir[0]
            track_row['dir_y'] = track.dir[1]
            track_row['dir_z'] = track.dir[2]
            track_row['energy'] = track.energy
            track_row['event_id'] = tracks.event_id
            track_row['id'] = track.id
            track_row['pos_x'] = track.pos[0]
            track_row['pos_y'] = track.pos[1]
            track_row['pos_z'] = track.pos[2]
            # track_row['run_id'] = track.run_id
            track_row['time'] = track.time
            track_row['type'] = track.type
            track_row.append()

    def _write_event_info(self, info, info_row):
        info_row['det_id'] = info.det_id
        try:  # dealing with aanet naming conventions
            info_row['event_id'] = info.id
        except AttributeError:
            info_row['event_id'] = info.event_id
        info_row['frame_index'] = info.frame_index
        info_row['mc_id'] = info.mc_id
        info_row['mc_t'] = info.mc_t
        info_row['overlays'] = info.overlays
        info_row['run_id'] = info.run_id
        # info_row['timestamp'] = info.timestamp
        info_row['trigger_counter'] = info.trigger_counter
        info_row['trigger_mask'] = info.trigger_mask
        info_row['utc_nanoseconds'] = info.utc_nanoseconds
        info_row['utc_seconds'] = info.utc_seconds
        info_row['weight_w1'] = info.weight_w1
        info_row['weight_w2'] = info.weight_w2
        info_row['weight_w3'] = info.weight_w3
        info_row.append()

    def _write_reco_track(self, track, reco_row):
        for colname, val in track.items():
            reco_row[colname] = val
        reco_row.append()

    def _gen_dtype(self, track):
        keys = [(k, np.float64) for k in track.keys() if k != 'event_id' if k != 'did_converge']
        keys.extend([('event_id', np.uint32), ('did_converge', np.bool_)])
        return np.dtype(sorted(keys))

    def _write_reco(self, reco_dict, reco_group):
        for recname, track in reco_dict.items():
            if recname not in self._reco_tables:
                dtype = self._gen_dtype(track)
                reco_table = self.h5file.create_table(
                    reco_group, recname.lower(),
                    dtype,      #np.dtype(recname_to_dtype[recname]),
                    recname, createparents=True, filters=self.filters
                )
                self._reco_tables[recname] = reco_table
            reco_table = self._reco_tables[recname]
            self._write_reco_track(track, reco_table.row)

    def process(self, blob):
        if 'Hits' in blob:
            hits = blob['Hits']
            self._write_hits(hits, self.hits.row)
        if 'MCHits' in blob:
            self._write_hits(blob['MCHits'], self.mc_hits.row)
        if 'MCTracks' in blob:
            self._write_tracks(blob['MCTracks'], self.mc_tracks.row)
        if 'Evt' in blob and 'EventInfo' not in blob:  # skip in emergency
            self._write_event_info(blob['Evt'], self.event_info.row)
        if 'EventInfo' in blob:  # TODO: decide how to deal with that class
            self._write_event_info(blob['EventInfo'], self.event_info.row)
        if 'Reco' in blob:
            # this is a group, not a single table
            self._write_reco(blob['Reco'], '/reco')

        if not self.index % 1000:
            self.hits.flush()
            self.mc_hits.flush()
            self.mc_tracks.flush()
            self.event_info.flush()
            for tab in self._reco_tables.values():
                tab.flush()

        self.index += 1
        return blob

    def finish(self):
        self.hits.flush()
        self.event_info.flush()
        self.mc_hits.flush()
        self.mc_tracks.flush()
        for tab in self._reco_tables.values():
            tab.flush()
        self.hits.cols.event_id.create_index()
        self.event_info.cols.event_id.create_index()
        self.mc_hits.cols.event_id.create_index()
        self.mc_tracks.cols.event_id.create_index()
        for tab in self._reco_tables.values():
            tab.cols.event_id.create_index()
        self.h5file.root._v_attrs.km3pipe = str(km3pipe.__version__)
        self.h5file.root._v_attrs.pytables = str(tables.__version__)
        self.h5file.close()

class HDF5Pump(Pump):
    """Provides a pump for KM3NeT HDF5 files"""
    def __init__(self, filename, **context):
        super(self.__class__, self).__init__(**context)
        self.filename = filename
        if os.path.isfile(self.filename):
            self.h5_file = tables.File(self.filename)
        else:
            raise IOError("No such file or directory: '{0}'"
                          .format(self.filename))
        self.index = None
        self._reset_index()

        try:
            event_info = self.h5_file.get_node('/', 'event_info')
            self.event_ids = event_info.cols.event_id[:]
        except tables.NoSuchNodeError:
            log.critical("No /event_info table found.")
            raise SystemExit

        self._n_events = len(self.event_ids)

    def process(self, blob):
        try:
            blob = self.get_blob(self.index)
        except KeyError:
            self._reset_index()
            raise StopIteration
        self.index += 1
        return blob

    def _get_hits(self, event_id, table_name='hits', where='/'):
        table = self.h5_file.get_node(where, table_name)
        rows = table.read_where('event_id == %d' % event_id)
        return HitSeries.from_table(rows, event_id)

    def _get_tracks(self, event_id, table_name='tracks', where='/'):
        table = self.h5_file.get_node(where, table_name)
        rows = table.read_where('event_id == %d' % event_id)
        return TrackSeries.from_table(rows, event_id)

    def _get_event_info(self, event_id, table_name='event_info', where='/'):
        table = self.h5_file.get_node(where, table_name)
        return EventInfo.from_table(table[event_id])

    def _get_reco(self, event_id, group_name='reco', where='/'):
        group = self.h5_file.get_node(where, group_name)
        out = {}
        for table in group:
            tabname = table.title
            data = table.read_where('event_id == %d' % event_id)
            out[tabname] = data
        return out

    def get_blob(self, index):
        event_id = self.event_ids[index]
        blob = {}
        blob['Hits'] = self._get_hits(event_id, table_name='hits')
        blob['MCHits'] = self._get_hits(event_id, table_name='mc_hits')
        blob['MCTracks'] = self._get_tracks(event_id, table_name='mc_tracks')
        blob['EventInfo'] = self._get_event_info(event_id,
                                                 table_name='event_info')
        blob['Reco'] = self._get_reco(event_id, group_name='reco')
        return blob

    def finish(self):
        """Clean everything up"""
        self.h5_file.close()

    def _reset_index(self):
        """Reset index to default value"""
        self.index = 0

    def __len__(self):
        return self._n_events

    def __iter__(self):
        return self

    def next(self):
        """Python 2/3 compatibility for iterators"""
        return self.__next__()

    def __next__(self):
        if self.index >= self._n_events:
            self._reset_index()
            raise StopIteration
        blob = self.get_blob(self.index)
        self.index += 1
        return blob

    def __getitem__(self, index):
        if isinstance(index, int):
            return self.get_blob(index)
        elif isinstance(index, slice):
            return self._slice_generator(index)
        else:
            raise TypeError("index must be int or slice")

    def _slice_generator(self, index):
        """A simple slice generator for iterations"""
        start, stop, step = index.indices(len(self))
        for i in range(start, stop, step):
            yield self.get_blob(i)
<commit_msg>add by and length<commit_after># coding=utf-8
# Filename: hdf5.py
# pylint: disable=C0103,R0903
# vim:set ts=4 sts=4 sw=4 et:
"""
Pumps for the EVT simulation dataformat.

"""
from __future__ import division, absolute_import, print_function

import os.path

import numpy as np
import tables

import km3pipe
from km3pipe import Pump, Module
from km3pipe.dataclasses import HitSeries, TrackSeries, EventInfo
from km3pipe.logger import logging

log = logging.getLogger(__name__)  # pylint: disable=C0103

__author__ = "Tamas Gal and Moritz Lotze"
__copyright__ = "Copyright 2016, Tamas Gal and the KM3NeT collaboration."
__credits__ = []
__license__ = "MIT"
__maintainer__ = "Tamas Gal and Moritz Lotze"
__email__ = "tgal@km3net.de"
__status__ = "Development"

class EventInfoDesc(tables.IsDescription):
    det_id = tables.IntCol()
    event_id = tables.UIntCol()
    frame_index = tables.UIntCol()
    mc_id = tables.IntCol()
    mc_t = tables.Float64Col()
    overlays = tables.UInt8Col()
    run_id = tables.UIntCol()
    trigger_counter = tables.UInt64Col()
    trigger_mask = tables.UInt64Col()
    utc_nanoseconds = tables.UInt64Col()
    utc_seconds = tables.UInt64Col()
    weight_w1 = tables.Float64Col()
    weight_w2 = tables.Float64Col()
    weight_w3 = tables.Float64Col()

class Hit(tables.IsDescription):
    channel_id = tables.UInt8Col()
    dom_id = tables.UIntCol()
    event_id = tables.UIntCol()
    id = tables.UIntCol()
    pmt_id = tables.UIntCol()
    run_id = tables.UIntCol()
    time = tables.IntCol()
    tot = tables.UInt8Col()
    triggered = tables.BoolCol()

class Track(tables.IsDescription):
    bjorkeny = tables.FloatCol()
    dir_x = tables.FloatCol()
    dir_y = tables.FloatCol()
    dir_z = tables.FloatCol()
    energy = tables.FloatCol()
    event_id = tables.UIntCol()
    id = tables.UIntCol()
    length = tables.FloatCol()
    pos_x = tables.FloatCol()
    pos_y = tables.FloatCol()
    pos_z = tables.FloatCol()
    run_id = tables.UIntCol()
    time = tables.IntCol()
    type = tables.IntCol()

class HDF5Sink(Module):
    def __init__(self, **context):
        """A Module to convert (KM3NeT) ROOT files to HDF5."""
        super(self.__class__, self).__init__(**context)
        self.filename = self.get('filename') or 'dump.h5'
        self.index = 1
        self.h5file = tables.open_file(self.filename, mode="w", title="KM3NeT")
        self.filters = tables.Filters(complevel=5, shuffle=True,
                                      fletcher32=True)
        self.hits = self.h5file.create_table('/', 'hits',
                                             Hit, "Hits",
                                             filters=self.filters)
        self.mc_hits = self.h5file.create_table('/', 'mc_hits',
                                                Hit, "MC Hits",
                                                filters=self.filters)
        self.mc_tracks = self.h5file.create_table('/', 'mc_tracks',
                                                  Track, "MC Tracks",
                                                  filters=self.filters)
        self.event_info = self.h5file.create_table('/', 'event_info',
                                                   EventInfoDesc, "Event Info",
                                                   filters=self.filters)
        self.h5file.create_group(
            '/', 'reco', createparents=True, filters=self.filters)
        self._reco_tables = {}

    def _write_hits(self, hits, hit_row):
        """Iterate through the hits and write them to the HDF5 table.

        Parameters
        ----------
        hits : HitSeries
        hit_row : HDF5 TableRow

        """
        event_id = hits.event_id
        if event_id is None:
            log.error("Event ID is `None`")
        for hit in hits:
            hit_row['channel_id'] = hit.channel_id
            hit_row['dom_id'] = hit.dom_id
            hit_row['event_id'] = event_id
            hit_row['id'] = hit.id
            hit_row['pmt_id'] = hit.pmt_id
            # hit_row['run_id'] = hit.run_id
            hit_row['time'] = hit.time
            hit_row['tot'] = hit.tot
            hit_row['triggered'] = hit.triggered
            hit_row.append()

    def _write_tracks(self, tracks, track_row):
        for track in tracks:
            track_row['bjorkeny'] = track.bjorkeny
            track_row['dir_x'] = track.dir[0]
            track_row['dir_y'] = track.dir[1]
            track_row['dir_z'] = track.dir[2]
            track_row['energy'] = track.energy
            track_row['event_id'] = tracks.event_id
            track_row['id'] = track.id
            track_row["length"] = track.length
            track_row['pos_x'] = track.pos[0]
            track_row['pos_y'] = track.pos[1]
            track_row['pos_z'] = track.pos[2]
            # track_row['run_id'] = track.run_id
            track_row['time'] = track.time
            track_row['type'] = track.type
            track_row.append()

    def _write_event_info(self, info, info_row):
        info_row['det_id'] = info.det_id
        try:  # dealing with aanet naming conventions
            info_row['event_id'] = info.id
        except AttributeError:
            info_row['event_id'] = info.event_id
        info_row['frame_index'] = info.frame_index
        info_row['mc_id'] = info.mc_id
        info_row['mc_t'] = info.mc_t
        info_row['overlays'] = info.overlays
        info_row['run_id'] = info.run_id
        # info_row['timestamp'] = info.timestamp
        info_row['trigger_counter'] = info.trigger_counter
        info_row['trigger_mask'] = info.trigger_mask
        info_row['utc_nanoseconds'] = info.utc_nanoseconds
        info_row['utc_seconds'] = info.utc_seconds
        info_row['weight_w1'] = info.weight_w1
        info_row['weight_w2'] = info.weight_w2
        info_row['weight_w3'] = info.weight_w3
        info_row.append()

    def _write_reco_track(self, track, reco_row):
        for colname, val in track.items():
            reco_row[colname] = val
        reco_row.append()

    def _gen_dtype(self, track):
        keys = [(k, np.float64) for k in track.keys() if k != 'event_id' if k != 'did_converge']
        keys.extend([('event_id', np.uint32), ('did_converge', np.bool_)])
        return np.dtype(sorted(keys))

    def _write_reco(self, reco_dict, reco_group):
        for recname, track in reco_dict.items():
            if recname not in self._reco_tables:
                dtype = self._gen_dtype(track)
                reco_table = self.h5file.create_table(
                    reco_group, recname.lower(),
                    dtype,      #np.dtype(recname_to_dtype[recname]),
                    recname, createparents=True, filters=self.filters
                )
                self._reco_tables[recname] = reco_table
            reco_table = self._reco_tables[recname]
            self._write_reco_track(track, reco_table.row)

    def process(self, blob):
        if 'Hits' in blob:
            hits = blob['Hits']
            self._write_hits(hits, self.hits.row)
        if 'MCHits' in blob:
            self._write_hits(blob['MCHits'], self.mc_hits.row)
        if 'MCTracks' in blob:
            self._write_tracks(blob['MCTracks'], self.mc_tracks.row)
        if 'Evt' in blob and 'EventInfo' not in blob:  # skip in emergency
            self._write_event_info(blob['Evt'], self.event_info.row)
        if 'EventInfo' in blob:  # TODO: decide how to deal with that class
            self._write_event_info(blob['EventInfo'], self.event_info.row)
        if 'Reco' in blob:
            # this is a group, not a single table
            self._write_reco(blob['Reco'], '/reco')

        if not self.index % 1000:
            self.hits.flush()
            self.mc_hits.flush()
            self.mc_tracks.flush()
            self.event_info.flush()
            for tab in self._reco_tables.values():
                tab.flush()

        self.index += 1
        return blob

    def finish(self):
        self.hits.flush()
        self.event_info.flush()
        self.mc_hits.flush()
        self.mc_tracks.flush()
        for tab in self._reco_tables.values():
            tab.flush()
        self.hits.cols.event_id.create_index()
        self.event_info.cols.event_id.create_index()
        self.mc_hits.cols.event_id.create_index()
        self.mc_tracks.cols.event_id.create_index()
        for tab in self._reco_tables.values():
            tab.cols.event_id.create_index()
        self.h5file.root._v_attrs.km3pipe = str(km3pipe.__version__)
        self.h5file.root._v_attrs.pytables = str(tables.__version__)
        self.h5file.close()

class HDF5Pump(Pump):
    """Provides a pump for KM3NeT HDF5 files"""
    def __init__(self, filename, **context):
        super(self.__class__, self).__init__(**context)
        self.filename = filename
        if os.path.isfile(self.filename):
            self.h5_file = tables.File(self.filename)
        else:
            raise IOError("No such file or directory: '{0}'"
                          .format(self.filename))
        self.index = None
        self._reset_index()

        try:
            event_info = self.h5_file.get_node('/', 'event_info')
            self.event_ids = event_info.cols.event_id[:]
        except tables.NoSuchNodeError:
            log.critical("No /event_info table found.")
            raise SystemExit

        self._n_events = len(self.event_ids)

    def process(self, blob):
        try:
            blob = self.get_blob(self.index)
        except KeyError:
            self._reset_index()
            raise StopIteration
        self.index += 1
        return blob

    def _get_hits(self, event_id, table_name='hits', where='/'):
        table = self.h5_file.get_node(where, table_name)
        rows = table.read_where('event_id == %d' % event_id)
        return HitSeries.from_table(rows, event_id)

    def _get_tracks(self, event_id, table_name='tracks', where='/'):
        table = self.h5_file.get_node(where, table_name)
        rows = table.read_where('event_id == %d' % event_id)
        return TrackSeries.from_table(rows, event_id)

    def _get_event_info(self, event_id, table_name='event_info', where='/'):
        table = self.h5_file.get_node(where, table_name)
        return EventInfo.from_table(table[event_id])

    def _get_reco(self, event_id, group_name='reco', where='/'):
        group = self.h5_file.get_node(where, group_name)
        out = {}
        for table in group:
            tabname = table.title
            data = table.read_where('event_id == %d' % event_id)
            out[tabname] = data
        return out

    def get_blob(self, index):
        event_id = self.event_ids[index]
        blob = {}
        blob['Hits'] = self._get_hits(event_id, table_name='hits')
        blob['MCHits'] = self._get_hits(event_id, table_name='mc_hits')
        blob['MCTracks'] = self._get_tracks(event_id, table_name='mc_tracks')
        blob['EventInfo'] = self._get_event_info(event_id,
                                                 table_name='event_info')
        blob['Reco'] = self._get_reco(event_id, group_name='reco')
        return blob

    def finish(self):
        """Clean everything up"""
        self.h5_file.close()

    def _reset_index(self):
        """Reset index to default value"""
        self.index = 0

    def __len__(self):
        return self._n_events

    def __iter__(self):
        return self

    def next(self):
        """Python 2/3 compatibility for iterators"""
        return self.__next__()

    def __next__(self):
        if self.index >= self._n_events:
            self._reset_index()
            raise StopIteration
        blob = self.get_blob(self.index)
        self.index += 1
        return blob

    def __getitem__(self, index):
        if isinstance(index, int):
            return self.get_blob(index)
        elif isinstance(index, slice):
            return self._slice_generator(index)
        else:
            raise TypeError("index must be int or slice")

    def _slice_generator(self, index):
        """A simple slice generator for iterations"""
        start, stop, step = index.indices(len(self))
        for i in range(start, stop, step):
            yield self.get_blob(i)
<|endoftext|>


<commit_before>
#
# deconvolution, etc...
#
import numpy as np
import numpy.ma as ma
from WavefrontPSF.stamp_collector import stamp_collector
from WavefrontPSF.psf_evaluator import Moment_Evaluator
from matplotlib import pyplot as plt
plt.interactive(True)

expid = 181243

fit_rzero = 0.168
fit_fwhm = 0.14/0.168
use_fwhm = np.sqrt(fit_fwhm*fit_fwhm - 0.3*0.3)
use_rzero = 0.14/use_fwhm

psf_df, model_stamps, decam_stamps = stamp_collector(expid, Nmax=0, rzero=use_rzero, snr_max=400, snr_min=90)

evaluator = Moment_Evaluator()

# model_stamps is the PSF model
# decam_stamps is the data...

# mask bad pixel data
# and crop it to 32x32
# this puts the center pixel in the 63x63 just below the center line in the 32x32 array
decam_stamps_clean = np.where(decam_stamps<1e6,decam_stamps,0.)
decam_stamps_clean = np.where(decam_stamps_clean>-1e5,decam_stamps_clean,0.)
decam_stamps_crop = decam_stamps_clean[:,16:48,16:48]

moments_decam = evaluator(decam_stamps_crop.astype(np.float64))
moments_model = evaluator(model_stamps)

Mxx = moments_decam['Mxx'] - moments_model['Mxx']
Myy = moments_decam['Myy'] - moments_model['Myy']
Mxy = moments_decam['Mxy'] - moments_model['Mxy']

# now try deconvolving...
# move RL code into GitHub
# setup on ki-ls

# list of expid analyzed
from glob import glob
import numpy as np
params_list = glob('/nfs/slac/g/ki/ki18/des/jamierod/SVA1/params/*.npy')
expids = np.sort([int(param.split('.npy')[-2].split('_')[-1]) for param in params_list])<commit_msg>possible default settings<commit_after>
#
# deconvolution, etc...
#
import numpy as np
import numpy.ma as ma
from scipy.stats import tstd
from matplotlib.colors import LogNorm
from matplotlib import pyplot as plt
plt.interactive(True)

from WavefrontPSF.stamp_collector import stamp_collector
from WavefrontPSF.psf_evaluator import Moment_Evaluator
from DeconvolvePSF.lucy import deconvolve, convolve, makeMask, makeGaussian

class Formatter(object):
    def __init__(self, im):
        self.im = im
    def __call__(self, x, y):
        z = self.im.get_array()[int(y), int(x)]
        return 'x={:.01f}, y={:.01f}, z={:.04f}'.format(x, y, z)

expid = 181243

fit_rzero = 0.168
fit_fwhm = 0.14/0.168
use_fwhm = np.sqrt(fit_fwhm*fit_fwhm - 0.3*0.3)
use_rzero = 0.14/use_fwhm

psf_df, model_stamps, decam_stamps = stamp_collector(expid, Nmax=0, rzero=use_rzero, snr_max=400, snr_min=90)

evaluator = Moment_Evaluator()

# model_stamps is the PSF model
# decam_stamps is the data...

# mask bad pixel data
# and crop it to 32x32
# this puts the center pixel in the 63x63 just below the center line in the 32x32 array
decam_stamps_clean = np.where(decam_stamps<1e6,decam_stamps,0.)
decam_stamps_clean = np.where(decam_stamps_clean>-1e5,decam_stamps_clean,0.)
decam_stamps_crop = decam_stamps_clean[:,16:48,16:48]

moments_decam = evaluator(decam_stamps_crop.astype(np.float64))
moments_model = evaluator(model_stamps)

Mxx = moments_decam['Mxx'] - moments_model['Mxx']
Myy = moments_decam['Myy'] - moments_model['Myy']
Mxy = moments_decam['Mxy'] - moments_model['Mxy']

# find background level from RMS of decam_stamps data

# now try deconvolving...
stdback = tstd(decam_stamps_clean.flatten(),limits=(-200,200))
background = stdback*stdback

#for i in range(decam_stamps_crop.shape[0]):

for i in range(10):

    psi0 = makeGaussian((32,32),Mxx[i],Myy[i],Mxy[i])

    # now try R-L
    deconImage,diffs,psiByIter,chi2ByIter = deconvolve(model_stamps[i],decam_stamps_crop[i],psi_0=psi0,mask=None,mu0=background,convergence=0.5e-3,chi2Level=1024.,niterations=1000,extra=True)

    # convolve back
    predictImage = convolve(model_stamps[i],deconImage)

    f1,axArr = plt.subplots(3,2)
    im00 = axArr[0,0].imshow(decam_stamps_crop[i]/np.sum(decam_stamps_crop[i]),interpolation='none',origin='lower',cmap='hot')
    ##,norm=LogNorm(vmin=1.0e-4, vmax=1.0))
    #axArr[0,0].colorbar(im00)
    axArr[0,0].format_coord = Formatter(im00)
    im01 = axArr[0,1].imshow(model_stamps[i]/np.sum(model_stamps[i]),interpolation='none',origin='lower',cmap='hot')
    ##norm=LogNorm(vmin=1.0e-4, vmax=1.0))
    #axArr[0,1].colorbar(im01)
    axArr[0,1].format_coord = Formatter(im01)

    im10 = axArr[1,0].imshow(psi0,interpolation='none',origin='lower',cmap='hot',norm=LogNorm(vmin=1.0e-4, vmax=1.0))
    #axArr[1,0].colorbar(im10)
    axArr[1,0].format_coord = Formatter(im10)
    im11 = axArr[1,1].imshow(deconImage,interpolation='none',origin='lower',cmap='hot',norm=LogNorm(vmin=1.0e-4, vmax=1.0))
    #axArr[1,1].colorbar(im11)
    axArr[1,1].format_coord = Formatter(im11)

    im20 = axArr[2,0].imshow(predictImage,interpolation='none',origin='lower',cmap='hot')
    ##,norm=LogNorm(vmin=1.0e-4, vmax=1.0))
    #axArr[2,0].colorbar(im20)
    axArr[2,0].format_coord = Formatter(im20)
    im21 = axArr[2,1].imshow(decam_stamps_crop[i]/np.sum(decam_stamps_crop[i])-predictImage,interpolation='none',origin='lower',cmap='hot',vmin=-0.01, vmax=0.01)
    #axArr[2,1].colorbar(im21)
    axArr[2,1].format_coord = Formatter(im21)

    f1.show()

    print "flux_adaptive,snr from sextractor",psf_df['FLUX_ADAPTIVE'].ilog[i],psf_df['SNR_WIN'].iloc[i]
    print "n iterations",len(diffs)
    print "starting Diff,Chi2", diffs[0],chi2ByIter[0]
    print "ending Diff,Chi2", diffs[-1],chi2ByIter[-1]

# list of expid analyzed
from glob import glob
import numpy as np
params_list = glob('/nfs/slac/g/ki/ki18/des/jamierod/SVA1/params/*.npy')
expids = np.sort([int(param.split('.npy')[-2].split('_')[-1]) for param in params_list])<|endoftext|>


<commit_before># This file is part of Archivematica.
#
# Copyright 2010-2013 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Archivematica is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Archivematica.  If not, see <http://www.gnu.org/licenses/>.

from django.conf import settings
from django.core.urlresolvers import reverse
from django.shortcuts import redirect

import components.helpers as helpers
from re import compile as re_compile

EXEMPT_URLS = [re_compile(settings.LOGIN_URL.lstrip('/'))]
if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
    EXEMPT_URLS += [re_compile(expr) for expr in settings.LOGIN_EXEMPT_URLS]

class ConfigurationCheckMiddleware:
    def process_request(self, request):
        # The presence of the UUID is an indicator of whether we've already set up.
        dashboard_uuid = helpers.get_setting('dashboard_uuid')
        if not dashboard_uuid:
            if reverse('installer.views.welcome') != request.path_info:
                return redirect('installer.views.welcome')
<commit_msg>Restore login redirect to config middleware<commit_after># This file is part of Archivematica.
#
# Copyright 2010-2013 Artefactual Systems Inc. <http://artefactual.com>
#
# Archivematica is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Archivematica is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Archivematica.  If not, see <http://www.gnu.org/licenses/>.

from django.conf import settings
from django.core.urlresolvers import reverse
from django.shortcuts import redirect

import components.helpers as helpers
from re import compile as re_compile

EXEMPT_URLS = [re_compile(settings.LOGIN_URL.lstrip('/'))]
if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
    EXEMPT_URLS += [re_compile(expr) for expr in settings.LOGIN_EXEMPT_URLS]

class ConfigurationCheckMiddleware:
    def process_request(self, request):
        # The presence of the UUID is an indicator of whether we've already set up.
        dashboard_uuid = helpers.get_setting('dashboard_uuid')
        if not dashboard_uuid:
            # Start off the installer
            if reverse('installer.views.welcome') != request.path_info:
                return redirect('installer.views.welcome')
        elif not request.user.is_authenticated():
            # Installation already happened - make sure the user is logged in.
            path = request.path_info.lstrip('/')
            if not any(m.match(path) for m in EXEMPT_URLS):
                return redirect(settings.LOGIN_URL)
<|endoftext|>


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

from __future__ import absolute_import

from flask import Blueprint, current_app

from eduid_common.api.decorators import require_user, can_verify_identity, MarshalWith, UnmarshalWith
from eduid_common.api.helpers import add_nin_to_user, verify_nin_for_user
from eduid_common.api.exceptions import AmTaskFailed, MsgTaskFailed
from eduid_userdb.proofing import ProofingUser
from eduid_userdb.logs import LetterProofing
from eduid_webapp.letter_proofing import pdf
from eduid_webapp.letter_proofing import schemas
from eduid_webapp.letter_proofing.ekopost import EkopostException
from eduid_webapp.letter_proofing.helpers import create_proofing_state, check_state, get_address, send_letter

__author__ = 'lundberg'

letter_proofing_views = Blueprint('letter_proofing', __name__, url_prefix='', template_folder='templates')

@letter_proofing_views.route('/proofing', methods=['GET'])
@MarshalWith(schemas.LetterProofingResponseSchema)
@require_user
def get_state(user):
    current_app.logger.info('Getting proofing state for user {}'.format(user))
    proofing_state = current_app.proofing_statedb.get_state_by_eppn(user.eppn, raise_on_missing=False)

    if proofing_state:
        current_app.logger.info('Found proofing state for user {}'.format(user))
        return check_state(proofing_state)
    return {}

@letter_proofing_views.route('/proofing', methods=['POST'])
@UnmarshalWith(schemas.LetterProofingRequestSchema)
@MarshalWith(schemas.LetterProofingResponseSchema)
@can_verify_identity
@require_user
def proofing(user, nin):
    current_app.logger.info('Send letter for user {} initiated'.format(user))
    proofing_state = current_app.proofing_statedb.get_state_by_eppn(user.eppn, raise_on_missing=False)

    # No existing proofing state was found, create a new one
    if not proofing_state:
        # Create a LetterNinProofingUser in proofingdb
        proofing_state = create_proofing_state(user.eppn, nin)
        current_app.logger.info('Created proofing state for user {}'.format(user))

    if proofing_state.proofing_letter.is_sent:
        current_app.logger.info('User {!r} has already sent a letter'.format(user))
        return check_state(proofing_state)

    try:
        address = get_address(user, proofing_state)
        if not address:
            current_app.logger.error('No address found for user {}'.format(user))
            return {'_status': 'error', 'message': 'No address found'}
    except MsgTaskFailed as e:
        current_app.logger.error('Navet lookup failed for user {}: {}'.format(user, e))
        current_app.stats.count('navet_error')
        return {'_status': 'error', 'error': 'error_navet_task'}

    # Set and save official address
    proofing_state.proofing_letter.address = address
    current_app.proofing_statedb.save(proofing_state)

    try:
        campaign_id = send_letter(user, proofing_state)
    except pdf.AddressFormatException as e:
        current_app.logger.error('{!r}'.format(e.message))
        return {'_status': 'error', 'message': 'Bad postal address'}
    except EkopostException as e:
        current_app.logger.error('{!r}'.format(e.message))
        return {'_status': 'error', 'message': 'Temporary technical problem'}

    # Save the users proofing state
    proofing_state.proofing_letter.transaction_id = campaign_id
    proofing_state.proofing_letter.is_sent = True
    proofing_state.proofing_letter.sent_ts = True
    current_app.proofing_statedb.save(proofing_state)
    # Add nin as not verified to user
    add_nin_to_user(user, proofing_state)
    return check_state(proofing_state)

@letter_proofing_views.route('/verify-code', methods=['POST'])
@UnmarshalWith(schemas.VerifyCodeRequestSchema)
@MarshalWith(schemas.VerifyCodeResponseSchema)
@require_user
def verify_code(user, verification_code):
    user = ProofingUser(data=user.to_dict())
    current_app.logger.info('Verifying code for user {}'.format(user))
    proofing_state = current_app.proofing_statedb.get_state_by_eppn(user.eppn, raise_on_missing=False)

    if not proofing_state:
        return {'_status': 'error', 'message': 'No proofing state found'}

    # Check if provided code matches the one in the letter
    if not verification_code == proofing_state.nin.verification_code:
        current_app.logger.error('Verification code for user {} does not match'.format(user))
        # TODO: Throttling to discourage an adversary to try brute force
        return {'_status': 'error', 'message': 'Wrong code'}

    try:
        official_address = get_address(user, proofing_state)
    except MsgTaskFailed as e:
        current_app.logger.error('Navet lookup failed for user {}: {}'.format(user, e))
        current_app.stats.count('navet_error')
        return {'_status': 'error', 'error': 'error_navet_task'}

    proofing_log_entry = LetterProofing(user, created_by='eduid_letter_proofing', nin=proofing_state.nin.number,
                                        letter_sent_to=proofing_state.proofing_letter.address,
                                        transaction_id=proofing_state.proofing_letter.transaction_id,
                                        user_postal_address=official_address, proofing_version='2016v1')
    try:
        # Verify nin for user
        verify_nin_for_user(user, proofing_state, proofing_log_entry)
        current_app.logger.info('Verified code for user {}'.format(user))
        # Remove proofing state
        current_app.proofing_statedb.remove_state(proofing_state)
        return {'success': True}
    except AmTaskFailed as e:
        current_app.logger.error('Verifying nin for user {} failed'.format(user))
        current_app.logger.error('{}'.format(e))
        return {'_status': 'error', 'error': 'technical_problems'}
<commit_msg>view to remove unconfirmed NINs<commit_after># -*- coding: utf-8 -*-

from __future__ import absolute_import

from flask import Blueprint, current_app

from eduid_common.api.decorators import require_user, can_verify_identity, MarshalWith, UnmarshalWith
from eduid_common.api.helpers import add_nin_to_user, verify_nin_for_user, rm_nin_from_user
from eduid_common.api.exceptions import AmTaskFailed, MsgTaskFailed
from eduid_userdb.proofing import ProofingUser
from eduid_userdb.logs import LetterProofing
from eduid_webapp.letter_proofing import pdf
from eduid_webapp.letter_proofing import schemas
from eduid_webapp.letter_proofing.ekopost import EkopostException
from eduid_webapp.letter_proofing.helpers import create_proofing_state, check_state, get_address, send_letter

__author__ = 'lundberg'

letter_proofing_views = Blueprint('letter_proofing', __name__, url_prefix='', template_folder='templates')

@letter_proofing_views.route('/proofing', methods=['GET'])
@MarshalWith(schemas.LetterProofingResponseSchema)
@require_user
def get_state(user):
    current_app.logger.info('Getting proofing state for user {}'.format(user))
    proofing_state = current_app.proofing_statedb.get_state_by_eppn(user.eppn, raise_on_missing=False)

    if proofing_state:
        current_app.logger.info('Found proofing state for user {}'.format(user))
        return check_state(proofing_state)
    return {}

@letter_proofing_views.route('/proofing', methods=['POST'])
@UnmarshalWith(schemas.LetterProofingRequestSchema)
@MarshalWith(schemas.LetterProofingResponseSchema)
@can_verify_identity
@require_user
def proofing(user, nin):
    current_app.logger.info('Send letter for user {} initiated'.format(user))
    proofing_state = current_app.proofing_statedb.get_state_by_eppn(user.eppn, raise_on_missing=False)

    # No existing proofing state was found, create a new one
    if not proofing_state:
        # Create a LetterNinProofingUser in proofingdb
        proofing_state = create_proofing_state(user.eppn, nin)
        current_app.logger.info('Created proofing state for user {}'.format(user))

    if proofing_state.proofing_letter.is_sent:
        current_app.logger.info('User {!r} has already sent a letter'.format(user))
        current_app.logger.info('This is the proofing state: '
                                '{!r}'.format(proofing_state.to_dict()))
        return check_state(proofing_state)

    try:
        address = get_address(user, proofing_state)
        if not address:
            current_app.logger.error('No address found for user {}'.format(user))
            return {'_status': 'error', 'message': 'No address found'}
    except MsgTaskFailed as e:
        current_app.logger.error('Navet lookup failed for user {}: {}'.format(user, e))
        current_app.stats.count('navet_error')
        return {'_status': 'error', 'error': 'error_navet_task'}

    # Set and save official address
    proofing_state.proofing_letter.address = address
    current_app.proofing_statedb.save(proofing_state)

    try:
        campaign_id = send_letter(user, proofing_state)
    except pdf.AddressFormatException as e:
        current_app.logger.error('{!r}'.format(e.message))
        return {'_status': 'error', 'message': 'Bad postal address'}
    except EkopostException as e:
        current_app.logger.error('{!r}'.format(e.message))
        return {'_status': 'error', 'message': 'Temporary technical problem'}

    # Save the users proofing state
    proofing_state.proofing_letter.transaction_id = campaign_id
    proofing_state.proofing_letter.is_sent = True
    proofing_state.proofing_letter.sent_ts = True
    current_app.proofing_statedb.save(proofing_state)
    # Add nin as not verified to user
    add_nin_to_user(user, proofing_state)
    return check_state(proofing_state)

@letter_proofing_views.route('/verify-code', methods=['POST'])
@UnmarshalWith(schemas.VerifyCodeRequestSchema)
@MarshalWith(schemas.VerifyCodeResponseSchema)
@require_user
def verify_code(user, code):
    user = ProofingUser(data=user.to_dict())
    current_app.logger.info('Verifying code for user {}'.format(user))
    proofing_state = current_app.proofing_statedb.get_state_by_eppn(user.eppn, raise_on_missing=False)

    if not proofing_state:
        return {'_status': 'error', 'message': 'No proofing state found'}

    # Check if provided code matches the one in the letter
    if not code == proofing_state.nin.verification_code:
        current_app.logger.error('Verification code for user {} does not match'.format(user))
        # TODO: Throttling to discourage an adversary to try brute force
        return {'_status': 'error', 'message': 'Wrong code'}

    try:
        official_address = get_address(user, proofing_state)
    except MsgTaskFailed as e:
        current_app.logger.error('Navet lookup failed for user {}: {}'.format(user, e))
        current_app.stats.count('navet_error')
        return {'_status': 'error', 'error': 'error_navet_task'}

    proofing_log_entry = LetterProofing(user, created_by='eduid_letter_proofing', nin=proofing_state.nin.number,
                                        letter_sent_to=proofing_state.proofing_letter.address,
                                        transaction_id=proofing_state.proofing_letter.transaction_id,
                                        user_postal_address=official_address, proofing_version='2016v1')
    try:
        # Verify nin for user
        verify_nin_for_user(user, proofing_state, proofing_log_entry)
        current_app.logger.info('Verified code for user {}'.format(user))
        # Remove proofing state
        current_app.proofing_statedb.remove_state(proofing_state)
        return {'success': True,
                'message': 'letter.verification_success'}
    except AmTaskFailed as e:
        current_app.logger.error('Verifying nin for user {} failed'.format(user))
        current_app.logger.error('{}'.format(e))
        return {'_status': 'error', 'error': 'technical_problems'}

@letter_proofing_views.route('/remove-nin', methods=['POST'])
@UnmarshalWith(schemas.LetterProofingRequestSchema)
@MarshalWith(schemas.VerifyCodeResponseSchema)
@require_user
def remove_nin(user, nin):
    current_app.logger.info('Removing NIN {} for user {}'.format(nin, user))
    proofing_state = current_app.proofing_statedb.get_state_by_eppn(user.eppn, raise_on_missing=False)

    if proofing_state:
        current_app.logger.info('Removing proofing state: '
                                '{!r}'.format(proofing_state.to_dict()))
        current_app.proofing_statedb.remove_state(proofing_state)

    rm_nin_from_user(user, nin)
    return {'success': True, 'message': 'nins.success_removal'}
<|endoftext|>


<commit_before>import os
import subprocess
import tempfile
from pprint import pprint

from django.template.loader import render_to_string

import email_signature.functions as functions

class GeraAssinatura():

    def __init__(self, conta):
        self.conta = conta
        tf = tempfile.NamedTemporaryFile(prefix=f"_{conta.id}_temp_assinatura_file_", suffix=".html")
        self.temp_file = tf.name
        self.transf = {}

    def apagar_assinatura_local(self):
        arquivo = os.path.join('.', self.temp_file)
        try:
            os.remove(arquivo)
        except FileNotFoundError:
            pass

    def gerar_assinatura_local(self, conta):
        context = {
            'nome': conta.nome,
            'setor': '' if conta.setor is None else conta.setor,
            'email': conta.email,
            'ddd_1': conta.ddd_1,
            'num_1': conta.num_1,
            'ddd_2': conta.ddd_2,
            'num_2': conta.num_2,
        }
        try:
            assinatura = render_to_string(self.template_file, context)
        except Exception:
            return 'Erro', 'Renderizando template'
        try:
            nome_de_arquivo = os.path.join('.', self.temp_file)
            with open(nome_de_arquivo, 'w') as arquivo:
                arquivo.write(assinatura)
        except Exception:
            return 'Erro', 'Escrevendo arquivo'
        return None, None

    def decode_rstrip(self, lines):
        output = []
        for line in lines:
            output.append(line.decode().rstrip())
        return output

    def executa_comando(self, comando):
        ssh = subprocess.Popen(comando, shell=False, stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
        result = self.decode_rstrip(ssh.stdout.readlines())
        error = self.decode_rstrip(ssh.stderr.readlines())
        streamdata = ssh.communicate()[0]
        returncode = ssh.returncode
        return returncode, result, error

    def executa_comando_ssh(self, comando):
        if self.transf['key_file']:
            ssh_call = [
                "ssh", "-p", self.transf['port'],
                "-i", self.transf['key_file'],
                self.transf['user@server']]
        else:
            ssh_call = [
                "ssh", "-p", self.transf['port'],
                self.transf['user@server']]
        return self.executa_comando(ssh_call + comando)

    def executa_comando_scp(self, comando):
        if self.transf['key_file']:
            ssh_call = ["scp", "-P", self.transf['port'],
                        "-i", self.transf['key_file']]
        else:
            ssh_call = ["scp", "-P", self.transf['port']]
        return self.executa_comando(ssh_call + comando)

    def scape_dirname(self, dirname):
        result = []
        for caractere in dirname:
            is_alfanum = caractere.isalnum()
            is_simbolo = caractere in '/._-'
            is_valido = is_alfanum or is_simbolo
            if not is_valido:
                caractere = f'\{caractere}'
            result.append(caractere)
        return ''.join(result)

    def mount_transfer_parameters(self, conta):
        diretorio = conta.diretorio
        servidor = diretorio.servidor

        self.transf['hostname'] = servidor.hostname
        self.transf['port'] = str(servidor.port)
        self.transf['user@server'] = f"{servidor.user}@{servidor.ip4}"
        self.transf['key_file'] = servidor.key_file.path

        caminho = diretorio.caminho.strip('/')
        subdiretorio = conta.subdiretorio.strip('/')
        caminho_completo = os.path.join('/', caminho, subdiretorio)
        dir_servidor = os.path.normpath(caminho_completo)
        self.transf['dir_servidor'] = self.scape_dirname(dir_servidor)

        usuario = self.scape_dirname(conta.email.split('@')[0])
        arquivo = f'assinatura_{usuario}.html'
        self.transf['arquivo_path'] = os.path.join(dir_servidor, arquivo)

        self.transf['file_perm'] = diretorio.file_perm
        self.transf['file_user'] = diretorio.file_user
        self.transf['file_group'] = diretorio.file_group

    def enviar_assinatura(self, conta):
        self.mount_transfer_parameters(conta)

        exitcode, result, error = self.executa_comando_ssh(["hostname"])
        if exitcode != 0:
            return 'Sem acesso ao servidor', error
        if result[0] != self.transf['hostname']:
            return 'Configuração de servidor errada', error

        exitcode, result, error = self.executa_comando_ssh(
            [f"stat -c '%A %U %G' '{self.transf['dir_servidor']}/'"])
        if exitcode != 0:
            return 'Erro acessando diretório', error
        # if result[0] != 'drwxrwxrwx nobody nogroup':
        #     return 'Diretório com direitos não esperados'

        exitcode, result, error = self.executa_comando_scp(
            [self.temp_file,
             f"{self.transf['user@server']}:{self.transf['arquivo_path']}"])
        if exitcode != 0:
            return 'Erro copiando arquivo', error

        stat_index = os.stat(self.temp_file)

        exitcode, result, error = self.executa_comando_ssh(
            [f"stat -c '%s' '{self.transf['arquivo_path']}'"])
        if exitcode != 0:
            return 'Erro verificando o arquivo', error
        if result[0] != str(stat_index.st_size):
            return 'Arquivo com tamanho errado', error

        exitcode, result, error = self.executa_comando_ssh(
            [f"chmod {self.transf['file_perm']} "
             f"'{self.transf['arquivo_path']}'"])
        if exitcode != 0:
            return 'Erro acertando permissões', error

        exitcode, result, error = self.executa_comando_ssh(
            [f"chown {self.transf['file_user']}:{self.transf['file_group']} "
             f"'{self.transf['arquivo_path']}'"])
        if exitcode != 0:
            return 'Erro acertando dono e grupo', error

        return None, None

    def exec(self):
        self.template_file = functions.gets.get_template_file(self.conta.tipo)

        erro, msg = self.gerar_assinatura_local(self.conta)
        if erro is None:
            erro, msg = self.enviar_assinatura(self.conta)
            self.conta.state = "N"
            self.conta.save()
        self.apagar_assinatura_local()

        return erro, msg
<commit_msg>apaga antes e depois<commit_after>import os
import subprocess
import tempfile
from pprint import pprint

from django.template.loader import render_to_string

import email_signature.functions as functions

class GeraAssinatura():

    def __init__(self, conta):
        self.conta = conta
        tf = tempfile.NamedTemporaryFile(prefix=f"_{conta.id}_temp_assinatura_file_", suffix=".html")
        self.temp_file = tf.name
        self.nome_de_arquivo = os.path.join('.', self.temp_file)
        self.transf = {}

    def apagar_assinatura_local(self):
        try:
            os.remove(self.nome_de_arquivo)
        except FileNotFoundError:
            pass

    def gerar_assinatura_local(self, conta):
        context = {
            'nome': conta.nome,
            'setor': '' if conta.setor is None else conta.setor,
            'email': conta.email,
            'ddd_1': conta.ddd_1,
            'num_1': conta.num_1,
            'ddd_2': conta.ddd_2,
            'num_2': conta.num_2,
        }
        try:
            assinatura = render_to_string(self.template_file, context)
        except Exception:
            return 'Erro', 'Renderizando template'
        try:
            with open(self.nome_de_arquivo, 'w') as arquivo:
                arquivo.write(assinatura)
        except Exception:
            return 'Erro', 'Escrevendo arquivo'
        return None, None

    def decode_rstrip(self, lines):
        output = []
        for line in lines:
            output.append(line.decode().rstrip())
        return output

    def executa_comando(self, comando):
        ssh = subprocess.Popen(comando, shell=False, stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE)
        result = self.decode_rstrip(ssh.stdout.readlines())
        error = self.decode_rstrip(ssh.stderr.readlines())
        streamdata = ssh.communicate()[0]
        returncode = ssh.returncode
        return returncode, result, error

    def executa_comando_ssh(self, comando):
        if self.transf['key_file']:
            ssh_call = [
                "ssh", "-p", self.transf['port'],
                "-i", self.transf['key_file'],
                self.transf['user@server']]
        else:
            ssh_call = [
                "ssh", "-p", self.transf['port'],
                self.transf['user@server']]
        return self.executa_comando(ssh_call + comando)

    def executa_comando_scp(self, comando):
        if self.transf['key_file']:
            ssh_call = ["scp", "-P", self.transf['port'],
                        "-i", self.transf['key_file']]
        else:
            ssh_call = ["scp", "-P", self.transf['port']]
        return self.executa_comando(ssh_call + comando)

    def scape_dirname(self, dirname):
        result = []
        for caractere in dirname:
            is_alfanum = caractere.isalnum()
            is_simbolo = caractere in '/._-'
            is_valido = is_alfanum or is_simbolo
            if not is_valido:
                caractere = f'\{caractere}'
            result.append(caractere)
        return ''.join(result)

    def mount_transfer_parameters(self, conta):
        diretorio = conta.diretorio
        servidor = diretorio.servidor

        self.transf['hostname'] = servidor.hostname
        self.transf['port'] = str(servidor.port)
        self.transf['user@server'] = f"{servidor.user}@{servidor.ip4}"
        self.transf['key_file'] = servidor.key_file.path

        caminho = diretorio.caminho.strip('/')
        subdiretorio = conta.subdiretorio.strip('/')
        caminho_completo = os.path.join('/', caminho, subdiretorio)
        dir_servidor = os.path.normpath(caminho_completo)
        self.transf['dir_servidor'] = self.scape_dirname(dir_servidor)

        usuario = self.scape_dirname(conta.email.split('@')[0])
        arquivo = f'assinatura_{usuario}.html'
        self.transf['arquivo_path'] = os.path.join(dir_servidor, arquivo)

        self.transf['file_perm'] = diretorio.file_perm
        self.transf['file_user'] = diretorio.file_user
        self.transf['file_group'] = diretorio.file_group

    def enviar_assinatura(self, conta):
        self.mount_transfer_parameters(conta)

        exitcode, result, error = self.executa_comando_ssh(["hostname"])
        if exitcode != 0:
            return 'Sem acesso ao servidor', error
        if result[0] != self.transf['hostname']:
            return 'Configuração de servidor errada', error

        exitcode, result, error = self.executa_comando_ssh(
            [f"stat -c '%A %U %G' '{self.transf['dir_servidor']}/'"])
        if exitcode != 0:
            return 'Erro acessando diretório', error
        # if result[0] != 'drwxrwxrwx nobody nogroup':
        #     return 'Diretório com direitos não esperados'

        exitcode, result, error = self.executa_comando_scp(
            [self.temp_file,
             f"{self.transf['user@server']}:{self.transf['arquivo_path']}"])
        if exitcode != 0:
            return 'Erro copiando arquivo', error

        stat_index = os.stat(self.temp_file)

        exitcode, result, error = self.executa_comando_ssh(
            [f"stat -c '%s' '{self.transf['arquivo_path']}'"])
        if exitcode != 0:
            return 'Erro verificando o arquivo', error
        if result[0] != str(stat_index.st_size):
            return 'Arquivo com tamanho errado', error

        exitcode, result, error = self.executa_comando_ssh(
            [f"chmod {self.transf['file_perm']} "
             f"'{self.transf['arquivo_path']}'"])
        if exitcode != 0:
            return 'Erro acertando permissões', error

        exitcode, result, error = self.executa_comando_ssh(
            [f"chown {self.transf['file_user']}:{self.transf['file_group']} "
             f"'{self.transf['arquivo_path']}'"])
        if exitcode != 0:
            return 'Erro acertando dono e grupo', error

        return None, None

    def exec(self):
        self.template_file = functions.gets.get_template_file(self.conta.tipo)

        self.apagar_assinatura_local()
        erro, msg = self.gerar_assinatura_local(self.conta)
        if erro is None:
            erro, msg = self.enviar_assinatura(self.conta)
            self.conta.state = "N"
            self.conta.save()
        self.apagar_assinatura_local()

        return erro, msg
<|endoftext|>


<commit_before>#!/usr/bin/python
"""Usage:
    python teach.py outputfile nmlfile
If outputfile is not specified, writes to standard output.

You must ". scripts/emc-environment" before running this script, if you use
run-in-place.
"""
#    Copyright 2007 Jeff Epler <jepler@unpythonic.net>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import emc
import Tkinter
import sys
import os
import tkMessageBox
from Tkinter import *

linenumber = 1;

if len(sys.argv) > 2:
    emc.nmlfile = sys.argv[1]

if len(sys.argv) > 1:
    outfile = sys.argv[1]
    sys.stdout = open(outfile, 'w')
#else:
#    sys.stdout = open('../Teached.ngc', 'w')

#
#  Output relative config ngc file header for SRX611
#

file_header = "\
(Generated by Arais Robot Technology: EMC2 teach-in 2010)\n\
\n\
#1000 = 0.0000  ( X Shift )\n\
#1001 = 0.0000  ( Y Shift )\n\
#1002 = 0.0000  ( Z Shift )\n\
#1003 = 0.0000  ( A Shift )\n\
\n\n\
"
feed_rate_string = ""

output_string = "(Learned Path)\n\
"

s = emc.stat()

def save_quit():
    if tkMessageBox.askokcancel("Quit", "You want to quit now?"):
        app.destroy()
    return
def get_textbox_data():
    global file_header
    global feed_rate_string
    global output_string
    file_header = text.get(0.0, 9.0)
#    print file_header
    feed_rate_string = text.get(9.0,"13.0")
    output_string = text.get(13.0,END)
    entry1.delete(0, END)
    entry1.insert(0, text.get("9.8","9.end"))
    entry2.delete(0, END)
    entry2.insert(0, text.get("10.8","10.end"))
    entry3.delete(0, END)
    entry3.insert(0, text.get("11.8","11.end"))
    button4.config(state=DISABLED)
    return

def textbox_changed(event):
# disable apply button
    button4.config(state=NORMAL)
    return

def feedrate_press(event):
    update_feed_string()
    return

def update_textbox():
    global file_header
    global feed_rate_string
    global output_string
    text.delete(0.0,END)
    text.insert(INSERT, file_header)
    text.insert(INSERT, feed_rate_string)
    text.insert(INSERT, output_string)
    return

def update_feed_string():
    global file_header
    global feed_rate_string
    global output_string
    feed_rate_string = "#3000 = " + entry1.get() +"\n"
    feed_rate_string = feed_rate_string+"#3001 = " + entry2.get() +"\n"
    feed_rate_string = feed_rate_string+"#3002 = " + entry3.get() +"\n\n"
    update_textbox()
    return

def get_cart():
    s.poll()
    position = " ".join(["%-.4f"] * s.axes)
    return position % s.position[:s.axes]

def get_joint():
    s.poll()
    position = " ".join(["%-.4f"] * s.axes)
    return position % s.joint_actual_position[:s.axes]

def log():
    global output_string
    p = get_cart()
#    p = "X:"+p.split(' ')[0]+" Y:"+p.split(' ')[1]+" Z:"+p.split(' ')[2]+" A:"+p.split(' ')[3]
#    label1.configure(text='%s' % p)
    #print "G1X["+p.split(' ')[0]+"+#1000]Y["+p.split(' ')[1]+ "+#1001]Z[" + p.split(' ')[2] +"+#1002]A["+ p.split(' ')[3]+"+#1003]"
    output_string = output_string +  "G1X["+p.split(' ')[0]+"+#1000]Y["+p.split(' ')[1]+ "+#1001]Z[" + p.split(' ')[2] +"+#1002]A["+ p.split(' ')[3]+"+#1003]\n"
    update_textbox()
    button4.config(state=DISABLED)

def ins_feed_3000():
    global output_string
    output_string = output_string +"F#3000\n"
    update_textbox()
    button4.config(state=DISABLED)
    return

def ins_feed_3001():
    global output_string
    output_string = output_string +"F#3001\n"
    update_textbox()
    button4.config(state=DISABLED)
    return

def ins_feed_3002():
    global output_string
    output_string = output_string +"F#3002\n"
    update_textbox()
    button4.config(state=DISABLED)
    return

def show():
    s.poll()
    #if world.get():
    p = get_cart()
    p = "X:["+p.split(' ')[0]+"] Y:["+p.split(' ')[1]+"] Z:["+p.split(' ')[2]+"] A:["+p.split(' ')[3]+"]"
    #else:
    #    p = get_joint()
#        p = "J0:["+p.split(' ')[0]+"] J1:["+p.split(' ')[1]+"] J2:["+p.split(' ')[2]+"] J3:["+p.split(' ')[3]+"]"
    label2.configure(text='%s' % p)
#    labXval.configure(text='%s' % p.split(' ')[0])
#    labYval.configure(text='%s' % p.split(' ')[1])
#    labZval.configure(text='%s' % p.split(' ')[2])
#    labAval.configure(text='%s' % p.split(' ')[3])
    app.after(100, show)

det = 0
def destory_bind(x):
    global det
    global file_header
    global feed_rate_string
    global output_string
    if det == 0:
        output_string = file_header+feed_rate_string+output_string + "M2\n"
        print output_string
    det = 1

app = Tkinter.Tk(); app.wm_title('EMC2 Teach-In')
app.bind('<Destroy>',destory_bind)
app.protocol("WM_DELETE_WINDOW", save_quit)

world = Tkinter.IntVar(app)

button = Tkinter.Button(app, command=log, text='Learn', font=("helvetica", 14))
button.grid(row=0, sticky= W+E)

label2 = Tkinter.Label(app, font='fixed', anchor="w")
label2.config(relief= SUNKEN)
label2.grid(row=0,column=1, ipadx=2, ipady=2, columnspan = 11, sticky= W+E)

label3 = Tkinter.Label(app, font='fixed', text="#3000=", anchor="w")
label3.grid(row=40,column=0, ipadx=2, ipady=2, sticky=E)

entry1 = Tkinter.Entry(app, font='fixed',width=10)
entry1.grid(row=40,column=1, sticky= W+E)
entry1.delete(0, END)
entry1.insert(0, "2000")
entry1.bind('<Return>', feedrate_press)

label4 = Tkinter.Label(app, font='fixed', text="#3001=", anchor="w")
label4.grid(row=40,column=2, ipadx=2, ipady=2, sticky=E)
entry2 = Tkinter.Entry(app,font='fixed',width=10)
entry2.grid(row=40,column=3, sticky= W+E)
entry2.delete(0, END)
entry2.insert(0, "2000")
entry2.bind('<Return>', feedrate_press)

label5 = Tkinter.Label(app, font='fixed', text="#3002=", anchor="w")
label5.grid(row=40,column=4, ipadx=2, ipady=2, sticky=E)
entry3 = Tkinter.Entry(app, font='fixed',width=10)
entry3.grid(row=40,column=5, sticky= W+E)
entry3.delete(0, END)
entry3.insert(0, "2000")
entry3.bind('<Return>', feedrate_press)

button1 = Tkinter.Button(app, command=ins_feed_3000, text='Insert F#3000', font=("helvetica", 14))
button1.grid(row=49, column=5, sticky= N+S+W+E)
button2 = Tkinter.Button(app, command=ins_feed_3001, text='Insert F#3001', font=("helvetica", 14))
button2.grid(row=50, column=5, sticky= N+S+W+E)
button3 = Tkinter.Button(app, command=ins_feed_3002, text='Insert F#3002', font=("helvetica", 14))
button3.grid(row=51, column=5, sticky= N+S+W+E)
button4 = Tkinter.Button(app, command=get_textbox_data, text='Apply', font=("helvetica", 14))
button4.grid(row=52, column=5, sticky= N+S+W+E)
button4.config(state=DISABLED)

button5 = Tkinter.Button(app, command=save_quit, text='Save Path', font=("helvetica", 14))
button5.grid(row=53, column=5, sticky= N+S+W+E)

text = Tkinter.Text(app)

text.grid(row=44,column=0,rowspan=10, columnspan=5, sticky = W+E)
update_feed_string()
update_textbox()
text.bind('<Button-1>', textbox_changed)

show()
app.mainloop()

<commit_msg>add init feedrate for ngc file<commit_after>#!/usr/bin/python
"""Usage:
    python teach.py outputfile nmlfile
If outputfile is not specified, writes to standard output.

You must ". scripts/emc-environment" before running this script, if you use
run-in-place.
"""
#    Copyright 2007 Jeff Epler <jepler@unpythonic.net>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import emc
import Tkinter
import sys
import os
import tkMessageBox
from Tkinter import *

linenumber = 1;

if len(sys.argv) > 2:
    emc.nmlfile = sys.argv[1]

if len(sys.argv) > 1:
    outfile = sys.argv[1]
    sys.stdout = open(outfile, 'w')
#else:
#    sys.stdout = open('../Teached.ngc', 'w')

#
#  Output relative config ngc file header for SRX611
#

file_header = "\
(Generated by Arais Robot Technology: EMC2 teach-in 2010)\n\
\n\
#1000 = 0.0000  ( X Shift )\n\
#1001 = 0.0000  ( Y Shift )\n\
#1002 = 0.0000  ( Z Shift )\n\
#1003 = 0.0000  ( A Shift )\n\
\n\n\
"
feed_rate_string = ""

output_string = "F#3000 \n\
(Learned Path)\n\
"

s = emc.stat()

def save_quit():
    if tkMessageBox.askokcancel("Quit", "You want to quit now?"):
        app.destroy()
    return
def get_textbox_data():
    global file_header
    global feed_rate_string
    global output_string
    file_header = text.get(0.0, 9.0)
#    print file_header
    feed_rate_string = text.get(9.0,"13.0")
    output_string = text.get(13.0,END)
    entry1.delete(0, END)
    entry1.insert(0, text.get("9.8","9.end"))
    entry2.delete(0, END)
    entry2.insert(0, text.get("10.8","10.end"))
    entry3.delete(0, END)
    entry3.insert(0, text.get("11.8","11.end"))
    button4.config(state=DISABLED)
    return

def textbox_changed(event):
# disable apply button
    button4.config(state=NORMAL)
    return

def feedrate_press(event):
    update_feed_string()
    return

def update_textbox():
    global file_header
    global feed_rate_string
    global output_string
    text.delete(0.0,END)
    text.insert(INSERT, file_header)
    text.insert(INSERT, feed_rate_string)
    text.insert(INSERT, output_string)
    return

def update_feed_string():
    global file_header
    global feed_rate_string
    global output_string
    feed_rate_string = "#3000 = " + entry1.get() +"\n"
    feed_rate_string = feed_rate_string+"#3001 = " + entry2.get() +"\n"
    feed_rate_string = feed_rate_string+"#3002 = " + entry3.get() +"\n\n"
    update_textbox()
    return

def get_cart():
    s.poll()
    position = " ".join(["%-.4f"] * s.axes)
    return position % s.position[:s.axes]

def get_joint():
    s.poll()
    position = " ".join(["%-.4f"] * s.axes)
    return position % s.joint_actual_position[:s.axes]

def log():
    global output_string
    p = get_cart()
#    p = "X:"+p.split(' ')[0]+" Y:"+p.split(' ')[1]+" Z:"+p.split(' ')[2]+" A:"+p.split(' ')[3]
#    label1.configure(text='%s' % p)
    #print "G1X["+p.split(' ')[0]+"+#1000]Y["+p.split(' ')[1]+ "+#1001]Z[" + p.split(' ')[2] +"+#1002]A["+ p.split(' ')[3]+"+#1003]"
    output_string = output_string +  "G1X["+p.split(' ')[0]+"+#1000]Y["+p.split(' ')[1]+ "+#1001]Z[" + p.split(' ')[2] +"+#1002]A["+ p.split(' ')[3]+"+#1003]\n"
    update_textbox()
    button4.config(state=DISABLED)

def ins_feed_3000():
    global output_string
    output_string = output_string +"F#3000\n"
    update_textbox()
    button4.config(state=DISABLED)
    return

def ins_feed_3001():
    global output_string
    output_string = output_string +"F#3001\n"
    update_textbox()
    button4.config(state=DISABLED)
    return

def ins_feed_3002():
    global output_string
    output_string = output_string +"F#3002\n"
    update_textbox()
    button4.config(state=DISABLED)
    return

def show():
    s.poll()
    #if world.get():
    p = get_cart()
    p = "X:["+p.split(' ')[0]+"] Y:["+p.split(' ')[1]+"] Z:["+p.split(' ')[2]+"] A:["+p.split(' ')[3]+"]"
    #else:
    #    p = get_joint()
#        p = "J0:["+p.split(' ')[0]+"] J1:["+p.split(' ')[1]+"] J2:["+p.split(' ')[2]+"] J3:["+p.split(' ')[3]+"]"
    label2.configure(text='%s' % p)
#    labXval.configure(text='%s' % p.split(' ')[0])
#    labYval.configure(text='%s' % p.split(' ')[1])
#    labZval.configure(text='%s' % p.split(' ')[2])
#    labAval.configure(text='%s' % p.split(' ')[3])
    app.after(100, show)

det = 0
def destory_bind(x):
    global det
    global file_header
    global feed_rate_string
    global output_string
    if det == 0:
        output_string = file_header+feed_rate_string+output_string + "M2\n"
        print output_string
    det = 1

app = Tkinter.Tk(); app.wm_title('EMC2 Teach-In')
app.bind('<Destroy>',destory_bind)
app.protocol("WM_DELETE_WINDOW", save_quit)

world = Tkinter.IntVar(app)

button = Tkinter.Button(app, command=log, text='Learn', font=("helvetica", 14))
button.grid(row=0, sticky= W+E)

label2 = Tkinter.Label(app, font='fixed', anchor="w")
label2.config(relief= SUNKEN)
label2.grid(row=0,column=1, ipadx=2, ipady=2, columnspan = 11, sticky= W+E)

label3 = Tkinter.Label(app, font='fixed', text="#3000=", anchor="w")
label3.grid(row=40,column=0, ipadx=2, ipady=2, sticky=E)

entry1 = Tkinter.Entry(app, font='fixed',width=10)
entry1.grid(row=40,column=1, sticky= W+E)
entry1.delete(0, END)
entry1.insert(0, "2000")
entry1.bind('<Return>', feedrate_press)

label4 = Tkinter.Label(app, font='fixed', text="#3001=", anchor="w")
label4.grid(row=40,column=2, ipadx=2, ipady=2, sticky=E)
entry2 = Tkinter.Entry(app,font='fixed',width=10)
entry2.grid(row=40,column=3, sticky= W+E)
entry2.delete(0, END)
entry2.insert(0, "2000")
entry2.bind('<Return>', feedrate_press)

label5 = Tkinter.Label(app, font='fixed', text="#3002=", anchor="w")
label5.grid(row=40,column=4, ipadx=2, ipady=2, sticky=E)
entry3 = Tkinter.Entry(app, font='fixed',width=10)
entry3.grid(row=40,column=5, sticky= W+E)
entry3.delete(0, END)
entry3.insert(0, "2000")
entry3.bind('<Return>', feedrate_press)

button1 = Tkinter.Button(app, command=ins_feed_3000, text='Insert F#3000', font=("helvetica", 14))
button1.grid(row=49, column=5, sticky= N+S+W+E)
button2 = Tkinter.Button(app, command=ins_feed_3001, text='Insert F#3001', font=("helvetica", 14))
button2.grid(row=50, column=5, sticky= N+S+W+E)
button3 = Tkinter.Button(app, command=ins_feed_3002, text='Insert F#3002', font=("helvetica", 14))
button3.grid(row=51, column=5, sticky= N+S+W+E)
button4 = Tkinter.Button(app, command=get_textbox_data, text='Apply', font=("helvetica", 14))
button4.grid(row=52, column=5, sticky= N+S+W+E)
button4.config(state=DISABLED)

button5 = Tkinter.Button(app, command=save_quit, text='Save Path', font=("helvetica", 14))
button5.grid(row=53, column=5, sticky= N+S+W+E)

text = Tkinter.Text(app)

text.grid(row=44,column=0,rowspan=10, columnspan=5, sticky = W+E)
update_feed_string()
update_textbox()
text.bind('<Button-1>', textbox_changed)

show()
app.mainloop()

<|endoftext|>


<commit_before>"""
WSGI config for ngw project.

This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting.

Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.

"""

from __future__ import division, absolute_import, print_function, unicode_literals
import os

# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "ngw.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ngw.settings")

from django import db
from django.utils.encoding import force_bytes
from django.contrib import auth
from django.contrib.auth.handlers.modwsgi import check_password
from ngw.core.models import ContactGroup

def groups_for_user(environ, username):
    """
    Authorizes a user based on groups
    """
    UserModel = auth.get_user_model()
    db.reset_queries()

    try:
        try:
            user = UserModel._default_manager.get_by_natural_key(username)
        except UserModel.DoesNotExist:
            return []
        if not user.is_active:
            return []
        groups = ContactGroup.objects.extra(where=[
            'perm_c_can_see_files_cg(%s, contact_group.id)' % user.id])
        return [force_bytes(group.id) for group in groups]
    finally:
        db.close_connection()

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)
<commit_msg>Added /usr/lib in wsgi pythonpath<commit_after>"""
WSGI config for ngw project.

This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting.

Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.

"""

from __future__ import division, absolute_import, print_function, unicode_literals
import sys
import os

sys.path.append('/usr/lib')

# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "ngw.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ngw.settings")

from django import db
from django.utils.encoding import force_bytes
from django.contrib import auth
from django.contrib.auth.handlers.modwsgi import check_password
from ngw.core.models import ContactGroup

def groups_for_user(environ, username):
    """
    Authorizes a user based on groups
    """
    UserModel = auth.get_user_model()
    db.reset_queries()

    try:
        try:
            user = UserModel._default_manager.get_by_natural_key(username)
        except UserModel.DoesNotExist:
            return []
        if not user.is_active:
            return []
        groups = ContactGroup.objects.extra(where=[
            'perm_c_can_see_files_cg(%s, contact_group.id)' % user.id])
        return [force_bytes(group.id) for group in groups]
    finally:
        db.close_connection()

# This application object is used by any WSGI server configured to use this
# file. This includes Django's development server, if the WSGI_APPLICATION
# setting points here.
from django.core.handlers.wsgi import WSGIHandler
application = WSGIHandler()

# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)
<|endoftext|>