Пример #1
0
def main(argv: Optional[Sequence[str]] = None) -> int:
    parser = argparse.ArgumentParser()
    parser.add_argument('filenames', nargs='*', help='Filenames to check.')
    args = parser.parse_args(argv)

    retval = 0
    for filename in args.filenames:
        with open(filename) as f:
            try:
                json.load(f, object_pairs_hook=raise_duplicate_keys)
            except ValueError as exc:
                print(f'{filename}: Failed to json decode ({exc})')
                retval = 1
    return retval
Пример #2
0
    def update_from_file(self, config_path):
        if config_path.endswith('.yaml'):
            with open(config_path, encoding='utf-8') as rf:
                config = yaml.safe_load(rf)
        else:
            # load config as json file
            with open(config_path, encoding='utf-8') as rf:
                config = jstyleson.load(rf)

        def _load_key_value_from_dict(config):
            _ret_dict = {}
            for k, v in config.items():
                if isinstance(v, dict):
                    _sub_ret_dict = _load_key_value_from_dict(v)
                    for sub_k, sub_v in _sub_ret_dict.items():
                        _new_k = '.'.join([k, sub_k])
                        _ret_dict[_new_k] = sub_v
                else:
                    if k.startswith('_'):
                        raise ValueError(
                            'Config key starts with "_" is private key, which is immutable!'
                        )
                    _ret_dict[k] = v
            return _ret_dict

        new_cfg = _load_key_value_from_dict(config)
        for k, v in new_cfg.items():
            self.__setitem__(k, v)
Пример #3
0
def read_cfjson(fname):
    '''
    Read a CF-JSON like JSON file, see https://cf-json.org for info. 
    
    Parameters
    ----------
    fname : str
        The filename to read
    
    Returns
    -------
    cfdict : dict-like
        The CF conventions metadata, stored in a dictionary
    '''
    with open(fname, 'r') as f:
        cf = json.load(f, object_pairs_hook=CfDict)
        for vnam in cf.variables.keys():
            v = cf.variables[vnam]
            if 'type' in v:
                if '_FillValue' in v.attributes:
                    val = val2type(v.attributes['_FillValue'], v.type)
                    v.attributes['_FillValue'] = val
                if 'flag_masks' in v.attributes:
                    mlist = [
                        val2type(m, v.type) for m in v.attributes['flag_masks']
                    ]
                    v.attributes['flag_masks'] = mlist
    return cf
Пример #4
0
        def read_json(json_file):
            # In case json_file is an @settings_property function
            if getattr(json_file, 'settings_property', None):
                json_file = json_file(settings)

            with open(json_file, 'r') as fid:
                return Settings(json.load(fid))
Пример #5
0
    def __init__(self, streamer):
        """The constructor for GuessingGame class."""
        logging.basicConfig()
        self.logger = logging.getLogger(__name__)
        self.database = {
            "streamer": streamer,
            "channel-id": streamer.channel_id,
            "current-session": None,
            "latest-session": None
        }
        with open('items.json') as items:
            self.items = jstyleson.load(items)
        self.commands = [
            '!guess', '!hud', '!points', '!guesspoints', '!firstguess',
            '!start', '!mode', '!modedel', '!song', '!finish', '!report'
        ]
        self.guesses = {"item": deque(), "medal": deque(), "song": deque()}
        self.guessables = {
            "blacklist": [
                'Keys', 'Treasures', 'Skulls', 'Tokens', 'Prize', 'Label',
                'Badge', 'Heart Container', 'Pieces'
            ],
            "medals": ['forest', 'fire', 'water', 'shadow', 'spirit', 'light'],
            "dungeons": ['deku', 'dodongo', 'jabu'],
            "songs": [
                "Zelda's Lullaby", "Saria's Song", "Epona's Song",
                "Sun's Song", "Song of Time", "Song of Storms",
                "Minuet of Forest", "Bolero of Fire", "Serenade of Water",
                "Requiem of Spirit", "Nocturne of Shadow", "Prelude of Light"
            ]
        }
        self.guessables['dungeons'] += [
            medal for medal in self.guessables['medals'] if medal != 'light'
        ]
        self.state = {
            "running":
            False,
            "freebie":
            None,
            "mode": [],
            "songs": {},
            "medals": {},
            "modes": [{
                "name": "keysanity",
                "items": ["Boss Key"]
            }, {
                "name": "songsanity",
                "items": self.guessables['songs']
            }, {
                "name": "egg",
                "items": ["Child Trade"]
            }, {
                "name": "ocarina",
                "items": ["Ocarina"]
            }]
        }

        self.database['latest-session'] = self._get_sessions()

        self.logger.setLevel(logging.DEBUG)
