Skip to content

Commit

Permalink
imports
Browse files Browse the repository at this point in the history
  • Loading branch information
tanghaibao committed May 1, 2024
1 parent a24a923 commit 72c354e
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 165 deletions.
4 changes: 2 additions & 2 deletions jcvi/utils/__main__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
Assortment of utility scripts implementing recipes from Python cookbooks, such as iterators, sorters, range queries, etc.
Assortment of utility scripts implementing recipes from Python cookbooks, such as iterators, sorters, range queries, etc.
"""

from jcvi.apps.base import dmain
from ..apps.base import dmain


if __name__ == "__main__":
Expand Down
58 changes: 31 additions & 27 deletions jcvi/utils/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,35 @@
"""
AWS-related methods.
"""
import fnmatch
import getpass
import json
import os
import os.path as op
import sys
import fnmatch
import boto3
import json
import logging
import time
import getpass

from configparser import NoOptionError, NoSectionError
from datetime import datetime
from multiprocessing import Pool

import boto3

from botocore.exceptions import ClientError, ParamValidationError

from jcvi.formats.base import BaseFile, SetFile, timestamp
from jcvi.utils.console import console
from jcvi.apps.base import (
OptionParser,
from ..apps.base import (
ActionDispatcher,
OptionParser,
datafile,
get_config,
logger,
popen,
sh,
)
from ..formats.base import BaseFile, SetFile, timestamp

from .console import console


AWS_CREDS_PATH = "%s/.aws/credentials" % (op.expanduser("~"),)

Expand Down Expand Up @@ -165,7 +169,7 @@ def start(args):
# Make sure the instance id is empty
instance_id = s.instance_id
if instance_id != "":
logging.error("Instance exists {}".format(instance_id))
logger.error("Instance exists {}".format(instance_id))
sys.exit(1)

launch_spec = s.launch_spec
Expand Down Expand Up @@ -208,14 +212,14 @@ def start(args):
if "InstanceId" in response["SpotInstanceRequests"][0]:
instance_id = response["SpotInstanceRequests"][0]["InstanceId"]
else:
logging.debug("Waiting to be fulfilled ...")
logger.debug("Waiting to be fulfilled ...")
time.sleep(10)

# Check if the instance is running
print("Instance id {}".format(instance_id), file=sys.stderr)
status = ""
while status != "running":
logging.debug("Waiting instance to run ...")
logger.debug("Waiting instance to run ...")
time.sleep(3)
response = client.describe_instance_status(InstanceIds=[instance_id])
if len(response["InstanceStatuses"]) > 0:
Expand Down Expand Up @@ -271,7 +275,7 @@ def stop(args):
# Make sure the instance id is NOT empty
instance_id = s.instance_id
if instance_id == "":
logging.error("Cannot find instance_id {}".format(instance_id))
logger.error("Cannot find instance_id {}".format(instance_id))
sys.exit(1)

block_device_mappings = []
Expand All @@ -289,7 +293,7 @@ def stop(args):

image_status = ""
while image_status != "available":
logging.debug("Waiting for image to be ready")
logger.debug("Waiting for image to be ready")
time.sleep(10)
response = client.describe_images(ImageIds=[new_image_id])
image_status = response["Images"][0]["State"]
Expand Down Expand Up @@ -488,7 +492,7 @@ def check_exists_s3(s3_store_obj_name: str, warn=False) -> bool:
counts = int(popen(cmd).read())
exists = counts != 0
if exists and warn:
logging.debug("{} exists. Skipped.".format(s3_store_obj_name))
logger.debug("{} exists. Skipped.".format(s3_store_obj_name))
return exists


Expand Down Expand Up @@ -577,7 +581,7 @@ def validate(args, config):
)
else:
role_msg = ""
logging.info("Validating credentials for profile: %s %s" % (profile, role_msg))
logger.info("Validating credentials for profile: %s %s" % (profile, role_msg))
reup_message = "Obtaining credentials for a new role or profile."

try:
Expand Down Expand Up @@ -642,7 +646,7 @@ def validate(args, config):
for option in required_options:
short_term[option] = config.get(profile, option)
except NoOptionError:
logging.warning(
logger.warning(
"Your existing credentials are missing or invalid, "
"obtaining new credentials."
)
Expand All @@ -654,12 +658,12 @@ def validate(args, config):
current_role = None

if args.force:
logging.info("Forcing refresh of credentials.")
logger.info("Forcing refresh of credentials.")
force_refresh = True
# There are not credentials for an assumed role,
# but the user is trying to assume one
elif current_role is None and args.assume_role:
logging.info(reup_message)
logger.info(reup_message)
force_refresh = True
# There are current credentials for a role and
# the role arn being provided is the same.
Expand All @@ -676,12 +680,12 @@ def validate(args, config):
and args.assume_role
and current_role != args.assume_role
):
logging.info(reup_message)
logger.info(reup_message)
force_refresh = True
# There are credentials for a current role and no role arn is
# being supplied
elif current_role is not None and args.assume_role is None:
logging.info(reup_message)
logger.info(reup_message)
force_refresh = True

should_refresh = True
Expand All @@ -691,10 +695,10 @@ def validate(args, config):
exp = datetime.strptime(config.get(profile, "expiration"), "%Y-%m-%d %H:%M:%S")
diff = exp - datetime.utcnow()
if diff.total_seconds() <= 0:
logging.info("Your credentials have expired, renewing.")
logger.info("Your credentials have expired, renewing.")
else:
should_refresh = False
logging.info(
logger.info(
"Your credentials are still valid for %s seconds"
" they will expire at %s" % (diff.total_seconds(), exp)
)
Expand All @@ -714,7 +718,7 @@ def get_credentials(profile, args, config):

if args.assume_role:

logging.info(
logger.info(
"Assuming Role - Profile: %s, Role: %s, Duration: %s",
profile,
args.assume_role,
Expand Down Expand Up @@ -747,7 +751,7 @@ def get_credentials(profile, args, config):
args.assume_role,
)
else:
logging.info(
logger.info(
"Fetching Credentials - Profile: %s, Duration: %s", profile, args.duration
)
try:
Expand Down Expand Up @@ -790,15 +794,15 @@ def get_credentials(profile, args, config):
with open(AWS_CREDS_PATH, "w") as configfile:
config.write(configfile)

logging.info(
logger.info(
"Success! Your credentials will expire in %s seconds at: %s"
% (args.duration, response["Credentials"]["Expiration"])
)


def log_error_and_exit(message):
"""Log an error message and exit with error"""
logging.error(message)
logger.error(message)
sys.exit(1)


Expand Down
7 changes: 4 additions & 3 deletions jcvi/utils/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
import os.path as op
import re
import sys
import logging

from collections import defaultdict

from ..apps.base import logger


def inspect(item, maxchar=80):
"""
Expand Down Expand Up @@ -37,7 +38,7 @@ def timed(*args, **kw):
te = time.time()

