Пример #1
0
def test_get_detail_title():
    testobj = None
    assert funcs.get_detail_title('gebrtaak', 'new', testobj) == 'Nieuw(e) gebruikerstaak'
    testobj = NS(naam='testnaam')
    assert funcs.get_detail_title('gebrtaak', '', testobj) == 'Gebruikerstaak testnaam'
    testobj = NS(nummer='testnummer')
    assert funcs.get_detail_title('gebrtaak', '', testobj) == 'Gebruikerstaak testnummer'
Пример #2
0
 def __init__(self,
              x='identity',
              y='identity',
              xlim=None,
              ylim=None,
              expand=True):
     self.trans = NS(x=gettrans(x), y=gettrans(y))
     self.limits = NS(x=xlim, y=ylim)
     self.expand = expand
Пример #3
0
 def read_codefiles(self):
     files = {
         variant: self.module.__file__ if variant == "main" else
         self.tmod.transfer(f"{self.basename}:{variant}")[1]
         for variant in self.variants
     }
     self.files = NS(**files)
     self.cf = NS(
         **{
             variant: CodeFile(file, self.module.__name__)
             for variant, file in files.items()
         })
Пример #4
0
def parse_conn_str(cs, scheme="http"):
    """
    Parse a connection string into a namespace
    """
    url = NS()

    parts = urlparse(cs.strip(), scheme=scheme)

    url.scheme = parts.scheme
    if url.scheme not in ("http", "https", ):
        raise ValueError(f"Unsupported scheme '{url.scheme}'")

    username = unquote(parts.username or "").strip()
    url.username = username or None

    password = unquote(parts.password or "").strip()
    url.password = password or None

    if (url.username is None) ^ (url.password is None):
        raise ValueError("Incomplete login credentials'")

    url.hostname = parts.hostname
    if url.hostname is None:
        raise ValueError("Missing hostname")

    url.port = parts.port

    url.domain = parts.path.lstrip("/")
    if url.domain == "":
        url.domain = "default"

    return url
Пример #5
0
    def view(self, limits=None, range=None):
        """
        Information about the trained scale
        """
        if limits is None:
            limits = self.limits

        if range is None:
            range = self.dimension(limits=limits)

        breaks = self.get_breaks(range)
        breaks = breaks.compress(np.isfinite(breaks))
        minor_breaks = self.get_minor_breaks(breaks, range)
        mask = (range[0] <= breaks) & (breaks <= range[1])
        breaks = breaks.compress(mask)
        labels = self.get_labels(breaks, mask)

        if minor_breaks is None:
            minor_breaks = []

        vs = NS(scale=self,
                aesthetics=self.aesthetics,
                name=self.name,
                limits=limits,
                range=range,
                breaks=breaks,
                labels=labels,
                minor_breaks=minor_breaks)
        return vs
Пример #6
0
 def function(self, meta, precision):
     data = Variable('data_p', 'const void *')
     back = Variable('back_p', 'void *')
     meta = NS(precision=precision, **meta.__dict__)
     return Function(name=self.name,
                     arguments=ArgumentList(data, back),
                     meta=meta)
Пример #7
0
    def view(self, limits=None, range=None):
        """
        Information about the trained scale
        """
        if limits is None:
            limits = self.limits

        if range is None:
            range = self.dimension(limits=limits)

        breaks_d = self.get_breaks(limits)
        breaks = self.map(pd.Categorical(breaks_d.keys()))
        minor_breaks = []
        labels = self.get_labels(breaks_d)

        vs = NS(
            scale=self,
            aesthetics=self.aesthetics,
            name=self.name,
            limits=limits,
            range=range,
            breaks=breaks,
            labels=labels,
            minor_breaks=minor_breaks
        )
        return vs
Пример #8
0
    def expand_limits(self, limits, expand=None, coord_limits=None,
                      trans=None):
        """
        Calculate the final range in coordinate space
        """
        expand_func = partial(scale_continuous.expand_limits, self,
                              trans=trans)
        n_limits = len(limits)
        range_c = (0, 1)
        range_d = (1, n_limits)
        is_only_continuous = n_limits == 0

        if hasattr(self.range, 'range_c'):
            is_only_discrete = self.range.range_c.range is None
            range_c = self.range.range_c.range
        else:
            is_only_discrete = True

        if self.is_empty():
            return expand_func(range_c, expand, coord_limits),
        elif is_only_continuous:
            return expand_func(range_c, expand, coord_limits)
        elif is_only_discrete:
            return expand_func(range_d, expand, coord_limits)
        else:
            no_expand = self.default_expand(0, 0)
            ranges_d = expand_func(range_d, expand, coord_limits)
            ranges_c = expand_func(range_c, no_expand, coord_limits)
            limits = np.hstack(ranges_d.range, ranges_c.range)
            range = (np.min(limits), np.max(limits))
            ranges = NS(range=range, range_coord=range)
            return ranges