Пример #6
0
    def _setup(self, name=None):
        """
    Load the config json file pointed to by the environment variable. This is
    used the first time settings are needed, if the user hasn't configured
    settings manually.

    Arguments
    ---------
    name : :class:`str`, optional
        The name used to describe the settings object. Defaults to ``settings``

    Raises
    ------
    ImproperlyConfigured
        If the settings has already been configured, will throw an error. Under
        normal circumstances, :func:`_setup` will not be called a second time.
    """
        settings_file = os.environ.get(ENVIRONMENT_VARIABLE)
        if not settings_file:
            desc = ("setting %s" % name) if name else "settings"
            raise ImproperlyConfigured(
                "Requested %s, but settings are not configured. "
                "You must either define the environment variable %s "
                "or call settings.configure() before accessing settings." %
                (desc, ENVIRONMENT_VARIABLE))
        with open(settings_file) as fid:
            self.configure(json.load(fid))
        self._wrapped.config_file = os.environ.get(ENVIRONMENT_VARIABLE)
Пример #7
0
def get_dataset_info(dataset_name):
    with open(PATHS2DATASETS_CONFIG.as_posix()) as f:
        datasets_paths = Dict(json.load(f))
        paths_config = datasets_paths[
            NAME_FROM_DEFINITIONS2DATASET_NAME[dataset_name]]
        data_source, dataset_info = paths_config.pop('source_dir'), {}
        for arg, path in paths_config.items():
            if path:
                dataset_info[arg] = path
        return data_source, dataset_info
Пример #8
0
def read_config_from_file(path):
    path = Path(path)
    extension = path.suffix.lower()
    with path.open() as f:
        if extension in ('.yaml', '.yml'):
            return yaml.load(f, Loader=yaml.SafeLoader)
        if extension in ('.json', ):
            return json.load(f)
        raise RuntimeError(
            'Unknown file extension for the file "{}"'.format(path))
Пример #9
0
def find_engine_config_locally(model_name):
    configs = Dict()
    for root, _, files in os.walk(ENGINE_CONFIG_PATH.as_posix()):
        for file in files:
            if file.endswith('.json'):
                configs[file.rstrip('.json')] = os.path.join(root, file)
    if model_name in configs.keys():
        with open(configs[model_name]) as f:
            engine_config = Dict(json.load(f))
        return engine_config
    return None
Пример #10
0
def _parse_config(config, **kwargs):
    if not os.path.isfile(config):
        log.error(f'missing config file: {config}')
        raise ValueError(f'missing config file: {config}')

    try:
        import jstyleson as json
    except:
        import json
    with open(config, 'r') as f:
        return json.load(f)
Пример #11
0
def load_model_config(path):
    with open(path) as f:
        model_config = Dict(json.load(f))
        mo_args = model_config.mo_args
        if not mo_args:
            return None
        for key, value in mo_args.items():
            if isinstance(value, str):
                mo_args[key] = string.Template(value).substitute(model_dir=CUSTOM_MODELS_PATH.as_posix())
            else:
                mo_args[key] = str(value)
        return model_config
Пример #12
0
def json_load(filename):
    # Helper function to load from json
    try:
        with open(filename, 'r') as fid:
            return json.load(fid)
    except JSONDecodeError as e:
        logger.critical(f'Error parsing the JSON config file {filename}: ' +
                        str(e))
        raise SystemExit(handledExitCode)
    except FileNotFoundError as e:
        logger.critical('Cannot find JSON config file: ' + str(e))
        raise SystemExit(handledExitCode)