msg = "{0}{1} {2:.2f}s".format(func.__name__, args, te - ts)
logging.debug(msg)
logger.debug(msg)

return result

Expand Down Expand Up @@ -149,7 +150,7 @@ def tofile(self, filename):
for x in self.data:
print(x, file=fw)
fw.close()
logging.debug(
logger.debug(
"Array of size {0} written to file `{1}`.".format(self.size, filename)
)

Expand Down
15 changes: 6 additions & 9 deletions jcvi/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
Connect to databases (Sybase, MySQL and PostgreSQL database backends)
"""
import os.path as op
import sys
import logging
import re
import sys

from jcvi.formats.base import must_open
from jcvi.apps.base import OptionParser, ActionDispatcher, sh, getusername
from jcvi.utils.cbook import AutoVivification
from ..apps.base import ActionDispatcher, OptionParser, getusername, logger, sh
from ..formats.base import must_open
from ..utils.cbook import AutoVivification


# set up valid database connection params
Expand Down Expand Up @@ -299,10 +298,8 @@ def query(args):
else:
for qry in qrys:
if re.search(r"{\d+}", qry):
logging.error(
"Query `{0}` contains placeholders, no datafile(s) specified".format(
qry
)
logger.error(
"Query `%s` contains placeholders, no datafile(s) specified", qry
)
sys.exit()
queries.add(qry)
Expand Down
1 change: 1 addition & 0 deletions jcvi/utils/range.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from collections import namedtuple, defaultdict
from itertools import groupby

from more_itertools import pairwise


Expand Down
7 changes: 3 additions & 4 deletions jcvi/utils/taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"""
import sys
import time
import logging

from functools import lru_cache

Expand All @@ -40,7 +39,7 @@
from ClientForm import ParseResponse
from BeautifulSoup import BeautifulSoup

from jcvi.apps.base import OptionParser, ActionDispatcher
from ..apps.base import ActionDispatcher, OptionParser, logger


URL = "http://itol.embl.de/other_trees.shtml"
Expand All @@ -65,8 +64,8 @@ def __init__(self, list_of_taxids):
response = urlopen(URL)
success = True
except (URLError, HTTPError, RuntimeError) as e:
logging.error(e)
logging.debug("wait 5 seconds to reconnect...")
logger.error(e)
logger.debug("wait 5 seconds to reconnect...")
time.sleep(5)

forms = ParseResponse(response, backwards_compat=False)
Expand Down
10 changes: 5 additions & 5 deletions jcvi/utils/webcolors.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
# Copyright © 2021 Haibao Tang. All rights reserved.
#

import logging

import numpy as np

from webcolors import CSS3_HEX_TO_NAMES, hex_to_rgb
from skimage.color import rgb2lab, deltaE_cmc
from webcolors import CSS3_HEX_TO_NAMES, hex_to_rgb

from ..apps.base import logger


def color_diff(rgb1, rgb2):
Expand All @@ -37,12 +37,12 @@ def closest_color(requested_color):
"""
Find closest color name for the request RGB tuple.
"""
logging.disable(logging.DEBUG)
logger.disable(logger.DEBUG)
colors = []
for key, name in CSS3_HEX_TO_NAMES.items():
diff = color_diff(hex_to_rgb(key), requested_color)
colors.append((diff, name))
logging.disable(logging.NOTSET)
logger.disable(logger.NOTSET)
_, min_color = min(colors)

return min_color
Expand Down
2 changes: 1 addition & 1 deletion jcvi/variation/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Set of scripts relating to variation studies such as imputation, phasing, SNP/CNV analysis, and other supporting routines
"""

from jcvi.apps.base import dmain
from ..apps.base import dmain


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit 72c354e

Please sign in to comment.