Пример #9
0
def init_torch(
        # Seed to use for random numbers
        seed: Argument & int = default(1234),
        # Use CUDA for this model
        cuda: Argument & bool = default(None),
        # Number of threads for PyTorch
        workers: Argument & int = default(None),
):
    np.random.seed(seed)
    torch.manual_seed(seed)
    torch.set_num_threads(workers or 1)

    if cuda is None:
        cuda = torch.cuda.is_available()

    if cuda:
        torch.cuda.manual_seed_all(seed)

    return NS(
        device=torch.device("cuda" if cuda else "cpu"),
        cuda=cuda,
        sync=torch.cuda.synchronize if cuda else None,
        workers=workers,
        seed=seed,
    )
Пример #10
0
 def add(name, scheme, pool=None):
     functions.append(
         Function(name=name,
                  arguments=ArgumentList(data, back),
                  meta=NS(length=length,
                          precision=precision,
                          scheme=scheme,
                          pool=pool)))
Пример #11
0
def list_new_large_kernels():
    """Return list of large kernels to generate with the new generator."""

    kernels = [
        NS(length=50,
           factors=[10, 5],
           use_3steps_large_twd={
               'sp': 'true',
               'dp': 'true'
           },
           threads_per_block=256),
        NS(length=64,
           factors=[8, 8],
           use_3steps_large_twd={
               'sp': 'true',
               'dp': 'false'
           }),
        NS(length=81,
           factors=[3, 3, 3, 3],
           use_3steps_large_twd={
               'sp': 'true',
               'dp': 'true'
           }),
        NS(length=100,
           factors=[5, 5, 4],
           use_3steps_large_twd={
               'sp': 'true',
               'dp': 'false'
           }),
        NS(length=128,
           factors=[8, 4, 4],
           use_3steps_large_twd={
               'sp': 'true',
               'dp': 'false'
           }),
        NS(length=200,
           factors=[8, 5, 5],
           use_3steps_large_twd={
               'sp': 'false',
               'dp': 'false'
           }),
        NS(length=256,
           factors=[4, 4, 4, 4],
           use_3steps_large_twd={
               'sp': 'true',
               'dp': 'true'
           })
    ]

    # for SBCC kernel, increase desired threads_per_block so that columns per
    # thread block is also increased. currently targeting for 16 columns
    block_width = 16
    for k in kernels:
        k.scheme = 'CS_KERNEL_STOCKHAM_BLOCK_CC'
        if not hasattr(k, 'threads_per_block'):
            k.threads_per_block = block_width * reduce(mul, k.factors,
                                                       1) // min(k.factors)
    return kernels
Пример #12
0
def test_dynamic_configuration(notes: Path) -> None:
    import pytz
    from types import SimpleNamespace as NS

    from my.core.cfg import tmp_config
    with tmp_config() as C:
        C.orgmode = NS(paths=[notes])
        # TODO ugh. this belongs to tz provider or global config or someting
        C.weight  = NS(default_timezone=pytz.timezone('Europe/London'))

        from my.body.weight import from_orgmode
        weights = [0.0 if isinstance(x, Exception) else x.value for x in from_orgmode()]

    assert weights == [
        0.0,
        62.0,
        0.0,
        61.0,
        62.0,
        0.0,
    ]
Пример #13
0
def pytorch_reader(dataset, transform):
    rval = NS()
    manifest = dataset["manifest"]
    assert all("partition" in entry
               for entry in manifest) or len(manifest) == 1
    for entry in manifest:
        if "partition" not in entry:
            return _load(dataset["environment"], entry, transform)
        else:
            setattr(rval, entry["partition"],
                    _load(dataset["environment"], entry, transform))
    return rval