Пример #13
0
 def reload(self):
     """
     Reload the JSON file
     """
     try:
         with open(self.filn, mode='r', encoding='utf-8',
                   errors='ignore') as f:
             ret = jstyleson.load(f)
     except JSONDecodeError as e:
         ret = {}
     self.commands = ret
     return ret
Пример #14
0
def add_lib_from_json(filename):
    def _resolve_includes(data, path):
        if "includes" in data:
            for include in data["includes"]:
                data.update(add_lib_from_json(os.path.join(path, include)))
            del data["includes"]

    with open(filename, 'r') as f:
        data = jstyleson.load(f)
        _json_module_library.update(data)
    path = os.sep.join(filename.split(os.sep)[:-1])
    _resolve_includes(data, path)
    return data
Пример #15
0
def _parse_config(config, **kwargs):
    if not os.path.isfile(config):
        error = f'missing config file: {config}'
        log.error(error)
        raise ValueError(error)

    try:
        with open(config, 'r') as f:
            return json.load(f)
    except Exception as ex:
        log.exception(
            f'failed to parse config file: "{config}". error: "{str(ex)}"')
        raise ex
    def from_json(cls, path):
        # pylint:disable=too-many-nested-blocks,too-many-branches
        with open(path) as f:
            json_config = json.load(f, object_pairs_hook=OrderedDict)
            hw_config = cls()
            hw_config.target_device = json_config['target_device']

            for algorithm_name, algorithm_configs in json_config.get(
                    'config', {}).items():
                hw_config.registered_algorithm_configs[algorithm_name] = {}
                for algo_config_alias, algo_config in algorithm_configs.items(
                ):
                    for key, val in algo_config.items():
                        if not isinstance(val, list):
                            algo_config[key] = [val]

                    hw_config.registered_algorithm_configs[algorithm_name][
                        algo_config_alias] = list(product_dict(algo_config))

            for op_dict in json_config.get('operations', []):
                for algorithm_name in op_dict:
                    if algorithm_name not in hw_config.registered_algorithm_configs:
                        continue
                    tmp_config = {}
                    for algo_and_op_specific_field_name, algorithm_configs in op_dict[
                            algorithm_name].items():
                        if not isinstance(algorithm_configs, list):
                            algorithm_configs = [algorithm_configs]

                        tmp_config[algo_and_op_specific_field_name] = []
                        for algorithm_config in algorithm_configs:
                            if isinstance(algorithm_config,
                                          str):  # Alias was supplied
                                tmp_config[
                                    algo_and_op_specific_field_name].extend(
                                        hw_config.registered_algorithm_configs[
                                            algorithm_name][algorithm_config])
                            else:
                                for key, val in algorithm_config.items():
                                    if not isinstance(val, list):
                                        algorithm_config[key] = [val]

                                tmp_config[
                                    algo_and_op_specific_field_name].extend(
                                        list(product_dict(algorithm_config)))

                    op_dict[algorithm_name] = tmp_config

                hw_config.append(ad.Dict(op_dict))

            return hw_config
Пример #17
0
def load_taskpool(path):
    config = jstyleson.load(fp=open(path))
    executor = config["executor"].split(" ")
    base_conf = parse_config(config["configs"]["==base=="])
    more_confs = list(map(parse_config, config["configs"]["==more=="]))
    if len(more_confs) == 0:
        more_confs = [[]]
    tasks = []
    for more_conf in more_confs:
        tasks.extend(config_to_tasks(executor, base_conf + more_conf))
    apply_arg_reference(tasks)
    taskpool = TaskPool()
    taskpool.set_tasks(tasks)
    return taskpool
