Ejemplo n.º 1
0
def announce(msg: str, only_once: bool = True) -> None:
    global _printed_announcement
    if only_once and _printed_announcement:
        return
    local_state_path = Path(appdirs.user_state_dir('backend.ai', 'Lablup'))
    try:
        with open(local_state_path / 'announcement.json', 'rb') as f_current:
            last_state = json.load(f_current)
    except IOError:
        last_state = {'hash': '', 'dismissed': False}

    hasher = hashlib.sha256()
    hasher.update(msg.encode('utf8'))
    msg_hash = hasher.hexdigest()

    if not (last_state['hash'] == msg_hash and last_state['dismissed']):
        print_info("The server has an announcement!", file=sys.stderr)
        print('----------', file=sys.stderr)
        print(msg, file=sys.stderr)
        print('----------', file=sys.stderr)
    _printed_announcement = True

    last_state['hash'] = msg_hash
    with open(local_state_path / 'announcement.json', 'w') as f_new:
        json.dump(last_state, f_new)
Ejemplo n.º 2
0
 def _sign(self, rel_url, access_key=None, secret_key=None, hash_type=None):
     '''
     Calculates the signature of the given request and adds the
     Authorization HTTP header.
     It should be called at the very end of request preparation and before
     sending the request to the server.
     '''
     if access_key is None:
         access_key = self.config.access_key
     if secret_key is None:
         secret_key = self.config.secret_key
     if hash_type is None:
         hash_type = self.config.hash_type
     if self.config.endpoint_type == 'api':
         hdrs, _ = generate_signature(self.method, self.config.version,
                                      self.config.endpoint, self.date,
                                      str(rel_url), self.content_type,
                                      self._content, access_key, secret_key,
                                      hash_type)
         self.headers.update(hdrs)
     elif self.config.endpoint_type == 'session':
         local_state_path = Path(
             appdirs.user_state_dir('backend.ai', 'Lablup'))
         try:
             self.session.aiohttp_session.cookie_jar.load(local_state_path /
                                                          'cookie.dat')
         except (IOError, PermissionError):
             pass
     else:
         raise ValueError('unsupported endpoint type')
Ejemplo n.º 3
0
 def test_helpers(self):
     self.assertIsInstance(
         appdirs.user_data_dir('MyApp', 'MyCompany'), STRING_TYPE)
     self.assertIsInstance(
         appdirs.site_data_dir('MyApp', 'MyCompany'), STRING_TYPE)
     self.assertIsInstance(
         appdirs.user_cache_dir('MyApp', 'MyCompany'), STRING_TYPE)
     self.assertIsInstance(
         appdirs.user_state_dir('MyApp', 'MyCompany'), STRING_TYPE)
     self.assertIsInstance(
         appdirs.user_log_dir('MyApp', 'MyCompany'), STRING_TYPE)
Ejemplo n.º 4
0
 def test_helpers(self):
     self.assertIsInstance(appdirs.user_data_dir('MyApp', 'MyCompany'),
                           STRING_TYPE)
     self.assertIsInstance(appdirs.site_data_dir('MyApp', 'MyCompany'),
                           STRING_TYPE)
     self.assertIsInstance(appdirs.user_cache_dir('MyApp', 'MyCompany'),
                           STRING_TYPE)
     self.assertIsInstance(appdirs.user_state_dir('MyApp', 'MyCompany'),
                           STRING_TYPE)
     self.assertIsInstance(appdirs.user_log_dir('MyApp', 'MyCompany'),
                           STRING_TYPE)
Ejemplo n.º 5
0
 def _sign(
     self,
     rel_url: URL,
     access_key: str = None,
     secret_key: str = None,
     hash_type: str = None,
 ) -> None:
     """
     Calculates the signature of the given request and adds the
     Authorization HTTP header.
     It should be called at the very end of request preparation and before
     sending the request to the server.
     """
     if access_key is None:
         access_key = self.config.access_key
     if secret_key is None:
         secret_key = self.config.secret_key
     if hash_type is None:
         hash_type = self.config.hash_type
     assert self.date is not None
     if self.config.endpoint_type == 'api':
         hdrs, _ = generate_signature(
             method=self.method,
             version=self.api_version,
             endpoint=self.config.endpoint,
             date=self.date,
             rel_url=str(rel_url),
             content_type=self.content_type,
             access_key=access_key,
             secret_key=secret_key,
             hash_type=hash_type,
         )
         self.headers.update(hdrs)
     elif self.config.endpoint_type == 'session':
         local_state_path = Path(
             appdirs.user_state_dir('backend.ai', 'Lablup'))
         try:
             cookie_jar = cast(aiohttp.CookieJar,
                               self.session.aiohttp_session.cookie_jar)
             cookie_jar.load(local_state_path / 'cookie.dat')
         except (IOError, PermissionError):
             pass
     else:
         raise ValueError('unsupported endpoint type')
Ejemplo n.º 6
0
 def get_selections_file() -> Path:
     dir = user_state_dir("tafels")
     return Path(dir, "selections.dat")
Ejemplo n.º 7
0
 def get_stats_file() -> Path:
     dir = user_state_dir("tafels")
     return Path(dir, "cardstate.dat")
Ejemplo n.º 8
0
def get_data_directory():
    if is_snap():
        return os.path.expandvars('$SNAP_COMMON')
    else:
        return appdirs.user_state_dir(APP_NAME, APP_AUTHOR)
Ejemplo n.º 9
0
)

import appdirs

__all__ = [
    'get_config',
    'set_config',
    'APIConfig',
]

_config = None
_undefined = object()

DEFAULT_CHUNK_SIZE = 256 * 1024  # 256 KiB

local_state_path = Path(appdirs.user_state_dir('backend.ai', 'Lablup'))
local_cache_path = Path(appdirs.user_cache_dir('backend.ai', 'Lablup'))


def get_env(key: str,
            default: Any = _undefined,
            *,
            clean: Callable[[str], Any] = lambda v: v):
    '''
    Retrieves a configuration value from the environment variables.
    The given *key* is uppercased and prefixed by ``"BACKEND_"`` and then
    ``"SORNA_"`` if the former does not exist.

    :param key: The key name.
    :param default: The default value returned when there is no corresponding
        environment variable.
Ejemplo n.º 10
0
import clipboard
import colorful as cf

from art import *
from cryptography.fernet import Fernet

appname = "CryptoCut"
appauthor = "Infinite"

path = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(path, "Files", "words-list.txt")
cache_path = appdirs.user_cache_dir(appname, appauthor)
config_path = appdirs.user_config_dir(appname, appauthor)
data_path = appdirs.user_data_dir(appname, appauthor)
log_path = appdirs.user_log_dir(appname, appauthor)
dtate_path = appdirs.user_state_dir(appname, appauthor)


def create_user_key(user_pass):
    '''creating a key baseed on user key'''
    from cryptography.hazmat.primitives import hashes
    from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
    import base64
    from cryptography.hazmat.backends import default_backend

    user_pass = user_pass.encode()
    # TODO : check if its the first time or user wants to change the pass

    # create a key based on user password
    # TODO : we can use os.urandom(16) or somthing that user want
    salt = b'cryptocut_'