Пример #14
0
 def _testSingleRule(self,
                     rule_lines,
                     target_types,
                     labelstr,
                     conditions=None,
                     flags=None):
     if isinstance(rule_lines, str):
         rule_lines = (rule_lines, )
     if flags is None:
         flags = NS(alert=False, halt=False)
     R, = list(parserules(rule_lines))
     D("R = %s", R)
     self.assertEqual(len(R.targets), len(target_types))
     for T, Ttype in zip(R.targets, target_types):
         self.assertIsInstance(T, Ttype)
     self.assertEqual(R.label, labelstr)
     self.assertEqual(R.flags.alert, flags.alert)
     self.assertEqual(R.flags.halt, flags.halt)
     if conditions is not None:
         self.assertEqual(len(R.conditions), len(conditions))
         for i in range(len(conditions)):
             C = conditions[i]
             RC = R.conditions[i]
             D("C = %s", C)
             for attr in dir(C):
                 if attr[0].isalpha():
                     D("test attr %s: %s VS %s", attr, getattr(C, attr),
                       getattr(RC, attr))
                     if attr == 'flags':
                         Cflags = C.flags
                         RCflags = RC.flags
                         for flag_name in dir(RC.flags):
                             if flag_name == 'D':
                                 continue
                             if flag_name[0].isalpha():
                                 if getattr(RCflags, flag_name):
                                     self.assertTrue(
                                         flag_name in Cflags,
                                         "\"%s\" in Rule but not expected" %
                                         (flag_name, ))
                                 else:
                                     self.assertTrue(
                                         flag_name not in Cflags,
                                         "\"%s\" expected, but not in Rule"
                                         % (flag_name, ))
                     else:
                         av1 = getattr(C, attr)
                         av2 = getattr(RC, attr)
                         if not callable(av1) and not callable(av2):
                             self.assertEqual(getattr(C, attr),
                                              getattr(RC, attr))
    def setup_panel_params(self, scale_x, scale_y):
        """
        Compute the range and break information for the panel
        """
        def get_view_limits(scale, coord_limits):
            expansion = scale.default_expansion(expand=self.expand)
            ranges = scale.expand_limits(scale.limits, expansion, coord_limits,
                                         identity_trans)
            vs = scale.view(limits=coord_limits, range=ranges.range)
            return vs

        out = NS(x=get_view_limits(scale_x, self.limits.x),
                 y=get_view_limits(scale_y, self.limits.y))
        return out
Пример #16
0
 def add(name, scheme, pool=None):
     use3Steps = {'sp': 'true', 'dp': 'true'}
     if length == 81:
         use3Steps['dp'] = 'false'
     elif length == 200:
         use3Steps['sp'] = use3Steps['dp'] = 'false'
     functions.append(
         Function(name=name,
                  arguments=ArgumentList(data, back),
                  meta=NS(length=length,
                          precision=precision,
                          scheme=scheme,
                          use_3steps_large_twd=use3Steps,
                          pool=pool)))
Пример #17
0
    def _draw_labels(self):
        """
        Draw x and y labels onto the figure
        """
        # This is very laboured. Should be changed when MPL
        # finally has a constraint based layout manager.
        figure = self.figure
        get_property = self.theme.themeables.property

        try:
            margin = get_property('axis_title_x', 'margin')
        except KeyError:
            pad_x = 5
        else:
            pad_x = margin.get_as('t', 'pt')

        try:
            margin = get_property('axis_title_y', 'margin')
        except KeyError:
            pad_y = 5
        else:
            pad_y = margin.get_as('r', 'pt')

        # Get the axis labels (default or specified by user)
        # and let the coordinate modify them e.g. flip
        labels = self.coordinates.labels(
            NS(x=self.layout.xlabel(self.labels),
               y=self.layout.ylabel(self.labels)))
        # The first axes object is on left, and the last axes object
        # is at the bottom. We change the transform so that the relevant
        # coordinate is in figure coordinates. This way we take
        # advantage of how MPL adjusts the label position so that they
        # do not overlap with the tick text. This works well for
        # facetting with scales='fixed' and also when not facetting.
        # first_ax = self.axs[0]
        # last_ax = self.axs[-1]

        xlabel = self.facet.last_ax.set_xlabel(labels.x, labelpad=pad_x)
        ylabel = self.facet.first_ax.set_ylabel(labels.y, labelpad=pad_y)

        xlabel.set_transform(
            mtransforms.blended_transform_factory(
                figure.transFigure, mtransforms.IdentityTransform()))
        ylabel.set_transform(
            mtransforms.blended_transform_factory(
                mtransforms.IdentityTransform(), figure.transFigure))

        figure._themeable['axis_title_x'] = xlabel
        figure._themeable['axis_title_y'] = ylabel
Пример #18
0
    def get_vehicle_status(self):
        command = '/-/vehicle-info/get-vehicle-details'
        r = self.session.post(self.url + command,
                              headers=carnet.request_headers)
        data = r.json()
        vehicle_data = data['vehicleDetails']

        mileage = vehicle_data['distanceCovered'].replace('.', '')
        service_components = vehicle_data['serviceInspectionData'].split()
        service_in_days = service_components[0]
        service_in_km = service_components[3].replace('.', '')

        self.odometer_values = NS(mileage=mileage,
                                  service_in_km=service_in_km,
                                  service_in_days=service_in_days)

        self.reveal(self.odometer, f'{mileage}', title='Matka km')
        self.display_odometer = True