Пример #18
0
def create_sample_config(args, parser) -> SampleConfig:
    sample_config = SampleConfig.from_json(args.config)
    sample_config.update_from_args(args, parser)

    file_path = Path(args.config).resolve()
    with safe_open(file_path) as f:
        loaded_json = json.load(f)

    if sample_config.get("target_device") is not None:
        target_device = sample_config.pop("target_device")
        loaded_json["target_device"] = target_device

    nncf_config = NNCFConfig.from_dict(loaded_json)
    sample_config.nncf_config = nncf_config
    return sample_config
Пример #19
0
    def from_json(cls, path):
        with open(path) as f:
            json_config = json.load(f, object_pairs_hook=OrderedDict)
            hw_config = cls()
            hw_config.append(
                Dict(('target_device', json_config['target_device'])))

            configs = {}
            for algorithm_name, algorithm_config in json_config.get(
                    'config', {}).items():
                configs[algorithm_name] = {}
                for config_name, config in algorithm_config.items():
                    for key, val in config.items():
                        if not isinstance(val, list):
                            config[key] = [val]

                    configs[algorithm_name][config_name] = list(
                        product_dict(config))

            for op_config in json_config.get('operations', []):
                for algorithm_name in op_config:
                    if algorithm_name not in configs:
                        continue
                    tmp_config = {}
                    for name, algorithm_config in op_config[
                            algorithm_name].items():
                        if not isinstance(algorithm_config, list):
                            algorithm_config = [algorithm_config]

                        tmp_config[name] = []
                        for config_item in algorithm_config:
                            if isinstance(config_item, str):
                                tmp_config[name].extend(
                                    configs[algorithm_name][config_item])
                            else:
                                for key, val in config_item.items():
                                    if not isinstance(val, list):
                                        config_item[key] = [val]

                                tmp_config[name].extend(
                                    list(product_dict(config_item)))

                    op_config[algorithm_name] = tmp_config

                hw_config.append(Dict(op_config))

            return hw_config
Пример #20
0
    def from_json(cls, path):
        with open(path) as f:
            loaded_json = cls(json.load(f))
        try:
            jsonschema.validate(loaded_json, schema=ROOT_NNCF_CONFIG_SCHEMA)

            compression_section = loaded_json.get("compression")
            if compression_section is None:
                # No compression specified
                return loaded_json

            if isinstance(compression_section, dict):
                validate_single_compression_algo_schema(compression_section)
            else:
                # Passed a list of dicts
                for compression_algo_dict in compression_section:
                    validate_single_compression_algo_schema(compression_algo_dict)

        except jsonschema.ValidationError:
            logger.error("Invalid NNCF config supplied!")
            raise
        return loaded_json
Пример #21
0
def vscode_run_config(args):
    parser = argparse.ArgumentParser(description='Process some integers.')
    parser.add_argument(
        'name',
        type=str,
        help="The name under which it should show in the run config dropdown.")
    parser.add_argument(
        'module',
        type=str,
        help=
        "The name of the module that should be executed, e.g. 'deeptech.core.cli'"
    )
    args, run_args = parser.parse_known_args(args)
    if not os.path.exists(".vscode"):
        os.mkdir(".vscode")
        print("Created .vscode folder.")
    if not os.path.exists(os.path.join(".vscode", "launch.json")):
        with open(os.path.join(".vscode", "launch.json"), "w") as f:
            data = {"version": "0.2.0", "configurations": []}
            f.write(json.dumps(data, indent=4, sort_keys=True))
            f.write("\n")
        print("Created launch.json.")
    with open(os.path.join(".vscode", "launch.json"), "r") as f:
        data = jstyleson.load(f)
        data["configurations"].append({
            "name": args.name,
            "type": "python",
            "request": "launch",
            "module": args.module,
            "args": run_args
        })
    with open(os.path.join(".vscode", "launch.json"), "w") as f:
        f.write(json.dumps(data, indent=4, sort_keys=True))
        f.write("\n")
    print(
        "Added configuration under name: {}. You may select it in the debug menu now."
        .format(args.name))
Пример #22
0
def load_config(path="sample_config.json"):
    config = jstyleson.load(fp=open(path))

    shared.executor = config["executor"]

    cuda = config["cuda"]
    if cuda == [] or cuda == -1:
        cuda = [-1]

    if cuda[0] != -1 and psutil.WINDOWS:
        print("CUDA shoule be -1 on windows")
        exit()
    shared.cuda = cuda

    concurrency = config["concurrency"]
    if concurrency == "#CUDA":
        if cuda[0] != -1:
            concurrency = len(cuda)
        else:
            print(
                "You must specify which CUDA devices you want to use if concurrency is set to #CUDA."
            )
    elif concurrency == "#CPU":
        concurrency = max(1, multiprocessing.cpu_count() - 1)
    else:
        concurrency = int(concurrency)
    shared.concurrency = concurrency

    base_conf = parse_config(config["configs"]["==base=="])
    more_confs = list(map(parse_config, config["configs"]["==more=="]))
    tasks = []
    if len(more_confs) == 0:
        tasks.extend(gen_tasks(base_conf))
    else:
        for more_conf in more_confs:
            tasks.extend(gen_tasks(base_conf + more_conf))
    shared.tasks = tasks
Пример #23
0
def processApiDef(filename):
    with open(filename) as jsonfile:
        return jstyleson.load(jsonfile)
Пример #24
0
    return model


if __name__ == '__main__':
    logger = Logger(__name__).logger

    parser = argparse.ArgumentParser(description='train')
    parser.add_argument('-c',
                        '--config',
                        default=None,
                        type=str,
                        help='config file path (default: None)')
    parser.add_argument('-r',
                        '--resume',
                        default=None,
                        type=str,
                        help='path to latest checkpoint (default: None)')

    args = parser.parse_args()

    config = None
    if args.resume is not None:
        if args.config is not None:
            logger.warning('Warning: --config overridden by --resume')
        config = torch.load(args.resume)['config']
    elif args.config is not None:
        config = jstyleson.load(open(args.config))
    assert config is not None

    main(config, args.resume)
Пример #25
0
 def from_json(cls, path) -> 'SampleConfig':
     file_path = Path(path).resolve()
     with safe_open(file_path) as f:
         loaded_json = json.load(f)
     return cls(loaded_json)
Пример #26
0
    def load_properties_n_headers(self):
        """
            1. in most scenarios, dev might want to short circut creating property file, but will wanted it as argument
                for those scenarios, user can define property's default value in http file itself.
                but there is a catch. if dev users same property twice, and passes different value,
                it would complicate scenario
            2. dev might want to separate property to a file. properties can be segregated in to multiple environments and dev
                can enable multiple at a time.  ('*' section exists, it will be by default used)
            3. dev can always mention which property file to load. but when he is lazy,
                he can define a `.dothttp.json` which will be loaded if dev didn't mention property file
            preference:
            command line property > property from file > default property
            property file syntax
            {
                "headers": {
                    "Content-Type": "application/json",
                },
                "*": {
                    "param1": "val1",
                    "param2": "val2",
                },
                "env1": {
                    "param1": "val3",
                    "param2": "val4",
                }
                // ....
            }
            currently environment has restriction to not use "*" and "headers"
        :return:
        """
        if not self.property_file:
            base_logger.debug('property file not specified')
            default = os.path.join(os.path.dirname(self.file), ".dothttp.json")
            if os.path.exists(default):
                base_logger.debug(
                    f'file: {default} exists. it will be used for property reference'
                )
                self.property_file = default
        if self.property_file and not os.path.exists(self.property_file):
            base_logger.debug(f'file: {self.property_file} not found')
            raise PropertyFileNotFoundException(
                propertyfile=self.property_file)
        if self.property_file:
            with open(self.property_file, 'r') as f:
                try:
                    props = json.load(f)
                    base_logger.debug(
                        f'file: {self.property_file} loaded successfully')
                except Exception as e:
                    base_logger.error(f'exception loading property file ',
                                      exc_info=True)
                    raise PropertyFileNotJsonException(
                        propertyfile=self.property_file)
                try:
                    validate(instance=props, schema=property_schema)
                except Exception as e:
                    base_logger.error(
                        f'property json schema validation failed! ',
                        exc_info=True)
                    raise PropertyFileException(
                        message="property file has invalid json schema",
                        file=self.property_file)

        else:
            props = {}
        self.default_headers.update(props.get('headers', {}))
        self.properties.update(props.get("*", {}))
        if self.env:
            for env_name in self.env:
                self.properties.update(props.get(env_name, {}))

        return self.properties