Пример #19
0
    def expand_limits(self,
                      limits,
                      expand=None,
                      coord_limits=None,
                      trans=None):
        """
        Calculate the final range in coordinate space
        """
        if limits is None:
            limits = (None, None)

        if coord_limits is None:
            coord_limits = (None, None)

        if trans is None:
            trans = self.trans

        def _expand_range_distinct(x, expand):
            # Expand ascending and descending order range
            if x[0] > x[1]:
                x = expand_range_distinct(x[::-1], expand)[::-1]
            else:
                x = expand_range_distinct(x, expand)
            return x

        # - Override None in coord_limits
        # - Expand limits in coordinate space
        # - Remove any computed infinite values &
        #   fallback on unexpanded limits
        limits = tuple(l if cl is None else cl
                       for cl, l in zip(coord_limits, limits))
        limits_coord_space = trans.transform(limits)
        range_coord = _expand_range_distinct(limits_coord_space, expand)
        with ignore_warnings(RuntimeWarning):
            # Consequences of the runtimewarning (NaNs and infs)
            # are dealt with below
            final_limits = trans.inverse(range_coord)
        final_range = tuple([
            fl if np.isfinite(fl) else l
            for fl, l in zip(final_limits, limits)
        ])
        ranges = NS(range=final_range, range_coord=range_coord)
        return ranges
Пример #20
0
 def _make_group(self, g, entries):
     valid = True
     ws = WorkSet([g])
     # Set of graphs that must all be augmented with the same fvs
     eqv = OrderedSet([g])
     # Call sites that will have to be updated
     call_sites = {}
     for currg in ws:
         eqv.add(currg)
         new_results = self.call_sites(currg)
         if new_results is None:
             valid = False
             break
         for _, new_eqv in new_results.items():
             ws.queue_all(new_eqv)
         call_sites.update(new_results)
     if valid:
         for gg in eqv:
             entries[gg] = NS(graph=gg, calls=call_sites, eqv=eqv)
             # Only the first graph gets the call sites
             call_sites = {}
Пример #21
0
    def setup_panel_params(self, scale_x, scale_y):
        """
        Compute the range and break information for the panel

        """
        def get_view_limits(scale, coord_limits, trans):
            if coord_limits:
                coord_limits = trans.transform(coord_limits)

            expansion = scale.default_expansion(expand=self.expand)
            ranges = scale.expand_limits(scale.limits, expansion, coord_limits,
                                         trans)
            vs = scale.view(limits=coord_limits, range=ranges.range)
            vs.range = np.sort(ranges.range_coord)
            vs.breaks = transform_value(trans, vs.breaks, vs.range)
            vs.minor_breaks = transform_value(trans, vs.minor_breaks, vs.range)
            return vs

        out = NS(x=get_view_limits(scale_x, self.limits.x, self.trans.x),
                 y=get_view_limits(scale_y, self.limits.y, self.trans.y))
        return out
Пример #22
0
def main(exp):

    # dataset to use
    dataset: Argument

    # batch size
    batch_size: Argument & int = default(32)

    # path to model checkpoint file
    checkpoint: Argument = default(None)

    torch_settings = init_torch()
    wrapper = iteration_wrapper(exp, sync=torch_settings.sync)

    args = NS(
        dataset=dataset,
        checkpoint=checkpoint,
        batch_size=batch_size,
        torch_settings=torch_settings,
        wrapper=wrapper,
    )
    train300_mlperf_coco(exp, args)
Пример #23
0
 def range(self, panel_params):
     """
     Return the range along the dimensions of the coordinate system
     """
     # Defaults to providing the 2D x-y ranges
     return NS(x=panel_params.x.range, y=panel_params.y.range)