Пример #27
0
strap_str = ""
header_str = "\
/*\n\
 * Generated by info2header.py\n\
 * Do not edit it.\n\
 */\n\n"

header_str = header_str + "#define OTP_INFO_VER\t\t\"{}\"\n".format(INFO_VER)

with open(ROOT_DIR + 'otp_header.h', 'r') as header_fd:
    header_str = header_str + header_fd.read()

for i in OTP_INFO:
    conf_path = i['config']
    with open(conf_path, 'r') as config_info_fd:
        config_info = jstyleson.load(config_info_fd)
    conf_str = conf_str + \
        "static const struct otpconf_info {}[] = {{\n".format(i['conf_name'])
    conf_str = conf_str + conf2info(config_info)
    conf_str = conf_str + '};\n\n'

for i in OTP_INFO:
    strap_path = i['strap']
    with open(strap_path, 'r') as strap_info_fd:
        strap_info = jstyleson.load(strap_info_fd)
    strap_str = strap_str + \
        "static const struct otpstrap_info {}[] = {{\n".format(i['strap_name'])
    strap_str = strap_str + strap2info(strap_info)
    strap_str = strap_str + '};\n\n'

print(header_str + strap_str + conf_str)
Пример #28
0
    def load_theme(self, theme_file):
        theme_d = {}
        theme_file = os.path.join(Theme.theme_folder(),
                                  os.path.basename(theme_file))
        if not os.path.exists(theme_file):
            pass
        with open(theme_file, 'rt') as f:
            try:
                theme_d = json.load(f)
            except:
                print('ERROR loading', theme_file)

        # now fill in the blanks
        self.theme_d = theme_d
        background = theme_d.get('background', "#fffdf6e3")
        foreground = theme_d.get('foreground', "#ff657b83")

        self.background = background
        self.foreground = foreground

        self.highlight = theme_d.get('linehighlight', "#3F3D3812")
        self.line_pad_bottom = theme_d.get('line_padding_bottom',
                                           self.line_pad_bottom)
        self.line_pad_top = theme_d.get('line_padding_top',
                                        self.line_pad_bottom)
        self.selection = theme_d.get('selection', {})
        self.selection['background'] = self.selection.get(
            'background', '#ffd33682')
        self.selection['foreground'] = self.selection.get(
            'foreground', '#fffdf6e3')
        self.caret = theme_d.get('caret', "#ff6ec3dc")

        font = theme_d.get('font', {})
        font['face'] = font.get('face', 'Ubuntu Mono')
        font['size'] = font.get('size', 16)
        font['style'] = font.get('style', 'normal')
        self.font_info = font
        self.style_infos['default'] = {
            'color': foreground,
            'background': background,
            'style': font['style'],
            'face': font['face'],
            'size': font['size']
        }

        for style in 'text.italic', 'text.bold', 'text.bolditalic', 'quote':
            self.get_theme_symbol_text(style)
        self.get_theme_style('h.symbol')
        for i in range(6):
            hname = f'h{i+1}.text'
            self.get_theme_style(hname)
        for style in ('code.fenced', 'code', 'list.symbol', 'list.unordered',
                      'list.ordered', 'tag', 'citekey', 'zettel.link',
                      'comment', 'footnote', 'search.name', 'search.spec',
                      'search.code'):
            self.get_theme_style(style)

        link_dict = theme_d.get('link', {})
        self.style_infos['link.caption'] = self.get_style(link_dict, 'title')
        self.style_infos['link.url'] = self.get_style(link_dict, 'url')
        self.style_infos['link.attr'] = self.get_style(link_dict, 'attr')
Пример #29
0
from stdproyectlib import *
import pyglet as pg
from math import pi
import jstyleson as json

# POR HACER:
# -[X] Incluir codigo para extraer modelos 3D de archivos json (con `import jstyleson as json`)
# -[ ] Incluir codigo para que haya varios menus (Imagen de fin de partida, menu principal, etc.)
# -[ ] A lo mejor incluir objetos 3D hechos de varios obj3D (para animacion). 
#      Tambien se puede usar una funcion especial para cada animacion que cambie la posicion relativa de los puntos
# -[ ] Hacer un 'R E M A S T E R' del juego corto que use para mostrar las posibilidades del motor 3D obsoleto 

obj_file = open("models.json",'r')
obj_data = json.load(obj_file)
obj_file.close()
for obj_name in obj_data:
    obj_data[obj_name]["points"] = [vector(*i) for i in obj_data[obj_name]["points"]]

win = pg.window.Window(fullscreen=True, resizable=True, caption="vector-3D")
win.set_exclusive_mouse(True)
batch = pg.graphics.Batch()


framerate = pg.text.Label('60',x=30,y=30,batch=batch)
grid = obj3D(batch,win.width,win.height,obj_data["Grid"]["points"],obj_data["Grid"]["lines"],0,0,0,vector(0,40,0),color=obj_data["Grid"]["color"])
cube_grid = [obj3D(batch,win.width,win.height,obj_data["Piramid"]["points"],obj_data["Piramid"]["lines"],m*0.1,0,0,vector(4*n,4*m,4*(100-m**2)**0.5),color=obj_data["Piramid"]["color"]) for n in range(10) for m in range(10)]
cube = obj3D(batch,win.width,win.height,obj_data["Cube"]["points"],obj_data["Cube"]["lines"],0,0,0,vector(0,0,0),color=obj_data["Cube"]["color"])
cubeRot = obj3D(batch,win.width,win.height,obj_data["Cube"]["points"],obj_data["Cube"]["lines"],0,0,0,vector(0,0,4),color=obj_data["Cube"]["color"])
cube0 = obj3D(batch,win.width,win.height,obj_data["Cube"]["points"],obj_data["Cube"]["lines"],0,0,0,vector(0,0,-10),color=obj_data["Cube"]["color"])
cube1 = obj3D(batch,win.width,win.height,obj_data["Cube"]["points"],obj_data["Cube"]["lines"],0,0,0,vector(50,50,-25),color=obj_data["Cube"]["color"])
cube2 = obj3D(batch,win.width,win.height,obj_data["Cube"]["points"],obj_data["Cube"]["lines"],0,0,0,vector(40,0,-10),color=obj_data["Cube"]["color"])
Пример #30
0
def init_config():
    global config, mqtt_config, args

    # Logger used for this phase, where full logging environment is not inizialized (i need config to initialize it!)
    h = logging.StreamHandler(sys.stdout)
    h.setFormatter(
        logging.Formatter('%(asctime)s - BOOT> %(message)s',
                          datefmt='%Y-%m-%d %H:%M:%S'))
    boot_logger = logging.getLogger("boot")
    boot_logger.propagate = False
    boot_logger.setLevel(logging.INFO)
    boot_logger.addHandler(h)

    # https://docs.python.org/2/library/argparse.html
    parser = argparse.ArgumentParser(description='Automato node')
    # parser.add_argument('event_dir', help = 'The directory in which the event files are stored')
    parser.add_argument('-c',
                        '--config',
                        metavar='file',
                        help='Load the specified config file',
                        default='/etc/automato/automato.conf')
    parser.add_argument(
        '-r',
        '--reset',
        action='store_true',
        required=False,
        help='Do not load module states (the same as starting from scratch)')
    parser.add_argument('--mqtt-client-id',
                        dest='client_id',
                        metavar='client-id',
                        required=False,
                        help='Use this client-id for mqtt connection')
    parser.add_argument(
        '--mqtt-client-id-prefix',
        dest='client_id_prefix',
        metavar='prefix',
        required=False,
        help=
        'The client-id for mqtt connection will be this value + a random string'
    )
    parser.add_argument(
        '-t',
        '--mqtt-topic',
        dest='topic',
        metavar='topic',
        required=False,
        help=
        'This command will turn "sender" mode on: the node will connecto to mqtt broker, send this message, close the connection and exit. Use with --mqtt-payload, --mqtt-qos and --mqtt-retain'
    )
    parser.add_argument(
        '-p',
        '--mqtt-payload',
        dest='payload',
        metavar='payload',
        required=False,
        help='The payload to send in sender mode. See --mqtt-topic')
    parser.add_argument(
        '-q',
        '--mqtt-qos',
        dest='qos',
        type=int,
        metavar='qos',
        required=False,
        help=
        'The qos of the message to be sent in sender mode. See --mqtt-topic')
    parser.add_argument(
        '--mqtt-retain',
        dest='retain',
        action='store_true',
        required=False,
        help=
        'The retain flag of the message to be sent in sender mode. See --mqtt-topic'
    )
    parser.add_argument(
        '--test',
        dest='test',
        nargs="?",
        const="*",
        help=
        'Start test suite (you can specifify a test id to execute only that one)'
    )
    #action='store_true',
    args = parser.parse_args()

    base_path = os.path.dirname(os.path.realpath(args.config))
    config = {}
    todo = [args.config]
    done = []

    while len(todo):
        f = todo.pop(0)
        if f and f not in done:
            done.append(f)
            try:
                boot_logger.info(
                    "Loaded config from file {file}".format(file=f))
                fconfig = jstyleson.load(open(f))
            except FileNotFoundError:
                boot_logger.error("File not found: %s" % (f))
                return False
            except json.decoder.JSONDecodeError as e:
                boot_logger.error("Error reading configuration file \"" +
                                  str(f) + "\": " + str(e))
                if hasattr(e, "lineno") and hasattr(e, "colno"):
                    lineno = 0
                    for line in e.doc.split("\n"):
                        lineno += 1
                        if lineno >= e.lineno - 3 and lineno <= e.lineno + 3:
                            boot_logger.error(
                                str(lineno).rjust(5, " ") + ": " +
                                line.rstrip())
                            if lineno == e.lineno:
                                boot_logger.error("".rjust(
                                    len(str(lineno)), "!").rjust(5, " ") +
                                                  ":" +
                                                  "^".rjust(e.colno, " "))
                return False
            except:
                boot_logger.exception("Error reading configuration file")
                return False
        if 'include' in fconfig:
            if not isinstance(fconfig['include'], list):
                boot_logger.error(
                    "Error reading configuration file \"" + str(f) +
                    "\": include must be a list of file specifications")
                return False

            for ff in fconfig['include']:
                l = glob.glob(
                    os.path.realpath(os.path.dirname(os.path.realpath(f))) +
                    '/' + ff)
                l.sort()
                for fff in l:
                    if os.path.isfile(fff):
                        todo.append(fff)
        # Merge strategy: first-level lists and dict will be merged together. Other levels or types will be overwritten.
        for k in fconfig:
            if isinstance(fconfig[k], list) and k in config and isinstance(
                    config[k], list):
                config[k] = config[k] + fconfig[k]
            elif isinstance(fconfig[k], dict) and k in config and isinstance(
                    config[k], dict):
                config[k] = {**config[k], **fconfig[k]}
            else:
                config[k] = fconfig[k]

    if 'name' not in config:
        config['name'] = os.uname().nodename
    if 'base_path' not in config:
        config['base_path'] = base_path
    if 'mqtt' not in config:
        config['mqtt'] = {}

    if args.client_id:
        config['mqtt']['client_id'] = args.client_id
        config['mqtt']['client_id_random_postfix'] = False
    if args.client_id_prefix:
        config['mqtt']['client_id'] = args.client_id_prefix
    if 'client_id' not in config['mqtt']:
        config['mqtt']['client_id'] = 'automato_' + config['name']

    if config['base_path'] not in sys.path:
        sys.path.insert(0, config['base_path'])

    return True