Пример #24
0
def list_new_kernels():
    """Return list of kernels to generate with the new generator."""

    # remaining lenghts less than 1024: 121 192 224 250 320 336 375
    # 384 405 432 450 480 500 512 576 600 625 640 675 750 768 800 810
    # 864 900 972 1000

    # dictionary of (flavour, threads_per_block) -> list of kernels to generate
    all_kernels = {
        ('uwide', 256): [
            #            NS(length=2, factors=[2]),
            #            NS(length=3, factors=[3]),
            #            NS(length=5, factors=[5]),
            #            NS(length=6, factors=[6]),
            #            NS(length=7, factors=[7]),
            #            NS(length=8, factors=[8]),
            NS(length=9, factors=[3, 3]),
            #            NS(length=10, factors=[10]),
            NS(length=12, factors=[6, 2]),
            NS(length=14, factors=[7, 2]),
            NS(length=15, factors=[5, 3]),
            #            NS(length=18, factors=[6,3]),
            NS(length=20, factors=[10, 2]),
            NS(length=21, factors=[7, 3]),
            NS(length=24, factors=[8, 3]),
            NS(length=25, factors=[5, 5]),
            #            NS(length=27, factors=[3,3,3]),
            NS(length=28, factors=[7, 4]),
            NS(length=30, factors=[10, 3]),
            NS(length=36, factors=[6, 6]),
            NS(length=42, factors=[7, 6]),
            NS(length=45, factors=[5, 3, 3]),
            #            NS(length=49, factors=[7,7]),
            NS(length=50, factors=[10, 5]),
            NS(length=54, factors=[6, 3, 3]),
            NS(length=56, factors=[8, 7]),
            #            NS(length=64, factors=[16,4]),
            #            NS(length=72, factors=[8,3,3]),
            NS(length=75, factors=[5, 5, 3]),
            NS(length=80, factors=[16, 5]),
            #            NS(length=81, factors=[3,3,3,3]),
            #            NS(length=96, factors=[16,6]),
            #            NS(length=100, factors=[10,10]),
            NS(length=108, factors=[6, 6, 3]),
            NS(length=112, factors=[16, 7]),
            NS(length=125, factors=[5, 5, 5]),
            #            NS(length=128, factors=[16,8]),
            #            NS(length=135, factors=[5,3,3,3]),
            #            NS(length=150, factors=[10,5,3]),
            NS(length=160, factors=[16, 10]),
            #            NS(length=162, factors=[6,3,3,3]),
            NS(length=168, factors=[8, 7, 3]),
            NS(length=180, factors=[10, 6, 3]),
            #            NS(length=216, factors=[8,3,3,3]),
            NS(length=225, factors=[5, 5, 3, 3]),
            NS(length=240, factors=[16, 5, 3]),
            #            NS(length=243, factors=[3,3,3,3,3]),
            #            NS(length=256, factors=[16,16]),
            #            NS(length=270, factors=[10,3,3,3]),
            #            NS(length=288, factors=[16,6,3]),
            NS(length=324, factors=[6, 6, 3, 3]),
            NS(length=343, factors=[7, 7, 7]),
            NS(length=360, factors=[10, 6, 6]),
            NS(length=400, factors=[16, 5, 5]),
            #            NS(length=486, factors=[6,3,3,3,3]),
            #            NS(length=540, factors=[10,6,3,3]),
            NS(length=648, factors=[8, 3, 3, 3, 3]),
            NS(length=720, factors=[16, 5, 3, 3]),
            #            NS(length=729, factors=[3,3,3,3,3,3]),
            NS(length=960, factors=[16, 10, 6]),
        ],
        ('wide', 64): [
            #            NS(length=11, factors=[11]),
            NS(length=22, factors=[2, 11]),
            NS(length=44, factors=[4, 11]),
            NS(length=60, factors=[6, 10]),
            NS(length=84, factors=[2, 6, 7]),
            NS(length=90, factors=[3, 3, 10]),
            NS(length=120, factors=[2, 6, 10]),
            #            NS(length=200, factors=[2,10,10]),
            NS(length=300, factors=[3, 10, 10]),
        ],
        ('uwide', 64): [
            NS(length=32, factors=[16, 2]),
            NS(length=40, factors=[10, 4]),
            NS(length=48, factors=[3, 4, 4]),
            NS(length=88, factors=[11, 8]),
            NS(length=176, factors=[16, 11]),
            NS(length=336, factors=[7, 8, 6]),
        ],
        # ('tall', X): [
        #     NS(length=4),
        #     NS(length=13),
        #     NS(length=16),
        #     NS(length=26),
        #     NS(length=52),
        #     NS(length=104),
        #     NS(length=169),
        #     NS(length=192),
        #     NS(length=208),
        #     NS(length=320),
        #     NS(length=512),
        #     NS(length=625),
        #     NS(length=864),
        #     NS(length=1000),
        # ]
    }

    expanded = []
    for params, kernels in all_kernels.items():
        flavour, threads_per_block = params
        expanded.extend(
            NS(**kernel.__dict__,
               flavour=flavour,
               threads_per_block=threads_per_block,
               scheme='CS_KERNEL_STOCKHAM') for kernel in kernels)

    return expanded
Пример #25
0
 def t(self):
   ts = NS()
   for tbl in self.tables:
     setattr(ts, tbl.name, tbl)
   return ts
Пример #26
0
import unittest
from kalliopepbx.utils import parse_conn_str
from types import SimpleNamespace as NS

test_values = [
    ('http://example.com',
     NS(domain='default',
        hostname='example.com',
        password=None,
        port=None,
        scheme='http',
        username=None)),
    ('https://example.com',
     NS(domain='default',
        hostname='example.com',
        password=None,
        port=None,
        scheme='https',
        username=None)),
    ('http://example.com/default',
     NS(domain='default',
        hostname='example.com',
        password=None,
        port=None,
        scheme='http',
        username=None)),
    ('http://example.com:8080/default',
     NS(domain='default',
        hostname='example.com',
        password=None,
        port=8080,
Пример #27
0
# FFprobe/FFmpeg Settings
threads = "8"  # The number of cores/threads your CPU has. Probably 4.
ffprobeLocation = "ffprobe"  # Change this if ffprobe isn't in your path.
# ffprobeLocation = os.path.realpath("./ffprobe") # If you have ffprobe as a local binary.
ffmpegLocation = "ffmpeg"  # Change this if ffmpeg isn't in your path.
# ffmpegLocation = os.path.realpath("./ffmpeg") # If you have ffmpeg as a local binary.

# The directories where everything is and should be located.
_baseDir = os.path.abspath(
    os.path.join(os.path.sep, "mnt", "sdb", "openings.moe"))
_deployDir = os.path.join(_baseDir, "deploy")
directories = NS(text=os.path.join(_baseDir, "bin"),
                 source=os.path.join(_baseDir, "source"),
                 encode=os.path.join(_baseDir, "encode"),
                 deploy=NS(videos=os.path.join(_deployDir, "videos"),
                           fonts=os.path.join(_deployDir, "fonts"),
                           subtitles=os.path.join(_deployDir, "subtitles")),
                 attachments=os.path.join(_baseDir, "attachments"))

# Video Settings
video = NS(
    default=NS(bitrate="3000K",
               crf="18",
               maxBitrate="5000k",
               maxWidth=1280,
               maxHeight=720),

    # VP9 Configuration
    VP9=NS(
        slices="4",
Пример #28
0
def setup_logging(
    cmd_name=None,
    main_log=None,
    format=None,
    level=None,
    flags=None,
    upd_mode=None,
    ansi_mode=None,
    trace_mode=None,
    module_names=None,
    function_names=None,
    verbose=None,
    supplant_root_logger=False,
):
    ''' Arrange basic logging setup for conventional UNIX command
      line error messaging; return an object with informative attributes.
      That object is also available as the global `cs.logutils.loginfo`.

      Amongst other things, the default logger now includes
      the `cs.pfx` prefix in the message.

      This function runs in two modes:
      - if logging has not been set up, it sets up a root logger
      - if the root logger already has handlers,
        monkey patch the first handler's formatter to prefix the `cs.pfx` state

      Parameters:
      * `cmd_name`: program name, default from `basename(sys.argv[0])`.
        Side-effect: sets `cs.pfx.cmd` to this value.
      * `main_log`: default logging system.
        If `None`, the main log will go to `sys.stderr`;
        if `main_log` is a string, is it used as a filename to
        open in append mode;
        otherwise main_log should be a stream suitable
        for use with `logging.StreamHandler()`.
        The resulting log handler is added to the `logging` root logger.
      * `format`: the message format for `main_log`.
        If `None`, use `DEFAULT_PFX_FORMAT_TTY`
        when `main_log` is a tty or FIFO,
        otherwise `DEFAULT_PFX_FORMAT`.
      * `level`: `main_log` logging level.
        If `None`, infer a level from the environment
        using `infer_logging_level()`.
      * `flags`: a string containing debugging flags separated by commas.
        If `None`, infer the flags from the environment using
        `infer_logging_level()`.
        The following flags have meaning:
        `D`: set cs.logutils.D_mode to True;
        `TDUMP`: attach a signal handler to SIGHUP to do a thread stack dump;
        `TRACE`: enable various noisy tracing facilities;
        `UPD`, `NOUPD`: set the default for `upd_mode` to True or False respectively.
      * `upd_mode`: a Boolean to activate cs.upd as the `main_log` method;
        if `None`, set it to `True` if `flags` contains 'UPD',
        otherwise to `False` if `flags` contains 'NOUPD',
        otherwise set it from `main_log.isatty()`.
        A true value causes the root logger to use `cs.upd` for logging.
      * `ansi_mode`: if `None`,
        set it from `main_log.isatty() and not cs.colourise.env_no_color()`,
        which thus honours the `$NO_COLOR` environment variable
        (see https://no-color.org/ for the convention).
        A true value causes the root logger to colour certain logging levels
        using ANSI terminal sequences (currently only if `cs.upd` is used).
      * `trace_mode`: if `None`, set it according to the presence of
        'TRACE' in flags. Otherwise if `trace_mode` is true, set the
        global `loginfo.trace_level` to `loginfo.level`; otherwise it defaults
        to `logging.DEBUG`.
      * `verbose`: if `None`, then if stderr is a tty then the log
        level is `INFO` otherwise `WARNING`. Otherwise, if `verbose` is
        true then the log level is `INFO` otherwise `WARNING`.
  '''
    global D_mode, loginfo  # pylint: disable=global-statement

    # infer logging modes, these are the initial defaults
    inferred = infer_logging_level(verbose=verbose)
    if level is None:
        level = inferred.level
    if flags is None:
        flags = inferred.flags
    if module_names is None:
        module_names = inferred.module_names
    if function_names is None:
        function_names = inferred.function_names

    if cmd_name is None:
        cmd_name = os.path.basename(sys.argv[0])
    cs.pfx.cmd = cmd_name

    if main_log is None:
        main_log = sys.stderr
    elif isinstance(main_log, str):
        main_log = open(main_log, "a")

    # determine some attributes of main_log
    try:
        fd = main_log.fileno()
    except (AttributeError, IOError):
        is_fifo = False
        ##is_reg = False                        # unused
        is_tty = False
    else:
        st = os.fstat(fd)
        is_fifo = stat.S_ISFIFO(st.st_mode)
        ##is_reg = stat.S_ISREG(st.st_mode)     # unused
        is_tty = stat.S_ISCHR(st.st_mode)

    if getattr(main_log, 'encoding', None) is None:
        main_log = codecs.getwriter("utf-8")(main_log)

    if trace_mode is None:
        trace_mode = 'TRACE' in flags

    if 'D' in flags:
        D_mode = True

    if upd_mode is None:
        if 'UPD' in flags:
            upd_mode = True
        elif 'NOUPD' in flags:
            upd_mode = False
        else:
            upd_mode = is_tty

    if ansi_mode is None:
        ansi_mode = is_tty and not env_no_color()

    if format is None:
        if is_tty or is_fifo:
            format = DEFAULT_PFX_FORMAT_TTY
        else:
            format = DEFAULT_PFX_FORMAT

    if 'TDUMP' in flags:
        # do a thread dump to the main_log on SIGHUP
        # pylint: disable=import-outside-toplevel
        import signal
        import cs.debug as cs_debug

        # pylint: disable=unused-argument
        def handler(sig, frame):
            cs_debug.thread_dump(None, main_log)

        signal.signal(signal.SIGHUP, handler)

    upd_ = Upd()

    root_logger = logging.getLogger()
    if root_logger.handlers:
        # The logging system is already set up.
        # Just monkey patch the leading handler's formatter.
        PfxFormatter.patch_formatter(root_logger.handlers[0].formatter)
    else:
        # Set up a handler etc.
        main_handler = logging.StreamHandler(main_log)
        if upd_mode:
            main_handler = UpdHandler(main_log,
                                      ansi_mode=ansi_mode,
                                      over_handler=main_handler)
            upd_ = main_handler.upd
        root_logger.setLevel(level)
        if loginfo is None:
            # only do this the first time
            # TODO: fix this clumsy hack, some kind of stackable state?
            main_handler.setFormatter(PfxFormatter(format))
            if supplant_root_logger:
                root_logger.handlers.pop(0)
            root_logger.addHandler(main_handler)

    if trace_mode:
        # enable tracing in the thread that called setup_logging
        Pfx._state.trace = info
        trace_level = level
    else:
        trace_level = logging.DEBUG

    if module_names or function_names:
        if importlib is None:
            warning(
                "setup_logging: no importlib (python<2.7?),"
                " ignoring module_names=%r/function_names=%r", module_names,
                function_names)
        else:
            for module_name in module_names:
                try:
                    M = importlib.import_module(module_name)
                except ImportError:
                    warning("setup_logging: cannot import %r", module_name)
                else:
                    M.DEBUG = True
            for module_name, func_name in function_names:
                try:
                    M = importlib.import_module(module_name)
                except ImportError:
                    warning("setup_logging: cannot import %r", module_name)
                    continue
                F = M
                for funcpart in func_name.split('.'):
                    M = F
                    try:
                        F = M.getattr(funcpart)
                    except AttributeError:
                        F = None
                        break
                if F is None:
                    warning("no %s.%s() found", module_name, func_name)
                else:
                    setattr(M, funcpart, _ftrace(F))

    loginfo = NS(
        logger=root_logger,
        level=level,
        verbose=verbose,
        trace_level=trace_level,
        flags=flags,
        module_names=module_names,
        function_names=function_names,
        cmd=cmd_name,
        upd=upd_,
        upd_mode=upd_mode,
        ansi_mode=ansi_mode,
        format=format,
    )

    return loginfo
Пример #29
0
from types import SimpleNamespace as NS

# Values and expected results as per example in documentation:
#   doc/Manuale API REST KalliopePBX V4 per CDR.pdf

credentials = NS(
    username="******",
    password="******",
    domain="default",
    salt="b5a8fdcf2f8d5acdad33c4a072a97d7a",
    created="2016-04-29T15:48:26Z",
    nonce="bfb79078ff44c35714af28b7412a702b",
)

expected = NS(
    digest="+PJg7Tb3v98XnL6iJVv+v5hwhYjdzQ2tIWxvJB2cE40=",
    digest_pass=("dd7b0be7fa37d6cbaf0b842bf7532f22"
                 "9cb79ab8d54d509c2aa7eea27a53cd5e"),
    xheader_key="X-authenticate",
    xheader_value=('RestApiUsernameToken Username="******", Domain="default", '
                   'Digest="+PJg7Tb3v98XnL6iJVv+v5hwhYjdzQ2tIWxvJB2cE40=", '
                   'Nonce="bfb79078ff44c35714af28b7412a702b", '
                   'Created="2016-04-29T15:48:26Z"'),
)

expected.xheader_dict = {expected.xheader_key: expected.xheader_value}
expected.xheader_str = f"{expected.xheader_key}: {expected.xheader_value}"
Пример #30
0
def infer_logging_level(env_debug=None, environ=None, verbose=None):
    ''' Infer a logging level from the `env_debug`, which by default
      comes from the environment variable `$DEBUG`.

      Usually default to `logging.WARNING`, but if `sys.stderr` is a terminal,
      default to `logging.INFO`.

      Parse the environment variable `$DEBUG` as a comma separated
      list of flags.

      Examine the in sequence flags to affect the logging level:
      * numeric < 1: `logging.WARNING`
      * numeric >= 1 and < 2: `logging.INFO`
      * numeric >= 2: `logging.DEBUG`
      * `"DEBUG"`: `logging.DEBUG`
      * `"STATUS"`: `STATUS`
      * `"INFO"`: `logging.INFO`
      * `"TRACK"`: `TRACK`
      * `"WARNING"`: `logging.WARNING`
      * `"ERROR"`: `logging.ERROR`

      Return an object with the following attributes:
      * `.level`: A logging level.
      * `.flags`: All the words from `$DEBUG` as separated by commas and uppercased.
      * `.module_names`: Module names to be debugged.
      * `.function_names`: Functions to be traced in the form *module_name*`.`*func_name*.
  '''
    if env_debug is None:
        if environ is None:
            environ = os.environ
        env_debug = os.environ.get('DEBUG', '')
    level = TRACK
    if verbose is None:
        if not sys.stderr.isatty():
            level = logging.WARNING
    elif verbose:
        level = logging.VERBOSE
    else:
        level = logging.WARNING
    flags = [F.upper() for F in env_debug.split(',') if len(F)]
    module_names = []
    function_names = []
    for flag in env_debug.split(','):
        flag = flag.strip()
        if not flag:
            continue
        if flag.isdigit():
            flag_level = int(flag)
            if flag_level < 1:
                level = logging.WARNING
            elif flag_level >= 2:
                level = logging.DEBUG
            else:
                level = logging.INFO
        elif flag[0].islower() and is_dotted_identifier(flag):
            # modulename
            module_names.append(flag)
        elif ':' in flag:
            # module:funcname
            module_name, func_name = flag.split(':', 1)
            if is_dotted_identifier(module_name) and is_dotted_identifier(
                    func_name):
                function_names.append((module_name, func_name))
        else:
            uc_flag = flag.upper()
            if uc_flag == 'DEBUG':
                level = logging.DEBUG
            elif uc_flag == 'STATUS':
                level = STATUS
            elif uc_flag == 'INFO':
                level = logging.INFO
            elif uc_flag == 'TRACK':
                level = TRACK
            # pylint: disable=consider-using-in
            elif uc_flag == 'WARN' or uc_flag == 'WARNING':
                level = logging.WARNING
            elif uc_flag == 'ERROR':
                level = logging.ERROR
    return NS(level=level,
              flags=flags,
              module_names=module_names,
              function_names=function_names)